Skip to content

Instantly share code, notes, and snippets.

@rw950431
Last active November 23, 2018 14:21
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 rw950431/2b1a632f49e89cb0c3ac6b77433f13fa to your computer and use it in GitHub Desktop.
Save rw950431/2b1a632f49e89cb0c3ac6b77433f13fa to your computer and use it in GitHub Desktop.
import os
import glob
import time
import sys
import datetime
import urllib2
baseURL = "https://api.thingspeak.com/update?api_key=(MY KEY)&field1=&field2="
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
# add more sensor variables here based on your setup
temps=[]
base_dir = '/sys/bus/w1/devices/'
device_folders = glob.glob(base_dir + '28*')
def read_temp_raw(device_file):
f = open(device_file, 'r')
lines = f.readlines()
f.close()
return lines
def read_temp(device_file): # checks the temp recieved for errors
lines = read_temp_raw(device_file)
while lines[0].strip()[-3:] != 'YES':
time.sleep(0.2)
lines = read_temp_raw(device_file)
equals_pos = lines[1].find('t=')
if equals_pos != -1:
temp_string = lines[1][equals_pos+2:]
# set proper decimal place for C
temp = float(temp_string) / 1000.0
# Round temp to 2 decimal points
temp = round(temp, 1)
# value of temp might be unknown here if equals_pos == -1
return temp
while True: #infinite loop
for sensors in range (2): # change number of sensors based on your setup
device_file=device_folders[sensors]+ '/w1_slave'
temp[sensors] = read_temp(device_file)
print device_file,sensors,temp[sensors]
g = urllib2.urlopen(baseURL + "&field1=%s&field2=%s" % (temp[0], temp[1]))
time.sleep(60)
@fajarbudin
Copy link

import os
import glob
import time
import sys
import datetime
import urllib, urllib2

baseURL = "https://api.thingspeak.com/update?api_key=XXXXXXXXXXX"

os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')

add more sensor variables here based on your setup( 28-

temp=['sensor code','tttttttttt','ddddddddddd','ssssssssss']

base_dir = '/sys/bus/w1/devices/'

device_folders = glob.glob(base_dir + '28*')

def read_temp_raw(device_file):
f = open(device_file, 'r')
lines = f.readlines()
f.close()
return lines

def read_temp(device_file): # checks the temp recieved for errors
lines = read_temp_raw(device_file)
while lines[0].strip()[-3:] != 'YES':
time.sleep(0.2)
lines = read_temp_raw(device_file)

equals_pos = lines[1].find('t=')
if equals_pos != -1:
    temp_string = lines[1][equals_pos+2:]
    # set proper decimal place for C
    temp = float(temp_string) / 1000.0
    # Round temp to 2 decimal points
    temp = round(temp, 1)
# value of temp might be unknown here if equals_pos == -1
return temp

while True: #infinite loop

for sensors in range (4): # change number of sensors based on your setup
    device_file=device_folders[sensors]+ '/w1_slave'
    temp[sensors] = read_temp(device_file)
    print device_file,sensors,temp[sensors]

g = urllib.urlopen(baseURL + "&field1=%s&field2=%s&field3=%s&field4=%s" % (temp[0],temp[1],temp[2],temp[3]))

time.sleep(60)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment