前言
用树莓派4b做一个温湿度检测仪,温湿度传感器模块SHT20收集温湿度数据,在OLED SSD1306上显示,同时把数据上传到onenet平台用网页显示。
所需材料:树莓派;SHT20温湿度传感器 ;Oled屏幕 ssd1306/1106;杜邦线;面包板
本文所出现的代码均在我的码云上,地址:temperater程序
[2020.6.11]
一、硬件连接
SHT20和SSD1306和树莓派之间使用I2C总线通信方式,引脚连接如下:
GND---GND
VCC---3.3V
SCL---SCL
SDA---SDA
SHT20:
SSD1306:
树莓派引脚图:
二、树莓派开启I2C功能
1、使能I2C Kernel Module
远程登陆树莓派后,输入sudo raspi-config
后,选择5.Interfacing Options
选择P5 I2C
选择 是
,如下图所示:
2、安装I2C-Tools
sudo apt-get install i2c-tools
sudo apt-get install python-smbus
sudo adduser pi i2c
sudo reboot
安装完成后输入i2cdetect -y 1
,检查各个模块的地址
图中0x40是SHT20地址, 0x30是OLED屏幕的地址
三、本地温度显示
1、安装luma.oled的包
sudo apt-get install libfreetype6-dev libjpeg-dev build-essentia
sudo apt-get install python3-dev python3-pip
sudo -H pip3 install --upgrade luma.oled
注:目前已经不支持python3以下的版本更新了
2、点亮oled屏幕
运行oled2.py(python3运行)python3 oled2.py
# oled2.py
from luma.core.interface.serial import i2c, spi
from luma.core.render import canvas
from luma.oled.device import ssd1306, ssd1325, ssd1331, sh1106
import time
import socket
import fcntl
import struct
serial = i2c(port=1, address=0x3C)
oled = ssd1306(serial)
def getIP(ifname):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
return socket.inet_ntoa(fcntl.ioctl(
s.fileno(),
0x8915, # SIOCGIFADDR
struct.pack('256s', ifname[:15].encode('utf-8'))
)[20:24])
def oledIP():
with canvas(oled) as draw:
draw.text((2, 5), "IP: " + getIP("wlan0") + '
' + "I'm working
but no people", fill=255)
def oledinit():
serial = i2c(port=1, address=0x3C)
oled = ssd1306(serial)
def oleddisplay(In, x, y):
with canvas(oled) as draw:
# draw.rectangle(device.bounding_box, outline="white", fill="black")
draw.text((x, y), In, fill="white")
'''
if __name__ == "__main__":
oledinit()
# oledIP()
In = "hello "
oleddisplay(In, 15, 20)
'''
oledinit()
while (1):
oledIP()
#In = "hello "
oleddisplay(In, 15, 20)
time.sleep(3)
显示效果:
3、本地温湿度检测
运行temp_local.py(python2.7运行)python temp_local
代码如下:
# temp_local.py
import smbus
import time
F8X16=
(
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x33,0x30,0x00,0x00,0x00,
0x00,0x10,0x0C,0x06,0x10,0x0C,0x06,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x40,0xC0,0x78,0x40,0xC0,0x78,0x40,0x00,0x04,0x3F,0x04,0x04,0x3F,0x04,0x04,0x00,
0x00,0x70,0x88,0xFC,0x08,0x30,0x00,0x00,0x00,0x18,0x20,0xFF,0x21,0x1E,0x00,0x00,
0xF0,0x08,0xF0,0x00,0xE0,0x18,0x00,0x00,0x00,0x21,0x1C,0x03,0x1E,0x21,0x1E,0x00,
0x00,0xF0,0x08,0x88,0x70,0x00,0x00,0x00,0x1E,0x21,0x23,0x24,0x19,0x27,0x21,0x10,
0x10,0x16,0x0E,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0xE0,0x18,0x04,0x02,0x00,0x00,0x00,0x00,0x07,0x18,0x20,0x40,0x00,
0x00,0x02,0x04,0x18,0xE0,0x00,0x00,0x00,0x00,0x40,0x20,0x18,0x07,0x00,0x00,0x00,
0x40,0x40,0x80,0xF0,0x80,0x40,0x40,0x00,0x02,0x02,0x01,0x0F,0x01,0x02,0x02,0x00,
0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x1F,0x01,0x01,0x01,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xB0,0x70,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x80,0x60,0x18,0x04,0x00,0x60,0x18,0x06,0x01,0x00,0x00,0x00,
0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x0F,0x10,0x20,0x20,0x10,0x0F,0x00,
0x00,0x10,0x10,0xF8,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,
0x00,0x70,0x08,0x08,0x08,0x88,0x70,0x00,0x00,0x30,0x28,0x24,0x22,0x21,0x30,0x00,
0x00,0x30,0x08,0x88,0x88,0x48,0x30,0x00,0x00,0x18,0x20,0x20,0x20,0x11,0x0E,0x00,
0x00,0x00,0xC0,0x20,0x10,0xF8,0x00,0x00,0x00,0x07,0x04,0x24,0x24,0x3F,0x24,0x00,
0x00,0xF8,0x08,0x88,0x88,0x08,0x08,0x00,0x00,0x19,0x21,0x20,0x20,0x11,0x0E,0x00,
0x00,0xE0,0x10,0x88,0x88,0x18,0x00,0x00,0x00,0x0F,0x11,0x20,0x20,0x11,0x0E,0x00,
0x00,0x38,0x08,0x08,0xC8,0x38,0x08,0x00,0x00,0x00,0x00,0x3F,0x00,0x00,0x00,0x00,
0x00,0x70,0x88,0x08,0x08,0x88,0x70,0x00,0x00,0x1C,0x22,0x21,0x21,0x22,0x1C,0x00,
0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x00,0x31,0x22,0x22,0x11,0x0F,0x00,
0x00,0x00,0x00,0xC0,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,
0x00,0x00,0x00,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x60,0x00,0x00,0x00,0x00,
0x00,0x00,0x80,0x40,0x20,0x10,0x08,0x00,0x00,0x01,0x02,0x04,0x08,0x10,0x20,0x00,
0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x00,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x00,
0x00,0x08,0x10,0x20,0x40,0x80,0x00,0x00,0x00,0x20,0x10,0x08,0x04,0x02,0x01,0x00,
0x00,0x70,0x48,0x08,0x08,0x08,0xF0,0x00,0x00,0x00,0x00,0x30,0x36,0x01,0x00,0x00,
0xC0,0x30,0xC8,0x28,0xE8,0x10,0xE0,0x00,0x07,0x18,0x27,0x24,0x23,0x14,0x0B,0x00,
0x00,0x00,0xC0,0x38,0xE0,0x00,0x00,0x00,0x20,0x3C,0x23,0x02,0x02,0x27,0x38,0x20,
0x08,0xF8,0x88,0x88,0x88,0x70,0x00,0x00,0x20,0x3F,0x20,0x20,0x20,0x11,0x0E,0x00,
0xC0,0x30,0x08,0x08,0x08,0x08,0x38,0x00,0x07,0x18,0x20,0x20,0x20,0x10,0x08,0x00,
0x08,0xF8,0x08,0x08,0x08,0x10,0xE0,0x00,0x20,0x3F,0x20,0x20,0x20,0x10,0x0F,0x00,
0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,0x20,0x3F,0x20,0x20,0x23,0x20,0x18,0x00,
0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,0x20,0x3F,0x20,0x00,0x03,0x00,0x00,0x00,
0xC0,0x30,0x08,0x08,0x08,0x38,0x00,0x00,0x07,0x18,0x20,0x20,0x22,0x1E,0x02,0x00,
0x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,0x20,0x3F,0x21,0x01,0x01,0x21,0x3F,0x20,
0x00,0x08,0x08,0xF8,0x08,0x08,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,
0x00,0x00,0x08,0x08,0xF8,0x08,0x08,0x00,0xC0,0x80,0x80,0x80,0x7F,0x00,0x00,0x00,
0x08,0xF8,0x88,0xC0,0x28,0x18,0x08,0x00,0x20,0x3F,0x20,0x01,0x26,0x38,0x20,0x00,
0x08,0xF8,0x08,0x00,0x00,0x00,0x00,0x00,0x20,0x3F,0x20,0x20,0x20,0x20,0x30,0x00,
0x08,0xF8,0xF8,0x00,0xF8,0xF8,0x08,0x00,0x20,0x3F,0x00,0x3F,0x00,0x3F,0x20,0x00,
0x08,0xF8,0x30,0xC0,0x00,0x08,0xF8,0x08,0x20,0x3F,0x20,0x00,0x07,0x18,0x3F,0x00,
0xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,0x0F,0x10,0x20,0x20,0x20,0x10,0x0F,0x00,
0x08,0xF8,0x08,0x08,0x08,0x08,0xF0,0x00,0x20,0x3F,0x21,0x01,0x01,0x01,0x00,0x00,
0xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,0x0F,0x18,0x24,0x24,0x38,0x50,0x4F,0x00,
0x08,0xF8,0x88,0x88,0x88,0x88,0x70,0x00,0x20,0x3F,0x20,0x00,0x03,0x0C,0x30,0x20,
0x00,0x70,0x88,0x08,0x08,0x08,0x38,0x00,0x00,0x38,0x20,0x21,0x21,0x22,0x1C,0x00,
0x18,0x08,0x08,0xF8,0x08,0x08,0x18,0x00,0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00,
0x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00,
0x08,0x78,0x88,0x00,0x00,0xC8,0x38,0x08,0x00,0x00,0x07,0x38,0x0E,0x01,0x00,0x00,
0xF8,0x08,0x00,0xF8,0x00,0x08,0xF8,0x00,0x03,0x3C,0x07,0x00,0x07,0x3C,0x03,0x00,
0x08,0x18,0x68,0x80,0x80,0x68,0x18,0x08,0x20,0x30,0x2C,0x03,0x03,0x2C,0x30,0x20,
0x08,0x38,0xC8,0x00,0xC8,0x38,0x08,0x00,0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00,
0x10,0x08,0x08,0x08,0xC8,0x38,0x08,0x00,0x20,0x38,0x26,0x21,0x20,0x20,0x18,0x00,
0x00,0x00,0x00,0xFE,0x02,0x02,0x02,0x00,0x00,0x00,0x00,0x7F,0x40,0x40,0x40,0x00,
0x00,0x0C,0x30,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x06,0x38,0xC0,0x00,
0x00,0x02,0x02,0x02,0xFE,0x00,0x00,0x00,0x00,0x40,0x40,0x40,0x7F,0x00,0x00,0x00,
0x00,0x00,0x04,0x02,0x02,0x02,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,
0x00,0x02,0x02,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x19,0x24,0x22,0x22,0x22,0x3F,0x20,
0x08,0xF8,0x00,0x80,0x80,0x00,0x00,0x00,0x00,0x3F,0x11,0x20,0x20,0x11,0x0E,0x00,
0x00,0x00,0x00,0x80,0x80,0x80,0x00,0x00,0x00,0x0E,0x11,0x20,0x20,0x20,0x11,0x00,
0x00,0x00,0x00,0x80,0x80,0x88,0xF8,0x00,0x00,0x0E,0x11,0x20,0x20,0x10,0x3F,0x20,
0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x1F,0x22,0x22,0x22,0x22,0x13,0x00,
0x00,0x80,0x80,0xF0,0x88,0x88,0x88,0x18,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,
0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x6B,0x94,0x94,0x94,0x93,0x60,0x00,
0x08,0xF8,0x00,0x80,0x80,0x80,0x00,0x00,0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20,
0x00,0x80,0x98,0x98,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,
0x00,0x00,0x00,0x80,0x98,0x98,0x00,0x00,0x00,0xC0,0x80,0x80,0x80,0x7F,0x00,0x00,
0x08,0xF8,0x00,0x00,0x80,0x80,0x80,0x00,0x20,0x3F,0x24,0x02,0x2D,0x30,0x20,0x00,
0x00,0x08,0x08,0xF8,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,
0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x00,0x20,0x3F,0x20,0x00,0x3F,0x20,0x00,0x3F,
0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x00,0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20,
0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00,
0x80,0x80,0x00,0x80,0x80,0x00,0x00,0x00,0x80,0xFF,0xA1,0x20,0x20,0x11,0x0E,0x00,
0x00,0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x0E,0x11,0x20,0x20,0xA0,0xFF,0x80,
0x80,0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x20,0x20,0x3F,0x21,0x20,0x00,0x01,0x00,
0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x33,0x24,0x24,0x24,0x24,0x19,0x00,
0x00,0x80,0x80,0xE0,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x1F,0x20,0x20,0x00,0x00,
0x80,0x80,0x00,0x00,0x00,0x80,0x80,0x00,0x00,0x1F,0x20,0x20,0x20,0x10,0x3F,0x20,
0x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,0x00,0x01,0x0E,0x30,0x08,0x06,0x01,0x00,
0x80,0x80,0x00,0x80,0x00,0x80,0x80,0x80,0x0F,0x30,0x0C,0x03,0x0C,0x30,0x0F,0x00,
0x00,0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x00,0x20,0x31,0x2E,0x0E,0x31,0x20,0x00,
0x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,0x80,0x81,0x8E,0x70,0x18,0x06,0x01,0x00,
0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x21,0x30,0x2C,0x22,0x21,0x30,0x00,
0x00,0x00,0x00,0x00,0x80,0x7C,0x02,0x02,0x00,0x00,0x00,0x00,0x00,0x3F,0x40,0x40,
0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,
0x00,0x02,0x02,0x7C,0x80,0x00,0x00,0x00,0x00,0x40,0x40,0x3F,0x00,0x00,0x00,0x00,
0x00,0x06,0x01,0x01,0x02,0x02,0x04,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
)
F_SIGN = (
0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x0F,0x10,0x20,0x20,0x10,0x0F,0x00,
0x06,0x09,0x09,0xE6,0xF8,0x0C,0x04,0x02,0x02,0x02,0x02,0x02,0x04,0x1E,0x00,0x00,
0x00,0x00,0x00,0x07,0x1F,0x30,0x20,0x40,0x40,0x40,0x40,0x40,0x20,0x10,0x00,0x00
)
Hzk =
[
[0x06,0x09,0x09,0xE6,0xF8,0x0C,0x04,0x02,0x02,0x02,0x02,0x02,0x04,0x1E,0x00,0x00],
[0x00,0x00,0x00,0x07,0x1F,0x30,0x20,0x40,0x40,0x40,0x40,0x40,0x20,0x10,0x00,0x00],
[0x10,0x60,0x02,0x8C,0x00,0x00,0xFE,0x92,0x92,0x92,0x92,0x92,0xFE,0x00,0x00,0x00],
[0x04,0x04,0x7E,0x01,0x40,0x7E,0x42,0x42,0x7E,0x42,0x7E,0x42,0x42,0x7E,0x40,0x00],
[0x00,0x00,0xFC,0x24,0x24,0x24,0xFC,0x25,0x26,0x24,0xFC,0x24,0x24,0x24,0x04,0x00],
[0x40,0x30,0x8F,0x80,0x84,0x4C,0x55,0x25,0x25,0x25,0x55,0x4C,0x80,0x80,0x80,0x00],
[0x10,0x60,0x02,0x8C,0x00,0xFE,0x92,0x92,0x92,0x92,0x92,0x92,0xFE,0x00,0x00,0x00],
[0x04,0x04,0x7E,0x01,0x44,0x48,0x50,0x7F,0x40,0x40,0x7F,0x50,0x48,0x44,0x40,0x00],
[0x00,0x00,0xFC,0x24,0x24,0x24,0xFC,0x25,0x26,0x24,0xFC,0x24,0x24,0x24,0x04,0x00],
[0x40,0x30,0x8F,0x80,0x84,0x4C,0x55,0x25,0x25,0x25,0x55,0x4C,0x80,0x80,0x80,0x00],
[0x00,0x00,0x7C,0x82,0x82,0x7C,0x00,0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x00,0x00],
[0x00,0x20,0x10,0x08,0x04,0x02,0x01,0x00,0x3E,0x41,0x41,0x3E,0x00,0x00,0x00,0x00],
]
eSHT2xOled_Addressess = 0x40
eTempHoldCmd= 0xE3
eRHumidityHoldCmd= 0xE5
eTempNoHoldCmd= 0xF3
eRHumidityNoHoldCmd= 0xF5
writeUserRegister= 0xE6
readUserRegister= 0xE7
softReset= 0xFE
i2c = smbus.SMBus(1)
Oled_Address = 0x3C
def Clear_Oled():
for i in range(8):
i2c.write_byte_data(Oled_Address , 0x00, 0xB0 + i)
i2c.write_byte_data(Oled_Address , 0x00, 0x02)
i2c.write_byte_data(Oled_Address , 0x00, 0x10)
for k in range(128):
i2c.write_byte_data(Oled_Address , 0x40, 0x00)
def On_Oled():
for i in range(8):
i2c.write_byte_data(Oled_Address , 0x00, 0xB0 + i)
i2c.write_byte_data(Oled_Address , 0x00, 0x02)
i2c.write_byte_data(Oled_Address , 0x00, 0x10)
for k in range(128):
i2c.write_byte_data(Oled_Address , 0x40, 0xFF)
def OLED_Set_Pos(x, y):
i2c.write_byte_data(Oled_Address, 0x00, 0xb0 + y)
i2c.write_byte_data(Oled_Address, 0x00, ((x & 0xf0) >> 4) | 0x10)
i2c.write_byte_data(Oled_Address, 0x00, (x & 0x0f) | 0x01)
def OLED_ShowChar(x, y, chr):
c = chr - 0x20
if (x > 128 - 1) :
x = 0
y = y + 2
OLED_Set_Pos(x, y)
for i in range(8):
i2c.write_byte_data(Oled_Address, 0x40, F8X16[c * 16 + i])
OLED_Set_Pos(x, y + 1)
for i in range(8):
i2c.write_byte_data(Oled_Address, 0x40, F8X16[c * 16 + i + 8])
def OLED_ShowSign(x, y, c):
if (x > 128 - 1) :
x = 0
y = y + 2
OLED_Set_Pos(x, y)
for i in range(8):
i2c.write_byte_data(Oled_Address, 0x40, F_SIGN[c * 16 + i])
OLED_Set_Pos(x, y + 1)
for i in range(8):
i2c.write_byte_data(Oled_Address, 0x40, F_SIGN[c * 16 + i + 8])
def OLED_ShowString(x, y, chr):
for j in range(len(chr)):
OLED_ShowChar(x, y, ord(chr[j]))
x += 8
if (x > 120):
x = 0
y += 2
def OLED_ShowCHinese(x, y, no):
OLED_Set_Pos(x, y)
for t in range(16):
i2c.write_byte_data(Oled_Address, 0x40, Hzk[2 * no][t])
OLED_Set_Pos(x, y + 1)
for t in range(16):
i2c.write_byte_data(Oled_Address, 0x40, Hzk[2 * no + 1][t])
def readSensor(command):
i2c.write_quick(eSHT2xOled_Addressess)
i2c.write_byte(eSHT2xOled_Addressess,command)
time.sleep(0.1)
result =(i2c.read_byte(eSHT2xOled_Addressess)<<8)
result += i2c.read_byte(eSHT2xOled_Addressess)
result &= ~0x0003
#print result
return result
def getRHumidity():
return (-6.0 + 125.0 / 65536.0 * float(readSensor(eRHumidityNoHoldCmd)))
def getTemperature():
return (-46.85 + 175.72 / 65536.0 * float(readSensor(eTempNoHoldCmd)))
i2c.write_byte_data(Oled_Address,0x00,0xAE)
i2c.write_byte_data(Oled_Address,0x00,0x02)
i2c.write_byte_data(Oled_Address,0x00,0x10)
i2c.write_byte_data(Oled_Address,0x00,0x40)
i2c.write_byte_data(Oled_Address,0x00,0xB0)
i2c.write_byte_data(Oled_Address,0x00,0x81)
i2c.write_byte_data(Oled_Address,0x00,0xFF)
i2c.write_byte_data(Oled_Address,0x00,0xA1)
i2c.write_byte_data(Oled_Address,0x00,0xA6)
i2c.write_byte_data(Oled_Address,0x00,0xA8)
i2c.write_byte_data(Oled_Address,0x00,0x3F)
i2c.write_byte_data(Oled_Address,0x00,0xAD)
i2c.write_byte_data(Oled_Address,0x00,0x8B)
i2c.write_byte_data(Oled_Address,0x00,0x33)
i2c.write_byte_data(Oled_Address,0x00,0xC8)
i2c.write_byte_data(Oled_Address,0x00,0xD3)
i2c.write_byte_data(Oled_Address,0x00,0x00)
i2c.write_byte_data(Oled_Address,0x00,0xD5)
i2c.write_byte_data(Oled_Address,0x00,0x80)
i2c.write_byte_data(Oled_Address,0x00,0xD8)
i2c.write_byte_data(Oled_Address,0x00,0x05)
i2c.write_byte_data(Oled_Address,0x00,0xD9)
i2c.write_byte_data(Oled_Address,0x00,0x1F)
i2c.write_byte_data(Oled_Address,0x00,0xDB)
i2c.write_byte_data(Oled_Address,0x00,0x40)
i2c.write_byte_data(Oled_Address,0x00,0xAF)
Clear_Oled()
OLED_ShowCHinese(5,1,1)
OLED_ShowCHinese(22,1,2)
OLED_ShowString(40,1,":")
OLED_ShowCHinese(110,1,0)
OLED_ShowCHinese(5,4,3)
OLED_ShowCHinese(22,4,4)
OLED_ShowString(40,4,":")
OLED_ShowCHinese(110,4,5)
print "OLED Init"
i2c.write_byte(0x40, 0xFE)
print "SHT20 Init"
time.sleep(0.800)
while (1):
H=getRHumidity()
T=getTemperature()
OLED_ShowString(60,1,str(round(T, 2)))
OLED_ShowString(60,4,str(round(H, 2)))
print "Temperature=%0.2f"%(T)
print "Humidity=%0.2f"%(H)
time.sleep(3)
运行效果:
四、ONENET云平台显示温湿度数据
1、设置onenet云平台
1.1 创建HTTP协议产品
产品信息自己填,技术参数如下:
1.2 创建设备
填写你的设备信息(随意填),网站自动生成相关API地址等信息。
1.3 添加数据流
数据流的名称 和 程序中的数据名称要一致
我这里添加了 CPU温度、温度、湿度三个数据流
1.4 添加应用
在显示模块中选择相应数据流
2、运行ONENET示例程序
运行示例程序,将树莓派CPU温度上传到onenet平台显示。
注意:需要将APIKEY、url的设备号 这两处代码修改成你自己的设备的相关编号。
python onenet.py
# -*- coding:utf-8 -*-
# File: onenet.py
# 向平台已经创建的数据流发送数据点
import urllib2
import json
import time
import datetime
APIKEY = 'xxxxxxx=' # 改成你的APIKEY
def get_temp():
# 打开文件
file = open("/sys/class/thermal/thermal_zone0/temp")
# 读取结果,并转换为浮点数
temp = float(file.read()) / 1000
# 关闭文件
file.close()
# 向控制台打印结果
print("CPU tempurature: %.3f" % temp)
# 返回温度值
return temp
def http_put():
temperature = get_temp() # 获取CPU温度并上传
CurTime = datetime.datetime.now()
# 这url只需把数字部分 xxxxxxx 换成你在onenet中创建的设备号就行
url = 'http://api.heclouds.com/devices/xxxxxxxx/datapoints'
# onenet平台上显示的值
values = {
'datastreams': [{"id": "temperature", "datapoints": [{"time": CurTime.isoformat(), "value": temperature}]}]}
print ("Cur_time:%s" % CurTime.isoformat())
print ("tempure:%.3f" % temperature)
jdata = json.dumps(values) # 对数据进行JSON格式化编码
# 打印json内容
print jdata
request = urllib2.Request(url, jdata)
request.add_header('api-key', APIKEY)
request.get_method = lambda: 'POST' # 设置HTTP的访问方式
request = urllib2.urlopen(request)
return request.read()
while True:
time.sleep(5)
resp = http_put()
print ("OneNET_result: %s" % resp)
# time.sleep(5)
3、将本地温湿度数据同步至ONENET平台
将 onenet.py 的代码移植到temp_local.py中,得到程序:temp_onenet.py
python temp_onenet.py
代码在temperater程序中,需要的自行下载,同样也需要将APIKEY、url的设备号 这两处代码修改成你自己的设备的相关编号。
显示效果: