esp8266_tower.ino
1 // SPDX-FileCopyrightText: 2019 Tony DiCola for Adafruit Industries 2 // 3 // SPDX-License-Identifier: MIT 4 5 /*************************************************** 6 Adafruit MQTT Library ESP8266 Example 7 Must use ESP8266 Arduino from: 8 https://github.com/esp8266/Arduino 9 Written by Tony DiCola for Adafruit Industries. 10 MIT license, all text above must be included in any redistribution 11 ****************************************************/ 12 13 #include <ESP8266WiFi.h> 14 #include "Adafruit_MQTT.h" 15 #include "Adafruit_MQTT_Client.h" 16 17 #define RED 13 18 #define YELLOW 5 19 #define GREEN 4 20 21 // Prototypes for functions 22 void MQTT_connect(); 23 24 /************************* WiFi Access Point *********************************/ 25 26 #define WLAN_SSID "YOURWIFI" 27 #define WLAN_PASS "YOURWIFIPASS" 28 29 /************************* Adafruit.io Setup *********************************/ 30 31 #define AIO_SERVER "io.adafruit.com" 32 #define AIO_SERVERPORT 1883 33 #define AIO_USERNAME "YOURUSERNAME" 34 #define AIO_KEY "YOURAPIKEY" 35 36 /************ Global State (you don't need to change this!) ******************/ 37 38 // Create an ESP8266 WiFiClient class to connect to the MQTT server. 39 WiFiClient client; 40 41 // Store the MQTT server, username, and password in flash memory. 42 // This is required for using the Adafruit MQTT library. 43 const char MQTT_SERVER[] PROGMEM = AIO_SERVER; 44 const char MQTT_USERNAME[] PROGMEM = AIO_USERNAME; 45 const char MQTT_PASSWORD[] PROGMEM = AIO_KEY; 46 47 // Setup the MQTT client class by passing in the WiFi client and MQTT server and login details. 48 Adafruit_MQTT_Client mqtt(&client, MQTT_SERVER, AIO_SERVERPORT, MQTT_USERNAME, MQTT_PASSWORD); 49 50 /****************************** Feeds ***************************************/ 51 52 // Setup a feed called 'onoff' for subscribing to changes. 53 const char RED_FEED[] PROGMEM = AIO_USERNAME "/feeds/redlight"; 54 Adafruit_MQTT_Subscribe redlight = Adafruit_MQTT_Subscribe(&mqtt, RED_FEED); 55 56 const char YELLOW_FEED[] PROGMEM = AIO_USERNAME "/feeds/yellowlight"; 57 Adafruit_MQTT_Subscribe yellowlight = Adafruit_MQTT_Subscribe(&mqtt, YELLOW_FEED); 58 59 const char GREEN_FEED[] PROGMEM = AIO_USERNAME "/feeds/greenlight"; 60 Adafruit_MQTT_Subscribe greenlight = Adafruit_MQTT_Subscribe(&mqtt, GREEN_FEED); 61 62 /*************************** Sketch Code ************************************/ 63 64 void setup() { 65 Serial.begin(115200); 66 delay(10); 67 68 Serial.println(F("Adafruit MQTT LED tower")); 69 70 // Connect to WiFi access point. 71 Serial.println(); Serial.println(); 72 Serial.print("Connecting to "); 73 Serial.println(WLAN_SSID); 74 75 WiFi.begin(WLAN_SSID, WLAN_PASS); 76 while (WiFi.status() != WL_CONNECTED) { 77 delay(500); 78 Serial.print("."); 79 } 80 Serial.println(); 81 82 Serial.println("WiFi connected"); 83 Serial.println("IP address: "); Serial.println(WiFi.localIP()); 84 85 // Setup MQTT subscription for onoff feed. 86 mqtt.subscribe(&redlight); 87 mqtt.subscribe(&yellowlight); 88 mqtt.subscribe(&greenlight); 89 90 pinMode(RED, OUTPUT); 91 pinMode(YELLOW, OUTPUT); 92 pinMode(GREEN, OUTPUT); 93 } 94 95 uint32_t x=0; 96 97 void loop() { 98 // Ensure the connection to the MQTT server is alive (this will make the first 99 // connection and automatically reconnect when disconnected). See the MQTT_connect 100 // function definition further below. 101 MQTT_connect(); 102 103 // this is our 'wait for incoming subscription packets' busy subloop 104 Adafruit_MQTT_Subscribe *subscription; 105 while ((subscription = mqtt.readSubscription(10000))) { 106 if (subscription == &redlight) { 107 Serial.print(F("RED Got: ")); 108 Serial.println((char *)redlight.lastread); 109 110 if (0 == strcmp((char *)redlight.lastread, "OFF")) { 111 digitalWrite(RED, LOW); 112 } 113 if (0 == strcmp((char *)redlight.lastread, "ON")) { 114 digitalWrite(RED, HIGH); 115 } 116 } 117 118 if (subscription == &yellowlight) { 119 Serial.print(F("YELLOW Got: ")); 120 Serial.println((char *)yellowlight.lastread); 121 122 if (0 == strcmp((char *)yellowlight.lastread, "OFF")) { 123 digitalWrite(YELLOW, LOW); 124 } 125 if (0 == strcmp((char *)yellowlight.lastread, "ON")) { 126 digitalWrite(YELLOW, HIGH); 127 } 128 } 129 130 if (subscription == &greenlight) { 131 Serial.print(F("GREEN Got: ")); 132 Serial.println((char *)greenlight.lastread); 133 134 if (0 == strcmp((char *)greenlight.lastread, "OFF")) { 135 digitalWrite(GREEN, LOW); 136 } 137 if (0 == strcmp((char *)greenlight.lastread, "ON")) { 138 digitalWrite(GREEN, HIGH); 139 } 140 } 141 } 142 143 144 // ping the server to keep the mqtt connection alive 145 if(! mqtt.ping()) { 146 mqtt.disconnect(); 147 } 148 } 149 150 // Function to connect and reconnect as necessary to the MQTT server. 151 // Should be called in the loop function and it will take care if connecting. 152 void MQTT_connect() { 153 int8_t ret; 154 155 // Stop if already connected. 156 if (mqtt.connected()) { 157 return; 158 } 159 160 Serial.print("Connecting to MQTT... "); 161 162 while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected 163 Serial.println(mqtt.connectErrorString(ret)); 164 Serial.println("Retrying MQTT connection in 5 seconds..."); 165 mqtt.disconnect(); 166 delay(5000); // wait 5 seconds 167 } 168 Serial.println("MQTT Connected!"); 169 }