esp32雖然目前還沒有加入http,不過有socket也可以完成一些操作;
先看net 模塊中的 socket 模塊幫助文檔
常用的socket接口都實現了。下面來用用
首先您需要一個socket服務器端程序。就用我原來寫好的一個吧
先開啟一個socket服務器端,端口設置為 3322 當然端口隨意
如何在esp32中寫代碼
srv = net.createConnection(net.TCP, 0) --新建一個客戶端 srv:on("receive", function(sck, c) print(c) end) --設置一個回調函數 接受信息 -- Wait for connection before sending. srv:on("connection", function(sck, c) --設置一個回調函數,連接成功就發送一條信息 -- 'Connection: close' rather than 'Connection: keep-alive' to have server -- initiate a close of the connection after final response (frees memory -- earlier here), https://tools.ietf.org/html/rfc7230#section-6.6 sck:send("GET /get HTTP/1.1\r\nHost: httpbin.org\r\nConnection: close\r\nAccept: */*\r\n\r\n") end) srv:connect(3322,"192.168.0.124") --連接服務器
收到了來自esp32的消息
然后發送一個消息給板子
是不是很簡單,使用nodemcu-esp32 做開發真的是簡單多了。