#prompt
#You are required to build a IoT based food quality monitoring solution by sensing temperature and humidity.
#Assume that you have been provided the following hardware:
#ESP32 Microcontroller
#DHT22 Digital Temperature and Humidity sensor
#Analog Temperature Sensor: NTC (Negative Temperature Coefficient) thermistor
#Standard 5mm LED
#Tasks to be completed in this exam:
#You are required perform the following tasks to simulate the proposed food quality monitoring solution
#using WOKWI and ThingSpeak simulation platforms. You will use microPython programming on ESP32.
#Tasks 1: [25%] Simulate the system on WOKWI and display the temperature and humidity values on the console
# after obtaining readings from DHT22 sensor that is connected to ESP32
#Task 2: [25%] Connect WiFi network to the ESP32, and send the temperature and humidity over WiFI to an external
# MQTT server (ThingSpeak).
#Task 3: [20%] Display and visualize the latest sensor data received from the DHT22 sensor on a ThingSpeak Channel.
# [Provide all supportive screenshots]
#Task 4: {10%] Connect a NTC sensor to measure analogue temperatures. Simulate the system on WOKWI and display the analog
# temperature value on the console. You will use the following formula to convert the analog value of NTC
# sensor reading:
# If beta = 3950; (the Beta is the Coefficient of the thermistor)
# and
# analog_value is the reading of NTC sensor
# celsius_value = 1 / (log(1 / (1023. / analog_value - 1)) / beta + 1.0 / 298.15) - 273.15
#Task 5: [10%] Demonstrate in your simulation by changing analog temperature value of the NTC sensor,
# that if sensor detects a value exceeding its set temperature value (20oC), it will be notified by lighting up a LED.
#Task 6: [10%] Display and visualize the NTC sensor data on the ThingSpeak Channel that you have created earlier.
# (You may display it on another field in the same ThingSpeak) [Provide all supportive screenshots]
#internet and sending mqtt messages
from machine import Pin, Timer, ADC
import network # module for network drivers and routing configuration
import ujson # module for Json data
import time # module for current time and date
import sys # Module that provides access to system-specific parameters and functions.
from umqtt.simple import MQTTClient # Built-in library for MQTT Protocol
from dht import DHT22
import math
dht22 = DHT22(Pin(12)) # GPIO Pin 12 (DHT22 object created at Pin 12)
#analog temp senosr out to gpio, grd and 3v3
BETA = 3950
adc = ADC(Pin(32))
adc.atten(ADC.ATTN_11DB)
led = Pin(27, Pin.OUT) # GPIO Pin 27 as an output
def read_ntc_temperature():
analog_value = adc.read()
print(analog_value)
#temperature_celsius = 1 / (math.log(1 / (4096 / analog_value - 1)) / BETA + 1.0 / 298.15) - 273.15
#celsius_value = 1 / (log(1 / (1023. / analog_value - 1)) / beta + 1.0 / 298.15) - 273.15
temperature_celsius = 1 / (math.log(1 / (4096 / analog_value - 1)) / BETA + 1.0 / 298.15) - 273.15
if(temperature_celsius>20):
{led.on()}
else:
{led.off()}
return temperature_celsius
while True:
#collect data
print("measuring temperature and humidity")
dht22.measure()
temperature = read_ntc_temperature()
print("NTC Temperature: {:.2f} ℃".format(temperature))
print("Temperature: {}".format(dht22.temperature()))
print("Humidity: {}".format(dht22.humidity()))
temp = dht22.temperature() # store dht22 temperature into temp variable
hum = dht22.humidity()
NTCTemp = temperature
#ThingSpeak MQTT Server Parameters from DEVICE
mqtt_client_id = "DhQRBRoCHSYfOg4qJQ05Bxg" #DhQRBRoCHSYfOg4qJQ05Bxg
mqtt_user = "DhQRBRoCHSYfOg4qJQ05Bxg" #DhQRBRoCHSYfOg4qJQ05Bxg
mqtt_password = "Q50BMBS742quB2ai3+0Ep+I3"#Q50BMBS742quB2ai3+0Ep+I3
mqtt_server = "mqtt3.thingspeak.com"#has to be included as is
# ThinkSpeak Credentials from CHANNELS
channel_ID = "2496258"#2496258
API_Key = "PST67KBEO00WM2BN"# for write api key#PST67KBEO00WM2BN
# Connect with WiFi, needed to log in
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 to wifi..')
timeout = 0
while (not wifi.isconnected() and timeout < 10):
print(10 - timeout)
timeout = timeout + 1
time.sleep(1)
if(wifi.isconnected()):
print('connected to wifi')
else:
print('not connected to wifi')
sys.exit()
print('network config:', wifi.ifconfig())
connect_wifi() # Connecting to WiFi Router
# Connection to MQTT Server
#create the MQTT client object
client = MQTTClient(client_id = mqtt_client_id, server =mqtt_server, user=mqtt_user, password=mqtt_password)
client.connect()
print("connected to MQTT server")
#create MQTT "Topic" to publish
publish_topic = b"channels/2496258/publish"
#publish_topic = "channels/" + channel_ID + "/publish
#Tasks 1: [25%] Simulate the system on WOKWI and display the temperature and humidity values on the console
# after obtaining readings from DHT22 sensor that is connected to ESP32
#led pin
#led = Pin(27, Pin.OUT) # GPIO Pin 27 as an output
#led.on() # Turn on the LED
#led.off() # Turn off the LED
#temperature and humidity, 1 gpio on sda, 3v3 volts, ground
#from machine import Pin, Timer
dht22 = DHT22(Pin(12)) # GPIO Pin 12 (DHT22 object created at Pin 12)
#analog temp senosr out to gpio, grd and 3v3
BETA = 3950
adc = ADC(Pin(D3))
adc.atten(ADC.ATTN_11DB)
def read_ntc_temperature():
analog_value = adc.read()
print(analog_value)
#temperature_celsius = 1 / (math.log(1 / (4096 / analog_value - 1)) / BETA + 1.0 / 298.15) - 273.15
temperature_celsius = 1 / (math.log(1 / (1023. / analog_value - 1)) / BETA + 1.0 / 298.15) - 273.15
return temperature_celsius
#led = Pin(27, Pin.OUT) # GPIO Pin 27 as an output
while True:
#collect data
print("measuring temperature and humidity")
dht22.measure()
temperature = read_ntc_temperature()
print("NTC Temperature: {:.2f} ℃".format(temperature))
print("Temperature: {}".format(dht22.temperature()))
print("Humidity: {}".format(dht22.humidity()))
temp = dht22.temperature() # store dht22 temperature into temp variable
hum = dht22.humidity()
NTCTemp = temperature
#publishing data
message = "field1=" + str(temp) + "&field2=" + str(hum)
#client.connect()
client.publish(publish_topic, message.encode("utf-8"))
#client.disconnect()
#led.on() # Turn on the LED
#led.off() # Turn off the LED
print(" Msg sent to Thingspeak channel successfully...")
time.sleep(1)
#time.sleep(5)
#Task 4: {10%] Connect a NTC sensor to measure analogue temperatures. Simulate the system on WOKWI and display the analog
# temperature value on the console. You will use the following formula to convert the analog value of NTC
# sensor reading:
# If beta = 3950; (the Beta is the Coefficient of the thermistor)
# and
# analog_value is the reading of NTC sensor
# celsius_value = 1 / (log(1 / (1023. / analog_value - 1)) / beta + 1.0 / 298.15) - 273.15