SHARE
TWEET

17.07.2016 Python CGI for ESP sensor receiption

a guest Jul 16th, 2016 103 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2.  
  3. import cgi, rrdtool, os
  4. import cgitb; cgitb.enable()  # for troubleshooting
  5.  
  6. print "Content-type: text/html"
  7. print
  8.  
  9. print """
  10. <html>
  11. <head><title>Sensor data</title></head>
  12. <body>
  13. <h3>Sensor data</h3>
  14. """
  15.  
  16. form = cgi.FieldStorage()
  17.  
  18. sensor = form.getvalue("sensor")
  19. temp = form.getvalue("temp")
  20. hum = form.getvalue("hum")
  21. vbat = form.getvalue("vbat")
  22.  
  23. if (sensor == "mobile"):
  24.   tempstring = "N:"+temp+":"+hum+":"+vbat
  25.   tempdata = temp+"\n"+hum+"\n"+vbat+"\n"
  26.   rrdfile = "/var/www/rrd/mobile.rrd"
  27.   tempfile = "/var/www/rrd/temp_mobile.txt"
  28.   print 'Sensor:'+sensor+': '+temp+' deg. C, '+hum+' rh, Voltage '+vbat+' V.<br>'
  29. elif (sensor == "bedroom"):
  30.   tempstring = "N:"+temp+":"+hum
  31.   tempdata = temp+"\n"+hum+"\n"
  32.   rrdfile = "/var/www/rrd/bedroom.rrd"
  33.   tempfile = "/var/www/rrd/temp_bedroom.txt"
  34.   print 'Sensor:'+sensor+': '+temp+' deg. C, '+hum+' rh.<br>'
  35. elif (sensor == "livingroom"):
  36.   tempstring = "N:"+temp+":"+hum
  37.   tempdata = temp+"\n"+hum+"\n"
  38.   rrdfile = "/var/www/rrd/livingroom.rrd"
  39.   tempfile = "/var/www/rrd/temp_livingroom.txt"
  40.   print 'Sensor:'+sensor+': '+temp+' deg. C, '+hum+' rh.<br>'
  41. elif (sensor == "workroom"):
  42.   tempstring = "N:"+temp+":"+hum
  43.   tempdata = temp+"\n"+hum+"\n"
  44.   rrdfile = "/var/www/rrd/workroom.rrd"
  45.   tempfile = "/var/www/rrd/temp_workroom.txt"
  46.   print 'Sensor:'+sensor+': '+temp+' deg. C, '+hum+' rh.<br>'
  47. elif (sensor == "bath"):
  48.   tempstring = "N:"+temp+":"+hum
  49.   tempdata = temp+"\n"+hum+"\n"
  50.   rrdfile = "/var/www/rrd/bath.rrd"
  51.   tempfile = "/var/www/rrd/temp_bath.txt"
  52.   print 'Sensor:'+sensor+': '+temp+' deg. C, '+hum+' rh.<br>'
  53. elif (sensor == "kitchen"):
  54.   tempstring = "N:"+temp+":"+hum
  55.   tempdata = temp+"\n"+hum+"\n"
  56.   rrdfile = "/var/www/rrd/kitchen.rrd"
  57.   tempfile = "/var/www/rrd/temp_kitchen.txt"
  58.   print 'Sensor:'+sensor+': '+temp+' deg. C, '+hum+' rh.<br>'
  59. elif (sensor == "outdoor"):
  60.   tempstring = "N:"+temp+":"+hum
  61.   tempdata = temp+"\n"+hum+"\n"
  62.   rrdfile = "/var/www/rrd/outdoor.rrd"
  63.   tempfile = "/var/www/rrd/temp_outdoor.txt"
  64.   print 'Sensor:'+sensor+': '+temp+' deg. C, '+hum+' rh.<br>'
  65.  
  66. rrdtool.update(rrdfile, tempstring)
  67. tempfile=open(tempfile, "wb")
  68. tempfile.write(tempdata)
  69. tempfile.close()
  70.  
  71. print """
  72. </body>
  73. </html>
  74. """
RAW Paste Data
Top