ESP8266连接阿里云IoT

对于ESP8266如有疑问可通过添加QQ群:733762735讨论学习。

固件下载

固件可通过在线编译下载,也可以使用我之前编译好的固件。如果你不爱折腾,推荐使用我之前编译好的,因为在线编译的固件会实时更新导致有些语句无法使用。另外还有一个好处就是可以直接连接阿里云IoT。

如下载历史固件请查看readme.txt,里面包含模块内容说明推荐使用3.23的固件。

固件烧录软件

烧录软件点击下载

烧录地址默认0x0

在这里插入图片描述

软件编辑器

ESPlore:点击下载
Java环境搭建视频教程:点击前往

软件基于Java环境,本人使用的是JDK1.8,具体实测为准。

在这里插入图片描述

连接阿里云IoT

内容可参考:https://blog.csdn.net/weixin_43368807/article/details/82984796

代码分享

代码较为简陋,自行修改。

//第一段
//********init.lua******/
wifi.setmode(wifi.STATION)
MQTTconnectFlag="Wait..."
ip="Wait..."
function u8g2_init()
sda = 5 -- GPIO14
scl = 6 -- GPIO12
sla = 0x3c
i2c.setup(0, sda, scl, i2c.SLOW)
disp = u8g2.ssd1306_i2c_128x64_noname(0, sla)

disp:setFont(u8g2.font_6x10_tf)
disp:setFontRefHeightExtendedText()
disp:setDrawColor(1)
disp:setFontPosTop()
disp:setFontDirection(0)
disp:clearBuffer()
end
u8g2_init()
disp:drawStr(27,0,"SYSTEM STATUS")
disp:drawStr(0,12,"========================")
disp:drawStr(0,25,"IP:"..ip)
disp:drawStr(0,38,"MQTT:"..MQTTconnectFlag)
disp:drawStr(105,55,"NEXT")
disp:sendBuffer()
-----[wifi config]-------------
---------------------------
cfg={}  
cfg.ssid="JMCC"
cfg.pwd="JMcc(}1dc"

zj={}  
zj.ssid="MEIZU 15"
zj.pwd="aaa10086"
cmcc={}
cmcc.ssid="CMCC-user"
cmcc.pwd="246x1234?"

wl={}
wl.ssid="2.4"
wl.pwd="fankai1213"

wifi.sta.config(wl)

wifi.eventmon.register(wifi.eventmon.STA_GOT_IP, function(T)
     print("Connected, IP is "..wifi.sta.getip())
     ip=wifi.sta.getip()
dofile("s5.lua")
end)

wifi.eventmon.register(wifi.eventmon.STA_DISCONNECTED, function(T)
     print("wifi disconnect")
end)

//第二段
//**************s5.lua***************//
jdqp=1
hwp=2
dhtp=8
led=4
fmqp=7
an=3
gpio.mode(an,gpio.INPUT)
gpio.mode(jdqp,gpio.OUTPUT)
gpio.write(jdqp,0)
gpio.mode(hwp,gpio.INPUT)
gpio.mode(dhtp,gpio.INPUT)
gpio.mode(led,gpio.OUTPUT)
gpio.write(led,1)
gpio.mode(fmqp,gpio.OUTPUT)
gpio.write(fmqp,0)

json="N/A"
wd="N/A"
sd="N/A"
js="N/A"
fs="N/A"
rw="N/A"
rw="N/A"
hw="N/A"
MQTTconnectFlag="N/A"
sj=0
ww="0"
ss="0"
jdq="N/A"
fmq="N/A"
flag=1
jc=1
jx=0


 


ProductKey ="修改"   
ClientId =wifi.sta.getmac()  
DeviceName ="修改" 
DeviceSecret="修改"
RegionId="cn-shanghai"     

myMQTTport=1883    --port
myMQTT=nil        --client

myMQTThost=ProductKey..".iot-as-mqtt."..RegionId..".aliyuncs.com"   --host
myMQTTusername=DeviceName.."&"..ProductKey          --username


t1="修改,填写TOPIC"
t2="修改,填写TOPIC"
t3="修改,填写TOPIC"
sb="修改,填写TOPIC"


function GetNetTime()
    sntp.sync({"203.107.6.88","0.nodemcu.pool.ntp.org","1.nodemcu.pool.ntp.org","2.nodemcu.pool.ntp.org","3.nodemcu.pool.ntp.org","www.beijing-time.org"},
         function(sec, usec, server, info)
                 print('sync', sec, usec, server,info)   
                 sj=sec-5
                 sj2=string.format(sec.."000")
                 print(sj2)
         end,
         function()
            print("get time error")
         end)  
    return sj
end

myMQTTtimes='6666'
hmacdata="clientId"..ClientId.."deviceName"..DeviceName.."productKey"..ProductKey.."timestamp"..myMQTTtimes  
myMQTTpassword=crypto.toHex(crypto.hmac("sha1",hmacdata,DeviceSecret))   
myMQTTClientId=ClientId.."|securemode=3,signmethod=hmacsha1,timestamp="..myMQTTtimes.."|"  

myMQTT=mqtt.Client(myMQTTClientId, 120,myMQTTusername,myMQTTpassword) 

   

MQTTconnectFlag=0
tmr.alarm(0,1000,1,function()
    if myMQTT~=nil then
        print("Attempting client connect...")
        myMQTT:connect(myMQTThost, myMQTTport,0,MQTTSuccess,MQTTFailed)
    end
end)


function MQTTSuccess(client)
    print("MQTT connected")
    client:subscribe({t1=0,t2=0,t3=0}, function(conn)    
        print("subscribe success") 
    end) 
    myMQTT=client
    MQTTconnectFlag=1
    ms="CONNCET"
    tmr.stop(0)      
end


function MQTTFailed(client,reson)
    print("Fail reson:"..reson)
    MQTTconnectFlag=0
    ms="FILED"
    tmr.start(0)     
end


myMQTT:on("offline", function(client) 
    print ("offline") 
    tmr.start(0)
end)

print(q)
myMQTT:on("message", function(client, topic, data) 
    print(topic ..":") 
    if data ~= nil then
        print(data)
            if data=="11"    then
                gpio.write(1,1)
            elseif data=="10"   then
                 gpio.write(1,0)
            elseif data=="41"   then
                 gpio.write(led,1)
            elseif  data=="40"  then
                gpio.write(led,0)
            elseif  data=="71"  then
                gpio.write(fmqp,1)
            elseif data=="70"   then
                gpio.write(fmqp,0)
            elseif data=="0"    then
                client:publish(sb,q,0,0,function(client)    print("updata ok!") end)
            elseif string.find(data,[[{"LightSwitch":0}]])~=nil then
                gpio.write(1,0)
            elseif string.find(data,[[{"LightSwitch":1}]])~=nil then
                gpio.write(1,1)
            end
            
    end
end)
function cj()
    z,wd,sd,x,y=dht.read11(dhtp)
    if wd~=-999 and sd~=-999    then
        ww=wd
        ss=sd
    end
    jdqs=gpio.read(jdqp)
        if jdqs==1 then
        jdq="ON"
        else
        jdq="OFF"
        end
    hws=gpio.read(hwp)
         if hws==1 then
            hwk="FIND"
            gpio.write(fmqp,1)
        else
            hwk="NONE"
            gpio.write(fmqp,0)
        end
    fmqs=gpio.read(fmqp)
        if fmqs==1  then
            fmq="SPEAK"
        else 
            fmq="STOP"
        end
    krz=adc.read(0)
    if krz>=600 then
        gpio.write(fmqp,1)
        tmr.delay(100000)
    end
end

function sc()

 json=string.format([[{
  "id": "123",
  "version": "1.0",
  "params": {
    "mtemp": {
      "value": %d,
      "time": %s
    },
    "mhumi": {
      "value": %d,
      "time":  %s
    },
     "LightSwitch": {
        "value":%d,
        "time": %s
        },
     "PowerSwitch_1":{
        "value":%d,
        "time":  %s
        },
      "RingLEDSwitch":{
        "value":%d,
        "time": %s
        },
       "GasConcentration":{
        "value":%d,
        "time": %s
        }   
  },
  "method": "thing.event.property.post"
}]],ww,sj2,ss,sj2,jdqs,sj2,fmqs,sj2,hws,sj2,krz,sj2)
end
tmr.alarm(2,20000,1,function()
tmr.alarm(1, 5000, 1, function() 
    sj=sj+5
    if MQTTconnectFlag==1 and myMQTT~=nil then 
        sj2=string.format(sj.."000")
        cj()
        sc()
        myMQTT:publish(sb,json,0,0,function(client)
                        print("send ok" ) 
        end)
    end
   
function me1()
disp:clearBuffer()
disp:drawStr(27,0,"SYSTEM STATUS")
disp:drawStr(0,12,"======================")
disp:drawStr(0,25,"IP:"..ip)
disp:drawStr(0,38,"MQTT:"..ms)
disp:drawStr(105,55,"NEXT")
--disp:drawStr(0,10,"temp:"..wd.."C")
disp:sendBuffer()
print("mw1")
end
function me2()
disp:clearBuffer()
i=12
disp:drawStr(0,12-i,"TMEP:          "..ww)
disp:drawStr(0,24-i,"HUMI:          "..ss)
disp:drawStr(0,36-i,"JDQ :          "..jdq)
disp:drawStr(0,48-i,"HTJC:          "..hwk)
disp:drawStr(0,55,"UP")
disp:drawStr(105,55,"NEXT")
--disp:drawStr(0,10,"temp:"..wd.."C")
disp:sendBuffer()
print("me2")
end
function me0()
disp:clearBuffer()
i=12
disp:drawStr(0,12-i,"MQ-5:          "..krz)
disp:drawStr(0,24-i,"FMQ:          "..fmq)
disp:drawStr(0,55,"UP")
disp:sendBuffer()
    print("me0")
end
--    if gpio.read(an)==0  then
--        me2()
--    elseif gpio.read(an)==1  then
--        me1()
--    end

   tmr.alarm(3,1500,1,function()

    a=gpio.read(3)
    tmr.delay(1000000)
    c=gpio.read(3)
    if a==1 and c ==0 then
        jx=jx+1
    
--    i=i-1
    end
    print(jx)
    if jx==10   then
        jx=0
    end
    qm=jx%3
    if qm==0    then
        me1()
     elseif qm==1   then
        me2()
    else me0()
    end
    
   end)
      
    
end)
GetNetTime()
end)


实物演示视频

演示视频:点击前往

优质学习内容推荐

传送门1:点击前往
在这里插入图片描述

  • 5
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值