What is a ESP8266?
It's a small Basic programmabe microcontroller with Wifi, TCP/IP stack plus server, GPIO's and... it's unbelievable cheap. For about 3€ you get it as a bulk version which you have to solder. For 5 more you get this one with a micro usb UART adapter and a voltage converter (5V will kill it).
http://www.ebay.co.uk/itm/NEW-NodeM...842030?hash=item35f6f9f12e:g:CVkAAOSwnipWU62m
Usually it comes with a basic os which you can connect to via a serial terminal like Putty. Here you can activate the Wifi and other things by AT commands. But we skip this here. There is a better solution: ESP8266 Basic.
You can run basic programs on the ESP which offers many functions for GPIO's and it's acts like a server & handles get requests. So we can call it via HttpUtils
Step 1:
Go to http://www.esp8266basic.com/flashing-instructions.html and download the flasher.exe. It's simple Windows-UI program which brings the OS to flash with it. No need to search for os-images or to load 6 files which is very annoying.
You can see the soldering needed on cheaper devices. Buying the USB one is totally worth it. Plug & Play. You will only need to connect a simple USB cable to your laptop/pc.
Step 2:
Connect the ESP8266 to a USB port on your laptop/pc and let it recognize the device (my WIN 10 did it all alone). Wait some seconds. Check it under System -> Devices/Drivers. Take a look at the COM-Port (usually it's #3). If the recognition fails then check the www for drivers for it.
Step 3 (important):
Disconnect it!
Step 4:
Start the flasher.exe. Select the COM-port (usually 3) and select th memory size. 1M did it for me. Don't flash yet!
Step 5:
The ESP ist still disconnected. It has a reset and a flash button (another advantage to the cheap ones). So press & hold(!) the flash button and connect the usb cable to power it up. Hold the button for 5 secs (maybe it has to be pressed just for e millisecond - I didn't try) and release it.
Step 6:
Wait some seconds after releasing the button and press "Firmware Flash". You should now see a small led flashing while the firmware is transmitted. It takes about 30 secs. A new window shows the success (a dotted line). Close the flasher and press the reset button on the ESP board.
http://www.esp8266basic.com/flashing-instructions.html
Step 7:
After the reset the ESP boots for the first time and set's itsself to a WiFi access point. Just connect to "ESP", start your browser and type "192.168.4.1" to access the ESP's gui.
You will see this:
Step 8:
As we want to integrate it into out network at home we have to do a small configuration. Click on "Settings":
Looks quite like your router's config page. Fill out the station mode section (your router's name & Wifi pw). Click on "Save" and reset it. Please set a login pw on the ESP later (for the first steps it might be annoying to do a login every time while playing with it).
If you like to start a program automatically after the boot is completed (autostart), you can enable it here. The name must be "default.bas". Use it later if you are more experienced.
Log in to your router and check if the ESP is connected to your Wifi network. Get it's ip-address.
Step 9:
Get back to your browser and start the ESP's UI again by typing it's WiFi ip address.
Step 10:
Select "Edit" to get to the source editor. It is very simple and stores the programs as files in the file system.
Step 11:
From the ESP8266's webpage I've taken this example to set/retrieve the status of the GPIO pins:
A "msgbranch" is some sort of an event. A given Sub (here "mybranch") will be called if a message arrives.
"msgget" is used to get the values from a get request.
The full command set/language reference can be found here: https://docs.google.com/document/d/1NMZvnnjZ5XNzkykINgYS_Ql6mQ3nJEAzYudbmScdiIg/pub#h.4eyo3bggeuan
As you can see it offers a lot of functions, even graphics.
Copy the lines to the editor and press "Save". Click on "Run" to start it. In another tab use this get request to control the gpio's:
A nice request we can handle with a simple B4A/B4J app:
Hints:
Getting back to the Editor will end any program. So take care to run it again (or you'll get some other message from the ESP)
Place a SPACE between each word, etc of a line:
Good: if a == 4 then ....
Bad: if a==4 then
It's a small Basic programmabe microcontroller with Wifi, TCP/IP stack plus server, GPIO's and... it's unbelievable cheap. For about 3€ you get it as a bulk version which you have to solder. For 5 more you get this one with a micro usb UART adapter and a voltage converter (5V will kill it).
http://www.ebay.co.uk/itm/NEW-NodeM...842030?hash=item35f6f9f12e:g:CVkAAOSwnipWU62m
Usually it comes with a basic os which you can connect to via a serial terminal like Putty. Here you can activate the Wifi and other things by AT commands. But we skip this here. There is a better solution: ESP8266 Basic.
You can run basic programs on the ESP which offers many functions for GPIO's and it's acts like a server & handles get requests. So we can call it via HttpUtils
Step 1:
Go to http://www.esp8266basic.com/flashing-instructions.html and download the flasher.exe. It's simple Windows-UI program which brings the OS to flash with it. No need to search for os-images or to load 6 files which is very annoying.
You can see the soldering needed on cheaper devices. Buying the USB one is totally worth it. Plug & Play. You will only need to connect a simple USB cable to your laptop/pc.
Step 2:
Connect the ESP8266 to a USB port on your laptop/pc and let it recognize the device (my WIN 10 did it all alone). Wait some seconds. Check it under System -> Devices/Drivers. Take a look at the COM-Port (usually it's #3). If the recognition fails then check the www for drivers for it.
Step 3 (important):
Disconnect it!
Step 4:
Start the flasher.exe. Select the COM-port (usually 3) and select th memory size. 1M did it for me. Don't flash yet!
Step 5:
The ESP ist still disconnected. It has a reset and a flash button (another advantage to the cheap ones). So press & hold(!) the flash button and connect the usb cable to power it up. Hold the button for 5 secs (maybe it has to be pressed just for e millisecond - I didn't try) and release it.
Step 6:
Wait some seconds after releasing the button and press "Firmware Flash". You should now see a small led flashing while the firmware is transmitted. It takes about 30 secs. A new window shows the success (a dotted line). Close the flasher and press the reset button on the ESP board.
http://www.esp8266basic.com/flashing-instructions.html
Step 7:
After the reset the ESP boots for the first time and set's itsself to a WiFi access point. Just connect to "ESP", start your browser and type "192.168.4.1" to access the ESP's gui.
You will see this:
B4X:
[ VARS ] [ EDIT ] [ RUN ] [ SETTINGS ] [ FILE MANAGER ]
ESP Basic 2.0.Alpha 20
Step 8:
As we want to integrate it into out network at home we have to do a small configuration. Click on "Settings":
Looks quite like your router's config page. Fill out the station mode section (your router's name & Wifi pw). Click on "Save" and reset it. Please set a login pw on the ESP later (for the first steps it might be annoying to do a login every time while playing with it).
If you like to start a program automatically after the boot is completed (autostart), you can enable it here. The name must be "default.bas". Use it later if you are more experienced.
Log in to your router and check if the ESP is connected to your Wifi network. Get it's ip-address.
Step 9:
Get back to your browser and start the ESP's UI again by typing it's WiFi ip address.
Step 10:
Select "Edit" to get to the source editor. It is very simple and stores the programs as files in the file system.
Step 11:
From the ESP8266's webpage I've taken this example to set/retrieve the status of the GPIO pins:
B4X:
msgbranch [mybranch]
print "You can send msgs to the esp and do things based on accessing a URL"
wait
[mybranch]
MyReturnMsg = "Not a valid msg received"
msgget "pin" pinNo
msgget "stat" pinStatus
msgget "action" pinAction
if pinAction == "po" then gosub [po.pin]
if pinAction == "pi" then gosub [pi.pin]
if pinAction == "pwo" then gosub [pwo.pin]
if pinAction == "pwi" then gosub [pwi.pin]
if pinAction == "ai" then gosub [ai.pin]
print "Done with return code"
msgreturn MyReturnMsg
wait
[po.pin]
po pinNo pinStatus
MyReturnMsg = "good"
return
[pi.pin]
pi pinNo MyReturnMsg
return
[pwo.pin]
pwo pinNo pinStatus
MyReturnMsg = "good"
return
[pwi.pin]
pwi pinNo MyReturnMsg
return
[ai.pin]
ai MyReturnMsg
return
A "msgbranch" is some sort of an event. A given Sub (here "mybranch") will be called if a message arrives.
"msgget" is used to get the values from a get request.
The full command set/language reference can be found here: https://docs.google.com/document/d/1NMZvnnjZ5XNzkykINgYS_Ql6mQ3nJEAzYudbmScdiIg/pub#h.4eyo3bggeuan
As you can see it offers a lot of functions, even graphics.
Copy the lines to the editor and press "Save". Click on "Run" to start it. In another tab use this get request to control the gpio's:
B4X:
192.168.xxx.xxx/msg?pin=2&stat=1&action=po
A nice request we can handle with a simple B4A/B4J app:
B4X:
'Non-UI application (console / server application)
#Region Project Attributes
#CommandLineArgs:
#MergeLibraries: True
#End Region
Sub Process_Globals
Public ServerName, Pin, Stat, Action As String
End Sub
Sub AppStart (Args() As String)
ServerName="http://192.168.178.37/"
Pin="1"
Stat="1"
Action="pwi"
'http://192.168.178.37/msg?pin=2&stat=1&action=po
Dim ESP8266 As HttpJob
ESP8266.Initialize("ESP8266", Me)
ESP8266.Download2(ServerName & "msg?", _
Array As String("pin", Pin,"stat", Stat,"action", Action ))
StartMessageLoop
End Sub
Sub JobDone(Job As HttpJob)
If Job.Success Then
Dim res As String
res = Job.GetString
Log("Response from ESP8266: " & res)
If res.Contains("good") Then
'Do something...
End If
Else
Log("Error: " & Job.ErrorMessage)
End If
Job.Release
End Sub
Hints:
Getting back to the Editor will end any program. So take care to run it again (or you'll get some other message from the ESP)
Place a SPACE between each word, etc of a line:
Good: if a == 4 then ....
Bad: if a==4 then
Last edited: