Web Service

Should you ever need a webservice that returns the current temperature, try the following script. Most of it was done by Jonathan M. (thank you!) who is another tech enthusiast. The following script will take a reading, on your request, and display it. Before you start, make sure that you have completed the whole tutorial and can take readings and store them in the database.

Depending on which sensor you use, you have to checkout the correct code. If you use DS18B20, then do git clone https://github.com/peterkodermac/Weather-Service.git. If you’re using DHT22 do git clone https://github.com/peterkodermac/Weather-Service.git -b dht22.

Start by installing Flask with sudo easy_install flask. Afterwards, you can test the whole script with sudo python service.py. If all goes well, you can now visit the IP of your Pi with the port and /temp typed afterwards. For example: By default, port 81 is being used, you can change it according to your wishes. Same goes for “temp”, change it to something you like. It should display the temperature and if you’re using DHT22 temperature and humidity.

Now that you got that out of the way, you need to have this script run at startup. The best thing is to make it run as a service that will autostart when your Pi boots. Start by installing Supervisor, with sudo apt install supervisor. In the XY you will have a file called wService.conf. It tells Supervisor how to start your script. Make sure that it points to the right path, where your service.py script is located. Copy the config file with sudo cp wService.conf /etc/supervisor/conf.d. To finish this part, you need to sudo supervisorctl update and then sudo supervisorctl restart WeatherService. By now the script should be running on your Pi! Take a look and just to be sure reboot your Pi and double check everything.

This script is a nice starting point and can be changed according to your needs and wishes. How exactly you use it, is entirely up to you. Remember, if you want to have access from outside of your local network, make sure that you correctly forward the port to your Pi.

Skip to content