nodemcu获取传感器值显示在OLED屏并上传onenet服务器

1、OLED相关材料参照文章:esp8266学习笔记⑨:OLED 屏幕的使用(u8g2图形库模块)
2、实现功能:
(1)获取温湿度传感器值;
(2)通过mqtt协议上传onenet服务器,实现在线显示数据;
(3)通过mqtt协议订阅功能,实现远程开关;
(4)将数据实时显示在oled屏幕上。所用oled是0.96寸I2C.
3、代码

--引脚初始化
led_net = 2--这个引脚是用作连接wifi后提示的
led_pin = 3--这个引脚是用作测试onenet开关的

--oled显示需要初始化2个引脚
sda = 5
scl = 6

dht_pin = 7--这个是温湿度传感器针脚
gpio.mode(led_net,gpio.OUTPUT)
gpio.mode(dht_pin,gpio.INPUT)
gpio.mode(led_pin,gpio.OUTPUT)
--oenet服务器参数声明
DeviceId = "587667371"--设备ID
ProductId = "325428"--产品ID
AuthoInfo = "test"--产品鉴权码
host = "183.230.40.39"--mqtt服务器
port = 6002-mqtt服务器端口
--连接wifi初始化
wifi.setmode(wifi.STATION)
cfg = {}
cfg.ssid = "kyn"
cfg.pwd = "20160118"
wifi.sta.config(cfg)
wifi.sta.connect()

--下边是代码1:oled初始化
--具体参数参照:https://blog.csdn.net/weixin_42573320/article/details/86648994
sla = 0x3c--oled的地址
i2c.setup(0, sda, scl, i2c.SLOW)--i2c设备初始化
disp = u8g2.ssd1306_i2c_128x64_noname(0, sla)--图形库初始化
disp:setFontPosTop()--字符出现的位置


timer = tmr.create()
function con()
    if wifi.sta.getip() == nil then
        print("coneting........")
    else
        timer:stop()
        print("success!~")
        gpio.write(led_net,gpio.HIGH)
        print(wifi.sta.getip())


        client_hum = mqtt.Client(DeviceId,120,ProductId,AuthoInfo)
        client_hum:connect(host,port,0,function(client)
        print("connect success!")
        end)


        function h()
                status,temp,humi,temp_dec, humi_dec = dht.read11(dht_pin)
                print("DHT Temperature:"..temp..";".."Humidity:"..humi)
        
        --下边是代码2:oled显示
                str = "TEMP:"
                str_1 = temp.."*C"
                str_2 = "HUMI:"
                str_3 = humi.."%"    
        
                disp:setFont(u8g2.font_6x10_tf)
                disp:drawStr(30,5,str)
                disp:drawStr(30,20,str_1)
                disp:drawStr(30,35,str_2)
                disp:drawStr(30,50,str_3)
                disp:sendBuffer()
        
                
        --订阅功能,实现开关
                client_hum:subscribe("$dp",0, function(client)
                    print("subscribe success")
                end)
                client_hum:on("message", function(client,topic,message)
                    print(message)
                    if(message == "1") then
                        gpio.write(led_pin,gpio.HIGH)
                    else
                        gpio.write(led_pin,gpio.LOW)
                    end
                end)
        
        
        --使用MQTT协议,上传数据
                weather = {}
                weather.Temperature = temp
                weather.Humidity = humi
                state,weather_message = pcall(sjson.encode,weather)
                --mqtt:publish(topic, payload, qos, retain[, function(client)])
                payload = string.char(3,0,string.len(weather_message))..weather_message
                client_hum:publish("$dp", payload, 0, 0, function(client)
                    print("upload success!~")
                end)
        end--h函数结束位置
        
        tmr.create():alarm(1500,tmr.ALARM_AUTO,h)   
    end
end
timer:alarm(1000,tmr.ALARM_AUTO,con)

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值