Network access (requests, MQTT)
Python on a simulated Raspberry Pi can reach the internet: call web APIs with requests, and send and receive MQTT messages with paho-mqtt. The Pico is modelled as a plain Pico (not a Pico W), so this is for Pi boards only.
HTTP with requests
import requests
reply = requests.get("https://api.open-meteo.com/v1/forecast", params={"latitude": 53.8, "longitude": -1.55, "current": "temperature_2m"})
reply.raise_for_status()
print(reply.json()["current"]["temperature_2m"])
requests.post("https://example.com/log", json={"temperature": 21.5})
get, post, put, patch, delete and Session take the usual arguments (params, data, json, headers, auth). A response has status_code, ok, headers, text, content, json() and raise_for_status().
MQTT with paho-mqtt
Connect to a public broker that accepts MQTT over WebSockets, or to "sandbox": a broker inside the page. Every board running on the page (in the Lab, or several boards in one build) shares the sandbox, and its messages never leave your browser.
Browsers can't open plain MQTT connections, so PyRasp connects over WebSockets. For broker.hivemq.com, broker.emqx.io, test.mosquitto.org and mqtt.eclipseprojects.io the WebSocket port is filled in for you, so client.connect("broker.hivemq.com", 1883) works as written. For another broker, give its WebSocket port (client.connect("mqtt.example.com", 9001) connects to wss://mqtt.example.com:9001/mqtt) or its full address (client.connect("wss://mqtt.example.com/ws")).
import time
import paho.mqtt.client as mqtt
def on_connect(client, userdata, flags, reason_code, properties):
client.subscribe("home/#")
def on_message(client, userdata, message):
print(message.topic, message.payload.decode())
client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2)
client.on_connect = on_connect
client.on_message = on_message
client.connect("sandbox")
client.loop_start()
while True:
client.publish("home/temperature", 21.5)
time.sleep(5)
Callbacks run in the network loop, as in paho: call loop_start(), loop_forever() or loop(). paho 1's style, mqtt.Client("my-id") with four-argument on_connect, works too. For a broker that needs TLS, call client.tls_set() before connecting.
You decide which sites a program may contact
The first time a program contacts a site or a broker, PyRasp asks: "This program wants to send data to api.example.com. Allow?" Allowed sites are remembered for the project. If you don't allow it, the call raises PermissionError with the site's name, and running the program again asks again. The sandbox broker never asks: nothing leaves the page.
How it works, and its limits
Your browser sends the requests itself. Many web APIs allow that; for a site that doesn't accept requests from web pages, PyRasp forwards the request through its own server, which needs you signed in (signed out, the call raises RuntimeError saying so). While your program waits for a reply, only your program (or that thread) waits: the circuit and the simulation keep running in simulated time.
- Only
httpandhttps, to public internet addresses. Local and private addresses (localhost,192.168.x.x,10.x.x.xand so on) are refused, so your home network isn't reachable from the simulator. - A request gives up after 10 seconds. Forwarded requests are limited to 1 MB each and a few hundred a day per account.
- The response headers a program sees are the ones the site lets web pages read.
- Sandbox messages are never retained.