fabelizer

21019 CODE

Feb 10th, 2019
104
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/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_STAIPASSIGNED:
  78. break;
  79. case SYSTEM_EVENT_AP_STACONNECTED:
  80. break;
  81. case SYSTEM_EVENT_AP_STADISCONNECTED:
  82. break;
  83. case SYSTEM_EVENT_AP_PROBEREQRECVED:
  84. break;
  85. case SYSTEM_EVENT_GOT_IP6:
  86. break;
  87. case SYSTEM_EVENT_ETH_START:
  88. break;
  89. case SYSTEM_EVENT_ETH_STOP:
  90. break;
  91. case SYSTEM_EVENT_ETH_CONNECTED:
  92. break;
  93. case SYSTEM_EVENT_ETH_DISCONNECTED:
  94. break;
  95. case SYSTEM_EVENT_ETH_GOT_IP:
  96. break;
  97. case SYSTEM_EVENT_MAX:
  98. break;
  99. }
  100. }
  101.  
  102. void onMqttConnect(bool sessionPresent) {
  103. Serial.println("Connected to MQTT.");
  104. Serial.print("Session present: ");
  105. Serial.println(sessionPresent);
  106. uint16_t packetIdSub = mqttClient.subscribe("test/lol", 2);
  107. Serial.print("Subscribing at QoS 2, packetId: ");
  108. Serial.println(packetIdSub);
  109. mqttClient.publish("test/lol", 0, true, "test 1");
  110. Serial.println("Publishing at QoS 0");
  111. uint16_t packetIdPub1 = mqttClient.publish("test/lol", 1, true, "test 2");
  112. Serial.print("Publishing at QoS 1, packetId: ");
  113. Serial.println(packetIdPub1);
  114. uint16_t packetIdPub2 = mqttClient.publish("test/lol", 2, true, "test 3");
  115. Serial.print("Publishing at QoS 2, packetId: ");
  116. Serial.println(packetIdPub2);
  117. }
  118.  
  119. void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) {
  120. Serial.println("Disconnected from MQTT.");
  121.  
  122. if (WiFi.isConnected()) {
  123. xTimerStart(mqttReconnectTimer, 0);
  124. }
  125. }
  126.  
  127. void onMqttSubscribe(uint16_t packetId, uint8_t qos) {
  128. Serial.println("Subscribe acknowledged.");
  129. Serial.print(" packetId: ");
  130. Serial.println(packetId);
  131. Serial.print(" qos: ");
  132. Serial.println(qos);
  133. }
  134.  
  135. void onMqttUnsubscribe(uint16_t packetId) {
  136. Serial.println("Unsubscribe acknowledged.");
  137. Serial.print(" packetId: ");
  138. Serial.println(packetId);
  139. }
  140.  
  141. void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) {
  142. Serial.println("Publish received.");
  143. Serial.print(" topic: ");
  144. Serial.println(topic);
  145. Serial.print(" qos: ");
  146. Serial.println(properties.qos);
  147. Serial.print(" dup: ");
  148. Serial.println(properties.dup);
  149. Serial.print(" retain: ");
  150. Serial.println(properties.retain);
  151. Serial.print(" len: ");
  152. Serial.println(len);
  153. Serial.print(" index: ");
  154. Serial.println(index);
  155. Serial.print(" total: ");
  156. Serial.println(total);
  157. }
  158.  
  159. void onMqttPublish(uint16_t packetId) {
  160. Serial.println("Publish acknowledged.");
  161. Serial.print(" packetId: ");
  162. Serial.println(packetId);
  163. }
  164.  
  165. void setup() {
  166. Serial.begin(115200);
  167. Serial.println();
  168. Serial.println();
  169.  
  170. mqttReconnectTimer = xTimerCreate("mqttTimer", pdMS_TO_TICKS(2000), pdFALSE, (void*)0, reinterpret_cast<TimerCallbackFunction_t>(connectToMqtt));
  171. wifiReconnectTimer = xTimerCreate("wifiTimer", pdMS_TO_TICKS(2000), pdFALSE, (void*)0, reinterpret_cast<TimerCallbackFunction_t>(connectToWifi));
  172.  
  173. WiFi.onEvent(WiFiEvent);
  174.  
  175. mqttClient.onConnect(onMqttConnect);
  176. mqttClient.onDisconnect(onMqttDisconnect);
  177. mqttClient.onSubscribe(onMqttSubscribe);
  178. mqttClient.onUnsubscribe(onMqttUnsubscribe);
  179. mqttClient.onMessage(onMqttMessage);
  180. mqttClient.onPublish(onMqttPublish);
  181. mqttClient.setServer(MQTT_HOST, MQTT_PORT);
  182.  
  183. connectToWifi();
  184. }
  185.  
  186. void loop() {
  187. }
RAW Paste Data