Advertisement
Guest User

esp8266-mqtt-publisher-pubsub

a guest
Nov 23rd, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.31 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <PubSubClient.h>
  3.  
  4. const char* ssid          = "0xd52211";
  5. const char* password      = "c0d3men#";
  6. const char* mqttServer    = "m14.cloudmqtt.com";
  7. const int   mqttPort      = 19032;
  8. const char* mqttUser      = "xvatcjpq";
  9. const char* mqttPassword  = "_VhOM2tvhFnc";
  10. const char* mqttClientID  = "ESP8266-ClientID-shaikh";
  11. const char* mqttOutTopic  = "ESP/Pub/PubHello";
  12. const char* mqttInTopic   = "ESP/Sub/SubHello";
  13.  
  14.  
  15. char  msg[50];
  16. int   value     = 0;
  17. long  lastMsg   = 0;
  18.  
  19.  
  20. WiFiClient espClient;
  21. PubSubClient client(espClient);
  22.  
  23. void setup()
  24. {
  25.   Serial.begin(115200);
  26.   WiFi.begin(ssid, password);
  27.   Serial.println("Connecting to WiFi..");
  28.   while (WiFi.status() != WL_CONNECTED)
  29.   {
  30.     delay(500);
  31.     Serial.println(".");
  32.   }
  33.  
  34.   Serial.println("CONNECTED!");
  35.   Serial.println("IP: ");
  36.   Serial.print(WiFi.localIP());
  37.   pinMode(BUILTIN_LED, OUTPUT);
  38.  
  39.   client.setServer(mqttServer, mqttPort);
  40.   client.setCallback(callback);
  41. }
  42.  
  43. void callback(char* topic, byte* payload, unsigned int length)
  44. {
  45.   Serial.print("Message arrived in topic: ");
  46.   Serial.println(topic);
  47.   Serial.print("Message:");
  48.   for (int i = 0; i < length; i++)
  49.   {
  50.     Serial.print((char)payload[i]);
  51.   }
  52.   Serial.println("\n-----------------------");
  53.  
  54.   if ((char)payload[0] == '1')
  55.   {
  56.     Serial.println("\n\nLIGHT TURNED ON\n\n\n");
  57.     digitalWrite(BUILTIN_LED, LOW);
  58.   }
  59.   else
  60.   {
  61.     Serial.println("\n\nLIGHT TURNED OFF\n\n\n");
  62.     digitalWrite(BUILTIN_LED, HIGH);
  63.   }
  64. }
  65.  
  66. void reconnect() {
  67.   while (!client.connected()) {
  68.     Serial.print("\n\nConnecting to CloudMQTT...");
  69.     if(client.connect(mqttClientID, mqttUser, mqttPassword))
  70.     {
  71.       delay(10);
  72.       Serial.println("CONNECTED");
  73.     }
  74.     else
  75.     {
  76.       Serial.print("Failed! RC = ");
  77.       Serial.print(client.state());
  78.       Serial.println(" try again in 5 seconds");
  79.       delay(5000);
  80.     }
  81.   }
  82. }
  83.  
  84. void loop() {
  85.   if (!client.connected()) {
  86.     reconnect();
  87.   }
  88.   client.loop();
  89.  
  90.   long now = millis();
  91.   if (now - lastMsg > 10000) {
  92.     lastMsg = now;
  93.     ++value;
  94.     snprintf (msg, 75, "Test message #%ld", value);
  95.     Serial.print("Publish message: ");
  96.     Serial.println(msg);
  97.     client.publish(mqttOutTopic, msg);
  98.     client.subscribe(mqttInTopic);
  99.   }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement