from machine import Pin, Timer
from machine import Pin, time_pulse_us #ultra*
import network
import urequests # for http API
import time
#from umqtt.robust import MQTTClient # for MQTT Broker
import sys
#import dht
from dht import DHT22
from machine import ADC, Pin
import time
dht22 = DHT22(Pin(13))
# Function to read DHT
def readDht():
dht22.measure()
return dht22.temperature(), dht22.humidity()
# test DHT function
print (readDht())
SOUND_SPEED=340 # Vitesse du son dans l'air
TRIG_PULSE_DURATION_US=10
trig_pin = Pin(12, Pin.OUT) #ultra*
echo_pin = Pin(14, Pin.IN) #ultra*
# Split Sensor reading..
#temp, hum = readDht()
# Connect with WiFi
WIFI_SSID = "Wokwi-GUEST"
WIFI_PASSWORD = ""
# Function to connect to local WiFi
def connect_wifi():
wifi = network.WLAN(network.STA_IF)
wifi.active(True)
wifi.disconnect()
wifi.connect(WIFI_SSID,WIFI_PASSWORD)
if not wifi.isconnected():
print('connecting..')
timeout = 0
while (not wifi.isconnected() and timeout < 10):
print(10 - timeout)
timeout = timeout + 1
time.sleep(1)
if(wifi.isconnected()):
print('connected')
else:
print('not connected')
sys.exit()
print('network config:', wifi.ifconfig())
connect_wifi() # Connecting to WiFi Router
# Thingspeak HTTP API Protocol (Connection)
HTTP_HEADERS = {'Content-Type': 'application/json'}
THINGSPEAK_WRITE_API_KEY = 'RJDAH9TUX2BOSD44' # Write API of the Channel
class LDR:
"""This class read a value from a light dependent resistor (LDR)"""
def __init__(self, pin, min_value=0, max_value=100):
"""
Initializes a new instance.
:parameter pin A pin that's connected to an LDR.
:parameter min_value A min value that can be returned by value() method.
:parameter max_value A max value that can be returned by value() method.
"""
if min_value >= max_value:
raise Exception('Min value is greater or equal to max value')
# initialize ADC (analog to digital conversion)
self.adc = ADC(Pin(pin))
# set 11dB input attenuation (voltage range roughly 0.0v - 3.6v)
self.adc.atten(ADC.ATTN_11DB)
self.min_value = min_value
self.max_value = max_value
def read(self):
"""
Read a raw value from the LDR.
:return A value from 0 to 4095.
"""
return self.adc.read()
def value(self):
"""
Read a value from the LDR in the specified range.
:return A value from the specified [min, max] range.
"""
return (self.max_value - self.min_value) * self.read() / 4095
# initialize an LDR
ldr = LDR(34)
while True:
# Prepare le signal
trig_pin.value(0)
time.sleep_us(5)
# Créer une impulsion de 10 µs
trig_pin.value(1)
time.sleep_us(TRIG_PULSE_DURATION_US)
trig_pin.value(0)
ultrason_duration = time_pulse_us(echo_pin, 1, 30000) # Renvoie le temps de propagation de l'onde (en µs)
distance_cm = SOUND_SPEED * ultrason_duration / 20000
print(f"Distance : {distance_cm} cm")
object = distance_cm
time.sleep_ms(500)
#pin = Pin(12, Pin.OUT, Pin.PULL_DOWN) #Rpi
dht22 = DHT22(Pin(13)) #DHT22 object created at Pin 12
dht22.measure()
print("Temperature: {}".format(dht22.temperature()))
print("Humidity: {}".format(dht22.humidity()))
temp = dht22.temperature() # store dht22 temperature into temp variable
hum = dht22.humidity()
# read a value from the LDR
value = 1 / ldr.value()
value = value * 20480
print('Luminous intensity = {} lux'.format(value))
dht_readings = {'field4':hum, 'field2':temp, 'field3':object, 'field5':value}
request = urequests.post( 'http://api.thingspeak.com/update?api_key=' + THINGSPEAK_WRITE_API_KEY, json = dht_readings, headers = HTTP_HEADERS )
request.close()
print(dht_readings)
print(" Msg sent to Thingspeak channel successfully...")
print(" ********************************************")