当前位置:网站首页>Lua language development, esp8266 access to Bafa cloud, mqtt and TCP protocols
Lua language development, esp8266 access to Bafa cloud, mqtt and TCP protocols
2022-06-24 02:59:00 【Bafa】
First step ,lua Language development environment configuration
Download and install java Environmental Science , Download address : Click to download
choice windows Download and install the version , As shown in the figure :
Download the development environment package :
Download address : Click to download
esp8266 Serial driver : Click to download
Decompress after downloading , Plug in the computer 8266 Development board , open NodeMCU-PyFlasher.exe Software , choice esp8266 Of com mouth , choice nodemcu.bin The firmware , Click on flash nodemcu Brush in , As shown in the figure :
When brush in nodemcu After firmware , You can do it lua Programming . decompression ESPlorer-0.2.0.zip after , Double click in the folder ESPlorer.jar execute ( To complete the first step java Environmental installation , Otherwise, click no response ) Software usage steps :
Uploading pictures ...
First of all : choice esp8266 Of com mouth .
second : Click on open Connect esp8266, Wait for the connection to succeed , If the connection is successful , Will be displayed nodemcu Firmware information , If the connection fails , It can be clicked twice open On the left side of the button RTS Button restart esp8266, Note that double click RTS.
Third : Click on open Folder icon selection init.lua Program .
Fourth : When the program modification is completed , Click on save to ESP Button upload program , perhaps ctrl+s You can also save and upload , After uploading , Double click... On the software RTS Button restart esp8266, Or restart it manually esp8266 It's fine too .
second , The sample program
tcp The sample program : Need to be revised wifi Name and password , User private key uid And the theme topic value .
Bafayun tcp Service address :bemfa.com
port :8344
-- wifi To configure
wifi.setmode(wifi.STATIONAP)
apcfg={}
--wifi Name and password
apcfg.ssid="newhtc2"
apcfg.pwd="qq123456"
wifi.sta.config(apcfg)
wifi.sta.connect()
wifi.sta.autoconnect(1)
function startup()
srv = net.createConnection(net.TCP, 0)
-- Connect to server
srv:connect(8344,"bemfa.com")
-- receive messages
srv:on("receive", function(sck, c)
print(c)
end)
-- When connecting to the server
srv:on("connection", function(sck, c)
ConnOK = 1
sck:send("cmd=1&uid=4d9ec352e0376f2110a0c601a2857225&topic=led002\r\n")
-- heartbeat
liveTimer = tmr.create()
liveTimer:register(30000, tmr.ALARM_AUTO, function() sck:send("ping\r\n") end)
liveTimer:start()
end)
-- When the connection breaks
srv:on("disconnection", function(sck, c)
tmr.create():alarm(1000, tmr.ALARM_SINGLE, startup)
end)
end
-- When wifi Successful connection
wifi.eventmon.register(wifi.eventmon.STA_GOT_IP, function(T)
print("WIFI CONNECTED OK")
tmr.create():alarm(2000, tmr.ALARM_SINGLE, startup)
end)Detailed bafayun tcp Instructions can be found in the access documentation : Click the jump
mqtt The sample program :
Bafayun mqtt service ip:bemfa.com
port :9501
Connect to server
1. The user's private key is used as the connection key MQTT The client side of the server ID
2. When connecting, the user name and password are empty , Or fill in at will , That is, the device does not need an account and password when connected
-- wifi To configure
wifi.setmode(wifi.STATIONAP)
apcfg={}
apcfg.ssid="newhtc2"
apcfg.pwd="qq123456"
wifi.sta.config(apcfg)
wifi.sta.connect()
wifi.sta.autoconnect(1)
function startup()
--client id Connect
m = mqtt.Client("4d9ec352e0376f2110a0c601a2857225", 60)
m:connect("bemfa.com", 9501, false, function(client)
print("connected")
client:subscribe("mylight002", 0, function(client) print("subscribe success") end)
end,
function(client, reason)
print("Connection failed reason: " .. reason)
tmr.create():alarm(3000, tmr.ALARM_SINGLE, startup)
end)
m:on("message", function(client, topic, data)
print(topic .. ":" )
if data ~= nil then
print(data)
end
end)
m:on("offline", function(client)
print ("offline")
tmr.create():alarm(3000, tmr.ALARM_SINGLE, startup)
end)
end
-- When wifi Successful connection
wifi.eventmon.register(wifi.eventmon.STA_GOT_IP, function(T)
print("WIFI CONNECTED OK")
tmr.create():alarm(3000, tmr.ALARM_SINGLE, startup)
end)The program on : First configuration wifi,, When the connection wifi success , adopt wifi.eventmon.STA_GOT_IP Event acquisition wifi Connection success status , start-up startup function ,startup Function to initialize the server connection , When the connection is disconnected , Pass statement :
tmr.create():alarm(3000, tmr.ALARM_SINGLE, startup)
Realization 3s Then start again startup Function to reconnect to the server , Default reconnection is 3 second , You can modify .
边栏推荐
- How to choose the appropriate configuration server?
- The server size of the cloud desktop. The cloud desktop faces the server configuration requirements
- Grpc: based on cloud native environment, distinguish configuration files
- Simple use of notification
- [51nod] 3216 Awards
- 2022-2028 global pilot night vision goggle industry research and trend analysis report
- Hook principle
- What is the use of cloud desktop security server configuration? What should I do?
- How to transfer files from the server connected to the fortress machine and how to access the server through the fortress machine
- South Korea's national network is disconnected. Who launched the network "attack"?
猜你喜欢

2022-2028 global marine wet exhaust hose industry research and trend analysis report

2022-2028 global medical modified polypropylene industry research and trend analysis report

IOS development - multithreading - thread safety (3)

2022-2028 global aircraft wireless intercom system industry research and trend analysis report

UI automation based on Selenium

2022-2028 global cancer biopsy instrument and kit industry research and trend analysis report
![[51nod] 2106 an odd number times](/img/af/59b441420aa4f12fd50f5062a83fae.jpg)
[51nod] 2106 an odd number times

2022-2028 global portable two-way radio equipment industry research and trend analysis report
![[51nod] 3395 n-bit gray code](/img/b5/2c072a11601de82cb92ade94672ecd.jpg)
[51nod] 3395 n-bit gray code
![[51nod] 3047 displacement operation](/img/cb/9380337adbc09c54a5b984cab7d3b8.jpg)
[51nod] 3047 displacement operation
随机推荐
How to choose the appropriate configuration server?
Objective-C downloads pictures from the network, saves them, and displays them from the save with uiimageview
LeetCode 724. Find the central subscript of the array
What is the GPU usage for cloud desktops and servers? What can cloud desktop do?
2022-2028 global third-party data platform industry research and trend analysis report
What is the case when easynvr non administrator logs in to view the empty video list?
Tstor onecos, focusing on a large number of object scenes
What are the security guarantees for cloud desktop servers? What are the cloud desktop server platforms?
Afnetworking server client
2022-2028 global indoor pressure monitor and environmental monitor industry research and trend analysis report
2022-2028 global portable two-way radio equipment industry research and trend analysis report
2022-2028 global pilot night vision goggle industry research and trend analysis report
Gin framework: add Prometheus monitoring
Ner's past, present and future Overview - past
2022-2028 global anti counterfeiting label industry research and trend analysis report
The server size of the cloud desktop. The cloud desktop faces the server configuration requirements
Some tips for using uitextview
Cp/rm/mv parameters
Coding helps Jiangsu high-speed information to achieve organizational agility and R & D agility, leading the new infrastructure of Intelligent Transportation
MySQL case deep excavation information_ Root causes of slow schema view query (Part 2)