当前位置:网站首页>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 :

001.png

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 :

002.png

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 .

原网站

版权声明
本文为[Bafa]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/10/20211021143624470W.html

随机推荐