Skip to content

Instantly share code, notes, and snippets.

@ShawnHymel
Created March 13, 2019 19:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ShawnHymel/3737d01898002e62a848c5fc3c33f1b1 to your computer and use it in GitHub Desktop.
Save ShawnHymel/3737d01898002e62a848c5fc3c33f1b1 to your computer and use it in GitHub Desktop.
Firmata Weather Station
import http.client
import time
from pyfirmata import Arduino, util
# Parameters
COM_PORT = 'COM37'
API_KEY = 'BMZ44FSBTE8JANSU'
HOSTNAME = 'api.thingspeak.com'
AREF = 5.0
SLEEP = 10 # seconds
# Connect to firmata
print('Connecting to Arduino')
board = Arduino(COM_PORT)
# Define pins
a0 = board.get_pin('a:0:i')
d3 = board.get_pin('d:3:o')
# Start thread to sample analog pin values
it = util.Iterator(board)
it.start()
time.sleep(0.1)
# Do forever
while True:
# Turn on LED to show that we are sampling
d3.write(1)
# Read temperature
voltage = a0.read() * AREF
temp_val = (voltage - 0.5) * 100
temp_str = '%.1f'%temp_val
print("Temperature: " + temp_str + " C")
# Construct GET request
get_str = '/update?api_key=' + API_KEY + '&field1=' + temp_str
print(HOSTNAME + get_str)
# Send request (update channel)
conn = http.client.HTTPSConnection(HOSTNAME)
conn.request('GET', get_str)
resp = conn.getresponse()
print(resp.status, resp.reason)
# Turn off LED to show that we are done
d3.write(0)
# Wait before next request
time.sleep(SLEEP)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment