safwan092

node mcu code with mqtt server io.adafruit

Jun 25th, 2020
134
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <ESP8266WiFi.h>
  2. #include "Adafruit_MQTT.h"
  3. #include "Adafruit_MQTT_Client.h"
  4. // WiFi parameters
  5. #define WLAN_SSID       "Network"
  6. #define WLAN_PASS       "123456789"
  7.  
  8. // Adafruit IO
  9. #define AIO_SERVER      "io.adafruit.com"
  10. #define AIO_SERVERPORT  1883
  11. #define AIO_USERNAME    "project961"
  12. #define AIO_KEY         "aio_Otov68k9Tf4vrfClHHM7keoEiuDQ"  // Obtained from account info on io.adafruit.com
  13.  
  14. // Create an ESP8266 WiFiClient class to connect to the MQTT server.
  15. WiFiClient client;
  16.  
  17. // Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
  18. Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
  19.  
  20. Adafruit_MQTT_Publish Attendance = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/COUNT");
  21.  
  22. char ID;
  23. /*************************** Sketch Code ************************************/
  24.  
  25. void setup() {
  26.   Serial.begin(115200);
  27.   Serial.println(F("Adafruit IO Example"));
  28.  
  29.   // Connect to WiFi access point.
  30.   Serial.println(); Serial.println();
  31.   delay(10);
  32.   Serial.print(F("Connecting to "));
  33.   Serial.println(WLAN_SSID);
  34.  
  35.   WiFi.begin(WLAN_SSID, WLAN_PASS);
  36.   while (WiFi.status() != WL_CONNECTED) {
  37.     delay(500);
  38.     Serial.print(F("."));
  39.   }
  40.   Serial.println();
  41.  
  42.   Serial.println(F("WiFi connected"));
  43.   Serial.println(F("IP address: "));
  44.   Serial.println(WiFi.localIP());
  45.  
  46.   // connect to adafruit io
  47.   connect();
  48.  
  49. }
  50.  
  51. // connect to adafruit io via MQTT
  52. void connect() {
  53.   Serial.print(F("Connecting to Adafruit IO... "));
  54.   int8_t ret;
  55.   while ((ret = mqtt.connect()) != 0) {
  56.     switch (ret) {
  57.       case 1: Serial.println(F("Wrong protocol")); break;
  58.       case 2: Serial.println(F("ID rejected")); break;
  59.       case 3: Serial.println(F("Server unavail")); break;
  60.       case 4: Serial.println(F("Bad user/pass")); break;
  61.       case 5: Serial.println(F("Not authed")); break;
  62.       case 6: Serial.println(F("Failed to subscribe")); break;
  63.       default: Serial.println(F("Connection failed")); break;
  64.     }
  65.  
  66.     if (ret >= 0)
  67.       mqtt.disconnect();
  68.  
  69.     Serial.println(F("Retrying connection..."));
  70.     delay(5000);
  71.   }
  72.   Serial.println(F("Adafruit IO Connected!"));
  73. }
  74.  
  75. void loop() {
  76.   // ping adafruit io a few times to make sure we remain connected
  77.   if (! mqtt.ping(3)) {
  78.     // reconnect to adafruit io
  79.     if (! mqtt.connected())
  80.       connect();
  81.   }
  82.   if ( Serial.available() ) { // Update and send only after 1 seconds
  83.     char a = Serial.read();
  84.     ID = a;
  85.  
  86.     if (! Attendance.publish(ID)) {                     //Publish to Adafruit
  87.       Serial.println(F("Failed"));
  88.     } else {
  89.       Serial.println(F("Sent!"));
  90.     }
  91.    
  92.   }
  93.   //delay(2000);
  94. }
RAW Paste Data