You are on page 1of 7

Ihaveheardsomanypeopletalkingaboutthecheap$4WiFichipknowastheESP8266.

Ikindofignoredit
atfirstwhilemyfriendwenthogwildandbuilttheESP8266.comcommunityandforum.EventuallyIdecidedto
joininonthefunandorderedacoupleoffofeBay.Inhindsight,IamwishingIhadorderedabunch!These
littleguyspackapunchespeciallywhencoupledwithanexternalMicroControllerlikeanArduino.

AsafirstattempatworkingwiththeESP8266IdecidedtobuildasimpleGreen/Redstatuslightformy
OwnCloudserverIhostathome.Iwillprobablyextendthisprojectinthefuturetobeabitmorerobustand
encompasmoreservicesbutithoughtitwouldmakeagreatgettingstartedtutorialforworkingwiththe
ESP8266andanArduinomicrocontroller.

Thefirststepoftheprojectissettingupthehardware.Todothiswefirstneedtounderstandthepinoutofthe
ESP8266.The1stversionoftheboard(ESP8266ESP01)iswhatIusedandincludesa2x4maleheader.
ThePinoutincludesVCC(+3.3v),GND,TX,RX,RESET,2IOpinsandaspecialCH_PDpin.

InmysetupIconnectedthistoanArduinoNanov3.0Thepinoutsareprettystraightforwardastheyareall
labeledontheboard.ForillustrationIamincludinganywaysjustincaseyoudonthaveoneinfrontofyou.
Connectingthetwoboardstogetherisverystraightforward.WewillsimplycrossoverbetweentheRXandTX
linesofthetwodevices.ThiswillallowtheArduinotosendSerialcommandstotheESP8266andthenreceive
theresultingdatafromit.Somepeoplehavereportedthatitisimportanttousealogiclevelconverterora
resistordividertodropthe5VTXfromtheArduinoto3.3VgoingintoWiFichiphoweverIhadnoissueswith
simplyconnectingthemdirectly.Thefinalsetuplookslikethefollowing
OncewehavemadetheserialconnectionswewillneedtopowertheESP8266.Itisimportanttonotethatthe
ESP8266requires3.3Vandthat5Vwilllikelyreleasethemagicblacksmoke.WhiletheArduinoNanodoes
havea3.3VVoltageRegularonboard,myparticularArduinowasnotcapableofprovidingsufficientcurrentto
powertheWiFimodule.AsaresultIusedaLD33Vvoltageregulator.Thiscanbepoweredoffofeitherthe
5VfromtheArduino(whichIshowbelow)orbyusinganexternalpowersource.Drawingtoomuchcurrent
throughtheArduinomayresultinheatissues.

ESP8266SerialCommands
UnderstandingtheSerialCommandsbeingsenttotheESP8266isagreatplacetostartlearningaboutthe
ESP8266

Thefirstthingweneedtoknowishowtorestartthechip.Todothiswesendthefollowingcommand:

AT+RST

Nextwillwillwanttosetthechipaseitherawirelessclientorasanaccesspoint.Inourcasewearelookingto
becomeaclienttovalidatewhetherornotaWebServerisavailable.IfwewantedtoactasawirelessAccess
Pointthenwewouldneedtosetthemodeto2insteadandissuesomeothercommands.Fornowletsstickto
theclientsettings.

AT+CWMODE=1

Ifweareusingaserialconnectiontotalktothechip,atthispointwewouldwouldprobablywanttocheckout
whatwecanhear.TheESP8266providesthefollowingcommandtolistalloftheavailableWirelessAccess
PointsintheareawhicharebroadcastingtheirSSID.

AT+CWLAP

Oncewehaveestablishedthatourwirelessnetworkisoutthere,weneedtoconnecttoit.Toconnectwe
simplyissuethefollowingcommandreplacingSSIDwiththenameofyourwirelessnetworkandpasswordwith
theWPAkeyusedtosecureyournetwork.

AT+CWJAP=SSID,PASSWORD

Youshouldnowbeconnected!Ifyouhavethatserialterminalstillopen,youcanasktheWiFimoduleforits
currentIPaddressbyissuingthecommand

AT+CWLIF

Wonderfull!Nowwearereadytoqueryawebpage!

QuerryWebPage
Thefirstthingweneedtodoisestablishaconnectiontothewebserver.Todothisyouusethefollowing
commandwhileobviouslyreplacingtheIPaddresswiththatofyourwebserver.Inourcaseweareusing
standardHTTP(NOTSSL/HTTPS)thereforeourportis80.IamnotsurehowthiswouldworkoverSSLsoif
youfigureitoutpleasepostinthecommentsbelow.

AT+CIPSTART=TCP,192.168.88.35,80

NextweneedtolettheWiFimoduleknowhowmanycharacterswearesendinginourquery.Todothiswe
issueacommandlikethefollowing

AT+CIPSEND=50

FinallywecanperformanHTTPgetagainsttheserver.Notethenumber50aboveisthetotalnumberof
charactersinthiscommand.Wecount\r&\nassinglecharactersandwedonotcountthelast\r\nsequence.

GET/status.htmlHTTP/1.0\r\nHost:192.168.88.35\r\n\r\n

Thisshouldresultinaresponsefromthewebserver.Thelaststepleftistobepropercitizensandclosethe
connectionbyissuingthefollowingcommand.

AT+CIPCLOSE

Thisisverycoolstuff!NowletsmovethisintosomethingusefullandstartwritingsomeArduinocode.

ArduinoCode
ThefollowingcodeiswhatIflashedtotheArduinoNanov3tocontroltheESP8266.

IMPORTANT:YoushouldbeawarethatwhileflashingtheArduinoyoumustpowerdownor
//mywebsite,replacewithyours Arduino
#defineDST_IP"192.168.88.35"

//DefinethepinfortheonboardLED
intled=13

//InitializeDevice
voidsetup()
{
//EnabletheOnboardLEDtoactasaStatusindicatorlight
pinMode(led,OUTPUT)
//SetthespeedforcommunicatingwiththeESP8266module
Serial.begin(9600)
//Sendaseriesofflashestoshowusweareinthebootupphase.
blinkcode()
//ResettheESP8266
Serial.println("AT+RST")
//WaitfortheWiFimoduletobootup
delay(800)
//Switchthechipto"client"mode
Serial.println("AT+CWMODE=1")
//ConnecttoourWirelessnetworkNOTE:changeSSIDtoyourwireless
//networkandPASSWORDtothekeyusedtojointhenetwork.
Serial.println("AT+CWJAP=\"SSID\",\"PASSWORD\"")
//Onceagainblinksomelightsbecauseitisfun!
blinkcode()
}

voidloop()
{
//Openaconnectiontothewebserver
Stringcmd="AT+CIPSTART=\"TCP\",\""//makethiscommand:AT+CPISTART="TCP","192.168.88.35",80
cmd+=DST_IP
cmd+="\",80"
Serial.println(cmd)

//waitalittlewhilefor'Linked'
delay(300)

//ThisisourHTTPGETRequestchangetothepageandserveryouwanttoload.
cmd="GET/status.htmlHTTP/1.0\r\n"
cmd+="Host:192.168.88.35\r\n\r\n"

//TheESP8266needstoknowthesizeoftheGETrequest
Serial.print("AT+CIPSEND=")
Serial.println(cmd.length())

//Lookforthe>promptfromtheesp8266
if(Serial.find(">"))
{
//SendourhttpGETrequest
Serial.println(cmd)
}
else
{
//Somethingdidn'twork...
Serial.println("AT+CIPCLOSE")
}

//Checkthereturnedheader&webpage.Lookingforakeyword.IusedYES12321

if(Serial.find("YES12321"))
{
//IfthestringwasfoundweknowthepageisupandweturnontheLEDstatus
//lighttoshowtheserverisONLINE
digitalWrite(led,HIGH)
else
{
//Ifthestringwasnotfoundthenwecanassumetheserverisofflinetherefore
//weshouldturnofthelight.
digitalWrite(led,LOW)
}
//Waitasecondforeverythingtosettle.
delay(1000)
//Begreatpeopleandcloseourconnection.
Serial.println("AT+CIPCLOSE")

//Introduceadelaytimerbeforewefinishandgobacktothebegining.
delay(5000)
}

voidblinkcode()
{
//Simplyloopafewtimesflashingthestatuslight(thisisusedduringbootup)
inti
for(i=1i<=10i++){
delay(100)
digitalWrite(led,HIGH)
delay(100)
digitalWrite(led,LOW)
}
}

You might also like