element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      • Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Raspberry Pi Projects
  • Products
  • Raspberry Pi
  • Raspberry Pi Projects
  • More
  • Cancel
Raspberry Pi Projects
Blog Raspberry Pi Zero Temperature Data Logger Multiple DS18B20 sensors to Thingspeak
  • Blog
  • Documents
  • Events
  • Polls
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Raspberry Pi Projects to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: Former Member
  • Date Created: 12 Mar 2016 7:01 PM Date Created
  • Views 1711 views
  • Likes 1 like
  • Comments 2 comments
  • raspberry_pi_projects
Related
Recommended

Raspberry Pi Zero Temperature Data Logger Multiple DS18B20 sensors to Thingspeak

Former Member
Former Member
12 Mar 2016

I was using a project by Mike Davis (A Raspberry Pi Data Logger for about $25 (Temperature Probe and ThingSpeak).. All is working fine for one sensor but I have 3 and am unable to figure out a loop to get it to upload all three. I can see all three sensors in the /devices/ directory. Any help would be appreciated.

 

Here is code from Mike I'm using:

import os

 

import glob

 

import time

 

import sys

 

import datetime

 

import urllib2

 

 

 

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

 

 

 

#initiate the temperature sensor

 

os.system('modprobe w1-gpio')

 

os.system('modprobe w1-therm')

 

 

 

#set up the location of the sensor in the system

 

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

 

device_folder = glob.glob(base_dir + '28*')[0]

 

device_file = device_folder + '/w1_slave'

 

 

 

def read_temp_raw(): #a function that grabs the raw temperature data from the sensor

 

     f = open(device_file, 'r')

 

     lines = f.readlines()

 

     f.close()

 

     return lines

 

 

 

def read_temp(): #a function that checks that the connection was good and strips out the temperature

 

     lines = read_temp_raw()

 

     while lines[0].strip()[-3:] != 'YES':

 

         time.sleep(0.2)

 

         lines = read_temp_raw()

 

     equals_pos = lines[1].find('t=')

 

     if equals_pos !=-1:

 

         temp_string = lines[1][equals_pos+2:]

 

         temp_c = float(temp_string)/1000.0

 

         temp_f = temp_c * 9.0/5.0 + 32.0

 

         return temp_c

 

 

 

while True: #infinite loop

 

     tempin = read_temp() #get the temp

 

     values = [datetime.datetime.now(), tempin]

 

     g = urllib2.urlopen(baseURL + "&field1=%s" % (tempin))

 

     time.sleep(60)

 

  • Sign in to reply
  • waveringwafu
    waveringwafu over 9 years ago in reply to Former Member

    Dale,

     

    A little late in the day, but hopefully will help somebody else.

     

    I wanted to measure 3 temperatures: Fridge, Beer and Outside Air Temp (OAT).  My ThingSpeak channel: 115595

     

    I re-used the same code as you, but changed the variables in each section so that I had 3 unique sets:

    OAT

    Fridge

    Beer.

     

    To send to ThingSpeak,  I separated temperature variables by commas and assigned the variables to fields

     

    values = [datetime.datetime.now(), fridge_tempin, oat_tempin, beer_tempin]

     

    j = urllib2.urlopen(baseURL + "&field1=%s" % (fridge_tempin) + "&field2=%s" % (oat_tempin) + "&field3=%s" % (beer_tempin))

     

    Code:

    import os

    import glob

    import time

    import sys

    import datetime

    import urllib2

     

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

     

    os.system('modprobe w1-gpio')

     

    os.system('modprobe w1-therm')

     

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

     

    #beer temperature device location

    beer_file = '/sys/bus/w1/devices/28-011562a640ff/w1_slave'

     

    #fridge temperature Device Location

    fridge_file = '/sys/bus/w1/devices/28-011562be55ff/w1_slave'

     

    #OAT temperature device location

    oat_file = '/sys/bus/w1/devices/28-021562c6c9ff/w1_slave'

     

    #Determine beer temperature

    def read_rawtemp_beer():

         f = open(beer_file,'r')

         lines = f.readlines()

         f.close

         return lines

     

    def read_temp_beer():

         lines = read_rawtemp_beer()

         while lines[0].strip()[-3:] !='YES':

              time.sleep(0.2)

              lines = read_rawtemp_beer()

         equals_pos = lines[1].find('t=')

         if equals_pos !=-1:

              temp_string = lines[1][equals_pos+2:]

              temp_beer = float(temp_string)/1000.0

              return temp_beer

     

    #Determine Fridge Temperature

    def read_rawtemp_fridge():

         y = open(fridge_file,'r')

         lines = y.readlines()

         y.close

         return lines

     

    def read_temp_fridge():

         lines = read_rawtemp_fridge()

         while lines[0].strip()[-3:] !='YES':

              time.sleep(0.2)

              lines = read_rawtemp_fridge()

         equals_pos = lines[1].find('t=')

         if equals_pos !=-1:

              temp_string = lines[1][equals_pos+2:]

              temp_fridge = float(temp_string)/1000.0

              return temp_fridge

    #Determine OAT Temperature

    def read_rawtemp_oat():

         x = open(oat_file,'r')

         lines = x.readlines()

         x.close

         return lines

     

    def read_temp_oat():

         lines = read_rawtemp_oat()

         while lines[0].strip()[-3:] !='YES':

              time.sleep(0.2)

              lines = read_rawtemp_oat()

         equals_pos = lines[1].find('t=')

              if equals_pos !=-1:

              temp_string = lines[1][equals_pos+2:]

              temp_oat = float(temp_string)/1000.0

              return temp_oat

     

    while True: #Loop

     

    #Send Fridge Temperature to Thingspeak

         fridge_tempin = read_temp_fridge()

     

    #Send Beer Temperature to Thingspeak

         beer_tempin = read_temp_beer()

     

    #Send OAT temperature to Thingspeak

         oat_tempin = read_temp_oat()

     

    #Pull results together

         values = [datetime.datetime.now(), fridge_tempin, oat_tempin, beer_tempin]

     

    #Open Thingspeak channel and assign fields to temperatures

         j = urllib2.urlopen(baseURL + "&field1=%s" % (fridge_tempin) + "&field2=%s" % (oat_tempin) + "&field3=%s" % (beer_tempin))

     

    #Time to next loop

    time.sleep(600)

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Former Member
    Former Member over 9 years ago

    Ok, I know this wasn't the cleanest but all I could figure out for now. Hope it helps someone.

     

    I removed:

     

    while true: line

    time.sleep line

     

    changed the field to match which field i was uploading to and save three versions of the script. I then just call each script from a cron job running every 10 minutes.

     

    whatever I know it sucks there is a much better way but I played with the script until I gave up and did it the cheap way. It's working now just sucks to have to do it that way.

     

    Dale

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube