2015-09-22

ESP8266 微控器快速入手

ESP8266 出貨內建的是AT指令,還真難用!

本來是想換用 micropython
https://github.com/micropython/micropython/tree/master/esp8266
但是目前很多功能還沒有,所以只好換用功能較齊全的 nodemcu lua
https://github.com/nodemcu/nodemcu-firmware/releases
下載它的 0.9.6-dev_20150704/nodemcu_float_0.9.6-dev_20150704.bin

用這裡的 esptool.py 把 bin 檔燒進ESP8266 裡就大功告成了
https://github.com/nodemcu/nodemcu-firmware/tree/master/tools

之後用這個把寫好的 lua 程式傳進ESP8266 裡就可以執行
https://github.com/CHERTS/esp8266-devkit/blob/master/Espressif/utils/nodemcutil.py

連線可以利用 python-serial 內附的 miniterm.py
miniterm.py -p /dev/ttyUSB0 -b 9600
連進去之後以 dofile('xxx.lua') 來執行即可

開機後會自動執行 init.lua, 可以在這個檔裡指定接下去要做什麼,
來呼叫其他的檔案,像是執行 web server 或telnet server, 在ESP裡
是不能同時執行兩種 TCP service, 只能擇一

ESP8266 的wifi 可以同時兼俱 client(接收) 跟 AP(發射), 兩者有不同的 MAC addr.

所以也可以完全關掉wifi 功能,單純當做MCU (如果想省電的話(電池供電時))
wifi.sta.disconnect()

開機後可以藉由按鈕(button) 來呼叫某項功能或執行檔案,例如第2 腳(GPIO 4)
接一個按鈕來執行讀取溫度的功能
gpio.mode(2,gpio.INT)
gpio.trig(2,"both",function() temp() tmr.delay(200000) end)
這裡的 tmr.delay(200000) 是用來避開雜訊以免按一下變成按很多下

ESP8266 只有一支analog 腳可用! 用法是 N=adc.read(0)
CHP_PD 是一定要接3.3V 所以乾脆把它跟VCC(3.3V) 拉一條OK線永久短路

而 GPIO 0 只有在燒 nodemcu firmware 時才需要接地,正常使用時就讓它懸空。
 使用新版nodemcu 的腳位在這裡
https://github.com/nodemcu/nodemcu-firmware
(往下拉到 GPIO NEW TABLE ( Build 20141219 and later) )
從正面看,右邊就是 1,2,3,4,8 左邊是 5,6,7,0 共九支 GPIO, 其中
第0腳(GPIO 16)會拉成HIGH 所以要把它改成LOW,
第4腳(GPIO 2)也有同樣情況,但是可以用 gpio.INPUT 把它改變成LOW

可以利用 tmr.alarm(0,xxxx) 來設定最多七組不同功能分別在背景裡等待執行或不斷循環執行。
其它像是利用 pwm 的 clock 來讓buzzer 發出不同頻率的聲音,利用 pwm 的duty
來指揮 servo 的旋臂轉動

使用超音波聲吶 HC-SR04, 不過好像遠一點就測不準?

函數語法說明在這裡
http://www.nodemcu.com/docs/

lua 還蠻容易上手的,也不用像Arduino 要用C 寫好再compile 再燒進去,
而是直接寫、直接執行。

有個情況是某些模組接了 PL23xx 的 USB TTL 轉接器時,無法通連 TX/RX
而換成 FTDI 的USB TTL 就可以,所以如果有問題的話,電源方面要多試試。

4 則留言:

terror bird kibble id 提到...

hi, I bought a black esp8266 ai inside cloud module without knowing it. when I connect vcc to 3.3v,gnd to gnd,tx to arduinos rx and rx to arduinos tx converted to 3.3v. as far a red led powers on. now when I connect ch_pd to 3.3v the red led powers off. also I type to serial monitor a single AT and I dont get any response. I tried many different connections with no result. what can I do ?
Read more

pachy ark 提到...

Hi, I recently bought on of these ESP 8266 boards, and I have done everything in the video, and downloaded many different drivers, every one in these suggestions and more. I have a mac, and when I plug the board in it gets power and transmits its wifi signal that I can connect to, and if I go to "About This Mac" I can see it in the USB section. Everything seems to be working, the only thing is when I use the Arduino IDE I can't find it in the "port" section under the tools bar. I have a bunch of different drivers, and did all the things regarding downloading the board and copying and pasting the link into the preferences. I only get in the port section the bluetooth incoming port and the bluetooth modem. It's ok if nobody can help, but I would really like to start using this board. Thanks!
Read more



eu4 console commands 提到...

請問之後會教如何用鏡頭人臉辨識的方法嗎?
因為大大的教學方式,真的很棒。讓完全不會的人,由淺入深的學習到最後動手做和小朋友一起做。很有成就感⋯⋯十分感謝您

Diy hair growth serum 提到...

The "SSID" and the "password" is compulsorily to be written in the program.Thus for every other wifi router one has to enter that SSID and password. It will be better that instead of the SSID and the password written in the program , a computer interface is made on a terminal where the SSID and the password for the WiFi router are entered and then only the ESP8266 is connected to the Arduino for the rest of the program to run for whatever output is required