element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • 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 Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • 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
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • 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
STEM Academy
  • Learn
  • Learning Center
  • STEM Academy
  • More
  • Cancel
STEM Academy
Files Raspberry Pi Temperature Logger Using gspread
  • Blog
  • Forum
  • Documents
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join STEM Academy to participate - click to join for free!
Actions
  • Next
  • Previous
  • Share
  • More
  • Cancel
Related
Recommended

Raspberry Pi Temperature Logger Using gspread

This is a set of instructions for a temperature logger using the Raspberry Pi.  It uses gspread, a python library to take data from the temperature probe and report it out to a google spreadsheet.  I found this to be incredibly helpful, and an easy way to log lots of information in a place that is easy to access and easy to download.

 

This is good for now, but the next step is to build a client website that will start and stop the temperature probe, open and title a new spreadsheet, and display the current temperature as well as a graph of the temperature over time.  I think that should be pretty straightforward, but its all things I don't know how to do.  It just seems to me like it is something I can learn to do, which is why I love this stuff overall.

 

Enjoy!

  • stem space
mikedavis
mikedavis
  • 28 Jul 2014
  • 8 Downloads
  • Share
  • More
  • Cancel
  • Sign in to reply
Parents
  • Former Member
    Former Member over 10 years ago

    Thank you @Mike Davis. I am using an Arduino to control the temperature of a tub of water via a fish tank heater (going to lager me some beer this winter!!) and this document helped me take data I was pulling from Arduino onto the Raspberry Pi and get onto gspread. Works great! The only comment I would make is that gspread may be best initiated by using cron on the raspberry pi. I was using it in an infinite loop, but occasionally and at random times, the gspread login function would cause my code to get stuck and not do anything until I used CTRL-C. Using cron and scheduling the code to execute every five minutes, it only missed measurements once or twice in a 24 hour period.Thank you!

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

    I am glad that worked for you Charles!  Can you share how put gspread in cron?  I think that is a really elegant solution.  I had repeated problems of missing measurements, and I would like to use it for other things more reliably.  Thank you!

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

    When I find some time I'm hoping to put together a blog post of the project, but my python code is somewhat similar to temp,py, except (1) I have a try/except statement for the gspread.login command where if it tries to log in and if it fails, it'll just log an error message and (2) There is no infinite loop, it tries to log the data only once.

     

    I originally had an infinite loop with time.sleep since I had hoped if gspread had failed to log in, it would just log the error and keep going. However, I notice sometimes that gspread got hung up on an infinite loop, so the try/except statement didn't work because it was still waiting on gspread to succeed or fail in logging in.

     

    So instead, you can use Crontab as follows:

     

        (1) in the command line, type:

     

                                  sudo crontab -e

     

        (2) Crontab provides a description of the format to put in the file. If you want to run the program when your pi boots, which I did so I could tell that it successfully worked right after I turned on the program, you can type:

     

                                  @reboot /bin/sleep 10 ; python /home/pi/subdirectory/temp.py &

     

    The sleep portion is to ensure that the pi is fully loaded and connected before trying to run the program. Then the second portion is the command to run the program.

     

       (3) In Crontab, you schedule what time you want the program to execute. The first value is minutes and I wanted this program to run every five minutes, so I typed in:

     

                                  0 * * * * python /home/pi/subdirectory/temp.py

                                  5 * * * * python /home/pi/subdirectory/temp.py

     

    and so on to account for every five minutes in an hour. You can schedule minutes, hours, days, or only certain dates if you want.

     

         (4) I was concerned about attempting to run the program if it was already stuck in an infinite loop due to gspread. This concern may be unfounded, but I also gave crontab a command to kill the program (which will only work if its running) one minute before trying to run it again. So my resulting code looked like:

     

                                  @reboot /bin/sleep 10 ; python /home/pi/lager/temp.py &

     

                            59 * * * * pkill /home/pi/lager/temp.py

                                  0 * * * * python /home/pi/lager/temp.py

     

                                  4 * * * * pkill /home/pi/lager/temp.py

                                  5 * * * * python /home/pi/lager/temp.py

     

    And so on and so forth. I am hoping that this would completely get rid of any infinite loops gspread may cause. So far this has worked great. In a 24 hour period it may have only missed 2 or 3 measurements, which for my purposes is not a big deal. Hope this helps!

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

    When I find some time I'm hoping to put together a blog post of the project, but my python code is somewhat similar to temp,py, except (1) I have a try/except statement for the gspread.login command where if it tries to log in and if it fails, it'll just log an error message and (2) There is no infinite loop, it tries to log the data only once.

     

    I originally had an infinite loop with time.sleep since I had hoped if gspread had failed to log in, it would just log the error and keep going. However, I notice sometimes that gspread got hung up on an infinite loop, so the try/except statement didn't work because it was still waiting on gspread to succeed or fail in logging in.

     

    So instead, you can use Crontab as follows:

     

        (1) in the command line, type:

     

                                  sudo crontab -e

     

        (2) Crontab provides a description of the format to put in the file. If you want to run the program when your pi boots, which I did so I could tell that it successfully worked right after I turned on the program, you can type:

     

                                  @reboot /bin/sleep 10 ; python /home/pi/subdirectory/temp.py &

     

    The sleep portion is to ensure that the pi is fully loaded and connected before trying to run the program. Then the second portion is the command to run the program.

     

       (3) In Crontab, you schedule what time you want the program to execute. The first value is minutes and I wanted this program to run every five minutes, so I typed in:

     

                                  0 * * * * python /home/pi/subdirectory/temp.py

                                  5 * * * * python /home/pi/subdirectory/temp.py

     

    and so on to account for every five minutes in an hour. You can schedule minutes, hours, days, or only certain dates if you want.

     

         (4) I was concerned about attempting to run the program if it was already stuck in an infinite loop due to gspread. This concern may be unfounded, but I also gave crontab a command to kill the program (which will only work if its running) one minute before trying to run it again. So my resulting code looked like:

     

                                  @reboot /bin/sleep 10 ; python /home/pi/lager/temp.py &

     

                            59 * * * * pkill /home/pi/lager/temp.py

                                  0 * * * * python /home/pi/lager/temp.py

     

                                  4 * * * * pkill /home/pi/lager/temp.py

                                  5 * * * * python /home/pi/lager/temp.py

     

    And so on and so forth. I am hoping that this would completely get rid of any infinite loops gspread may cause. So far this has worked great. In a 24 hour period it may have only missed 2 or 3 measurements, which for my purposes is not a big deal. Hope this helps!

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

    Hey, this looks really good.  I would like to implement it for a training I am doing soon.  Would you have a link to your python code?  I am not very good at coding yet, so I always have to start from someone else's example and go from there.  Thanks!

    • 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