So I've cobbled together various ESP8266 projects over the past few months to learn how to use these cheap [but powerful] IoT modules. Below is my first ACTUAL real-world application.
Using the ESP, a basic LED project from my local Microcenter, and a transistor, I'm able to show my wife I'm thinking of her (LED heart blinks) while I'm away. Whether I'm downstairs in my workshop, sitting at my desk at work, or out and about running errands, as long as I have access to the Internet I can send the command or navigate to the ESP webserver page to click the "on" button.
1. Gather Parts- ESP-8266 (01)
- transistor (2N3904)
- 47k ohm resistor
- Velleman flashing heart LED kit
- power adapter (3.3v or 5v w/ voltage regulator) -OR- 3.3v lithium polymer battery
- FTDI to flash ESP-8266
- lithium charger module (optional)
I won't cover the actual hookup/flashing of the ESP-8266 as there are SEVERAL guides out there so please see this one or this one and replace the code with the code below.
Below is sample code (which can also be found in my Github repository here). Directions for changing variables are in the readme but in short you need to change:
- ssid
- password
- ArduinoOTA.setHostname (name of device for OTA update/DNS)
- rest.title (webserver's index page title)
- rest.button (gpio pin to control)
- rest.set_name (name of device)
/*
blinkingheartbox by Nick Reichley 14 March 2016
using aREST and aREST_UI by Marco Schwartz and the Arduino OTA (no more serial connection uploads!) toggle on/off an LED (LED heart) attached to collector side of NPN transistor using pin #2
*/
#include <ESP8266WiFi.h>
#include <aREST.h>
#include <aREST_UI.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
aREST_UI rest = aREST_UI();
// WiFi parameters
const char* ssid = "YOUR SSID";
const char* password = "YOUR PASSWORD";
// The port to listen for incoming TCP connections
#define LISTEN_PORT 80
// Create an instance of the server
WiFiServer server(LISTEN_PORT);
void setup(void)
{
// Start Serial
Serial.begin(115200);
Serial.println("Booting");
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("Connection Failed! Rebooting...");
delay(5000);
ESP.restart();
}
// Port defaults to 8266
// ArduinoOTA.setPort(8266);
// Hostname defaults to esp8266-[ChipID]
ArduinoOTA.setHostname("heartbox");
// No authentication by default
// ArduinoOTA.setPassword((const char *)"123");
ArduinoOTA.onStart([]() {
Serial.println("Start");
});
ArduinoOTA.onEnd([]() {
Serial.println("End");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\n", (progress / (total / 100)));
});
ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
else if (error == OTA_END_ERROR) Serial.println("End Failed");
});
ArduinoOTA.begin();
Serial.println("Ready");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
// Create UI
rest.title("Blinking Heart Box");
rest.button(2);
// Give name and ID to device
rest.set_id("1");
rest.set_name("heartbox");
// Connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Start the server
server.begin();
Serial.println("Server started");
// Print the IP address
Serial.println(WiFi.localIP());
}
void loop() {
ArduinoOTA.handle();
// Handle REST calls
WiFiClient client = server.available();
if (!client) {
return;
}
while(!client.available()){
delay(1);
}
rest.handle(client);
}
3. Wire up the circuit(When viewing module w/ antenna to the right)
VCC, CH_PD --> 3.3v (VCC)
GND --> Ground
GPIO 2 (adjacent to GND) --> base of transistor
After booting, the ESP starts a web server. It's available at either the local ip (found via a scan of your network or presented in serial output during .ino upload) OR since we're using mDNS and aREST libraries we can use name of device to get to device's homepage. (http://localip or http://devicename.local).
You may toggle the ESP's GPIO (to trigger your NPN transistor connected to your LED blinking heart box) in two ways:
1. Use the UI's "on" and "off" buttons
2. Entering the following into your browser to set pin HIGH and/or LOW
- http://localip/digital/pin#/1 (HIGH) or http://devicename.local/digital/pin#/1 (HIGH)
- http://localip/digital/pin#/0 (LOW) or http://devicename.local/digital/pin#/0 (LOW)
Comments
Please log in or sign up to comment.