资讯详情

ESP8266 python3 直接使用 gy-39 传感器

gy-39放在raspberry移动不方便,耗电量大,只是收集数据有点浪费,直接使用esp8266。

ESP8266也烧成python环境。省略配置wifi部分。

将附件中lib上传,ESP8266的lib与rasbperry不同。

ESP8266接口图。

6ec4fc239de45e555fd6f313602ab085.png

ESP8266 接口图

板子上的D1和D2对应的是ESP8266上的4,5脚,连接如图。

python3 bme280 max44009

到ESP查看硬件连接

>>> from machine import Pin, I2C

>>> i2c = I2C(scl=Pin(5), sda=Pin(4))

>>> i2c.scan()

[74, 118]

转换16进制算,两个传感器已经认识到,文档在raspberry pi连接GY-39文档中。

查看上传文件

>>> import os

>>> os.listdir()

[‘boot.py’, ‘bme280.py’, ‘max44009.py’]

测试传感器数据

>>> import machine

>>> import bme280

>>> i2c = machine.I2C(scl=machine.Pin(5), sda=machine.Pin(4))

>>> bme = bme280.BME280(i2c=i2c)

>>> print(bme.values)

(‘20.73C’, ‘1020.55hPa’, ‘24.31%’)

>>> import max44009

>>> lum = max44009.MAX44009()

>>> print(lum.luminosity())

3.06

一切正常。

在raspberry pi上做个简单http接口收集数据,代码如下:

import tornado.ioloop

import tornado.web

import time

def getNowTime():

return time.strftime('%Y-%m-%d %T',time.localtime(time.time()))

class MainHandler(tornado.web.RequestHandler):

def get(self):

#try:

lum = self.get_argument("lum")

hum = self.get_argument("hum")

temp = self.get_argument("temp")

press = self.get_argument("press")

f = open("./gy39.log", "a")

tmpData = "%s %s %s %s %s\n" % (getNowTime(), lum, hum, temp, press)

f.write(tmpData)

f.close()

#except:

# pass

self.write("ok")

application = tornado.web.Application([

(r"/", MainHandler),

])

if __name__ == "__main__":

application.listen(9999)

tornado.ioloop.IOLoop.instance().start()

ESP8266调用传感器代码

#!/usr/bin/env python3

# -*- coding:utf-8 -*-

# Revision:

# Author: simonzhang

# Email: simon-zzm@163.com

# Web: www.simonzhang.net

# -------------------------------

# http://192.168.3.212:9999/?lum=12&hum=3.43&temp=23423&press=12132

class timegy30():

def run():

import machine

#

import bme280

i2c = machine.I2C(scl=machine.Pin(5), sda=machine.Pin(4))

bme = bme280.BME280(i2c=i2c)

temp = bme.values[0][:-1]

press = bme.values[1][:-3]

hum = bme.values[2][:-1]

#

import max44009

lum = max44009.MAX44009()

lum = lum.luminosity()

url = "http://192.168.3.212:9999/?lum=%s&hum=%s&temp=%s&press=%s" % \

(lum, hum, temp, press)

#

import urequests

urequests.get(url)

ESP8266启动调用定时器,代码main.py。

from machine import Timer

tim = Timer(1)

def func(t):

import timetask

timetask.timegy30.run()

tim.init(period=3000, mode=Timer.PERIODIC, callback=func)

raspberry在日志上跑了8个多小时,收集了9000多次,应该是因为中间断了一个小。

电量使用43mha,每小时是5.3MHA。

充电宝3.7v 1800MHA电池大约是10元。按照3秒的速度连接到这个板上,不能用15天。如果5分钟发一次,能不能用150天?我不确定电池的这部分,我也没有稳压板。我的测试似乎比在线测试低。

如果后续优化,应该有三个部分。

1)cpu按计算量将频率降至最低。

2)关闭无用端口。

3)优化代码,设计睡眠模式。

ESP8266看门狗与自动重启相结合,防止停机。

使用功能mqtt收发,这个芯片在物联网上的。

标签: 55c传感器光敏传感器连线

锐单商城拥有海量元器件数据手册IC替代型号,打造 电子元器件IC百科大全!

锐单商城 - 一站式电子元器件采购平台