前言
一直在開發軟件。今日突然心血來潮想嘗試一下硬件。
於是就買了這樣一個板子:
買的淘寶上大佬幫忙找的一個套裝。 除了板子之外還有一些線和其他配件:溫濕度傳感器,氣壓傳感器,光線傳感器,小屏幕。
板子的型號是 ESP8266 CH340G 。
另外還買了一個霍爾傳感器:
嘗試入門
按照網上的很多教程,拿到板子之后第一步先刷固件。
參考 https://blog.csdn.net/qq_41736609/article/details/80980954
這里就不多講了。
然后,隨后的事情令我猝不及防:我的板子插到我筆記本上的時候不能識別為COM串口,只能識別成“未知設備”..... 這令我費解,我查了很多資料,都說是驅動沒有安裝。我隨機拿着板子插到家里的台式機上,同樣都是win10系統,台式機立馬正確識別COM端口,驅動正常。???!!! なに???(納尼)
這令我費解,然后開始找驅動程序,找了半天終於找到了很多驅動,結果卻都不能用....
最后我上網搜了一下 未知設備 的名字,什么FT232R之類的,我忘記了,當時沒有截圖。結果真的有響應的驅動:
哈哈哈哈,然后直接從設備管理器里面安裝驅動,要安裝兩邊 。我看了一個國外的網站里面有類似的教程 :
參考 : https://www.usb-drivers.org/ft232r-usb-uart-driver.html
防止丟失,我把內容都貼出來了
Step 1:
Download the driver file and extract it to your hard driver where you can find it
FT232R USB UART Driver Download
Step 2:
You first need to uninstall the incorrect drivers on your computer. In Windows, go to Control Panel then to Device Manager
Now press the delete button on your computer and it will ask for confirmation.
Collapse the Ports menu and now click on the USB controller at the bottom of the list.
Now you need to uninstall the existing USB Serial Convertor.
Disconnect the USB cable from your PC to KMX1 or KMX2
reconnect the USB cable from your PC to KMX1 or KMX2
A new yellow flagged item will appear FT232R USB UART
Right Click on FT232R USB UART
Select Update Driver Software
Now you need to find where you extracted the drivers from the file you downloaded above and point the software to the directory.
The driver software for USB Serial Converter has been installed
The USB Serial Convertor is now installed. Next you have to install the port. If you are old enough to remember software modems then you will recall that it was done in the same manner.
The yellow flagged item will have changed name to USB Serial Port
Right Click on USB Serial Port
Select Update Driver Software
The driver software for the Port is in the same directory where you had the other drivers which you had just installed.
The Serial Port will now install and that will be the final installation. There should be no yellow flags any more.
Done!
上面是我引用的內容。
--------------------------------------------------------------------------分割線 --------------------------------------------------------------------------------
驅動搞完了,然后去nodemcu官網構建自己的固件,打開下面的網址
我自己不懂,就勾選了這些
然后開始教程里面的刷固件,那個教程也有對應的軟件下載,再次感謝他的奉獻
開發的過程中,主要用到兩個軟件:
1. 燒寫固件工具 ESP8266Flasher
2. 燒寫程序工具 ESPlorer
嗯.... 硬件里面都叫燒寫,要么燒錄 都是發燒友...
接着 ,我連上了套件里送的 溫濕度傳感器。具體的連接方法: https://www.qutaojiao.com/233.html
貼出最重要的東西:
DHT11上標有(+或VCC)引腳的連接nodemcu的+ 3V引腳。
DHT11上標有(S或OUT)引腳的連接nodemcu的D4V引腳。
DHT11上標有(-或GND)引腳的連接nodemcu的GND引腳。
至此,get到新知識 :VCC連接電源正極,GND連接地線,其他基本都是數據傳輸用的。
然后使用剛說的第二個軟件 ESPlorer 燒寫程序:
運行結果如下:
不知道為啥.... 溫濕度數值都不太正常,可能時配件失靈了...
新的想法
做到上一步,基本已經熟悉了一點皮毛了。可以開始着手實現idea了
之前買了一個霍爾傳感器,就是想把我家的普通動感單車改造成一個智能的,可以傳輸數據的動感單車 。基本原理是
用霍爾傳感器測出轉速,然后傳輸到手機或者電腦上,然后寫一個程序比如,吹氣球。 實現單車控制吹氣球的速度。哈哈哈哈哈,想想就好玩
第一步:連線
連好之后是這樣的,這個傳感器有兩個燈,一個是電源指示燈,一個是 開關指示燈。 開關指示燈在霍爾傳感器被激活的時候就會亮。
不過目前我還不知道霍爾傳感器怎么被激活......
霍爾傳感器的D0接口,連接的是板子的D1或者D2 或者其他IO引腳。都可以
第二步:操作IO接口,獲取數據
這一步卡了很久 連上之后不管怎么都沒反應 。下面是我嘗試時的視頻,為此我還專門去旁邊的五金店買了一個磁鐵 7元錢,真貴。
算了 不能直接上傳視頻 ,弄個圖片吧
然后,找呀找 卡在這里很痛苦,睡覺都睡不好。 結果在小破站找到了一個大佬的視頻:
https://www.bilibili.com/video/BV1Zv411t7xk
發現他用的板子,和傳感器跟我的一模一樣,這對於一個新手來說簡直是天大的福利。 能比葫蘆畫瓢的學習,最好了 哈哈哈哈 ,要是有老師更好
如果你也在開發這個,請看他的視頻...
第三步:編寫程序讀取霍爾傳感器數據
參考官方文檔:https://nodemcu.readthedocs.io/en/release/modules/gpio
這里有對gpio這個模塊的詳細解釋,不過是英文的,一點點啃吧。
inputPin = 4 gpio.mode(inputPin, gpio.OUTPUT) gpio.write(0, gpio.LOW) gpio.mode(inputPin, gpio.INPUT) tmr.create():alarm(50, tmr.ALARM_AUTO, function() print(gpio.read(inputPin), tmr.time()) end)
哦對了,這里有個很重要的知識點,也是卡我很久重大原因,就是關於引腳對應的GPIO編號,和index值 這個一開始我一直搞混了....
放一個圖:
參考: https://www.jianshu.com/p/c3ef61c9c59b
然后,正常獲取引腳的數據了。
第五步:編寫websocket 上傳數據
使用websocket是我開發javaWeb這么多年 必選的,當然可以用其他方式交互數據,當然也是恰好官網上有websocket對這個固件的支持:
--- 2021-03-25 更新
首先,本地用spring 跑一個webSocket服務 。Java寫的
@Override public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) { registry .addHandler(getNodemcuHandle(), "/websocket/nodemcu") .addInterceptors(new ClientSocketInterceptor()) .setAllowedOrigins("*"); }
然后我電腦的IP是 192.168.3.246 ,監聽端口是 8081 ,接着
那就直接上一下 websocket代碼吧
-- websocket ws = websocket.createClient() ws:on("connection", function() print('got ws connection') function hallInterupt(level, when, eventcount) gpio.trig(inputPin) -- tmr.delay(70000) -- wait 700 ms print('level:',level) -- print level of on pin print('when(us):',tmr.now() - when ) -- print timestamp while interrupt occur print('eventcount:', eventcount)--eventcount sendData = '{"actionName":"hallData","data":"1"}' ws:send(sendData) gpio.trig(inputPin,"up", hallInterupt) end gpio.trig(inputPin,"up", hallInterupt) end) ws:on("receive", function(_, msg, opcode) print('got message:', msg, opcode) -- opcode 為 1 則是文本消息, 為 2 則是二進制消息 end) ws:on("close", function(_, status) print('connection closed', status) connectServer(myIp) --ws = nil -- 需要lua釋放回收websocket客戶端實例 hallTmr:stop() end) ws:connect('ws://192.168.3.246:8081/server/websocket/nodemcu?clientId=testId')
在ws的 connection事件里面,監聽IO的中斷,不過目前為止我還不知什么叫中斷,畢竟才是一個剛買板子3天的小白。
雖然不懂,但是起碼知道 如果霍爾傳感器有感應到數據,IO中斷就會被觸發 。 類似監聽器一樣 。注意看上面的hallInterupt方法。里面有三個參數,參考官方解釋:
callback function when trigger occurs. The level of the specified pin at the interrupt passed as the first parameter to the callback. The timestamp of the event is passed as the second parameter. This is in microseconds and has the same base as for tmr.now()
. This timestamp is grabbed at interrupt level and is more consistent than getting the time in the callback function. This timestamp is normally of the first interrupt detected, but, under overload conditions, might be a later one. The eventcount is the number of interrupts that were elided for this callback. This works best for edge triggered interrupts and enables counting of edges. However, beware of switch bounces -- you can get multiple pulses for a single switch closure. Counting works best when the edges are digitally generated. The previous callback function will be used if the function is omitted.
我懶得翻譯了。
參考: https://www.qutaojiao.com/10542.html (感謝分享)
下面三個print 方法,分別打印了這三個參數.
然后ws:send 是向服務器發送數據 。 websocket 程序至此完畢。
--- 2021-03-25 更新
第六步:根據獲取的數據處理程序
2021-03-24 首次編輯:
不過目前我只做到了第四步。敬請期待吧...
2021-03-25 更新:
已經做到第五步了