doushang1880 2015-12-30 03:03
浏览 73

动态创建配置文件

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.

  • 写回答

1条回答 默认 最新

  • dongmu1951 2015-12-31 04:15
    关注

    I found the answer by creating a python script that looks up everything in my tables and then writes a new file using f.write. So I create a new config file each time the database table I am working on gets updated, added to or deleted.

    Now I need a way to have it done automatically after the table gets changed. I have to either click on a button or find a way that can monitor a table. That is next!

    评论

报告相同问题?

悬赏问题

  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 正弦信号发生器串并联电路电阻无法保持同步怎么办
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)