Thursday, July 30, 2015

IoT Hackathon Part III : Some enhancements to the sensor example

In my previous post I showed you how to setup a simple weather-station using Raspberry Pi, GroovyPi sensors and Python. It worked very well, but there is definitely room for improvement. In this short post I describe some of these improvements. First you will learn how to start the weather-station when you reboot the Pi. Next I will show you how to create some decent log info.

Autostart
The weather-station works like a charm, but if the Raspi is rebooted, you need to manually restart the python script. That is not the way I want this to work, I want the weather-station start on reboot. Actually this is very simple. The Raspberry Pi uses Linux as OS and thus we can use the crontab to schedule when to start tasks. With the @reboot directive, a task starts on reboot. The only thin we need to do is to add a line to the crontab that tells that we want to start our weather-station on each and every reboot. You can open te crontab for editing by issuing the following command:
 $ sudo crontab -e  
Next you simply add the following line of code to the crontab and you are good to go.
 @reboot python /home/pi/Desktop/Lucs_projects/weatherstation/weatherstation.py &  
The “&” at the end of the line means the command is run in the background and it won’t stop the system booting up. It is as simple as that.

Logging
In my initial setup I used a print statement to send the reading to the console.
 print "temp =", temp, "C\thumadity =", hum,"%"  
However, when the weather-station is started on reboot, instead of from the command prompt, there is no console and there is now way that we can see the data. For that reason I decided to add some logging to the Python script. For this we can use the logging library. This enables you to create a logger, create a loghandler to write info to file and console, and also to add some formatting to your logstatements. All of this is explained in the Python Documentation. So first import the logging library, next create a logger and some loghandlers, optionally add some formatting and your logging is ready to use.
 import logging  
 import datetime
  
 # lets create a logger 
 logger = logging.getLogger('weather.logger')  
 logger.setLevel('DEBUG')

 # create a log handler to log to file   
 file_log_handler = logging.FileHandler('/home/pi/Desktop/Lucs_projects/weatherstation/weather.log')  
 logger.addHandler(file_log_handler)  

 # create a log handler to log to the console
 stderr_log_handler = logging.StreamHandler()  
 logger.addHandler(stderr_log_handler)  

 #now add some formatting (note the import of datetime is required)
 formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')  
 file_log_handler.setFormatter(formatter)  
 stderr_log_handler.setFormatter(formatter)  

 #Now we can write use the logger  
 logger.info("temp ="+ t + "C\thumidity ="+ h + "%")   
When the weather-station is started on reboot, the log statements can be found in 'weather.log'. When you start from command-line, log statements are visible in both, the console and the 'weather.log' file.

Resources
1) Start at reboot
2) Python Logging