+ Basic MQTT example with Authentication
+ - connects to an MQTT server, providing username
+ - publishes "hello world" to the topic "outTopic"
+ - subscribes to the topic "inTopic"
+#include <ESP8266WiFi.h>
+#include <PubSubClient.h>
+const char *ssid = "wififorus"; // cannot be longer than 32 characters!
+const char *pass = "mywifipass"; //
+void callback(const MQTT::Publish& pub) {
+ Serial.print("Incoming: "); Serial.println(pub.payload_string());
+PubSubClient client(wclient, "things.mydomain.com");
+ if (WiFi.status() != WL_CONNECTED) {
+ Serial.print("Connecting to ");
+ WiFi.begin(ssid, pass);
+ if (WiFi.waitForConnectResult() != WL_CONNECTED)
+ Serial.println("WiFi connected");
+ if (WiFi.status() == WL_CONNECTED) {
+ if (!client.connected()) {
+ Serial.println("Connecting to MQTT server");
+ if (client.connect(MQTT::Connect("arduinoClient")
+ .set_auth("admin", "mymqttpass"))) {
+ Serial.println("Connected to MQTT server");
+ client.set_callback(callback);
+ client.subscribe("inTopic");
+ Serial.println("Could not connect to MQTT server");
+ if (client.connected())
+ if (mytimer<millis()) { client.publish("inTopic","hello world"); mytimer=millis()+1000; }