Raspberry school learning notes - temperature and humidity detection (local OLED display, ONENET cloud platform display)

Article catalog

preface

The temperature and humidity sensor module SHT20 collects the temperature and humidity data and displays it on OLED SSD1306. At the same time, it uploads the data to onenet platform and displays it on webpage.

Materials required: Raspberry pie; SHT20 temperature and humidity sensor; old screen ssd1306/1106; DuPont line; bread plate

The codes in this article are all on my code cloud, address: Temperature program

[2020.6.11]

1, Hardware connection

I2C bus communication mode is used between SHT20 and SSD1306 and raspberry pie, and pin connection is as follows:

GND---GND
VCC---3.3V
SCL---SCL
SDA---SDA

SHT20:

SSD1306:

Pinouts of raspberry Pie:

2, Raspberry pie enable I2C function

Reference 1: SHT20 temperature and humidity collection

The application of OLED with reference to 2:I2C interface in raspberry pie

1. Enable I2C Kernel Module

After logging in raspberry pie remotely, input sudo raspi config, select 5. Interface options, select P5 I2C, and select Yes, as shown in the following figure:

2. Install I2C tools

sudo apt-get install i2c-tools
sudo apt-get install python-smbus
sudo adduser pi i2c
sudo reboot

After installation, input i2cdetect-y 1 and check the address of each module

In the figure, 0x40 is the address of SHT20, and 0x30 is the address of OLED screen

3, Local temperature display

1. Installation luma.oled 's bag

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

Note: the version below python3 is not supported at present

2. Light up the oled screen

Run oled2.py (python3 run) 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)

Display effect:

3. Local temperature and humidity detection

Run temp_local.py (python2.7 running) python temp_local

The code is as follows:

# 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)


Operation effect:

4, ONENET cloud platform displays temperature and humidity data

Reference 3: real time temperature and humidity detection tutorial

Reference 4: Raspberry pie transfers data to OneNET (HTTP format)

1. Setting up onenet cloud platform

1.1 create HTTP protocol products

Fill in the product information by yourself, and the technical parameters are as follows:

1.2 create device

[external link picture transfer failed. The source station may have anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-ho368rno-1591860948205) (raspberry school learning notes (4) -. Assets / image-2020061151021054. PNG))

Fill in your device information (fill in at will), the website automatically generates relevant API address and other information.

1.3 add data flow

The name of the data flow should be consistent with the name of the data in the program

I added three data streams of CPU temperature, temperature and humidity here

1.4 add application

Select the corresponding data flow in the display module

2. Run the ONENET sample program

Run the sample program to upload the CPU temperature of raspberry pie to onenet platform for display.

Note: you need to change the code of APIKEY and url to the relevant number of your own device.

python onenet.py

# -*- coding:utf-8 -*-
# File: onenet.py
# Send data points to the data flow that the platform has created
import urllib2
import json
import time
import datetime

APIKEY = 'xxxxxxx='  # Change to your APIKEY


def get_temp():
    # Open file
    file = open("/sys/class/thermal/thermal_zone0/temp")
    # Read the result and convert it to a floating-point number
    temp = float(file.read()) / 1000
    # Close file
    file.close()
    # Print results to console
    print("CPU tempurature: %.3f" % temp)
    # Return temperature value
    return temp


def http_put():
    temperature = get_temp()  # Get CPU temperature and upload
    CurTime = datetime.datetime.now()

    # This url only needs to change the digital part XXXXXX to the device number you created in onenet
    url = 'http://api.heclouds.com/devices/xxxxxxxx/datapoints'

    #  Values displayed on onenet platform
    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 format and code the data
    # Print json content
    print jdata
    request = urllib2.Request(url, jdata)
    request.add_header('api-key', APIKEY)
    request.get_method = lambda: 'POST'  # Set the access method of 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. Synchronize local temperature and humidity data to ONENET platform

Will onenet.py Code migration to temp_local.py Get the program: temp_onenet.py

python temp_onenet.py

Code in Temperature program In, you need to download by yourself. You also need to change the two codes of APIKEY and url to the relevant number of your own device.

Display effect:

Keywords: sudo socket Python JSON

Added by matts12290 on Thu, 11 Jun 2020 10:48:32 +0300