├── README.md ├── esp8266_lamp_module └── esp8266_lamp_module.ino └── esp8266_sensor_module └── esp8266_sensor_module.ino /README.md: -------------------------------------------------------------------------------- 1 | # adafruit-io-esp8266 2 | ESP8266 Home Automation projects using Adafruit IO 3 | -------------------------------------------------------------------------------- /esp8266_lamp_module/esp8266_lamp_module.ino: -------------------------------------------------------------------------------- 1 | /*************************************************** 2 | Adafruit ESP8266 Lamp Controller Module 3 | 4 | Must use ESP8266 Arduino from: 5 | https://github.com/esp8266/Arduino 6 | Works great with Adafruit's Huzzah ESP board: 7 | ----> https://www.adafruit.com/product/2471 8 | Adafruit invests time and resources providing this open source code, 9 | please support Adafruit and open-source hardware by purchasing 10 | products from Adafruit! 11 | Written by Tony DiCola for Adafruit Industries. 12 | Adafruit IO example additions by Todd Treece. 13 | MIT license, all text above must be included in any redistribution 14 | ****************************************************/ 15 | 16 | // Libraries 17 | #include 18 | #include "Adafruit_MQTT.h" 19 | #include "Adafruit_MQTT_Client.h" 20 | 21 | // Lamp pin 22 | const int lamp_pin = 5; 23 | 24 | // WiFi parameters 25 | #define WLAN_SSID "WLAN_SSID" 26 | #define WLAN_PASS "WLAN_PASS" 27 | 28 | // Adafruit IO 29 | #define AIO_SERVER "io.adafruit.com" 30 | #define AIO_SERVERPORT 1883 31 | #define AIO_USERNAME "AIO_USERNAME" 32 | #define AIO_KEY "AIO_KEY" 33 | 34 | // Functions 35 | void connect(); 36 | 37 | // Create an ESP8266 WiFiClient class to connect to the MQTT server. 38 | WiFiClient client; 39 | 40 | // Store the MQTT server, client ID, username, and password in flash memory. 41 | // This is required for using the Adafruit MQTT library. 42 | const char MQTT_SERVER[] PROGMEM = AIO_SERVER; 43 | // Set a unique MQTT client ID using the AIO key + the date and time the sketch 44 | // was compiled (so this should be unique across multiple devices for a user, 45 | // alternatively you can manually set this to a GUID or other random value). 46 | const char MQTT_CLIENTID[] PROGMEM = AIO_KEY __DATE__ __TIME__; 47 | const char MQTT_USERNAME[] PROGMEM = AIO_USERNAME; 48 | const char MQTT_PASSWORD[] PROGMEM = AIO_KEY; 49 | 50 | // Setup the MQTT client class by passing in the WiFi client and MQTT server and login details. 51 | Adafruit_MQTT_Client mqtt(&client, MQTT_SERVER, AIO_SERVERPORT, MQTT_CLIENTID, MQTT_USERNAME, MQTT_PASSWORD); 52 | 53 | /****************************** Feeds ***************************************/ 54 | 55 | // Setup a feed called 'lamp' for subscribing to changes. 56 | // Notice MQTT paths for AIO follow the form: /feeds/ 57 | const char LAMP_FEED[] PROGMEM = AIO_USERNAME "/feeds/lamp"; 58 | Adafruit_MQTT_Subscribe lamp = Adafruit_MQTT_Subscribe(&mqtt, LAMP_FEED); 59 | 60 | /*************************** Sketch Code ************************************/ 61 | 62 | void setup() { 63 | 64 | // Set lamp pin to output 65 | pinMode(lamp_pin, OUTPUT); 66 | 67 | Serial.begin(115200); 68 | 69 | Serial.println(F("Adafruit IO Example")); 70 | 71 | // Connect to WiFi access point. 72 | Serial.println(); Serial.println(); 73 | delay(10); 74 | Serial.print(F("Connecting to ")); 75 | Serial.println(WLAN_SSID); 76 | 77 | WiFi.begin(WLAN_SSID, WLAN_PASS); 78 | while (WiFi.status() != WL_CONNECTED) { 79 | delay(500); 80 | Serial.print(F(".")); 81 | } 82 | Serial.println(); 83 | 84 | Serial.println(F("WiFi connected")); 85 | Serial.println(F("IP address: ")); 86 | Serial.println(WiFi.localIP()); 87 | 88 | // listen for events on the lamp feed 89 | mqtt.subscribe(&lamp); 90 | 91 | // connect to adafruit io 92 | connect(); 93 | 94 | } 95 | 96 | void loop() { 97 | 98 | Adafruit_MQTT_Subscribe *subscription; 99 | 100 | // ping adafruit io a few times to make sure we remain connected 101 | if(! mqtt.ping(3)) { 102 | // reconnect to adafruit io 103 | if(! mqtt.connected()) 104 | connect(); 105 | } 106 | 107 | // this is our 'wait for incoming subscription packets' busy subloop 108 | while (subscription = mqtt.readSubscription(1000)) { 109 | 110 | // we only care about the lamp events 111 | if (subscription == &lamp) { 112 | 113 | // convert mqtt ascii payload to int 114 | char *value = (char *)lamp.lastread; 115 | Serial.print(F("Received: ")); 116 | Serial.println(value); 117 | 118 | // Apply message to lamp 119 | String message = String(value); 120 | message.trim(); 121 | if (message == "ON") {digitalWrite(lamp_pin, HIGH);} 122 | if (message == "OFF") {digitalWrite(lamp_pin, LOW);} 123 | 124 | } 125 | 126 | } 127 | 128 | } 129 | 130 | // connect to adafruit io via MQTT 131 | void connect() { 132 | 133 | Serial.print(F("Connecting to Adafruit IO... ")); 134 | 135 | int8_t ret; 136 | 137 | while ((ret = mqtt.connect()) != 0) { 138 | 139 | switch (ret) { 140 | case 1: Serial.println(F("Wrong protocol")); break; 141 | case 2: Serial.println(F("ID rejected")); break; 142 | case 3: Serial.println(F("Server unavail")); break; 143 | case 4: Serial.println(F("Bad user/pass")); break; 144 | case 5: Serial.println(F("Not authed")); break; 145 | case 6: Serial.println(F("Failed to subscribe")); break; 146 | default: Serial.println(F("Connection failed")); break; 147 | } 148 | 149 | if(ret >= 0) 150 | mqtt.disconnect(); 151 | 152 | Serial.println(F("Retrying connection...")); 153 | delay(5000); 154 | 155 | } 156 | 157 | Serial.println(F("Adafruit IO Connected!")); 158 | 159 | } 160 | -------------------------------------------------------------------------------- /esp8266_sensor_module/esp8266_sensor_module.ino: -------------------------------------------------------------------------------- 1 | /*************************************************** 2 | Adafruit ESP8266 Sensor Module 3 | 4 | Must use ESP8266 Arduino from: 5 | https://github.com/esp8266/Arduino 6 | Works great with Adafruit's Huzzah ESP board: 7 | ----> https://www.adafruit.com/product/2471 8 | Adafruit invests time and resources providing this open source code, 9 | please support Adafruit and open-source hardware by purchasing 10 | products from Adafruit! 11 | Written by Tony DiCola for Adafruit Industries. 12 | MIT license, all text above must be included in any redistribution 13 | ****************************************************/ 14 | 15 | // Libraries 16 | #include 17 | #include "Adafruit_MQTT.h" 18 | #include "Adafruit_MQTT_Client.h" 19 | #include "DHT.h" 20 | 21 | // DHT 11 sensor 22 | #define DHTPIN 5 23 | #define DHTTYPE DHT22 24 | 25 | // WiFi parameters 26 | #define WLAN_SSID "WLAN_SSID" 27 | #define WLAN_PASS "WLAN_PASS" 28 | 29 | // Adafruit IO 30 | #define AIO_SERVER "io.adafruit.com" 31 | #define AIO_SERVERPORT 1883 32 | #define AIO_USERNAME "AIO_USERNAME" 33 | #define AIO_KEY "AIO_KEY" // Obtained from account info on io.adafruit.com 34 | 35 | // DHT sensor 36 | DHT dht(DHTPIN, DHTTYPE, 15); 37 | 38 | // Create an ESP8266 WiFiClient class to connect to the MQTT server. 39 | WiFiClient client; 40 | 41 | // Setup the MQTT client class by passing in the WiFi client and MQTT server and login details. 42 | Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY); 43 | 44 | // Setup feeds for temperature & humidity 45 | Adafruit_MQTT_Publish temperature = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/temperature"); 46 | Adafruit_MQTT_Publish humidity = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/humidity"); 47 | 48 | /*************************** Sketch Code ************************************/ 49 | 50 | void setup() { 51 | 52 | // Init sensor 53 | dht.begin(); 54 | 55 | Serial.begin(115200); 56 | Serial.println(F("Adafruit IO Example")); 57 | 58 | // Connect to WiFi access point. 59 | Serial.println(); Serial.println(); 60 | delay(10); 61 | Serial.print(F("Connecting to ")); 62 | Serial.println(WLAN_SSID); 63 | 64 | WiFi.begin(WLAN_SSID, WLAN_PASS); 65 | while (WiFi.status() != WL_CONNECTED) { 66 | delay(500); 67 | Serial.print(F(".")); 68 | } 69 | Serial.println(); 70 | 71 | Serial.println(F("WiFi connected")); 72 | Serial.println(F("IP address: ")); 73 | Serial.println(WiFi.localIP()); 74 | 75 | // connect to adafruit io 76 | connect(); 77 | 78 | } 79 | 80 | void loop() { 81 | 82 | // ping adafruit io a few times to make sure we remain connected 83 | if(! mqtt.ping(3)) { 84 | // reconnect to adafruit io 85 | if(! mqtt.connected()) 86 | connect(); 87 | } 88 | 89 | // Grab the current state of the sensor 90 | int humidity_data = (int)dht.readHumidity(); 91 | int temperature_data = (int)dht.readTemperature(); 92 | 93 | // By default, the temperature report is in Celsius, for Fahrenheit uncomment 94 | // following line. 95 | // temperature_data = temperature_data*(9.0/5.0) + 32.0; 96 | 97 | // Publish data 98 | if (! temperature.publish(temperature_data)) 99 | Serial.println(F("Failed to publish temperature")); 100 | else 101 | Serial.println(F("Temperature published!")); 102 | 103 | if (! humidity.publish(humidity_data)) 104 | Serial.println(F("Failed to publish humidity")); 105 | else 106 | Serial.println(F("Humidity published!")); 107 | 108 | // Repeat every 10 seconds 109 | delay(10000); 110 | 111 | } 112 | 113 | // connect to adafruit io via MQTT 114 | void connect() { 115 | 116 | Serial.print(F("Connecting to Adafruit IO... ")); 117 | 118 | int8_t ret; 119 | 120 | while ((ret = mqtt.connect()) != 0) { 121 | 122 | switch (ret) { 123 | case 1: Serial.println(F("Wrong protocol")); break; 124 | case 2: Serial.println(F("ID rejected")); break; 125 | case 3: Serial.println(F("Server unavail")); break; 126 | case 4: Serial.println(F("Bad user/pass")); break; 127 | case 5: Serial.println(F("Not authed")); break; 128 | case 6: Serial.println(F("Failed to subscribe")); break; 129 | default: Serial.println(F("Connection failed")); break; 130 | } 131 | 132 | if(ret >= 0) 133 | mqtt.disconnect(); 134 | 135 | Serial.println(F("Retrying connection...")); 136 | delay(5000); 137 | 138 | } 139 | 140 | Serial.println(F("Adafruit IO Connected!")); 141 | 142 | } 143 | 144 | --------------------------------------------------------------------------------