#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); | |
} | |
This comment has been minimized.
This comment has been minimized.
I have the same issue as daxkamala. I'm curious to know what source you got ESP8266WiFi.h from. The one I found posted here(http://goo.gl/rqdfHY) on github doesn't contain the functions you show it has in your example. Thanks. |
This comment has been minimized.
This comment has been minimized.
@daxkamala @drewlsvern The library is bundled with the ESP8266 port of the Arduino IDE. |
This comment has been minimized.
This comment has been minimized.
@igrr I don't see the library here: https://github.com/esp8266/Arduino/tree/esp8266/libraries |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Nope. I have just a few hours ago downloaded this lot and in that directory there is EEPROM, ESP8266mDNS, ESP8266WebServer, ESP8266WiFi, SPI, Ticker and Wire. I'm not seeing any signs of pubsubclient - and the original library just doesn't compile. ??? |
This comment has been minimized.
This comment has been minimized.
Cannot find that PubSub library - if one tries the orgiinal PubSub this happens in compilation - it also happens when trying a web page example - same thing.. This is from an attempt to try a web page example but same error.. c:/users/peter/documents/arduino/hardware/arduino-esp8266-master/tools/xtensa-lx106-elf/bin/../lib/gcc/xtensa-lx106-elf/4.8.2/../../../../xtensa-lx106-elf/bin/ld.exe: C:\Users\Peter\AppData\Local\Temp\build71807972327932918.tmp/WiFiWebServer.cpp.elf: hidden symbol `__dso_handle' isn't defined |
This comment has been minimized.
This comment has been minimized.
It was all starting to look good - added in the void * _dso_handle - that works a treat for the web page example - but PUBSUB... I added that in and it compiles just fine. In the example the connection is to a non-password-protected MQTT broker. I don't see that happening too often and my own Pi-based MQTT server (Mosquitto) has a user name (admin) and password. I tried the example with a modification to add username and password and it failed - so on a hunch - I tried a rubbish password... Here's the code - same as original but with username and dummy pass if (client.connect((char *) clientName.c_str()),"admin","werwer") { "werwer" is a completely fictitious password and yet - the connect returned true and of course the publish failed. What SHOULD have happened is that the connection itself should have failed. Any ideas? (Connectedd is deliberately spelling incorrectly - I wanted to ensure I'd actually blown the new code into the chip) - the important point is it thinks it has connected with an utterly wrong password. Connecting to loft-east |
This comment has been minimized.
This comment has been minimized.
Thanks for sharing this example - helped me a lot. But if somebody experience the same problems I've did - maybe (s)he will find this information useful: This sketch above does not contain a subscribe function - not a big deal, you just have to add a few lines to get it running
But then you think - ok, lets remove the constant noise on the mqtt bus and you remove the "client.publish" section. Now things get interesting, because your mqtt broker (mosquito in my case) will throw you out, because you're beeing idle and your not commiting messages... Why is that? As mentioned on the site of nick o'leary (http://knolleary.net/arduino-client-for-mqtt/api/#loop) you have to do the "loop" constantly. So if you add the following lines to your sketch, you should be able to subscribe to topics without the need to constantly publish stuff. After the client.connect you should add this
My main loop looks pretty easy:
For a proper handling of the callback-section of pubsubclient take a look at: http://m2mio.tumblr.com/post/30048662088/a-simple-example-arduino-mqtt-m2m-io - they provide a good code example. Cheers Björn |
This comment has been minimized.
This comment has been minimized.
Has anyone done any work on this to make it resilient. I think I'm right in saying that if you lose the connection for any reason (issue with MQTT broker, WIFI issue) thats' it, end of story. Would save some R&D if someone has already done this. In a remote situation, an error message is no good :-) |
This comment has been minimized.
This comment has been minimized.
Surely exercising the WDT is the way to go? |
This comment has been minimized.
This comment has been minimized.
if you want to just use watchdog, I think a call to abort() if not (client.connected()){ should do the trick. |
This comment has been minimized.
This comment has been minimized.
it worked for me when putting the folder PubSubClient from https://github.com/knolleary/pubsubclient into arduino-1.6.1\hardware\esp8266com\esp8266\libraries |
This comment has been minimized.
This comment has been minimized.
I have use this example it work find but some thing make me confuse, when I use callback
I publish full serial print
|
This comment has been minimized.
This comment has been minimized.
With latest build (installed via Boards Manager) I have PROGMEM incompatible issue with PubSubClient library. After some patches (deleting PROGMEM from PubSubClient.h and PubSubClient.cpp) working well. |
This comment has been minimized.
This comment has been minimized.
Hi, I've tried on both Arduino IDE's (1.6.1 & the latest 1.6.4), and I still see the same error: In file included from esp8266_pubsubclient.ino:1:0:
|
This comment has been minimized.
This comment has been minimized.
Hi |
This comment has been minimized.
This comment has been minimized.
Subscribe Problems |
This comment has been minimized.
This comment has been minimized.
@dogrocker, "String payloads = String((char_)payload);" will not work since payload is a byte_ and not null terminated, that's why it sends so much, until it runs into a null or max chars. @ parrotface, this would help you to I guess... |
This comment has been minimized.
This comment has been minimized.
Thanks for info I will try as soon as I get time |
This comment has been minimized.
This comment has been minimized.
I've got mine working fine now so just leave a reply if you get stuck. |
This comment has been minimized.
This comment has been minimized.
Arduino: 1.6.5 (Windows 7), Board: "Generic ESP8266 Module, 80 MHz, 115200, 512K (64K SPIFFS)" Build options changed, rebuilding all In file included from sketch_jul05a.ino:1:0: Always has error above, any suggestions? |
This comment has been minimized.
This comment has been minimized.
For the example above I had to use this PubSubClient library: But I had to change the call from the main program so it looked like this: And then... it worked. |
This comment has been minimized.
This comment has been minimized.
Naish21~ Thanks a lot^^ |
This comment has been minimized.
This comment has been minimized.
Compilation failed with arduino ide, had to move the void callback above PubSubClient, but it should be fine as Naish21 mentioned above aswell.. |
This comment has been minimized.
This comment has been minimized.
Has anyone got this working with connection to port 8883 with username/pass/TLS etc ? |
This comment has been minimized.
This comment has been minimized.
@AdamMiltonBarker - I've been trying and keep getting |
This comment has been minimized.
This comment has been minimized.
Thanks Naish21 for your advice. I downloaded my version of Mosquitto (v0.15) direct from the rasperian wheezy library & it was failing. When I wised up and downloaded Mosquitto from the Debian library (Latest version) all was goodness. So if you have followed the instructions above and are still not connecting, try checking your Mosquitto version - you need the latest as the old ones done work Instructions here |
This comment has been minimized.
This comment has been minimized.
i have used esp 12e pubsub client. i will sent the message through mqtt using ardunio ide 1,6.5.once upload my code with iot.eclipse.org mqtt server url means data will send . so i change my own mqtt server means message not send it show the error give me any suggestion pls? |
This comment has been minimized.
This comment has been minimized.
Thank you. It works good. |
This comment has been minimized.
Have you had this working? I can't get it to compile and the ESP8266Wifi.h doesn't provide the headers that pubsubClient requires (i.e. to mimic an Ethernet Shield). Am I using the wrong version?
Any help appreciated.