Beginner IoT Project (esp8266 + Blynk)
by c26ab1 in Circuits > Arduino
1667 Views, 6 Favorites, 0 Comments
Beginner IoT Project (esp8266 + Blynk)
This is an introductory project to learn the basics of Blynk and the esp2866 NodeMCU! This project allows you to control an LED from your phone, but other components like servo motors can replace the LED with a little adaptation to the code.
Supplies
- esp8266 NodeMCU
- Breadboard
- Phone
- WiFi
- LED
Set Up Esp8266 NodeMCU
If you have not already set up your esp8266 NodeMCU, follow this guide and then come back for step 2!
Set Up Circuit
This is a very easy circuit. Simply connect the anode of the LED to D0 and the cathode to ground. It is a good idea, but not necessary, to add a 120 ohm resistor in the D0-cathode connection.
Set Up Blynk
Download the Blynk app and follow the quickstart instructions.
Make Blynk Template
Log into Blynk on your laptop and navigate to templates on the left menu. Then click new template and name it LED, use esp8266 as your hardware, and WiFi as connection type. Then go to your datastreams in the template. Click new datastream, name it LED, pick V0 as the pin, and integer as datatype. Next click on web dashboard and drag and drop a switch from the control panel into the main screen. Name it LED, and pick LED (V0) as your datastream. In addition, toggle on On/Off values and name them accordingly. Click save.
Code
Copy and paste the following code into the Arduino IDE. Replace the first three lines of code with your specific template data which can be found by clicking search-->LED-->Device Info-->firmware configuration. Also replace wifi name and wifi password with your name and password.
#define BLYNK_TEMPLATE_ID "blynk_template_ID"
#define BLYNK_DEVICE_NAME "blynk_device_name"
#define BLYNK_AUTH_TOKEN "blynk_auth_token"
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
char auth[] = BLYNK_AUTH_TOKEN;
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "wifi name";
char pass[] = "wifi password";
BlynkTimer timer;
// This function is called every time the Virtual Pin 0 state changes
BLYNK_WRITE(V0)
{
// Set incoming value from pin V0 to a variable
int value = param.asInt();
// Update state
Blynk.virtualWrite(V1, value);
digitalWrite(D0, value);
}
// This function is called every time the device is connected to the Blynk.Cloud
BLYNK_CONNECTED()
{
// Change Web Link Button message to "Congratulations!"
Blynk.setProperty(V3, "offImageUrl", "https://static-image.nyc3.cdn.digitaloceanspaces.com/general/fte/congratulations.png");
Blynk.setProperty(V3, "onImageUrl", "https://static-image.nyc3.cdn.digitaloceanspaces.com/general/fte/congratulations_pressed.png");
Blynk.setProperty(V3, "url", "https://docs.blynk.io/en/getting-started/what-do-i-need-to-blynk/how-quickstart-device-was-made");
}
// This function sends Arduino's uptime every second to Virtual Pin 2.
void myTimerEvent()
{
// You can send any value at any time.
// Please don't send more that 10 values per second.
Blynk.virtualWrite(V2, millis() / 1000);
}
void setup()
{
// Debug console
Serial.begin(115200);
pinMode(D0, OUTPUT);
Blynk.begin(auth, ssid, pass);
// Setup a function to be called every second
timer.setInterval(1000L, myTimerEvent);
}
void loop()
{
Blynk.run();
timer.run();
}