How to use ESP8266 ESP-01 as a SENSOR web client

来源:互联网 发布:上海程序员培训学校 编辑:程序博客网 时间:2024/05/22 01:53


How to use ESP8266 ESP-01 as a SENSOR web client

I’m not going to explain in detail what is ESP8266 because if you have found this post I’m sure you already know it. But just in case, it is an awesome cheap board (less than 4$) with built-in wifi communication (802.11 b/g/n), and SPI, UART. You can also use its processor to run your code.

Wiring: ESP8266 - reflash firmware

Use FTDI with 3V3 output. If you face problems with this running on Windows check this link: Unbrick FTDI 232R

Burning LuaFirmware

Download Coolterm http://freeware.the-meiers.org/ (putty like app but much cooler)
Check that your ESP8266 has some firmware through coolterm
Enter with coolterm at 115200 (most probable default speed)
Once in, type AT+GMR, my firmware was based on 00160901

esp8266-update-01

Congrats, you’ve got a working ESP8266 with a espressif firmware in it. If you want to play with this firmware check this out: http://www.electrodragon.com/w/Wi07c#Other_firmware

Now to burn LUA firmware:
To Burn a firmware: CH_PD pin must be always connected to HIGH and GPIO0 pin to GROUND (LOW)

Download ESP8266 flasher: https://docs.google.com/file/d/0B3dUKfqzZnlwVGc1YnFyUjgxelE/edit
Download LUA Firmware: https://github.com/nodemcu/nodemcu-firmware
Execute ESP8266_flasher.exe and burn the bin inside LUA Firmware

esp8266-update-02

After burning it,  GPIO0 pin should be disconnected from ground in order to reboot in normal mode. Otherwise it will be reboot in UPLOAD mode. So power it OFF, disconnect GPIO0 pin  from ground… and voilà! you’ve got a ESP8266 with LUA Firmware


BasicCoding

Now with a burned LUA Firmware, we should reconfigure CoolTerm at 9600bps to be able to communicate with the board.
Connect and type this basic code to check the wifi connectivity:

1
2
3
4
5
6
print(wifi.sta.getip())
--0.0.0.0
wifi.setmode(wifi.STATION)
wifi.sta.config("SSID","password")
print(wifi.sta.getip())
--192.168.18.110

Congrats, now each time you power it on back again, it will remember last wifi connection wifi setup.


Coding the automatic script

For this example we have used the following wiring:

ESP8266 - Push button web

With this basic script we will be able to know the state of a push button from the web.
Firstly we will build a LUA script named door.lua

So boot the terminal and type the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
file.remove(“door.lua”)
file.open(“door.lua”,”w”)
writeline([[srv=net.createServer(net.TCP) srv:listen(80,function(conn)]])
writeline([[conn:on("receive",function(conn,payload)]])
writeline([[print(node.heap())]])
writeline([[door="open"]])
file.writeline([[ifgpio.read(8)==1 then door="OPEN"else door="CLOSED"end]])
file.writeline([[conn:send("<h1> The door is ".. door ..".</h1>")]])
file.writeline([[end)]])
file.writeline([[conn:on("sent",function(conn) conn:close() end)]])
file.writeline([[end)]])
file.close()<b>
</b>

After this, you will have written a door.lua script that it’s gonna be available in the ESP8266 after each power off and on.

To execute it type:

1
dofile(“door.lua”)

and if you don’t know the ip of ESP8266 type:

1
2
print(wifi.sta.getip())
--192.168.18.110

Now input the IP of the ESP8266 as a URL on your favorite browser and you will have the result you’ve been looking for!

An issue is that the script is stored on the circuit but it’s not loading automatically. To fix this, we have to modify the init.lua script. Type at console:

1
2
3
4
5
file.remove(“init.lua”)
file.open(“init.lua”,”w”)
writeline([[print("Pete's LUA module 0.1")]])
file.writeline([[tmr.alarm(4000, 0, function() dofile("door.lua") end )]])
file.close()

Reboot, and now each time will be automatically executing the code. Plug the esp8266 to two 1.5V battery, remove FTDI and voilà! Automatic DOOR Sensor IOT!!!

REFERENCES:
http://www.nodemcu.com/
http://www.esp8266.com/
http://blog.electrodragon.com/cloud-updating-your-wi07c-esp8266-now/
http://scargill.wordpress.com/
http://www.electrodragon.com/w/Wi07c

Thanks to everyone who has spend many hours developing and sharing.

Bonus

ESP8266 range test with awesome results!

About these ads

19 Comments on “How to use ESP8266 ESP-01 as a SENSOR web client”

  1. Using the ESP8266 as a Web-enabled sensor | Hackadaysays:

    […] firmware, opening the doors to an Internet of Things based around an ESP8266. [Marc] and [Xavi] just wrote up a quick tutorial on how to turn the ESP8266 into a WiFi sensor platform that will relay the state of a GPIO pin to […]

    Reply
  2. Using the ESP8266 as a Web-enabled sensor | Hack The Planetsays:

    […] firmware, opening the doors to an Internet of Things based around an ESP8266. [Marc] and [Xavi] just wrote up a quick tutorial on how to turn the ESP8266 into a WiFi sensor platform that will relay the state of a GPIO pin to […]

    Reply
  3. geonomadsays:

    Attempting to write your firmware, everything starts OK, but then I always end up with:

    Writing at 0x00007000… (5 %)
    Invalid head of packet

    Any ideas?

    Reply
    • geonomadsays:

      So, after trying different ESP8266 boards, power supplies, etc. I tried using a different USB port on the computer and the problem solved itself. Go figure.

      Now… “The door is open” “The door is closed” Brilliant!

      Thanks guys! This is great stuff!

      Molt be!

      Reply
  4. Using the ESP8266 as a Web-enabled sensor | Ad Pubsays:

    […] firmware, opening the doors to an Internet of Things based around an ESP8266. [Marc] and [Xavi] just wrote up a quick tutorial on how to turn the ESP8266 into a WiFi sensor platform that will relay the state of a GPIO pin to […]

    Reply
  5. umindedsays:

    Anybody have luck modifying the network firmware into a mesh setup? Once this chip runs LUA scripts on a mesh topology the IOT will bloody explode!

    p.s – There needs to be some encryption, and a heartbeat to be secure or else someone could DDOS the router and gain entrance to the building is this was the only security sensor on that door.

    Reply
  6. Using the ESP8266 as a power monitor | Geonomad's Blogsays:

    […] by Mark and Xavi’s How to use ESP8266 ESP-01 as a SENSOR web client post, I decided to use Lua for this quick […]

    Reply
  7. geonomadsays:

    Thanks guys! You saved me a lot of time with this afternoon’s project:

    Using the ESP8266 as a Power Monitor.

    Not knowing the chip or Lua, I couldn’t have done it without this post. You showed me what I needed to know to get going.

    Reply
  8. Anonymous says:

    Hi, are those " meant to be there or are they supposed to be “”?

    Reply
    • Anonymous says:

      Correction:
      Are those & quot ; meant to be there or are they supposed to be “”?

      Reply
      • geonomadsays:

        You need to un-html them to single quotes like ‘ (although that would screw up the Pete’s line) so maybe ” is needed sometimes.

        I hope the code on my posting (above) is more readable, but browsers and WordPress can play havoc with this stuff.

  9. ESP8266 – Nejlevnější počítač na světě se síťovým připojením a možností programování ve vyšším jazyce | Pavlův blogsays:

    […] večer jsem objevil přímo neuvěřitelnou věc – existuje alternativní FW, který obsahuje interpreter jazyka LUA (učebnici LUA mám ve svém Kindlu tuším od začátku roku…). Pro účely přehrávání […]

    Reply
  10. Bo-Erik Sandholmsays:

    Is it possible for you to create a simple temperature reporter using a TMP36 sensor.
    Using a identity for the individual board to be able to use several at the same time but identify the reported temperatures.
    Having it to report the temperature every 10 minutes.
    Configuring the WLAN ssid in a changable way ( maybe over the serial)

    Reply
    • umindeduMindedsays:

      That was my plan next summer. Create a bunch of solar charged temp, humidity, water meters for my greenhouse. Make each sensor create a random SSID and an app to connect to each for naming, pairing. I was originally using the Synaptics python mondules but they where bloody expensive at $50 a pop.

      Reply
  11. umindeduMindedsays:

    @OP Would you consider running a smaller scale test and measure power consumption over a 10m/100m/250m distances? I ordered my boards but with Christmas coming and new Hong Kong export regs im afraid I will be waiting a long while…

    Reply
  12. mosheen says:

    The code shows “writeline” without the file.writeline, and there is a at the end.

    Here’s a corrected version of it (hopefully):

    file.remove(‘door.lua’)
    file.open(‘door.lua’,’w’)
    file.writeline([[srv=net.createServer(net.TCP) srv:listen(80,function(conn)]])
    file.writeline([[conn:on(‘receive’,function(conn,payload)]])
    file.writeline([[print(node.heap())]])
    file.writeline([[door=’open’]])
    file.writeline([[if gpio.read(8)==1 then door=’OPEN’ else door=’CLOSED’ end]])
    file.writeline([[conn:send(‘ The door is ‘ .. door ..’.’)]])
    file.writeline([[end)]])
    file.writeline([[conn:on(‘sent’,function(conn) conn:close() end)]])
    file.writeline([[end)]])
    file.close()

    Reply
    • mosheen says:

      Sorry, I posted some mistakes. This code works:

      file.remove(‘door.lua’)
      file.open(‘door.lua’,’w’)
      file.writeline([[srv=net.createServer(net.TCP) srv:listen(80,function(conn)]])
      file.writeline([[conn:on(“receive”,function(conn,payload)]])
      file.writeline([[print(node.heap())]])
      file.writeline([[door=’open’]])
      file.writeline([[if gpio.read(8)==1 then door =”OPEN” else door=”CLOSED” end]])
      file.writeline([[conn:send(” The door is ” .. door ..”.”)]])
      file.writeline([[end)]])
      file.writeline([[conn:on(“sent”,function(conn) conn:close() end)]])
      file.writeline([[end)]])
      file.close()

      Then:
      dofile(‘door.lua’)

      Reply
      • mosheen says:

        Also note too, that if you leave GPIO connected to the switch setup in the example, it will boot into flash mode next time power is cycled. I pulled my hair out thinking it was a power supply issue!

  13. Using the ESP8266 as a Web-enabled sensor |says:

    […] firmware, opening the doors to an Internet of Things based around an ESP8266. [Marc] and [Xavi] just wrote up a quick tutorial on how to turn the ESP8266 into a WiFi sensor platform that will relay the state of a GPIO pin to […]

    Reply

Leave a Reply Cancel reply

Fill in your details below or click an icon to log in:

  • <iframe name="googleplus-sign-in" width="24" height="24" id="googleplus-sign-in" src="https://public-api.wordpress.com/connect/?googleplus-sign-in=https%3A%2F%2Fimporthack.wordpress.com" frameborder="0" scrolling="no" allowtransparency="true" seamless="seamless"></iframe>
Gravatar
WordPress.com Logo

You are commenting using your WordPress.com account.( Log Out / Change )

Twitter picture

You are commenting using your Twitter account.( Log Out / Change )

Facebook photo

You are commenting using your Facebook account.( Log Out / Change )

Google+ photo

You are commenting using your Google+ account.( Log Out / Change )

Cancel

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Build a website with WordPress.com
    &lt;img src="https://pixel.wp.com/b.gif?v=noscript" style="height:0px;width:0px;overflow:hidden" alt="" /&gt;
    0 0
    原创粉丝点击
    热门IT博客
    热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 你好,中校 中校的情人 刘培茄中校 你好中校先生 中校是什么级别 中校溺宠小小妻 中校老婆惹不得 中校的闪婚妻 你好 中校先生 你好中校先生全文免费阅读 中校大人,结婚吧 心静如水 中校大人橙子熟了 中校的下等小妻 冰彬儿 你好,中校先生 莫萦 恋空 中校的闪婚妻 中校的新娘全文免费阅读 腹黑中校请离婚 中校的温存小娇妻 中校的大明星 中校的下等小妻 中校的私宠小妻 腹黑中校惹不得 与中校圈叉之后 中校的新娘 胡狸 中校先生我们恋爱吧 替父从军腹黑中校惹不得 小妻不好惹晚安中校老公 中核集团 中核 中核404 中核钛白 中核集团电子采购平台 中核科技 中核华兴 中核集团招聘 中核钛白股吧 中核华辰建设有限公司 中核科技股吧 002145中核钛白 中核二三建设有限公司 中核华兴建设有限公司