-
-
Save matbor/65d97ec40415fde62303 to your computer and use it in GitHub Desktop.
PubSubClient sample for ESP8266 Arduino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <PubSubClient.h> | |
#include <ESP8266WiFi.h> | |
const char* ssid = "................."; | |
const char* password = "................"; | |
char* topic = "esp8266_arduino_out"; | |
char* server = "iot.eclipse.org"; | |
WiFiClient wifiClient; | |
PubSubClient client(server, 1883, callback, wifiClient); | |
void callback(char* topic, byte* payload, unsigned int length) { | |
// handle message arrived | |
} | |
String macToStr(const uint8_t* mac) | |
{ | |
String result; | |
for (int i = 0; i < 6; ++i) { | |
result += String(mac[i], 16); | |
if (i < 5) | |
result += ':'; | |
} | |
return result; | |
} | |
void setup() { | |
Serial.begin(115200); | |
delay(10); | |
Serial.println(); | |
Serial.println(); | |
Serial.print("Connecting to "); | |
Serial.println(ssid); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(""); | |
Serial.println("WiFi connected"); | |
Serial.println("IP address: "); | |
Serial.println(WiFi.localIP()); | |
// Generate client name based on MAC address and last 8 bits of microsecond counter | |
String clientName; | |
clientName += "esp8266-"; | |
uint8_t mac[6]; | |
WiFi.macAddress(mac); | |
clientName += macToStr(mac); | |
clientName += "-"; | |
clientName += String(micros() & 0xff, 16); | |
Serial.print("Connecting to "); | |
Serial.print(server); | |
Serial.print(" as "); | |
Serial.println(clientName); | |
if (client.connect((char*) clientName.c_str())) { | |
Serial.println("Connected to MQTT broker"); | |
Serial.print("Topic is: "); | |
Serial.println(topic); | |
if (client.publish(topic, "hello from ESP8266")) { | |
Serial.println("Publish ok"); | |
} | |
else { | |
Serial.println("Publish failed"); | |
} | |
} | |
else { | |
Serial.println("MQTT connect failed"); | |
Serial.println("Will reset and try again..."); | |
abort(); | |
} | |
} | |
void loop() { | |
static int counter = 0; | |
String payload = "{\"micros\":"; | |
payload += micros(); | |
payload += ",\"counter\":"; | |
payload += counter; | |
payload += "}"; | |
if (client.connected()){ | |
Serial.print("Sending payload: "); | |
Serial.println(payload); | |
if (client.publish(topic, (char*) payload.c_str())) { | |
Serial.println("Publish ok"); | |
} | |
else { | |
Serial.println("Publish failed"); | |
} | |
} | |
++counter; | |
delay(5000); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment