Snippets

Peter Scargill MQTT Test ESP/Arduino

Updated by Peter Scargill

File snippet.txt Modified

  • Ignore whitespace
  • Hide word diff
  
   - connects to an MQTT server, providing username
     and password
-  - publishes "hello world" to the topic "outTopic"
-  - subscribes to the topic "inTopic"
+  - publishes a topic and reads the result every second....
+   - this appears to work reliably even with (a) lost WIFI and (b) lost MQTT. Count up to 1839 - will leave it running
 */
 
 #include <ESP8266WiFi.h>
             client.loop();
             if (mytimer<millis()) { client.publish("inTopic","hello world"); mytimer=millis()+1000; }
             delay(10); // added in further to a suggestion that some kind of delay() use may be needed in a tight loop.
+            if (WiFi.status() != WL_CONNECTED)  break;     // added these two checks in as without them there is no way in my inner loop to detect loss of signal    
+            if (!client.connected()) break;
           }
     }
  }
Updated by Peter Scargill

File snippet.txt Modified

  • Ignore whitespace
  • Hide word diff
           {
             client.loop();
             if (mytimer<millis()) { client.publish("inTopic","hello world"); mytimer=millis()+1000; }
+            delay(10); // added in further to a suggestion that some kind of delay() use may be needed in a tight loop.
           }
     }
  }
Created by Peter Scargill

File snippet.txt Added

  • Ignore whitespace
  • Hide word diff
+/*
+ Basic MQTT example with Authentication
+ 
+  - connects to an MQTT server, providing username
+    and password
+  - 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());
+}
+
+WiFiClient wclient;
+PubSubClient client(wclient, "things.mydomain.com");
+
+void setup() {
+  // Setup console
+  Serial.begin(115200);
+  delay(10);
+  Serial.println();
+  Serial.println();
+}
+
+void loop() {
+  if (WiFi.status() != WL_CONNECTED) {
+    Serial.print("Connecting to ");
+    Serial.print(ssid);
+    Serial.println("...");
+    WiFi.begin(ssid, pass);
+
+    if (WiFi.waitForConnectResult() != WL_CONNECTED)
+      return;
+    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");
+      } else {
+        Serial.println("Could not connect to MQTT server");   
+      }
+    }
+
+    if (client.connected())
+    {
+      unsigned long mytimer;
+      mytimer=millis()+1000;
+        while(1)
+          {
+            client.loop();
+            if (mytimer<millis()) { client.publish("inTopic","hello world"); mytimer=millis()+1000; }
+          }
+    }
+ }
+}
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.