IOT using ESP8266
IOT using ESP8266
What is IoT?
• The Internet of Things (IoT) is the network
of physical objects
• Devices, vehicles, buildings and other items
embedded with electronics, software,
sensors, and network connectivity
• —that enables these objects to collect and
exchange data
NODE MCU devkit
Esp12 esp8266
Installing the ESP8266 Board
{
digitalWrite(LED_BUILTIN,LOW);
//prepare the HTML document to responds and add button:
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";
s+="<br><input type=\"button\"name=\"b1\"value=\"Turn LED ON\"onclick=\"location.href='/ON'\">";
//button is created and it contains text Turn LED ON when clicked address changes to 192.168.4.1/ON
s+="<br><br><br>"; // for spacing
s+="<br><input type=\"button\"name=\"b1\"value=\"Turn LED OFF\"onclick=\"location.href='/OFF'\">";
//button is created and it contains text Turn LED ON when clicked address changes to 192.168.4.1/ON
s+="</html>\n";
//Serve the HTML document to the browser.
client.flush(); //clear previous info in the stream
client.print(s); // Send the response to the client
delay(1);
Serial.println("Client disonnected"); //Looking under the hood
}
using an external WiFi modem to Control your
device
#include <ESP8266WiFi.h>
const char* ssid = "TP-LINK_472528"; //type your ssid
const char* password = "9961880990"; //type your password
int ledPin = LED_BUILTIN; // GPIO2 of ESP8266
WiFiServer server(80); //Service Port
void setup()
{
Serial.begin(115200);
delay(10);
pinMode(LED_BUILTIN, OUTPUT);
// Connect to WiFi network
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("WiFi connected");
// Start the server
server.begin();
Serial.println("Server started");
// Print the IP address
Serial.print("Use this URL to connect: ");
Serial.print("http://");
Serial.print(WiFi.localIP());
void loop()
{
// Check if a client has connected
WiFiClient client = server.available();
if (!client)
{
return;
}
if (request.indexOf("/LED=ON") != -1)
{
digitalWrite(LED_BUILTIN, HIGH);
}
if (request.indexOf("/LED=OFF") != -1)
{
digitalWrite(LED_BUILTIN, LOW);
}
• // Return the response
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println(""); // importent
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("Click <a href=\"/LED=ON\">here turn the LED on pin 2 ON<br></a>");
client.println("Click <a href=\"/LED=OFF\">here turn the LED on pin 2 OFF<br></a>");
client.println("</html>");
client.flush(); //clear previous info in the stream
delay(1);
Serial.println("Client disconnected");
}
// Getting response to display
int value = LOW;
if (request.indexOf("/LED=ON") != -1) { digitalWrite(LED_BUILTIN, HIGH);
value = HIGH;
}
if (request.indexOf("/LED=OFF") != -1) { digitalWrite(LED_BUILTIN, LOW);
value = LOW;
}
…………………………………………………………..
client.println("<html>");
client.print("Led pin is now: ");
if(value == HIGH) {
client.print("On");
} else {
client.print("Off");
}
USING AN APP TO CONTROL
YOUR DEVICE (LOCAL AREA)
INTRODUCTION
• MIT App Inventor is a web-based tool for building
Android apps
• This is often referred to as visual programming,
• means the user is able to perform programming tasks
without entering any computer code.
• allows all sorts of people to program their phones using
a visual environment that involves
• snapping together color-coded instruction blocks
Lets Get Started
• Go to http://ai2.appinventor.mit.edu
• Select the Create button in the top right corner of the page:
void loop() {
Blynk.run();//Run the Blynk library
timer.run();//Run the Blynk timer
}
BLYNK_WRITE(V2) {
bool RelayTwo = param.asInt();
if (RelayTwo == 1) {
digitalWrite(relay2, LOW);
Blynk.virtualWrite(V3, 0);
} else {
digitalWrite(relay2, HIGH);
Blynk.virtualWrite(V3, 1);
}
}
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
char auth[] = "893aa5cb24a64a3db959ab99c01b75bd";
char ssid[] = "belkin.F04";
char pass[] = "maneesht"; Write to on/off
// This function will be called every time Slider Widget
// in Blynk app writes values to the Virtual Pin V1
BLYNK_WRITE(D0)
{
int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
// process received value
}
void setup()
{
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
}
void loop()
{
Blynk.run();
}
• Blynk.virtualWrite(pin, "abc");
• Send data from app to hardware
• BLYNK_WRITE(V1) //Button Widget is writing to pin V1
•{
• int pinData = param.asInt();
•}
• Get data from hardware
• Perform requests by Widget
• BLYNK_READ(V5) // Widget in the app READs Virtal Pin V5 with the
certain frequency
• {
• // This command writes Arduino's uptime in seconds to Virtual Pin V5
• Blynk.virtualWrite(5, millis() / 1000);
•}
• Pushing data from hardware
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
char auth[] = "YourAuthToken"; // Put your token here
BlynkTimer timer; // Create a Timer object called "timer"!
void setup()
{
Serial.begin(9600);
Blynk.begin(auth);
timer.setInterval(1000L, sendUptime);
// Here you set interval (1sec) and which function to call
}
void sendUptime()
{ // This function sends Arduino up time every 1 second to Virtual Pin (V5) //
In the app, Widget's reading frequency should be set to PUSH //
You can send anything with any interval using this construction //
Don't send more that 10 values per second
Blynk.virtualWrite(V5, millis() / 1000);
}
void loop()
{ Blynk.run(); // all the Blynk magic happens here
timer.run(); // BlynkTimer is working...
}
State syncing
For hardware
• If your hardware looses Internet connection or resets, you can restore all the values
from Widgets in the Blynk app.
• BLYNK_CONNECTED()
•{
• Blynk.syncAll();
• } //here handlers for sync command
• BLYNK_WRITE(V0)
• { ....
• }
• For app
• Blynk.virtualWrite
• LED
• WidgetLED led1(V1); //register to virtual pin 1
• led1.off();
• led1.on();
• WidgetLED led2(V2);
• led2.setValue(127); //set brightness of LED to 50%.
• LCD
• lcd.print(x, y, "Your Message");//Where x is a symbol position (0-15), y
is a line number (0 or 1),
• lcd.clear();
GPS Trigger
• BLYNK_WRITE(V1) GPS trigger widget allows easily trigger
• { events when you arrive to or leave from
some destination.
• int state = param.asInt();
This widget will work in
• if (state) background and periodically will check
• { //You enter destination your coordinates. In case your location is
within/out required radius
• }
(selected on widget map) widget
• else will send HIGH/LOW command to hardware.
• {
• //You leave destination
• }
• }
GPS Streaming
• BLYNK_WRITE(V1)
•{
• GpsParam gps(param); // Print 6 decimal places for Lat
Serial.println(gps.getLat(), 7);
• Serial.println(gps.getLon(), 7);
• Serial.println(gps.getAltitude(), 2);
• Serial.println(gps.getSpeed(), 2);
•}
USING AN APP & FIREBASE TO
CONTROL YOUR DEVICE (WEB
CONECTION)
FIREBASE
SETTINGS
•Visit https://console.firebase.google.com/
Database Secrets
Note this Secret Code
Setting App inventor
• Go to App inventor site and sign in using your Google account
• Start a new Project
Create front
Screen using
Button and
Properties
Add Experimental
firebaseDB
Paste the
Firebase token
and FirebaseURL
to firebaseDB
Properties
Blocks Tab
Add Blocks to
store firebase
data when
Buttons are
clicked
Install Required Libraries for Firebase settings.
Arduino Settings
In Examples Select Code Beginners_start_here
Add the Required datas
Change all Int to String
And int to string to
make the match the
data send from app
inventor