# # Project objective: Blink the onboard LED of the Raspberry Pi Pico
# # Programmer: Adrian Josele G. Quional
# # modules
# from machine import Pin # to be able to work with the hardware
# from time import sleep # to add delays in the program
# # Setting the pin mode to OUT
# # Pin 25 (GP25) is the onboard LED in Raspberry Pi Pico W
# # for Raspberry Pi Pico
# LED_PIN = Pin(9, Pin.OUT)
# # for Raspberry Pi Pico W
# # LED = Pin("LED", Pin.OUT)
# # continuously blink the onboard LED for an interval of 1 sec while the board has power
# while True:
# LED_PIN.on() # turn on LED
# print"LED ON"
# sleep(1) # wait for 1 sec
# LED_PIN.off() # turn off LED
# print"LED OFF"
# sleep(1) # wait for 1 sec
# import machine as gpio
# import utime as TM
# # Define the GPIO pin for the IR sensor
# Sensor_In = gpio.Pin(22, gpio.Pin.IN)
# LED_PIN=gpio.Pin(9,gpio.Pin.OUT)
# # Function to read and print the IR sensor's digital output
# def read_ir_sensor():
# sensor_output = Sensor_In.value()
# if sensor_output == 0:
# print("IR sensor detected an object.")
# LED_PIN.on()
# else:
# print("IR sensor did not detect an object.")
# LED_PIN.off()
# # Main loop
# while True:
# read_ir_sensor()
# TM.sleep(1) # Add a delay between readings
# import machine as gpio
# import utime as TM
# # Define the GPIO pins for the ultrasonic sensor
# trigger_pin = gpio.Pin(28, gpio.Pin.OUT)
# echo_pin = gpio.Pin(27, gpio.Pin.IN)
# # Define a unique variable name to store the distance
# distance_cm = 0
# # Function to measure the distance using the ultrasonic sensor
# def measure_distance():
# # Send a 10us trigger pulse to start the measurement
# trigger_pin.value(1)
# TM.sleep_us(10)
# trigger_pin.value(0)
# # Wait for the echo pin to go high
# while echo_pin.value() == 0:
# pass
# start_time = TM.ticks_us()
# # Wait for the echo pin to go low
# while echo_pin.value() == 1:
# pass
# end_time = TM.ticks_us()
# # Calculate the duration of the pulse and convert it to distance (cm)
# pulse_duration = TM.ticks_diff(end_time, start_time)
# distance_cm = (pulse_duration / 2) / 29.1 # Speed of sound in air at 20°C is approximately 343 m/s
# return distance_cm
# # Main loop
# while True:
# distance_cm = measure_distance()
# print("Distance: {:.2f} cm".format(distance_cm))
# TM.sleep(1) # Add a delay between measurements
import network
# import time
import urequests as requests
import gc
from machine import Pin # to be able to work with the hardware
from time import sleep # to add delays in the program
# Led_pin define
Blue_Led = Pin(5, Pin.OUT)
# Define the server URLs & auth_token
uplink_server_url = "https://console.thingzmate.com/api/v1/device-types/raspi-http/devices/raspi-demo-http/uplink"
downlink_server_url = "https://console.thingzmate.com/api/v1/device-types/raspi-http/devices/raspi-demo-http/downlink"
auth_token = "728ba712814387c1d7c7cb566720ce91"
# Your Wi-Fi credentials
ssid = "Wokwi-GUEST"
password = ""
# const char* ssid = "Wokwi-GUEST";
# const char* password = "";
# Example data to send (for POST request)
data_to_send = {
"sensor_id": 1,
"value": 25.5
}
# Create a Wi-Fi station interface
wlan = network.WLAN(network.STA_IF)
# Activate the interface
wlan.active(True)
# Connect to the Wi-Fi network
wlan.connect(ssid, password)
# Wait for the connection to be established
while not wlan.isconnected():
pass
# Print the network configuration
print('Connected to', ssid)
print('IP address:', wlan.ifconfig()[0])
# Define headers with the Authorization token
headers = {
"Authorization": "Bearer " + auth_token
}
# Initialize the response variable as None
response = None
while True:
try:
# Perform a POST request to send data to the server
response = requests.post(uplink_server_url, json=data_to_send, headers=headers)
if response.status_code == 200:
print("Data sent successfully!")
else:
print("Failed to send data. Status code:", response.status_code)
response.close() # Close the response to free up memory
# Perform a GET request to retrieve data from the server
response = requests.get(downlink_server_url, headers=headers)
if response.status_code == 200:
server_data = response.json() # Assuming the server responds with JSON data
print("Received data:", server_data)
if server_data == 1:
print("LED_ON")
# Blue_Led.value(1)
Blue_Led.on()
elif server_data == 2:
print("LED_OFF")
# Blue_Led.value(0)
Blue_Led.off()
else:
print("Failed to retrieve data. Status code:", response.status_code)
except Exception as e:
print("No_RX_DATA")
finally:
if response is not None:
response.close()
# Force garbage collection to free up memory
gc.collect()
time.sleep(30) # Adjust the sleep interval as needed