Skip to content

Instantly share code, notes, and snippets.

@16Dcarrick
Last active October 6, 2021 09:26
Show Gist options
  • Save 16Dcarrick/2755a8e71c2e3ca19bf932a703b59b2b to your computer and use it in GitHub Desktop.
Save 16Dcarrick/2755a8e71c2e3ca19bf932a703b59b2b to your computer and use it in GitHub Desktop.
Winter ALT Project
import serial
import sys
from time import sleep
from urllib.request import urlopen
myAPI = "RXM5Z9R2ZBV9JHAH"
def updateThingSpeak():
print('Now updating thingspeak')
baseURL = 'https://api.thingspeak.com/update?api_key=%s' % myAPI
f = urlopen(baseURL + "&field1=%s" % (temperature) + "&field2=%s" % (switch_position) )
print ("Success! I uploaded data point No. ", f.read())
f.close()
ser = serial.Serial()
ser.baudrate = 115200
# <<< YOU NEED TO CHANGE THIS BIT! >>>
# Try COM...ah...whateverYouSeeOnDeviceManager under ports. Type Device Manager in the start menu.
ser.port = "COM3"
print("Type CTRL + C to exit. (may take 5 seconds)")
ser.open()
try:
while True:
print("Reading Serial Data and Switch position (1 or 0)")
# reads and cleans the first line from serial (temp)
microbitdata = str(ser.readline())
temperature = microbitdata[2:]
temperature = temperature.replace(" ","")
temperature = temperature.replace("'","")
temperature = temperature.replace("\\r\\n","")
temperature = int(temperature)
print(temperature)
# reads and cleans the second line from serial
microbitdata = str(ser.readline())
switch_position = microbitdata[2:]
switch_position = switch_position.replace(" ","")
switch_position = switch_position.replace("'","")
switch_position = switch_position.replace("\\r\\n","")
switch_position = int(switch_position)
print(switch_position)
# You could copy the code block above to read a third line etc.
# Updates Thingspeak
updateThingSpeak()
except KeyboardInterrupt:
#https://api.thingspeak.com/update?api_key=RXM5Z9R2ZBV9JHAH
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment