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