For my indoor garden I wanted to monitor the temperature inside and outside the tent. It is in the basement, which I don’t think ever gets above 70 or below 50, but to control the plant’s cycles I need to control light and temp. The light came with an App so I just needed to get temp data into my database. Here is the code I’m using at the moment. I’d also like to log outside temperature data, but I haven’t figured that out yet. I’m think there has got to be an API for the NWS that I can put in a zip code and get the temp and humidity. Here is the code, and some notes.
We need to get some libraries and connect to the sensors.
I have two functions. One to get the data from the sensors, and one to insert the data into the database.
Where the magic happens
and some cleanup
So I calculated the space needed by putting 10 readings in the db, and if the average holds(which it should only go down) I will use about 840 MB a year to get a reading every minute, which I don’t really think I would need, so I’ll probably lower it to once an hour maybe later.
The starting point for this project is the Kasa powerstrip I posted about, like a month ago. I’m trying o code up something to log power levels directly to a mysql db.
Both seem to have good documentation on how to use them.
The kasa library requires asyncio, which I haven’t really messed with async programing before, so I get to learn some new concepts. Though what little I know tells me it’s just a way to keep a connection open while waiting on the other end to respond.
asyncio is a library to write concurrent code using the async/await syntax.
asyncio is used as a foundation for multiple Python asynchronous frameworks that provide high-performance network and web-servers, database connection libraries, distributed task queues, etc.
I’ve slapped together this to print the data I want with python
import asyncio
from kasa import Discover
async def main():
dev = await Discover.discover_single("powerstrip.lan")
await dev.update()
# possible features:
# state
# rssi
# on_since
# reboot
# led
# cloud_connection
# current_consumption
# consumption_today
# consumption_this_month
# consumption_total
# voltage
# current
state = dev.features.get("state")
voltage = dev.features.get("voltage")
current = dev.features.get("current")
current_consumption = dev.features.get("current_consumption")
consumption_this_month = dev.features.get("consumption_this_month")
consumption_today = dev.features.get("consumption_today")
# open file and write values
f = open("power.txt", "w")
f.write(f'Power On: {state.value}<br>{voltage.value} V {current.value} A {current_consumption.value} W<br>Today: {consumption_today.value} kwh<br>This Month: {consumption_this_month.value} kwh')
if __name__ == "__main__":
asyncio.run(main())
Which gives me this in WordPress:
Not directly of course, I haven’t gotten that far yet. For now, the python writes a text file with HTML tags for display. I have an hourly cron job that uses scp to copy the file over to /var/www, and then some php in my functions.php file to load the file, and display it as a wordpress shortcode.
The stupid WordPress part took the longest, but I understand the layout of wordpress a little better. It has been around and popular for so long, that it is easy finding well written documentation, but I’ve found that a lot of it is outdated. It is very annoying to read through something, try it out, and then find your error is because you’re trying to use a deprecated feature.
The vision is a cron job that runs a python script to import data directly into the DB, and then php pulls the data from the DB, but the next step is to just get something into to DB.
I impulse-bought 2 of these because it seemed like exactly what I needed for a couple projects. 6 individually controlled power outlets, and three always on USB ports. Each port monitors voltage and amps. And I even found a Python library that controls it and all the other Kasa smart devices I own. 😍
# commented out code is example code... for now
import asyncio
from kasa import Discover
async def main():
# dev = await Discover.discover_single("127.0.0.1",username="un@example.com",password="pw")
# me no think me need login
dev = await Discover.discover_single("192.168.1.230")
# await dev.turn_on()
# await dev.update()
# me just want Info for now
devInfo = dev.hw_info
print(type(devInfo))
print(devInfo)
if __name__ == "__main__":
asyncio.run(main())