Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import machine
- import network
- import urequests as requests
- import time
- import bluetooth
- # Define the onboard LED (you can choose any GPIO pin)
- led_onboard = machine.Pin("LED", machine.Pin.OUT)
- # Initialize WiFi
- wlan = network.WLAN(network.STA_IF)
- wlan.active(True)
- wlan.connect('your_wifi_ssid', 'your_wifi_password')
- # Wait for connection or fail
- max_wait = 10
- while max_wait > 0:
- if wlan.status() < 0 or wlan.status() >= 3:
- break
- max_wait -= 1
- print('Waiting for WiFi connection...')
- time.sleep(1)
- if wlan.status() != 3:
- raise RuntimeError('Network connection failed')
- else:
- print('Connected to WiFi')
- status = wlan.ifconfig()
- print('IP address:', status[0])
- # Initialize Bluetooth
- ble = bluetooth.BLE()
- # Callback when a central device (e.g., smartphone) connects
- def on_connect(event, data):
- conn_handle, _, _ = data
- print("Connected to device:", conn_handle)
- led_onboard.on() # Turn on the LED
- # Callback when a central device disconnects
- def on_disconnect(event, data):
- conn_handle, _, _ = data
- print("Disconnected from device:", conn_handle)
- led_onboard.off() # Turn off the LED
- # Set up BLE callbacks
- ble.irq(handler=on_connect, event=bluetooth.IRQ_CENTRAL_CONNECT)
- ble.irq(handler=on_disconnect, event=bluetooth.IRQ_CENTRAL_DISCONNECT)
- # Start advertising (make the Pico discoverable)
- ble.gap_advertise(interval_us=500000, adv_data=b"\x02\x01\x06\x03\x03\x09\x18")
- # Callback function to send POST request
- def send_post_request(timer):
- timestamp = machine.RTC().datetime()
- timestring = "%04d-%02d-%02d %02d:%02d:%02d" % (timestamp[0:3] + timestamp[4:7])
- api_endpoint = "https://your.api.endpoint.com/data" # Replace with your API URL
- payload = {"timestamp": timestring, "event": "proximity_detected"}
- try:
- response = requests.post(api_endpoint, json=payload)
- print(timestring, "Status Code:", response.status_code)
- response.close()
- except Exception as e:
- print("Error sending POST request:", e)
- # Initialize timer for periodic API requests
- timer = machine.Timer()
- timer.init(period=10000, mode=machine.Timer.PERIODIC, callback=send_post_request)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement