Python Forum
Python loop for posting data to web
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python loop for posting data to web
#1
I got this code for posting data to thingspeak from a raspberry pi (zero):

#!/usr/local/bin/python3 
from envirophat import light, motion, weather, leds
import thingspeak
import time
from datetime import datetime

# Structure of the record - semicolon separated:
# UNIX timestamp;ISO date; light; rgb; motion; heading; temp (celsius); pressure (hPa)

# your ThnkSpeak Channel and API Write Key
channel_id="123"
api_key="456"
thingspeak_data = {}
try:
    now = datetime.now()
    ch = thingspeak.Channel(channel_id, api_key)
    timestamp = round(datetime.timestamp(now))
    cas = now.isoformat()
    leds.off()
    lux = light.light()
    print('%s;%s;%.1f' % (timestamp,cas,lux))
    thingspeak_data['field1'] = cas
    thingspeak_data['field2'] = lux
    response = ch.update(thingspeak_data)
    leds.off()

except KeyboardInterrupt:
    leds.off()
When I run the script from the terminal using the python command it works and posts once of course. But I need it to post every few minutes, so I did this. Replaced try: with while True: and added time.sleep(1800):
#!/usr/local/bin/python3 
from envirophat import light, motion, weather, leds
import thingspeak
import time
from datetime import datetime

# Structure of the record - semicolon separated:
# UNIX timestamp;ISO date; light; rgb; motion; heading; temp (celsius); pressure (hPa)


# your ThnkSpeak Channel and API Write Key
channel_id="123"
api_key="456"
thingspeak_data = {}
#try:
while True:
    now = datetime.now()
    ch = thingspeak.Channel(channel_id, api_key)
    timestamp = round(datetime.timestamp(now))
    cas = now.isoformat()
    leds.off()
    lux = light.light()
    print('%s;%s;%.1f' % (timestamp,cas,lux))
    thingspeak_data['field1'] = cas
    thingspeak_data['field2'] = lux
    response = ch.update(thingspeak_data)
    leds.off()
    time.sleep(1800)

#except KeyboardInterrupt:
#    leds.off()
It works, but only runs a few times and then stops after about the 3rd time. Why?

Mars
Reply
#2
I think you want to move this outside the loop.
ch = thingspeak.Channel(channel_id, api_key)
I wouldn't be surprised if disposing of a channel object is messing up your connection.

I also think you should remove this from the loop and have it only happen when the loop is done.
leds.off()
You should also put the keyboard interrupt exception handler back in.

I don't know what you want to do with the LED's, so I turn them on for as long as the program is running.
#!/usr/local/bin/python3 
from envirophat import light, motion, weather, leds
import thingspeak
from datetime import datetime
import time

channel_id="123"
api_key="456"
ch = thingspeak.Channel(channel_id, api_key)
leds.on()  # ??
try:
    while True:
        cas = datetime.now().isoformat()
        lux = light.light()
        print(cas, lux)
        print(ch.update({'field1': cas, 'field2': lux}))  # Maybe the reply has useful info
        time.sleep(20)  # Speed up for testing
except KeyboardInterrupt:
    pass
leds.off()
Reply
#3
Ok here is how I run the script on reboot:

@reboot python3 /home/mkzero/envirophat2.py &
And here is the code I ended up with based on your suggestion:

#!/usr/local/bin/python3
from envirophat import light, motion, weather, leds
import thingspeak
import time
from datetime import datetime

# Structure of the record - semicolon separated:
# UNIX timestamp;ISO date; light; rgb; motion; heading; temp (celsius); pressure (hPa)

channel_id="123"
api_key="456"
thingspeak_data = {}
leds.on()
ch = thingspeak.Channel(channel_id, api_key)
try:
        while True:
                now = datetime.now()
                #ch = thingspeak.Channel(channel_id, api_key)
                timestamp = round(datetime.timestamp(now))
                cas = now.isoformat()
                #leds.off()
                lux = light.light()
                rgb = str(light.rgb())[1:-1].replace(' ', '')
                #leds.on()
                acc = str(motion.accelerometer())[1:-1].replace(' ', '')
                heading = motion.heading()
                temp = weather.temperature()
                press = round(weather.pressure()/100)
                print('%s;%s;%.1f;%s;%s;%.1f;%.1f;%.1f' % (timestamp,cas,lux, rgb, acc, heading, temp, press))
                thingspeak_data['field1'] = cas
                thingspeak_data['field2'] = lux
                thingspeak_data['field3'] = rgb
                thingspeak_data['field4'] = acc
                thingspeak_data['field5'] = heading
                thingspeak_data['field6'] = temp
                thingspeak_data['field7'] = press
                response = ch.update(thingspeak_data)
                #leds.off()
                time.sleep(18)
except KeyboardInterrupt:
        pass
leds.off()
But when i run it from the terminal like so:

python3 envirophat2.py 
It runs fine, begins logging to the cloud and the terminal. ut when i reboot the pi so that it runs the crontab command with that script, nothing gets logged. Is my crontab command right? What am I missing?



(Jul-17-2023, 08:39 PM)deanhystad Wrote: I think you want to move this outside the loop.
ch = thingspeak.Channel(channel_id, api_key)
I wouldn't be surprised if disposing of a channel object is messing up your connection.

I also think you should remove this from the loop and have it only happen when the loop is done.
leds.off()
You should also put the keyboard interrupt exception handler back in.

I don't know what you want to do with the LED's, so I turn them on for as long as the program is running.
#!/usr/local/bin/python3 
from envirophat import light, motion, weather, leds
import thingspeak
from datetime import datetime
import time

channel_id="123"
api_key="456"
ch = thingspeak.Channel(channel_id, api_key)
leds.on()  # ??
try:
    while True:
        cas = datetime.now().isoformat()
        lux = light.light()
        print(cas, lux)
        print(ch.update({'field1': cas, 'field2': lux}))  # Maybe the reply has useful info
        time.sleep(20)  # Speed up for testing
except KeyboardInterrupt:
    pass
leds.off()
Reply
#4
To schedule with crontab you should get rid of the loop and the sleep. Let crontab schedule how often you update the channel.

From what I can find online, the way to run a python script using crontab is make he python file executable (chmod +x) and add a shebang to the python file. You already have the shebang.

Your crontab entry to run you script every 30 minutes would look like "/30 * * * * /home/mkzero/envirophat2.py"
Reply
#5
Ok I’ll try that but it’s weird because I have animo their script that runs fine without making the file executable.

(Jul-19-2023, 05:17 PM)deanhystad Wrote: To schedule with crontab you should get rid of the loop and the sleep. Let crontab schedule how often you update the channel.

From what I can find online, the way to run a python script using crontab is make he python file executable (chmod +x) and add a shebang to the python file. You already have the shebang.

Your crontab entry to run you script every 30 minutes would look like "/30 * * * * /home/mkzero/envirophat2.py"
Reply
#6
Where is your python3? Maybe you need to provide complete path?
Reply
#7
Ok and how do I do that? I believe I have installed both 2.7 and 3. How do I get the path for each?

I know i have 3.9.2 from doing python -V

and this:

mkzero@raspberrypi:~ $ which python
/usr/bin/python
mkzero@raspberrypi:~ $ which python2
/usr/bin/python2
mkzero@raspberrypi:~ $ which python3
/usr/bin/python3
Reply
#8
Ok sorry to jump around but i decided to move it to rc.local because i remember reading (might be wrong though) that sometimes crontab starts up too fast and scripts arent run because certain "things" arent yet loaded or something along those lines.

So i decided to move it to rc.local and did this plus added a logging bit I found and got this in the txt file:

rc.local:
#added jul20 2023
sudo python /home/mkzero/envirophat2.py > /home/mkzero/logrc.txt 2>&1
exit 0
results:
Traceback (most recent call last):
Quote: File "/home/mkzero/envirophat2.py", line 3, in <module>
import thingspeak
ModuleNotFoundError: No module named 'thingspeak'

which is weird because im pretty sure it DID log results at some point. Of course then I noticed i was using python instead of python3, so i changed the command line to:

#added jul20 2023
sudo python3 /home/mkzero/envirophat2.py > /home/mkzero/logrc.txt 2>&1
exit 0
and the logrc file is now empty but Im still not getting any logged data on the cloud. But when I run the command from the terminal directly using python (and python3) it works, logging data to the cloud & terminal.



(Jul-20-2023, 10:25 PM)marciokoko Wrote: Ok and how do I do that? I believe I have installed both 2.7 and 3. How do I get the path for each?

I know i have 3.9.2 from doing python -V

and this:

mkzero@raspberrypi:~ $ which python
/usr/bin/python
mkzero@raspberrypi:~ $ which python2
/usr/bin/python2
mkzero@raspberrypi:~ $ which python3
/usr/bin/python3
Reply
#9
Hi guys, just wanted to bump this thread because I've made no progress on this situation. For some reason the script isnt running at the start in rc.local but it does run from the terminal using the python command.

Any ideas why that might happen?

Thx
Reply
#10
bump again...help anyone?

I still havent been able to figure this out...
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problem posting image to clipboard noel 0 2,175 Sep-26-2020, 10:50 AM
Last Post: noel
  loop through python pandas data frame Johnse 4 2,878 Sep-02-2019, 01:45 AM
Last Post: Johnse
  module logging posting randomly DJ_Qu 2 2,224 May-14-2019, 06:41 AM
Last Post: DJ_Qu
  Posting zip file on HTTP network using Python2.7 deepti92 1 6,797 May-15-2018, 12:41 PM
Last Post: deepti92
  Need help with posting to API website pasi12 4 4,363 Feb-15-2017, 07:07 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020