Have you ever wanted to put an internet-controlled light in your house that you can control from anywhere? If you want to, then read on!
Parts List
- 9-12V LED strip or other lighting
- ESP8266 development board (link to one I found on Amazon)
- Wires
- 2 10K? resistors
- 9V battery and battery snap
- Op-amp
- Micro USB data cable (do not use a charging cable! If you do, you will not be able to upload anything to the ESP8266!)
- Breadboard
First, go to my other ESP8266 tutorial if you have not worked with an ESP8266 in the Arduino IDE yet to install the IDE and the driver and test the ESP8266. When you're done, the instructions are below.
Step 1: Configure Adafruit IO
Make an account on Adafruit IO. Sign in with your account and click on "Feeds", then "Actions", then "Create a New Feed". Make the feed name "closetlight".
Step 2: Build the Circuit
Here is the circuit diagram:
Step 3: Run the Code
#include <ESP8266WiFi.h> #include "Adafruit_MQTT.h" #include "Adafruit_MQTT_Client.h" /************************* WiFi Access Point *********************************/ #define WLAN_SSID "Your WiFi Network Name" #define WLAN_PASS "Your WiFi Password" /************************* Adafruit.io Setup *********************************/ #define AIO_SERVER "io.adafruit.com" #define AIO_SERVERPORT 1883 // use 8883 for SSL #define AIO_USERNAME "Your Adafruit IO Username" #define AIO_KEY "Your Adafruit IO Key" // pin = 5; /************ Global State (you don't need to change this!) ******************/ // Create an ESP8266 WiFiClient class to connect to the MQTT server. WiFiClient client; // or... use WiFiFlientSecure for SSL //WiFiClientSecure client; // Store the MQTT server, username, and password in flash memory. // This is required for using the Adafruit MQTT library. // Setup the MQTT client class by passing in the WiFi client and MQTT server and login details. Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY); /****************************** Feeds ***************************************/ // Setup a feed called 'onoff' for subscribing to changes. Adafruit_MQTT_Subscribe light = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/closetlight"); /*************************** Sketch Code ************************************/ // Bug workaround for Arduino 1.6.6, it seems to need a function declaration // for some reason (only affects ESP8266, likely an arduino-builder bug). //void MQTT_connect(); void setup() { Serial.begin(115200); delay(10); Serial.println(F("IoT Light w/ Adafruit IO")); // Connect to WiFi access point. Serial.println(); Serial.println(); Serial.print("Connecting to "); Serial.println(WLAN_SSID); WiFi.begin(WLAN_SSID, WLAN_PASS); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); // Setup MQTT subscription for onoff feed. yield(); //MQTT_connect(); mqtt.subscribe(&light); pinMode(D1, OUTPUT); } void loop() { // Ensure the connection to the MQTT server is alive (this will make the first // connection and automatically reconnect when disconnected). See the MQTT_connect // function definition further below. MQTT_connect(); // this is our 'wait for incoming subscription packets' busy subloop // try to spend your time here Adafruit_MQTT_Subscribe *subscription; while ((subscription = mqtt.readSubscription(5000))) { if (subscription == &light) { char *val = (char *)light.lastread; Serial.print(F("Got: ")); Serial.println(val); if (strcmp(val, "1") == 0) { digitalWrite(D1, HIGH); Serial.println("Light turned on"); } if (strcmp(val, "0") == 0) { digitalWrite(D1, LOW); Serial.println("Light turned off"); } } } if(! mqtt.ping()) { mqtt.disconnect(); } } void MQTT_connect() { int8_t ret; // Stop if already connected. if (mqtt.connected()) { return; } Serial.print("Connecting to MQTT... "); uint8_t retries = 3; yield(); while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected Serial.println(mqtt.connectErrorString(ret)); Serial.println("Retrying MQTT connection in 5 seconds..."); mqtt.disconnect(); delay(2500); // wait 5 seconds retries--; if (retries == 0) { // basically die and wait for WDT to reset me while (1); } } Serial.println("MQTT Connected!"); }
Step 4: How To Use It
If the value of your feed called "closetlight" in Adafruit IO is 1, then your light will be on. If the value is 0, then your light will be off. Have fun!