fabelizer

Untitled

Feb 9th, 2019
86
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. This example uses FreeRTOS softwaretimers as there is no built-in Ticker library
  3. */
  4.  
  5.  
  6. #include <WiFi.h>
  7. //extern "C" {
  8. #include <FreeRTOS.h>
  9. #include <freertos/timers.h>
  10. //}
  11. #include <AsyncMqttClient.h>
  12.  
  13. #define WIFI_SSID "yourSSID"
  14. #define WIFI_PASSWORD "yourpass"
  15.  
  16. #define MQTT_HOST IPAddress(192, 168, 1, 10)
  17. #define MQTT_PORT 1883
  18.  
  19. AsyncMqttClient mqttClient;
  20. TimerHandle_t mqttReconnectTimer;
  21. TimerHandle_t wifiReconnectTimer;
  22.  
  23. void connectToWifi() {
  24. Serial.println("Connecting to Wi-Fi...");
  25. WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  26. }
  27.  
  28. void connectToMqtt() {
  29. Serial.println("Connecting to MQTT...");
  30. mqttClient.connect();
  31. }
  32.  
  33. void WiFiEvent(WiFiEvent_t event) {
  34. Serial.printf("[WiFi-event] event: %d\n", event);
  35. switch(event) {
  36. case SYSTEM_EVENT_STA_GOT_IP:
  37. Serial.println("WiFi connected");
  38. Serial.println("IP address: ");
  39. Serial.println(WiFi.localIP());
  40. connectToMqtt();
  41. break;
  42. case SYSTEM_EVENT_STA_DISCONNECTED:
  43. Serial.println("WiFi lost connection");
  44. xTimerStop(mqttReconnectTimer, 0); // ensure we don't reconnect to MQTT while reconnecting to Wi-Fi
  45. xTimerStart(wifiReconnectTimer, 0);
  46. break;
  47. //DAK added events below to satisfy compiler for enumerated event handler in 'switch' function.
  48. case SYSTEM_EVENT_WIFI_READY:
  49. Serial.println("WIFI Ready!");
  50. break;
  51. case SYSTEM_EVENT_SCAN_DONE:
  52. break;
  53. case SYSTEM_EVENT_STA_START:
  54. break;
  55. case SYSTEM_EVENT_STA_STOP:
  56. break;
  57. case SYSTEM_EVENT_STA_CONNECTED:
  58. break;
  59. case SYSTEM_EVENT_STA_AUTHMODE_CHANGE:
  60. Serial.println("Authmode has CHANGED!");
  61. break;
  62. case SYSTEM_EVENT_STA_LOST_IP:
  63. Serial.println("Lost the IP");
  64. break;
  65. case SYSTEM_EVENT_STA_WPS_ER_SUCCESS:
  66. break;
  67. case SYSTEM_EVENT_STA_WPS_ER_FAILED:
  68. break;
  69. case SYSTEM_EVENT_STA_WPS_ER_TIMEOUT:
  70. break;
  71. case SYSTEM_EVENT_STA_WPS_ER_PIN:
  72. break;
  73. case SYSTEM_EVENT_AP_START:
  74. break;
  75. case SYSTEM_EVENT_AP_STOP:
  76. break;
  77. case SYSTEM_EVENT_AP_STACONNECTED:
  78. break;
  79. case SYSTEM_EVENT_AP_STADISCONNECTED:
  80. break;
  81. case SYSTEM_EVENT_AP_PROBEREQRECVED:
  82. break;
  83. case SYSTEM_EVENT_GOT_IP6:
  84. break;
  85. case SYSTEM_EVENT_ETH_START:
  86. break;
  87. case SYSTEM_EVENT_ETH_STOP:
  88. break;
  89. case SYSTEM_EVENT_ETH_CONNECTED:
  90. break;
  91. case SYSTEM_EVENT_ETH_DISCONNECTED:
  92. break;
  93. case SYSTEM_EVENT_ETH_GOT_IP:
  94. break;
  95. case SYSTEM_EVENT_MAX:
  96. break;
  97. }
  98. }
  99.  
  100. void onMqttConnect(bool sessionPresent) {
  101. Serial.println("Connected to MQTT.");
  102. Serial.print("Session present: ");
  103. Serial.println(sessionPresent);
  104. uint16_t packetIdSub = mqttClient.subscribe("test/lol", 2);
  105. Serial.print("Subscribing at QoS 2, packetId: ");
  106. Serial.println(packetIdSub);
  107. mqttClient.publish("test/lol", 0, true, "test 1");
  108. Serial.println("Publishing at QoS 0");
  109. uint16_t packetIdPub1 = mqttClient.publish("test/lol", 1, true, "test 2");
  110. Serial.print("Publishing at QoS 1, packetId: ");
  111. Serial.println(packetIdPub1);
  112. uint16_t packetIdPub2 = mqttClient.publish("test/lol", 2, true, "test 3");
  113. Serial.print("Publishing at QoS 2, packetId: ");
  114. Serial.println(packetIdPub2);
  115. }
  116.  
  117. void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) {
  118. Serial.println("Disconnected from MQTT.");
  119.  
  120. if (WiFi.isConnected()) {
  121. xTimerStart(mqttReconnectTimer, 0);
  122. }
  123. }
  124.  
  125. void onMqttSubscribe(uint16_t packetId, uint8_t qos) {
  126. Serial.println("Subscribe acknowledged.");
  127. Serial.print(" packetId: ");
  128. Serial.println(packetId);
  129. Serial.print(" qos: ");
  130. Serial.println(qos);
  131. }
  132.  
  133. void onMqttUnsubscribe(uint16_t packetId) {
  134. Serial.println("Unsubscribe acknowledged.");
  135. Serial.print(" packetId: ");
  136. Serial.println(packetId);
  137. }
  138.  
  139. void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) {
  140. Serial.println("Publish received.");
  141. Serial.print(" topic: ");
  142. Serial.println(topic);
  143. Serial.print(" qos: ");
  144. Serial.println(properties.qos);
  145. Serial.print(" dup: ");
  146. Serial.println(properties.dup);
  147. Serial.print(" retain: ");
  148. Serial.println(properties.retain);
  149. Serial.print(" len: ");
  150. Serial.println(len);
  151. Serial.print(" index: ");
  152. Serial.println(index);
  153. Serial.print(" total: ");
  154. Serial.println(total);
  155. }
  156.  
  157. void onMqttPublish(uint16_t packetId) {
  158. Serial.println("Publish acknowledged.");
  159. Serial.print(" packetId: ");
  160. Serial.println(packetId);
  161. }
  162.  
  163. void setup() {
  164. Serial.begin(115200);
  165. Serial.println();
  166. Serial.println();
  167.  
  168. mqttReconnectTimer = xTimerCreate("mqttTimer", pdMS_TO_TICKS(2000), pdFALSE, (void*)0, reinterpret_cast<TimerCallbackFunction_t>(connectToMqtt));
  169. wifiReconnectTimer = xTimerCreate("wifiTimer", pdMS_TO_TICKS(2000), pdFALSE, (void*)0, reinterpret_cast<TimerCallbackFunction_t>(connectToWifi));
  170.  
  171. WiFi.onEvent(WiFiEvent);
  172.  
  173. mqttClient.onConnect(onMqttConnect);
  174. mqttClient.onDisconnect(onMqttDisconnect);
  175. mqttClient.onSubscribe(onMqttSubscribe);
  176. mqttClient.onUnsubscribe(onMqttUnsubscribe);
  177. mqttClient.onMessage(onMqttMessage);
  178. mqttClient.onPublish(onMqttPublish);
  179. mqttClient.setServer(MQTT_HOST, MQTT_PORT);
  180.  
  181. connectToWifi();
  182. }
  183.  
  184. void loop() {
  185. }
RAW Paste Data