# try:
# import urequests as requests
# except:
# import requests
# import network
# from machine import Pin
# import dht, time
# #Your network credentials
# ssid = 'Wokwi-GUEST'
# password = ''
# def connect_wifi(ssid, password):
# #Connect to your network
# station = network.WLAN(network.STA_IF)
# station.active(True)
# station.connect(ssid, password)
# while station.isconnected() == False:
# pass
# print('Connection successful')
# print(station.ifconfig())
# def send_message(phone_number, api_key):
# sensor = dht.DHT11(Pin(5))
# sensor.measure()
# temp = sensor.temperature()
# hum = sensor.humidity()
# print('Temperature: %3.1f C' % temp)
# print('Humidity: %3.1f %%' % hum)
# message = f"Temperature%3A%20{temp}%0AHumidity%3A%20{hum}" #YOUR MESSAGE HERE (URL ENCODED)https://www.urlencoder.io/
# #set your host URL
# url = 'https://api.callmebot.com/whatsapp.php?phone='+phone_number+'&text='+message+'&apikey='+api_key
# #make the request
# response = requests.get(url)
# #check if it was successful
# if response.status_code == 200:
# print('Success!')
# else:
# print('Error')
# print(response.text)
# # Connect to WiFi
# connect_wifi(ssid, password)
# #Your phone number in international format
# while 1:
# phone_number = '+923182266300'
# #Your callmebot API key
# api_key = '8349290'
# # Send message to WhatsApp "Hello"
# send_message(phone_number, api_key)
# time.sleep(1)
# import pickle
# from sklearn.linear_model import LinearRegression
# # Sample data
# X = [[1], [2], [3]] # Humidity values
# y = [20, 21, 22] # Corresponding temperature values
# # Create and train a linear regression model
# model = LinearRegression()
# model.fit(X, y)
# # Save the model to a .pkl file
# with open('temperature_model.pkl', 'wb') as model_file:
# pickle.dump(model, model_file)
########## for ESP32 ##########
# import urequests as requests
# import random
# import time
# import network
# import gc
# from machine import Pin
# from time import sleep
# import dht
# sensor = dht.DHT22(Pin(5))
# def do_connect():
# sta_if = network.WLAN(network.STA_IF)
# if not sta_if.isconnected():
# print('connecting to network...')
# sta_if.active(True)
# sta_if.connect("Wokwi-GUEST","")
# # sta_if.connect("HUAWEI-UtK2","FjFyt2w3")
# while not sta_if.isconnected():
# pass
# print('network config:', sta_if.ifconfig())
# do_connect()
# # Server URL to send the data
# SERVER_URL = "https://raheelapi.pythonanywhere.com/api/update-dht-data"
# def generate_random_data():
# # Generate random temperature and humidity values
# # temperature = random.uniform(30, 90)
# # humidity = random.uniform(40, 90)
# sensor.measure()
# temp = sensor.temperature()
# hum = sensor.humidity()
# return {"temperature": temp, "humidity": hum}
# def send_dht_data_to_server(data):
# global SERVER_URL
# try:
# # Send the data to the server using a POST request
# response = requests.post(SERVER_URL, json=data)
# if response.status_code == 200:
# print("Data sent successfully to the server.")
# else:
# print("Failed to send data to the server. Status code:", response.status_code)
# response.close() # Close the connection to free memory
# except Exception as e:
# print("Error occurred while sending data to the server:", str(e))
# if __name__ == "__main__":
# while True:
# try:
# # Generate random data
# dht_data = generate_random_data()
# # Send the data to the server
# send_dht_data_to_server(dht_data)
# # Free up memory
# gc.collect()
# # Wait for some time before sending the next data
# time.sleep(1)
# except KeyboardInterrupt:
# break
# # Close the network connection when done
# network.WLAN(network.STA_IF).disconnect()