Skip to content

Instantly share code, notes, and snippets.

@kndt84
Created June 3, 2017 11:35
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 kndt84/43f50af85be6dca281667409af25f825 to your computer and use it in GitHub Desktop.
Save kndt84/43f50af85be6dca281667409af25f825 to your computer and use it in GitHub Desktop.
ThingSpeak sample
#!/usr/bin/python
import smbus
import time
import sys
import requests
from datetime import datetime
api_key=''
bus = smbus.SMBus(1)
lm75b_address = 0x49
tmp102_address = 0x48
def get_temp_from_tmp102():
data = bus.read_i2c_block_data(tmp102_address, 0)
msb = data[0]
lsb = data[1]
temp= (((msb << 8) | lsb) >> 4) * 0.0625 #printing the temperature value in Celsius.
return temp
def get_temp_lmp75b():
block = bus.read_i2c_block_data(lm75b_address, 0x00, 2)
val = block[0] << 8
val = val | block[1]
if(val >= 0x7fff):
val = val - 0xffff
result = ((val >>5) * 0.125)
return result
while True:
tmp102=get_temp_from_tmp102()
lmp75b=get_temp_lmp75b()
now=datetime.now().strftime('%Y-%m-%d %H:%M:%S')
requests.get('http://api.thingspeak.com/update?api_key=%s&field1=%s&field2=%s' % (api_key, tmp102, lmp75b))
print("%s TMP102=%+6.2f LMP75B=%+6.2f " % (now, tmp102, lmp75b))
sys.stdout.flush()
time.sleep(60)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment