Pyth0n implementation ov reading ein DS18B20 temperature sens0r
|
11 months ago | |
---|---|---|
.editorconfig | 11 months ago | |
.gitignore | 2 years ago | |
README.md | 2 years ago | |
muhconf.ini.example | 2 years ago | |
temps.py | 2 years ago |
This shit is a qt lil Pyth0n implementation ov reading ein DS18B20
temperature sens0r, the values of which get stored in a MySQL/MariaDB database somewhere. =]]]
It will run 5 samples (with a required 1 second delay between em) and get the average of that to store in the DB. It also supports multiple sensors but it will only store the first average. xd
You prolly need to install a few Pythinz modules cuz they may not be available by default. I'll just list everything in use:
argparse
ConfigParser
datetime
json
MySQLdb
onesignal
os
requests
sys
time
var_dump
(for printing object properties etc ;])Copy muhconf.ini.example
to muhconf.ini
and fire up een editor. All the options are self-explanatory or explained in there so git to reading fam. The config file must be kept in the same dir as the skrip.
Then simply slam a new entry in your crontab
to make it run periodically (the skrip is set up to read/store once, then quit). You should be able to run it as any non-root user, since the sysfs
path belonging to the sensor data (e.g. /sys/bus/w1/devices/28-02155265b0ff/w1_slave
) should be world-readable. My cr0n looks leik dis:
*/2 * * * * kill -9 $(ps aux | grep '[p]ython.*temps.py' | grep -v '/bin/sh' | awk '{ print $2 }') >/dev/null 2>&1 ; /usr/bin/python /home/hecks/temp_sensor/temps.py
As you can see I have it set to run every 2 minutes, faster is not really necessary and would only clutter the DB. The kill
part is in case a previous instance is hanging on something for some reason, and should usually fail (hence the redirections to /dev/null
;]). To me this only really happens when my internetz connection is being gay and takes 2+ minutes to set up a usable MySQL connection. This logic is not present in the code itself because of the "set up to read/store once, then quit" reasoning. ;];];];] It would be p hard to account for anyways, since the TCP handshake has been completed and there are packets flowing, just many get dropped and have to be resent.
ez pz ;]
You can just simply run ./temps.py
obviously, but in case you need to troubleshoot something you can use ./temps.py --debug
to force debug mode without having to touch the config. [=[=[[=[==[=[=[
Or just do ./temps.py -h
and check the built-in halp for more inf0z. ;]
So this thing also supports sending a notification when the outside temperature gets below the indoor one. I maed dis cuz I have an underpowered AC unit and if the outdoors temp is lower I can open el windows to make it cool off faster inside. =]] This also allows the air pressure to equalise again. The same is true for the other way around; when the outside temperature gets higher I should closem wind0ze, so it will also notify about that.
For checkin' em outdoors temperature I'm using an API from OpenWeatherMap, which as of writing this shit is free for 60 calls/minute up to 1 million/month. Should be more than enough. =]
id
variable)weathermon
section in the config file and update the enable
, weather_key
and city_id
fieldsweather_url
too, if there's a newer version of the API (it may not work with our skrip though)OWP supports Celsius, Fahrenheit and Kelvin as temperature units, but the sensor always uses Celsius afaik so we'll force OWP to work with that too, deal w/ it. ;]
Now, in order to actually receive the notifications I made a qt lil Andr0id app to receive push notifications from multiple sources. =] Just refer to that shit to read how to set em up and make the necessary config changes for temps.py
. ;];];];]