Micro station météo avec un ESP 8266

Le matériel utilisé

Capteur barométrique, température et humidité : BME280
Module d'affichage : module IIC OLED LCD 128X64
Mini carte de développement : ESP8266 WiFi

Câblage

 

Code micro python

# Affichage OLED SSD1306 en MicroPython | MicroPython SSD1306  OLED display
# affiche les mesures d'un capteur BME280 i2c sur un ecran OLED
# Display values from an i2c BME280 sensor on OLED screen

from machine import Pin, I2C
import time, ssd1306, bme280, PostHTTP, ConnectWifi, ujson, urequests

pinScl       = 5  #ESP8266 GPIO5 (D1)
pinSda       = 4  #ESP8266 GPIO4 (D2)
addrOled     = 60  #0x3c
addrBME280   = 118 #0x76
hSize        = 64  # Hauteur ecran en pixels | display heigh in pixels
wSize        = 128 # Largeur ecran en pixels | display width in pixels

dustDensity  = 5

oledIsConnected = False
bmeIsConnected  = False
pa = 0
hum = 0
hum = 0
apiKey = 'apiKey'
url = 'https://api.ms-only.net/meteo/'

# init ic2 object
i2c = I2C(scl=Pin(pinScl), sda=Pin(pinSda)) #ESP8266 5/4

# Scan le bus i2c et verifie si le BME280 et l'ecran OLED sont connectes
# Scan i2c bus and check if BME2 and OLDE display are connected
#print('Scan i2c bus...')
devices = i2c.scan()
if len(devices) == 0:
  print("No i2c device !")
else:
  print('i2c devices found:', len(devices))
  for device in devices: 
    if device == addrOled:
      oledIsConnected = True
    if device == addrBME280:
      bmeIsConnected = True  
    print(device)

#print("Tentative de connexion")    
ConnectWifi.CONNECTWIFI.connect()
    
while True:  

  if bmeIsConnected:
    bme = bme280.BME280(i2c=i2c,address=addrBME280)
    print("BME280 values:")
    print(bme.values)
    temp,pa,hum = bme.values 
    print(temp)
    print(pa) 
    print(hum)
    
  if oledIsConnected:
    oled = ssd1306.SSD1306_I2C(wSize, hSize, i2c, addrOled)
    oled.fill(0)
    if bmeIsConnected:     
      oled.text("Temp. "+temp, 0, 0)
      oled.text("PA "+pa, 0, 17)
      oled.text("Hum. "+hum, 0, 34)       
      oled.show()
    else:   
      oled.text("BME KO", 0, 0)
      oled.show()
  else:
    print('! No i2c display')
   
  if bmeIsConnected:      
   
    headers = {'Content-Type': 'application/json', 'charset': 'utf-8'}
    
    dict = PostHTTP.POSTHTTP.send(apiKey, temp, pa, hum, dustDensity)
    #print(dict)
    response = urequests.post(url , data = dict, headers=headers)
    print(response.text)
    time.sleep_ms(30000)
class CONNECTWIFI:  
    def connect():
        import network
        
        ip        = '192.168.0.27'
        subnet    = '255.255.255.0'
        gateway   = '192.168.0.1'
        dns       = '1.1.1.1'
        ssid      = "SSID"
        password  = "passwd"
     
        station = network.WLAN(network.STA_IF)
     
        if station.isconnected() == True:
            print("Already connected")
            return
     
        station.active(True)
        station.ifconfig((ip,subnet,gateway,dns))
        station.connect(ssid, password)
     
        while station.isconnected() == False:
            pass
     
        print("Connection successful")
        print(station.ifconfig())

    def disconnect():
        import network
        station = network.WLAN(network.STA_IF)
        station.disconnect()
        station.active(False)
class POSTHTTP:  
    def send(apiKey, temp, pa, hum, dust):
      dict = '['
      dict = dict + '{"apikey":"' + apiKey + '"},'
      dict = dict + '{"location": [48.772938, 2.072463]},'
      dict = dict + '{"sensors":' 
      dict = dict + '['
      dict = dict + '{"device":"temperature",'
      dict = dict + '"unity":"c",'
      dict = dict + '"values":"' + temp + '"},'
      dict = dict + '{"device":"pressure",'
      dict = dict + '"unity":"hpa",'
      dict = dict + '"values":"' + pa + '"},'
      dict = dict + '{"device":"hygrometry",' 
      dict = dict + '"unity":"%",'  
      dict = dict + '"values":"' + hum + '"}'
      dict = dict + ']'
      dict = dict + '}'      
      dict = dict + '] '
      return dict
Code sources ESP8266MOD MicroPython
jensepas
Publié le : 11/08/2020
Mise à jour : 03/08/2022