Advertisement
Guest User

MQTTClientAndrew

a guest
May 31st, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.60 KB | None | 0 0
  1. /**
  2.  * \file
  3.  *       ESP8266 MQTT Bridge example
  4.  * \author
  5.  *       Tuan PM <tuanpm@live.com>
  6.  */
  7. #include "Config.h"
  8. #include <SoftwareSerial.h>
  9. #include <espduino.h>
  10. #include <mqtt.h>
  11.  
  12. #define DEBUG_PORT_RX 2
  13. #define DEBUG_PORT_TX 3
  14. #define ESP8266_POWER_DOWN 4
  15.  
  16. SoftwareSerial debugPort(DEBUG_PORT_RX, DEBUG_PORT_TX); // RX, TX
  17. ESP esp(&Serial, &debugPort, ESP8266_POWER_DOWN);
  18. MQTT mqtt(&esp);
  19. boolean wifiConnected = false;
  20.  
  21. void wifiCb(void* response)
  22. {
  23.   uint32_t status;
  24.   RESPONSE res(response);
  25.  
  26.   if(res.getArgc() == 1) {
  27.     res.popArgs((uint8_t*)&status, 4);
  28.     if(status == STATION_GOT_IP) {
  29.       debugPort.println("WIFI CONNECTED");
  30.       mqtt.connect(MQTT_BROKER_IP, MQTT_BROKER_PORT, false);
  31.       wifiConnected = true;
  32.       //or mqtt.connect("host", 1883); /*without security ssl*/
  33.     } else {
  34.       wifiConnected = false;
  35.       mqtt.disconnect();
  36.     }
  37.    
  38.   }
  39. }
  40.  
  41. void mqttConnected(void* response)
  42. {
  43.   debugPort.println("Connected");
  44.   mqtt.subscribe(MQTT_SUBSCRIBED_TOPIC); //or mqtt.subscribe("topic"); /*with qos = 0*/
  45. //  mqtt.subscribe("/topic/1");
  46. //  mqtt.subscribe("/topic/2");
  47. //  mqtt.publish("/onoff_tutorial/0", "data0");
  48.  
  49. }
  50. void mqttDisconnected(void* response)
  51. {
  52.   debugPort.println("Disconnected123");
  53. }
  54.  
  55. void mqttPublished(void* response)
  56. {
  57.   debugPort.println(F("Published"));
  58. }
  59.  
  60. void mqttData(void* response)
  61. {
  62.  
  63.   char words[4];
  64.  
  65.   RESPONSE res(response);
  66.  
  67.   debugPort.print("Received: topic=");
  68.   String topic = res.popString();
  69.   debugPort.println(topic);
  70.  
  71.   debugPort.print("data=");
  72.   String data = res.popString();
  73.   debugPort.println(data);
  74.  
  75.   data.toCharArray(words,4);
  76. //  words(words);
  77.  
  78. }
  79.  
  80. void setup() {
  81.   Serial.begin(19200);
  82.   debugPort.begin(19200);
  83.   esp.enable();
  84.   delay(500);
  85.   esp.reset();
  86.   delay(500);
  87.   while(!esp.ready());
  88.  
  89.   debugPort.println("ARDUINO: setup mqtt client");
  90.   if(!mqtt.begin(MQTT_CLIENT_ID, MQTT_USERNAME, MQTT_PASSWORD, 120, 1)) {
  91.     debugPort.println("ARDUINO: fail to setup mqtt");
  92.     while(1);
  93.   }
  94.  
  95. //  debugPort.println("ARDUINO: setup mqtt lwt");
  96. //  mqtt.lwt("/lwt", "offline", 0, 0); //or mqtt.lwt("/lwt", "offline");
  97.  
  98. /*setup mqtt events */
  99.   mqtt.connectedCb.attach(&mqttConnected);
  100.   mqtt.disconnectedCb.attach(&mqttDisconnected);
  101.   mqtt.publishedCb.attach(&mqttPublished);
  102.   mqtt.dataCb.attach(&mqttData);
  103.  
  104.   /*setup wifi*/
  105.   debugPort.println("ARDUINO: setup wifi");
  106.   esp.wifiCb.attach(&wifiCb);
  107.  
  108.   esp.wifiConnect(AP_SSID,AP_PASSWORD);
  109.   debugPort.println("ARDUINO: system started");
  110. }
  111.  
  112. void loop() {
  113.   esp.process();
  114.   if(wifiConnected) {
  115.   }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement