Documentos de Académico
Documentos de Profesional
Documentos de Cultura
9Vbatterypowered,$2WebServerwithArduino
Back
Blog Resources
ESP8266
I originally had an ESP8266 module with only 8 pins and it looked like this and cost
$2:
http://www.webondevices.com/9vbatterypowered2webserverwitharduino/
1/11
22/9/2016
9Vbatterypowered,$2WebServerwithArduino
Back
Blog Resources
NodeMCU
Last month I came across and ordered from the brand new NodeMCU 1.0
development boards which is, again, based on the ESP8266 chip. This time its the
latest 12-E generation:
http://www.webondevices.com/9vbatterypowered2webserverwitharduino/
2/11
22/9/2016
9Vbatterypowered,$2WebServerwithArduino
Back
Blog Resources
3/11
22/9/2016
9Vbatterypowered,$2WebServerwithArduino
Uploading
sketches from the Arduino IDE
Back
Blog Resources
Before programming the board you need to install it from the Arduino Additional
Download FREE Ebook: Introduction to JavaScript Electronics
Board Manager. Here are the steps to follow:
Install Arduino 1.6.5 from the Arduino website
Start Arduino and open Preferences window
Enter http://arduino.esp8266.com/stable/package_esp8266com_index.json into
Additional Board Manager URLs eld. You can add multiple URLs, separating them
with commas
Open Boards Manager from Tools > Board menu and install esp8266 platform (and
dont forget to select your ESP8266 board from Tools > Board menu after
installation)
These steps were been taken from the original ESP8266 Arduino project github
repository: https://github.com/esp8266/arduino
Once these steps are successfully completed you need to make sure that the chip on
the board responsible for the USB serial communication is compatible with your
computer. On my NodeMCU board it was a chip labelled as CP2102. For that I had to
install the driver from a Chinese website:
http://www.wch.cn/download/CH341SER_MAC_ZIP.html
This did the job and after installation the board appeared in my Arduino IDE on OS X
Yosemite. I used the below settings in the Arduino IDE:
Now is the best time to run an LED blink test. The only thing you need to be aware is
the the pin labels on the NodeMCU dont correspond to the numbers in your Arduino
sketch. Here are the numbers you need to use instead:
http://www.webondevices.com/9vbatterypowered2webserverwitharduino/
4/11
22/9/2016
9Vbatterypowered,$2WebServerwithArduino
pin 0 = D3
Blog
pinBack
1 = TX
pin 2 = D4
FREE Ebook: Introduction to JavaScript Electronics
pin 3 = Download
RX
pin 4 = D2
pin
pin
pin
pin
5 = D1
9 = SD2
10 = SD3
12 = D6
pin
pin
pin
pin
13 = D7
14 = D5
15 = D8
16 = D0
Resources
If you wish to switch an LED on connected to D4 then your Arduino sketch will be:
pinMode(2,OUTPUT);
digitalWrite(2,HIGH);
//Loadlibraries
#include<ESP8266WiFi.h>
#include<WiFiClient.h>
#include<ESP8266WebServer.h>
#include<ESP8266mDNS.h>
//Yourwifidetails
constchar*ssid="nameofyourwifi";
constchar*password="passwordofyourwifi";
//Startserver
http://www.webondevices.com/9vbatterypowered2webserverwitharduino/
5/11
22/9/2016
9Vbatterypowered,$2WebServerwithArduino
ESP8266WebServerserver(80);
Back
Blog Resources
constintled=4;
Download FREE Ebook: Introduction to JavaScript Electronics
//FunctionthatreturnsourHTMLdocumentasacharacterchain
//Noticehowourbuttonsnavigateto/onandto/offinthebrowser
char*returnHTML(){
chartemp[400];
intsec=millis()/1000;
intmin=sec/60;
inthr=min/60;
snprintf(temp,400,
"<html>\
<head>\
<title>ESP8266Demo</title>\
<style>\
body{backgroundcolor:#cccccc;fontfamily:Arial,Helvetica,
SansSerif;Color:#000088;}\
</style>\
</head>\
<body>\
<h1>HellofromESP8266!</h1>\
<ahref=\"\/on\">ON</a>\
<ahref=\"\/off\">OFF</a>\
</body>\
</html>",
hr,min%60,sec%60
);
returntemp;
}
voidhandleRoot(){
Serial.println("root");
server.send(200,"text/html",returnHTML());
}
voidledOn(){
Serial.println("ON");
digitalWrite(led,HIGH);
http://www.webondevices.com/9vbatterypowered2webserverwitharduino/
6/11
22/9/2016
9Vbatterypowered,$2WebServerwithArduino
server.send(200,"text/html",returnHTML());
}Back
Blog Resources
voidledOff(){
Download FREE Ebook: Introduction to JavaScript Electronics
Serial.println("OFF");
digitalWrite(led,LOW);
server.send(200,"text/html",returnHTML());
}
voidhandleNotFound(){
Stringmessage="FileNotFound\n\n";
message+="URI:";
message+=server.uri();
message+="\nMethod:";
message+=(server.method()==HTTP_GET)?"GET":"POST";
message+="\nArguments:";
message+=server.args();
message+="\n";
for(uint8_ti=0;i<server.args();i++){
message+=""+server.argName(i)+":"+server.arg(i)+"\n";
}
server.send(404,"text/plain",message);
}
voidsetup(void){
//SetupLED
pinMode(led,OUTPUT);
digitalWrite(led,LOW);
Serial.begin(115200);
//ConnecttoWiFi
WiFi.begin(ssid,password);
Serial.println("");
//Waitforconnection
while(WiFi.status()!=WL_CONNECTED){
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connectedto");
Serial.println(ssid);
Serial.print("IPaddress:");
http://www.webondevices.com/9vbatterypowered2webserverwitharduino/
7/11
22/9/2016
9Vbatterypowered,$2WebServerwithArduino
Serial.println(WiFi.localIP());
Back
Blog Resources
if(MDNS.begin("esp8266")){
Serial.println("MDNSresponderstarted");
Download FREE Ebook: Introduction to JavaScript Electronics
}
//CallhandleRootfunctionwhenbrowserhitstherootURL
server.on("/",handleRoot);
//CallledOnfunctionwhenbrowserhitsthe/onURL
server.on("/on",ledOn);
//CallledOfffunctionwhenbrowserhitsthe/offURL
server.on("/off",ledOff);
server.onNotFound(handleNotFound);
server.begin();
Serial.println("HTTPserverstarted");
}
voidloop(void){
//Handleincomingconnections
server.handleClient();
}
Modify your wi details and upload the code to the board. For me it normally takes 2
5 attempts before the actual upload process starts and Im not entirely sure why
The server code is uploaded onto your board so you no longer need to be connected to
your computer through the USB. Unplug it and try powering it from an external
power source (minimum 6 maximum 15 volts). I connected the positive pole of a 9V
battery to the VIN pin and the negative to GND. The board will power up and connect
to your Wi-Fi in a few seconds then by navigating to the IP address of the board you
will see the HTML page you added into the returnHTML function.
http://www.webondevices.com/9vbatterypowered2webserverwitharduino/
8/11
22/9/2016
9Vbatterypowered,$2WebServerwithArduino
Back
Blog Resources
Notice how in line 91 we output the IP address of the board to the Serial port:
Serial.println(WiFi.localIP());
If you open up the Serial Monitor of your Arduino IDE you will see the IP address.
This is what you need to enter into the browser to access your ESP8266 mini server
and to see the HTML page.
Now as you click on the HTML buttons the LED will switch on and o connected to
your server.
Whats next
Now that this is setup the possibilities are endless. You can add sensors and make
your server return a JSON. This will expose an API for your JavaScript application to
access sensor readings from your house.
Alternatively you can add this $2 web server into a desk lamp. You just need to
replace the LED with a 240V relay to switch the power supply instead. This means
that your desk lamp is now a web server that can serve a responsive website to
control itself. Your desk lamp is now part of the Internet of Things
http://www.webondevices.com/9vbatterypowered2webserverwitharduino/
9/11
22/9/2016
9Vbatterypowered,$2WebServerwithArduino
Back
Blog Resources
Back to all
Leave a Reply
You must be logged in to post a comment.
Free Ebook
Step up your web developer career and learn hardware prototyping.
This ebook will get you started with JavaScript Arduino electronics development in a
couple of hours.
Email address
http://www.webondevices.com/9vbatterypowered2webserverwitharduino/
10/11
22/9/2016
9Vbatterypowered,$2WebServerwithArduino
Back
Blog Resources
Web on Devices
Electronics Hacking with JavaScript and other Web Technologies
Twitter
Mate Marschalko
Front-end Web Developer, Creative Technologist and Maker. Builds Internet connected devices for the Internet of
Things.
http://www.webondevices.com/9vbatterypowered2webserverwitharduino/
11/11