卧室数据采集器——温度、湿度、光照强度、夜间手机使用监控

主要内容:暂定使用ESP8266(MicroPython)完成产品原型,主要用于监测控制夜间手机使用时间,数据传至onenet来综合分析睡眠质量。

5.1 ESP12F引脚功能规划:

TM1637四位数码管 IIC

GPIO4:clk

GPIO5:dio

微动按键 GPIO13
蜂鸣器 GPIO16(只能做为输出,不能输入,否则也会引起错误)
板载LED,低电平亮 GPIO2:

BME280大气压强温湿度传感器

MAX44009光照强度传感器

预留HX711称重传感器接口

GPIO12:sda
GPIO14:scl

5.2电路绘制

卧室数据采集器——温度、湿度、光照强度、夜间手机使用监控_第1张图片

5.3绘制PCB(请忽略板子外形,另有他用。)

卧室数据采集器——温度、湿度、光照强度、夜间手机使用监控_第2张图片

5.10先放几张图,下午慢慢写

卧室数据采集器——温度、湿度、光照强度、夜间手机使用监控_第3张图片

卧室数据采集器——温度、湿度、光照强度、夜间手机使用监控_第4张图片

 

卧室数据采集器——温度、湿度、光照强度、夜间手机使用监控_第5张图片

5.17代码已整理好。

已实现功能

TM1637四位数码管 IIC

轮流显示时间(WIFI实现NTP对时)、温度、气压、湿度

微动按键 手机放上状态为0,拿开状态为1,可简单得出夜间手机使用时间。
蜂鸣器 未焊接,准备用来设置手机到时放下,不到时间不准拿起,否则报警。
板载LED,低电平亮 onenet通过命令控制亮灭。

BME280大气压强温湿度传感器

MAX44009光照强度传感器

预留HX711称重传感器接口

只焊接了BME280来监测温度、气压、湿度。

未焊接光强传感器,准备监测室内亮灯状态辅助分析睡眠质量。

未焊接称重传感器,准备通过称重手机质量代替微动按键,精确区分监测主人手机。

主要代码

卧室数据采集器——温度、湿度、光照强度、夜间手机使用监控_第6张图片

main.py(函数略糙,DIY勿喷)

from machine import Pin, I2C
import utime
import tm1637
import ntptime
import bme280
from umqttsimple import MQTTClient
import json

led = Pin(2,Pin.OUT,value=0)
beep = Pin(16,Pin.OUT)
phone = Pin(13,Pin.IN,Pin.PULL_UP)
dio = Pin(5, Pin.OUT)
clk = Pin(4, Pin.OUT)
tm=tm1637.TM1637(dio=dio,clk=clk)

i2c = I2C(scl=machine.Pin(14), sda=machine.Pin(12))
bme = bme280.BME280(i2c=i2c)

mqtt_server = '183.230.40.39'
mqtt_user='你自己的用户ID'
mqtt_pwd='你自己的onenet产品key'
client_id = "你自己的设备ID"
topic_sub = b'onenetpub'
topic_pub = b'esp8266pub'

def pubdata(data):
    j_d = json.dumps(data)
    j_l = len(j_d)
    arr = bytearray(j_l + 3)
    arr[0] = 1 #publish数据类型为json
    arr[1] = int(j_l / 256) # json数据长度 高位字节
    arr[2] = j_l % 256      # json数据长度 低位字节
    arr[3:] = j_d.encode('ascii') # json数据
    return arr

def sub_cb(topic, msg):
  print(topic, msg)
  if "$creq" in topic:
    if msg == b'ledon':
      led.value(0)
      Led_State = {'datastreams':[{'id':'Led_State','datapoints':[{'value':'ledon'}]}]}
      client.publish('$dp',pubdata(Led_State))
    elif msg == b'ledoff':
      led.value(1)
      Led_State = {'datastreams':[{'id':'Led_State','datapoints':[{'value':'ledoff'}]}]}
      client.publish('$dp',pubdata(Led_State))
  if topic == b'onenetpub' and msg == b'received':
    print('ESP received hello message')
def connect_and_subscribe():
  global client_id, mqtt_server, topic_sub,mqtt_user,mqtt_pwd
  client = MQTTClient(client_id, mqtt_server,port=6002,user=mqtt_user, password=mqtt_pwd, keepalive=60)
  client.set_callback(sub_cb)
  client.connect()
  client.subscribe(topic_sub)
  print('Connected to %s MQTT broker, subscribed to %s topic' % (mqtt_server, topic_sub))
  return client
  
def restart_and_reconnect():
  print('Failed to connect to MQTT broker. Reconnecting...')
  time.sleep(10)
  machine.reset()
  
try:
  client = connect_and_subscribe()
except OSError as e:
  restart_and_reconnect()
  
IF_NTP=0
def ntpsettime(IF_NTP):
    try:
      ntptime.time()
      ntptime.settime()
      rtc=machine.RTC()
      tampon1=utime.time() 
      tampon2=tampon1+8*60*60
      rtc.datetime ( utime.localtime(tampon2)[0:3] + (0,) + utime.localtime(tampon2)[3:6] + (0,))
      IF_NTP=1
    except:
       print('XXX')
       IF_NTP=0
    return IF_NTP
#开始循环
utime.sleep(3)
IF_NTP=ntpsettime(IF_NTP)
tm.brightness(4)
count=0
loop_count=0
while 1 :
    client.check_msg()
    (year, month, mday, hour, minute, second, weekday, yearday)=utime.localtime()
    #print (year,'-','%02d' % month, '-','%02d'% mday, ' ', '%2d'% hour, ':', '%02d'% minute, ':','%02d'% second, '  Week:',weekday+1, sep = '')
    count=count+1
    if ((count % (60 * 30) == 0) or (count % 10==0 and IF_NTP == 0)):
        IF_NTP = ntpsettime(IF_NTP)
        count = 0
    #led.value(count % 2)
    
    BME280 = bme.values
    #数据处理保留1位小数
    Temperature = BME280[0]*10//1/10
    Humidity = BME280[2]*10//1/10
    Pressure = BME280[1]*10//1/10
    Phone_State = phone.value()
    if loop_count % 180 == 0:#180s 3min
        msg = {'datastreams':[
                              {'id':'Temperature','datapoints':[{'value':Temperature}]},
                              {'id':'Humidity','datapoints':[{'value':Humidity}]},
                              {'id':'Pressure','datapoints':[{'value':Pressure}]},
                              {'id':'Phone_State','datapoints':[{'value':Phone_State}]}
                             ]
              }
    else:
        msg = {'datastreams':[
                              {'id':'Temperature','datapoints':[{'value':Temperature}]},
                              {'id':'Humidity','datapoints':[{'value':Humidity}]},
                              {'id':'Pressure','datapoints':[{'value':Pressure}]}
                             ]
              }
    try:
        client.publish('$dp',pubdata(msg))
    except OSError as e:
        tm.show('erro')
        restart_and_reconnect() 
        
    display_count = loop_count % 4
    if display_count == 0:
        tm.numbers(hour, minute, True)
    elif display_count == 1:
        tm.show('%02d*C'%BME280[0])
    elif display_count == 2:
        tm.show('%04d'%BME280[1])
    elif display_count == 3:
        tm.show('%02dRH'%BME280[2])
    elif display_count == 4:
        tm.show('SEND')
    elif display_count == 5:
        tm.show('err5')
    elif display_count == 6:
        tm.show('err6')    
    elif display_count == 7:
        tm.show('err7')    
    elif display_count == 8:
        tm.show('err8') 
    elif display_count == 9:
        tm.show('err')     
    else:
        tm.show('err')
    
    loop_count = loop_count+1
    if loop_count == 3600:
        loop_count = 0
        
    utime.sleep_ms(1000)

所有代码放在压缩包里,部分驱动文件有改动,赚点积分,没积分可以留个邮箱点个赞,到时候发你邮箱。

代码压缩包在这里:点这里下载

结贴。。。

 

你可能感兴趣的:(卧室数据采集器——温度、湿度、光照强度、夜间手机使用监控)