Está en la página 1de 11

22/9/2016

9Vbatterypowered,$2WebServerwithArduino

Back

Blog Resources

9V battery powered, $2 Web Server with


Arduino
Published last year by Mate Marschalko
Ive been playing around with the ESP8266 Wi-Fi module for a while and has been
fascinated by its versatility, the built in microcontroller and wireless antenna to be
specic. The brain of these dev boards and modules is the 5mm ESP8266 chip. The
price of the chip is a few dollar cents and dev boards start from $1. The chip has
gone through a few generations and has now become reliable but luckily the price
has stayed low.

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

Download FREE Ebook: Introduction to JavaScript Electronics

Only 14mm x 25mm for wi- and microcontroller


This was not too reliable and only exposed GPIO (general purpose input and output)
pins from the chip. I also had problems programming it from OS X possibly because
of my FTDI converter. Unfortunately this module didnt have a USB connector and an
onboard voltage regulator.

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

Download FREE Ebook: Introduction to JavaScript Electronics

wi- , microcontroller, voltage regulator and a micro USB connector


This module has everything I wanted: wi-, microcontroller, voltage regulator and a
micro USB connector for easy code uploads. This costs $4.50 but once you prototyped
your project and ready to package it up you can just buy the brain, the 12-E module
for 2$. Thats the little rectangle with the metal heat sink.
The onboard processor is 32 bit with SDIO, SPI, UART and 16 GPIO pins and it has a
lot more power than an Arduino. The ESP8266:
is running at 80MHz
has 32KBytes of instruction RAM
has 96KBytes of data RAM
has 64KBytes boot ROM
Pretty powerful properties for a microcontroller. Compare this to the Arduino UNOs
16Mhz clock speed.
You might say at this point that the clear benet of the Arduino UNO is that it can be
programmed with the Arduino language from the Arduino IDE. The good news is that
the Arduino IDE has been made compatible with the ESP8266 chip so you can upload
your regular Arduino code onto the board.
http://www.webondevices.com/9vbatterypowered2webserverwitharduino/

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);

A 9V battery powered $2 Web Server


The cool thing about the ESP8266 chip is that once its connected to your Wi-Fi
modem you can connect to its IP address. You can also setup an HTTP server on the
board which means that you can send HTML responds back if the IP address is
entered into a browser from a device connected to the same Wi-Fi network.
Lets build this web server and switch on an LED. First connect an LED to pin D2,
then add this sketch to the Arduino IDE. I added a few comments to help you
understand whats happening in the background:

//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

Download FREE Ebook: Introduction to JavaScript Electronics

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

Download FREE Ebook: Introduction to JavaScript Electronics

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

Send me the PDF

http://www.webondevices.com/9vbatterypowered2webserverwitharduino/

10/11

22/9/2016

9Vbatterypowered,$2WebServerwithArduino

Back

Blog Resources

Download FREE Ebook: Introduction to JavaScript Electronics

Web on Devices
Electronics Hacking with JavaScript and other Web Technologies
Twitter

Facebook

Mate Marschalko
Front-end Web Developer, Creative Technologist and Maker. Builds Internet connected devices for the Internet of
Things.

All rights reserved | Contact at hello@webondevices.com

http://www.webondevices.com/9vbatterypowered2webserverwitharduino/

11/11

También podría gustarte