Created
April 4, 2023 18:40
-
-
Save misterhay/61375b2f25e3887845bf5cc2a3f2b4be to your computer and use it in GitHub Desktop.
Micropython for datalogging with Raspberry Pi Pico W
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from machine import Pin, ADC | |
import network | |
from time import sleep | |
import urequests as requests | |
led = Pin("LED", Pin.OUT) | |
led.on() | |
wlan = network.WLAN(network.STA_IF) | |
wlan.active(True) | |
wlan.connect('CompWorksWest', 'password') | |
wait = 10 | |
while wait > 0: | |
if wlan.status() < 0 or wlan.status() >= 3: | |
break | |
wait -= 1 | |
print('waiting for connection...') | |
sleep(1) | |
if wlan.status() != 3: # connection error | |
raise RuntimeError('WiFi connection failed') | |
for x in range(100): # blink the LED for connection failure | |
led.off() | |
sleep(1) | |
led.on() | |
sleep(1) | |
else: | |
ip = wlan.ifconfig()[0] | |
print('IP: ', ip) | |
led.off() | |
spreadsheet_name = 'FaceyPlant01' | |
base_url = 'https://ethercalc.net/' | |
post_url = base_url + '_/' + spreadsheet_name | |
sensor = ADC(26) | |
for x in range(100): # repeat the sensor loop 100 times | |
sensor_value = sensor.read_u16() | |
dt = requests.get('http://date.jsontest.com/').json() | |
date_and_time = dt['date']+' '+dt['time'] | |
data_string = date_and_time+','+str(sensor_value) | |
print('posting', data_string, 'to', post_url) | |
r = requests.post(post_url, data=data_string) | |
#print(r.status_code) | |
#print(r.json()['command'][1]) | |
sleep(60) # sleep for a minute |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment