TRBTStevenson
Posts: 9
Joined: Sat May 28, 2016 2:16 am

Help with making Python code send data every 5 mins

Tue Jun 21, 2016 7:43 pm

Hi I am pretty new to python but I am trying to get this code to repeat every 5 mins so that I can have it running and sending temp data to Thingspeak any help appreciated thank you :)!!!

Code: Select all

import httplib, urllib, os, glob, time

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

# Add sensors here for more data
temp_0 = 0
temp_1 = 0
temp_2 = 0
temp_3 = 0

for sensors in range (4): # CHANGE THIS NUMBER to match how many sensors are attached
   base_dir = '/sys/bus/w1/devices/'
   device_folder = glob.glob(base_dir + '28*')[sensors]
   device_file = device_folder + '/w1_slave'
   print device_file
   print sensors

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

   def read_temp(): # Checks for any errors
      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:]
         # set proper decimal place for C
         temp = float(temp_string) / 1000.0
         # Round temp to 2 decimal points
         temp = round(temp, 1)
         return temp

# Asks for temps to be read and store each in a variable
   if sensors == 0:
      temp_0 = read_temp()
      temp_1 = read_temp()
      temp_2 = read_temp()
      temp_3 = read_temp()
   if sensors == 1:
      temp_1 = read_temp()



      print temp_0
      print temp_1
      print temp_2
      print temp_3

#Gathers data and controls to which field it goes to and where the data is coming from
temp_0 = read_temp()
temp_1 = read_temp()
temp_2 = read_temp()
temp_3 = read_temp()
#Be sure to add new field and temp info when adding more sensors
params = urllib.urlencode({'field1': temp_0, 'field2': temp_1, 'field3': temp_2, 'field4': temp_3, 'key':''})
headers = {"Content-type": "application/x-www-form-urlencoded","Accept":  
    "text/plain"}  
conn = httplib.HTTPConnection("api.thingspeak.com:80")  
conn.request("POST", "/update", params, headers)  
response = conn.getresponse()  
print response.status, response.reason  
data = response.read()  
conn.close()

SonOfAMotherlessGoat
Posts: 690
Joined: Tue Jun 16, 2015 6:01 am

Re: Help with making Python code send data every 5 mins

Tue Jun 21, 2016 7:50 pm

Python is very sensitive to whitespace, you have some formatting issues it looks like. Did you follow an online tutorial? Can we have a link to the tutorial.

And what errors are you getting when you run this code?
Account Inactive

TRBTStevenson
Posts: 9
Joined: Sat May 28, 2016 2:16 am

Re: Help with making Python code send data every 5 mins

Tue Jun 21, 2016 7:56 pm

I don't have any errors at all everything runs great and it sends the data fine I just need it to repeat the code every 5 mins, I am not sure where I found the code originally but I then tweaked it till I got it working for me.

SonOfAMotherlessGoat
Posts: 690
Joined: Tue Jun 16, 2015 6:01 am

Re: Help with making Python code send data every 5 mins

Tue Jun 21, 2016 8:15 pm

Ah, well in that case you have a myriad of options.
  1. Cronjob
  2. systemD Timers
  3. sleep() wrapper
I'm sure others will come up with more options and choices, but those should get you started with your research.
Account Inactive

TRBTStevenson
Posts: 9
Joined: Sat May 28, 2016 2:16 am

Re: Help with making Python code send data every 5 mins

Tue Jun 21, 2016 9:03 pm

Thank you for the quick response I'm still very new to this and am basically guessing where to put this code anyway I could get an example?

SonOfAMotherlessGoat
Posts: 690
Joined: Tue Jun 16, 2015 6:01 am

Re: Help with making Python code send data every 5 mins

Tue Jun 21, 2016 9:12 pm

I hate to resort to this, but the best way to tackle things is to dive in and try out different solutions. If you search for "How to make a Python script sleep for 5 minutes" you'll get a lot of great code examples. (I'm partial to anything from StackOverflow.) I know it's kind of a crappy answer for you, but teach a man to fish...
Account Inactive

TRBTStevenson
Posts: 9
Joined: Sat May 28, 2016 2:16 am

Re: Help with making Python code send data every 5 mins

Tue Jun 21, 2016 9:34 pm

Thank you for the help that's pretty much how I've been getting through this is by figuring it out step by step, it wouldn't be fun if it was all done for me anyways lol have a good day :)

User avatar
bensimmo
Posts: 4622
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: Help with making Python code send data every 5 mins

Wed Jun 22, 2016 8:33 am

I would just run it in a loop in python with sleep, not sure if that is the most efficeint fo rthe Pi and it's power?


There are lots of things to grab ideas from on this very site.
As I have a SenseHat and do datalogging, Iused htis anad have adapted it for 1-Wire and GPS logging among other things.

https://www.raspberrypi.org/learning/se ... worksheet/
the full program on sheet2 may be overkill for the simple logging use, but If found it a great basis for easily adding other sensors.

or another place show you just the loop at the bottom
https://thepihut.com/blogs/raspberry-pi ... he-ds18b20


Also as you are using 1-Wire sensors, look for w1thermsensor it's a nice library to simplify the coding.


[new to python myself.]

gordon77
Posts: 5036
Joined: Sun Aug 05, 2012 3:12 pm

Re: Help with making Python code send data every 5 mins

Wed Jun 22, 2016 9:08 am

I think you have a couple of choices.

Do you want the script to run all the time or every 5 minutes, do you need the Pi to be doing other things during the 5 minutes ?

crontab can call it every 5 minutes.

If this is the only task then add a time.sleep(300) to put it to sleep for 5 minutes.

Do you need to add other things to your script and just use this section every 5 minutes ? is so you could use a timer in a loop.

Massi
Posts: 1691
Joined: Fri May 02, 2014 1:52 pm
Location: Italy

Re: Help with making Python code send data every 5 mins

Wed Jun 22, 2016 9:25 am

apscheduler :)

bytesoup
Posts: 4
Joined: Mon Jul 30, 2012 12:14 pm

Re: Help with making Python code send data every 5 mins

Sat Jun 25, 2016 2:32 pm

I would say using cron would be best here.

Some good examples here: http://alvinalexander.com/linux/unix-li ... day-syntax

TRBTStevenson
Posts: 9
Joined: Sat May 28, 2016 2:16 am

Re: Help with making Python code send data every 5 mins

Tue Jun 28, 2016 9:03 pm

I ended up using Cron thank you for all the help everyone :)

Return to “General discussion”