I am working on a Raspberry pi and creating a site for our Greenhouse. I am using temperature sensors to read temperatures in various places in our main greenhouse and have created several webpages that display gauges showing those temperatures every 1 minute.
When I add a new sensor to the greenhouse I add that same thing to the database. I am using GroceryCRUD for this and use default values for the gauges but each temp sensor has a unique value and a unique folder that gets created on the pi.
I am using a Python script to read the data and insert it into a mysql database then a Python script selects the last entry and displays it to a php file with all the gauges. The gauges page creates the count of gauges based on what is in the sensors table. This is all working great with a few things that needs to be coded better.
The database has another table called relays which is relational to sensors so I can get what relays go to what sensors.
The next thing I am working on is a Python script (or it can be any other language that will work) that reads those sensors and does something to open and close the relays that are connected to the pi based on the readings.
Is there a way to create a config file on the fly based on what there is in the sensors and relays table?
I want to be able to have the config file have the sensor code and GPIO pin number in it but if I add or delete a sensor in the sensors table it will add or delete that info in the config file.
Here is an example....
temp_sensor1 = '/sys/bus/w1/devices/28-000007299ce9/w1_slave'
temp_sensor2 = '/sys/bus/w1/devices/28-0000072963c5/w1_slave'
temp_sensor3 = '/sys/bus/w1/devices/28-000007299cg5/w1_slave'
When I add a new sensor to our greenhouse I need to hook it up to the pi and get the new code that starts with 28-00000 and enter it into the database as well as what relays are needed and the GPIO pin they get connected to and put that into the database. Then go into the python script and add the new temp_sensor and path or delete one.
I have tried to use this to create the temp_sensor variable above.
x=1
tempsensor = 'temp_sensor'
try:
cursor.execute(sql)
rows = cursor.fetchone()
for row in rows:
sensorcode = row
tempsensor2 = str(tempsensor) + str(x)
test = " = '/sys/bus/w1/devices/%s/w1_slave'" % sensorcode
test2 = str(tempsensor2) + str(test)
use(test2)
x = x + 1
except:
print "Error: unable to fecth data"
# disconnect from server
db.close()
When I run the script I get the correct values to print out if I put a print command at the end to see test2 but I need that to be a variable in the script and now it is not.
I would like to have a script read the sensors table then create/write a new config file with the correct temp_sensor variables as well as create/write new functions based on the 3 sensors or how many sensors there are so I do not have to always go back inside the code and make these changes.
I could also use a script to put these values in a database table and then write to a file the new config file but not sure if that would work so I am asking what would be a good way to do this using ether perl, python, php and mysql.