1. 先下載 NodeMCU studio, 主要是用來上傳Lua script到板子上
2. 插入板子到NB, 開啟NodeMCU studio
3. 按下重新整理, 確認有沒有掃到COM port, 掃到後按上連接
4. 把下方的code, 存成Test.lua, 編輯SSID/密碼/Thingspeak API, 然後再NodeMCU studio開啟這個檔案
5. 按下上傳, 這時候會把剛剛開啟的Test.lua上傳到NodeMCU
6. 按下執行, 這邊會先列出板子上有的script, 然後彈出視窗看要執行哪一個
7. 接下來就會一直上傳電壓到Thinkspeak了
Source code is referenced from - POSTING TO THINGSPEAK WITH ESP8266 AND NODEMCU
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
| wifi.setmode(wifi.STATION);wifi.sta.config("SSID" ,"WIFIKEY");function postThingSpeak(level) connout = nil connout = net.createConnection(net.TCP, 0) connout:on("receive", function(connout, payloadout) if (string.find(payloadout, "Status: 200 OK") ~= nil) then print("Posted OK"); end end) connout:on("connection", function(connout, payloadout) print ("Posting..."); local volt = node.readvdd33(); connout:send("GET /update?api_key=CHANNEL_API_KEY&field1=" .. (volt/1000) .. "." .. (volt%1000) .. " HTTP/1.1\r\n" .. "Host: api.thingspeak.com\r\n" .. "Connection: close\r\n" .. "Accept: */*\r\n" .. "User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n" .. "\r\n") end) connout:on("disconnection", function(connout, payloadout) connout:close(); collectgarbage(); end) connout:connect(80,'api.thingspeak.com')endtmr.alarm(1, 60000, 1, function() postThingSpeak(0) end) |