#-----------------------------------------------------------------------------
# ATMOSPHERIC PRESSURE AND TEMPERATURE ON THE CLOUD
# =================================================
#
# The ambient temperature and pressure sensor BMP280 is connected to Pico.
# The project reads the temperature and atmospheric pressure and sends
# to the cloud where it can be accessed from anywhere. In addition, change
# of the temperature and the pressure can be plotted in the cloud.
#
#
# The program uses the Thingspeak cloud service
#
# Author: Dogan Ibrahim
# File : Cloud.py
# Date : January, 2021
#------------------------------------------------------------------------------
import network
import socket
import utime
from machine import Pin,I2C
from bmp280 import *
bus = I2C(0,scl=Pin(1),sda=Pin(0),freq=200000)
bmp = BMP280(bus)
bmp.use_case(BMP280_CASE_INDOOR)
APIKEY = "BUHXZZC718WT0X0P" # Thingspeak API key
host = "api.thingspeak.com" # Thigspeak host
#
# This function attempts to connect to Wi-Fi
#
def connect():
global wlan
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect("BTHomeSpot-XNH", "49345we34g")
while wlan.isconnected() == False:
print("Waiting to be connected")
utime.sleep(1)
#
# Send data to Thingspeak. This function sends the temperature and
# humidity data to the cloud every 30 seconds
#
connect()
while True:
connect()
sock = socket.socket()
addr = socket.getaddrinfo("api.thingspeak.com",80)[0][-1]
sock.connect(addr)
pressure=bmp.pressure
p=pressure/133.3224 # Pressure in mmH
t=bmp.temperature # Temperature in C
path = "api_key="+APIKEY+"&field1="+str(p)+"&field2="+str(t)
sock.send(bytes("GET /update?%s HTTP/1.0\r\nHost: %s\r\n\r\n"
%(path,host),"utf8"))
utime.sleep(5)
sock.close()
wlan.disconnect()
utime.sleep(25)