Quantcast
Viewing all articles
Browse latest Browse all 5766

MicroPython • Re: Get file from server and execute it, http/Rest

As promised. I didn't use 'main.py', created 'wifi_download.py' which connects to the network, downloads 'sensor.py', runs it. No error checking etc.

wifi_download.py

Code:

import networkimport requestsimport secretsimport time# Connect to the networkwlan = network.WLAN(network.STA_IF)wlan.active(True)wlan.connect(secrets.ssid, secrets.password)print("Connecting to router")while not wlan.isconnected():  print("  Wait ...")  time.sleep(1)print("Connected")print("")# Download the latest 'sensor.py'print("Download 'sensor.py'")reply = requests.get("http://192.168.0.130/sensor.py")print("  Saving 'sensor.py' to file system")with open("/sensor.py", "w") as f:  f.write(reply.text)reply.close()print("Download finished")print("")# Run the latest sensor.pyprint("Running 'sensor.py'")import sensorprint("Finished")
sensor.py

Code:

print("=== Inside sensor.py ===")
Executing

Code:

pi@Pi3B:~ $ mpremote run wifi_download.pyConnecting to router  Wait ...  Wait ...ConnectedDownload 'sensor.py'  Saving 'sensor.py' to file systemDownload finishedRunning 'sensor.py'=== Inside sensor.py ===Finished

Statistics: Posted by hippy — Sat Apr 27, 2024 10:56 pm



Viewing all articles
Browse latest Browse all 5766

Trending Articles