树莓派学习笔记(四)——温湿度检测(本地OLED显示、ONENET云平台显示)

前言

用树莓派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:

img

树莓派引脚图:

rpi-pins-40-0


二、树莓派开启I2C功能

参考1:SHT20温湿度采集

参考2:I2C接口的OLED在树莓派上的应用

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") + '\n' + "I'm working\nbut 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)

显示效果:

img

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,
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值