If you come home at night and you forgot to leave your light on, you should build this. =D
The development board in this project makes its own WiFi network, so when you are connected to it, you can control a light. That way, you can turn your light on from outside your house.
The development board in this project makes its own WiFi network, so when you are connected to it, you can control a light. That way, you can turn your light on from outside your house.
Parts List
- ESP8266 development board (link to one I found on Amazon)
- LED (I used green)
- 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
- solid-core wire
- USB power bank (like a battery with a USB output and Micro USB input)
- 330Ω resistor
So, let's get started!
If you have not used/uploaded code to an ESP8266 before, you will need to install a USB driver. "What for?" you ask. Well, do you see that little square chip on the ESP8266 that says, "SILABS CP2102"? Your computer does not have the driver software for that CP2102 chip. So you will have to install the CP2102 driver from Silicon Labs.
We will be using the Arduino IDE for this project. (If you don't know what Arduino is, click here. To install the IDE, click here.) If you do not have version 1.6.4 or later of the Arduino IDE, you will have to update it. Once you have version 1.6.4 or later and have installed the driver, you are ready to follow along.
If you have not used/uploaded code to an ESP8266 before, you will need to install a USB driver. "What for?" you ask. Well, do you see that little square chip on the ESP8266 that says, "SILABS CP2102"? Your computer does not have the driver software for that CP2102 chip. So you will have to install the CP2102 driver from Silicon Labs.
We will be using the Arduino IDE for this project. (If you don't know what Arduino is, click here. To install the IDE, click here.) If you do not have version 1.6.4 or later of the Arduino IDE, you will have to update it. Once you have version 1.6.4 or later and have installed the driver, you are ready to follow along.
Open up Arduino. Then, if you have a Mac, go to Arduino->Preferences. If you have a Windows computer, go to File->Preferences. See the "Additional Boards Manager URLs" section? Type "http://arduino.esp8266.com/stable/package_esp8266com_index.json" into it. Press "OK". Then go to Tools->Boards->Boards Manager. Look for a package that says, "esp8266 by ESP8266 Community". Then press "Install". Waiting, waiting, done! Now, select your kind of ESP8266 from Tools->Board in "ESP8266 Modules". Select NodeMCU 1.0 (ESP-12E Module) if you got the one I showed you. Now if you go into Tools, you should see something like this
on a Mac. Now, select your serial port by doing this: Plug in your ESP8266 to your computer. Then, look under Tools->Port. You should see a few different options. One of them should say "/dev/cu.SLAB_USBtoUART" in it on a Mac. On Windows, look at the COM port that only appears when the ESP8266 is plugged in. (I know, odd port name for Mac. That's just the driver (SLAB is for Silicon Labs) working.)
Now you're ready to upload code! (Arduino users, the ESP8266 uploading is WAY slower than Arduino uploading. Beware!) Well, almost. First you have to hook up pin 0 to GND. "Why do you have to do that? Hook up pin 0 to GND?", I hear you asking. It's because you are setting the ESP8266 into bootloader mode (ESP8266-speak for coding mode).
Now, load up the "Blink" sketch (sketch is Arduino terminology for program) in File->Examples->01.Basics->Blink. When it refers to pin 13, you should change it to pin 2 to blink the onboard blue LED. Now, let 'er upload! If this works, and the ESP8266 starts blinking its blue LED once a second, then your ESP8266 works and we can move on. If not, make sure you have a USB data cable and your upload speed is 115200.
Now, we are ready to build the project. Here is an image of the circuit:
And here is the code:
#include <ESP8266WiFi.h>
//////////////////////
// WiFi Definitions //
//////////////////////
const char WiFiAPPSK[] = "your-password";
/////////////////////
// Pin Definitions //
/////////////////////
const int LED_PIN = 2; // Thing's onboard, green LED
const int ANALOG_PIN = A0; // The only analog pin on the Thing
const int DIGITAL_PIN = 12; // Digital pin to be read
WiFiServer server(80);
void setup()
{
initHardware();
setupWiFi();
server.begin();
}
void loop()
{
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}
// Read the first line of the request
String req = client.readStringUntil('\r');
Serial.println(req);
client.flush();
// Match the request
int val = -1; // We'll use 'val' to keep track of both the
// request type (read/set) and value if set.
if (req.indexOf("/led/0") != -1)
val = 0; // Will write LED low
else if (req.indexOf("/led/1") != -1)
val = 1; // Will write LED high
digitalWrite(LED_PIN, val);
client.flush();
// Prepare the response. Start with the common header:
String s = "HTTP/1.1 200 OK\r\n";
s += "Content-Type: text/html\r\n\r\n";
s += "<!DOCTYPE HTML>\r\n<html>\r\n";
// If we're setting the LED, print out a message saying we did
if (val >= 0)
{
s += "LED is now ";
s += (val)?"on":"off";
s += "<br> <a href='/led/1'>Turn LED On</a><br><a href='/led/0'>Turn LED Off</a>";
}
else
{
s += "Invalid Request.<br> <a href='/led/1'>Turn LED On</a><br><a href='/led/0'>Turn LED Off</a>";
}
s += "</html>\n";
// Send the response to the client
client.print(s);
delay(1);
Serial.println("Client disonnected");
// The client will actually be disconnected
// when the function returns and 'client' object is detroyed
}
void setupWiFi()
{
WiFi.mode(WIFI_AP);
// Do a little work to get a unique-ish name. Append the
// last two bytes of the MAC (HEX'd) to "Thing-":
uint8_t mac[WL_MAC_ADDR_LENGTH];
WiFi.softAPmacAddress(mac);
String macID = String(mac[WL_MAC_ADDR_LENGTH - 2], HEX) +
String(mac[WL_MAC_ADDR_LENGTH - 1], HEX);
macID.toUpperCase();
String AP_NameString = "your-network-name";
char AP_NameChar[AP_NameString.length() + 1];
memset(AP_NameChar, 0, AP_NameString.length() + 1);
for (int i=0; i<AP_NameString.length(); i++)
AP_NameChar[i] = AP_NameString.charAt(i);
WiFi.softAP(AP_NameChar, WiFiAPPSK);
}
void initHardware()
{
Serial.begin(115200);
pinMode(DIGITAL_PIN, INPUT_PULLUP);
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
// Don't need to set ANALOG_PIN as input,
// that's all it can be.
}
#include <ESP8266WiFi.h>
//////////////////////
// WiFi Definitions //
//////////////////////
const char WiFiAPPSK[] = "your-password";
/////////////////////
// Pin Definitions //
/////////////////////
const int LED_PIN = 2; // Thing's onboard, green LED
const int ANALOG_PIN = A0; // The only analog pin on the Thing
const int DIGITAL_PIN = 12; // Digital pin to be read
WiFiServer server(80);
void setup()
{
initHardware();
setupWiFi();
server.begin();
}
void loop()
{
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}
// Read the first line of the request
String req = client.readStringUntil('\r');
Serial.println(req);
client.flush();
// Match the request
int val = -1; // We'll use 'val' to keep track of both the
// request type (read/set) and value if set.
if (req.indexOf("/led/0") != -1)
val = 0; // Will write LED low
else if (req.indexOf("/led/1") != -1)
val = 1; // Will write LED high
digitalWrite(LED_PIN, val);
client.flush();
// Prepare the response. Start with the common header:
String s = "HTTP/1.1 200 OK\r\n";
s += "Content-Type: text/html\r\n\r\n";
s += "<!DOCTYPE HTML>\r\n<html>\r\n";
// If we're setting the LED, print out a message saying we did
if (val >= 0)
{
s += "LED is now ";
s += (val)?"on":"off";
s += "<br> <a href='/led/1'>Turn LED On</a><br><a href='/led/0'>Turn LED Off</a>";
}
else
{
s += "Invalid Request.<br> <a href='/led/1'>Turn LED On</a><br><a href='/led/0'>Turn LED Off</a>";
}
s += "</html>\n";
// Send the response to the client
client.print(s);
delay(1);
Serial.println("Client disonnected");
// The client will actually be disconnected
// when the function returns and 'client' object is detroyed
}
void setupWiFi()
{
WiFi.mode(WIFI_AP);
// Do a little work to get a unique-ish name. Append the
// last two bytes of the MAC (HEX'd) to "Thing-":
uint8_t mac[WL_MAC_ADDR_LENGTH];
WiFi.softAPmacAddress(mac);
String macID = String(mac[WL_MAC_ADDR_LENGTH - 2], HEX) +
String(mac[WL_MAC_ADDR_LENGTH - 1], HEX);
macID.toUpperCase();
String AP_NameString = "your-network-name";
char AP_NameChar[AP_NameString.length() + 1];
memset(AP_NameChar, 0, AP_NameString.length() + 1);
for (int i=0; i<AP_NameString.length(); i++)
AP_NameChar[i] = AP_NameString.charAt(i);
WiFi.softAP(AP_NameChar, WiFiAPPSK);
}
void initHardware()
{
Serial.begin(115200);
pinMode(DIGITAL_PIN, INPUT_PULLUP);
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
// Don't need to set ANALOG_PIN as input,
// that's all it can be.
}
You may be wondering why the variable LED_PIN in the code is set to 2, because the circuit image hooks up the LED to pin 4. Here is why:
I got the above image from: http://www.instructables.com/id/Programming-ESP8266-ESP-12E-NodeMCU-Using-Arduino-/
Do you see the "D" pins and the "GPIO" pins? What is D4 linked up to? GPIO2! So this is why it makes sense to use the "GPIO" pin, not the "D" pin and why the code makes sense.
Do you see the "D" pins and the "GPIO" pins? What is D4 linked up to? GPIO2! So this is why it makes sense to use the "GPIO" pin, not the "D" pin and why the code makes sense.
How to control: connect to your ESP8266 at the network name and password in the code then go to this link: http://192.168.4.1 and you will be able to control your green LED. The blue onboard LED is the opposite state of the green LED.
P.S. I based this on Sparkfun's ESP8266 Thing tutorial