Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
420
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.42 KB | None | 0 0
  1. /***************************************************
  2. Adafruit MQTT Library ESP8266 Example
  3.  
  4. Must use ESP8266 Arduino from:
  5. https://github.com/esp8266/Arduino
  6.  
  7. Works great with Adafruit's Huzzah ESP board & Feather
  8. ----> https://www.adafruit.com/product/2471
  9. ----> https://www.adafruit.com/products/2821
  10.  
  11. Adafruit invests time and resources providing this open source code,
  12. please support Adafruit and open-source hardware by purchasing
  13. products from Adafruit!
  14.  
  15. Written by Tony DiCola for Adafruit Industries.
  16. MIT license, all text above must be included in any redistribution
  17. ****************************************************/
  18. #include <ESP8266WiFi.h>
  19. #include "Adafruit_MQTT.h"
  20. #include "Adafruit_MQTT_Client.h"
  21. #include "LedControl.h"
  22.  
  23. /************************* WiFi Access Point *********************************/
  24.  
  25. #define WLAN_SSID "ssid"
  26. #define WLAN_PASS "pass"
  27.  
  28. /************************* Adafruit.io Setup *********************************/
  29.  
  30. #define AIO_SERVER "broker ip"
  31. #define AIO_SERVERPORT 1883 // use 8883 for SSL
  32. #define AIO_USERNAME "user"
  33. #define AIO_KEY "pass"
  34.  
  35. /************ Global State (you don't need to change this!) ******************/
  36.  
  37. // Create an ESP8266 WiFiClient class to connect to the MQTT server.
  38. WiFiClient client;
  39. // or... use WiFiFlientSecure for SSL
  40. //WiFiClientSecure client;
  41. LedControl lc = LedControl(13, 14, 2, 1);
  42.  
  43.  
  44. // Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
  45. Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
  46.  
  47. /****************************** Feeds ***************************************/
  48.  
  49. // Setup a feed called 'onoff' for subscribing to changes.
  50. Adafruit_MQTT_Subscribe subscribers = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/subscriber/count");
  51.  
  52. /*************************** Sketch Code ************************************/
  53.  
  54. // Bug workaround for Arduino 1.6.6, it seems to need a function declaration
  55. // for some reason (only affects ESP8266, likely an arduino-builder bug).
  56. void MQTT_connect();
  57.  
  58. void setup() {
  59. Serial.begin(115200);
  60. delay(10);
  61.  
  62. Serial.println(F("Adafruit MQTT demo"));
  63.  
  64. // Connect to WiFi access point.
  65. Serial.println(); Serial.println();
  66. Serial.print("Connecting to ");
  67. Serial.println(WLAN_SSID);
  68.  
  69. WiFi.begin(WLAN_SSID, WLAN_PASS);
  70. while (WiFi.status() != WL_CONNECTED) {
  71. delay(500);
  72. Serial.print(".");
  73. }
  74. Serial.println();
  75.  
  76. Serial.println("WiFi connected");
  77. Serial.println("IP address: "); Serial.println(WiFi.localIP());
  78.  
  79. // Setup MQTT subscription for onoff feed.
  80. mqtt.subscribe(&subscribers);
  81. lc.shutdown(0,false);
  82. /* Set the brightness to a medium values */
  83. lc.setIntensity(0,6);
  84. /* and clear the display */
  85. lc.clearDisplay(0);
  86. }
  87.  
  88. uint32_t x=0;
  89. int num0 = 0;
  90. int num1 = 0;
  91. int num2 = 0;
  92. int num3 = 0;
  93. int num4 = 0;
  94. int num5 = 0;
  95. int num6 = 0;
  96. int num7 = 0;
  97. int num8 = 0;
  98.  
  99.  
  100. void loop() {
  101. // Ensure the connection to the MQTT server is alive (this will make the first
  102. // connection and automatically reconnect when disconnected). See the MQTT_connect
  103. // function definition further below.
  104. MQTT_connect();
  105.  
  106. // this is our 'wait for incoming subscription packets' busy subloop
  107. // try to spend your time here
  108.  
  109. Adafruit_MQTT_Subscribe *subscription;
  110. while ((subscription = mqtt.readSubscription(5000))) {
  111. if (subscription == &subscribers) {
  112. Serial.print(F("Got: "));
  113. Serial.println((char *)subscribers.lastread);
  114. uint16_t number = atoi((char *)subscribers.lastread);
  115.  
  116.  
  117. num1 = (number / 1) % 10;
  118. lc.setDigit(0, 0, num1, false);
  119.  
  120. num2 = (number / 10) % 10;
  121. if (number > 9) {lc.setDigit(0, 1, num2, false);}
  122.  
  123. num3 = (number / 100) % 10;
  124. if (number > 99) {lc.setDigit(0, 2, num3, false);}
  125.  
  126. num4 = (number / 1000) % 10;
  127. if (number > 999) {lc.setDigit(0, 3, num4, false);}
  128.  
  129. num5 = (number / 10000) % 10;
  130. if (number > 9999) {lc.setDigit(0, 4, num5, false);}
  131.  
  132. num6 = (number / 100000) % 10;
  133. if (number > 99999) {lc.setDigit(0, 5, num6, false);}
  134.  
  135. num7 = (number / 1000000) % 10;
  136. if (number > 999999) {lc.setDigit(0, 6, num7, false);}
  137.  
  138. num8 = (number / 10000000) % 10;
  139. if (number > 9999999) {lc.setDigit(0, 7, num8, false);}
  140.  
  141. }
  142. }
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149. // ping the server to keep the mqtt connection alive
  150. // NOT required if you are publishing once every KEEPALIVE seconds
  151. /*
  152. if(! mqtt.ping()) {
  153. mqtt.disconnect();
  154. }
  155. */
  156. }
  157.  
  158. // Function to connect and reconnect as necessary to the MQTT server.
  159. // Should be called in the loop function and it will take care if connecting.
  160. void MQTT_connect() {
  161. int8_t ret;
  162.  
  163. // Stop if already connected.
  164. if (mqtt.connected()) {
  165. return;
  166. }
  167.  
  168. Serial.print("Connecting to MQTT... ");
  169.  
  170. uint8_t retries = 3;
  171. while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
  172. Serial.println(mqtt.connectErrorString(ret));
  173. Serial.println("Retrying MQTT connection in 5 seconds...");
  174. mqtt.disconnect();
  175. delay(5000); // wait 5 seconds
  176. retries--;
  177. if (retries == 0) {
  178. // basically die and wait for WDT to reset me
  179. while (1);
  180. }
  181. }
  182. Serial.println("MQTT Connected!");
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement