import urequests
import time
import json
# Define the GPS module and passenger counter sensor endpoints
GPS_ENDPOINT = " XX "
PASSENGER_COUNTER_ENDPOINT = " XX "
# Define the transit information platform URL
TIP_URL = " XX "
# Main loop
while True:
try:
# Read the GPS data from the GPS module
gps_response = urequests.get(GPS_ENDPOINT)
gps_data = gps_response.json()
# Read the passenger count data from the passenger counter sensor
passenger_count_response = urequests.get(PASSENGER_COUNTER_ENDPOINT)
passenger_count_data = passenger_count_response.text
# Form the data payload to send to the TIP
data_payload = {
"latitude": gps_data["latitude"],
"longitude": gps_data["longitude"],
"passenger_count": int(passenger_count_data)
}
# Send the data payload to the TIP
response = urequests.post(TIP_URL, json=data_payload)
# Check if the data was sent successfully
if response.status_code == 200:
print("Data sent successfully")
else:
print(f"Error sending data: {response.status_code}")
except Exception as e:
print(f"An error occurred: {e}")
# Wait for 1 second before sending the next update
time.sleep(1)