I am playing with the RPi and setting up some monitors. I have the DHT22 running fine and reporting the values properly to the SQL database.
I am setting up a light/LUX sensor and I need a little help with the coding.
I have the code that reads the DHT22 and writes the output to the database.
I also have code that reads the light sensor and prints the output value to the screen every 1.0 seconds.
Both are working properly
What I need, is code that will read the light sensor and write the value to the database instead of printing it to the screen.
Here is the working code that reads the DHT22 and writes the output to the database...
<php>
<?php function readSensor($sensor) { $output = array(); $return_var = 0; $i=1; exec('sudo /usr/local/bin/loldht '.$sensor, $output, $return_var); while (substr($output[$i],0,1)!="H") { $i++; } $humid=substr($output[$i],11,5); $temp=substr($output[$i],33,5); $db = mysql_connect("localhost","datalogger","datalogger") or die("DB Connect error"); mysql_select_db("datalogger"); $q = "INSERT INTO datalogger VALUES (now(), $sensor, '$temp', '$humid',0)"; mysql_query($q); mysql_close($db); return; } readSensor(9); readSensor(8); readSensor(7); ?>
</php>
Here is the code that reads the light sensor and prints to screen..
<code>
#!/usr/local/bin/python import RPi.GPIO as GPIO import time __author__ = 'Gus (Adapted from Adafruit)' __license__ = "GPL" __maintainer__ = "pimylifeup.com" GPIO.setmode(GPIO.BOARD) #define the pin that goes to the circuit---CHANGE THIS TO 11 pin_to_circuit = 7 def rc_time (pin_to_circuit): count = 0 #Output on the pin for GPIO.setup(pin_to_circuit, GPIO.OUT) GPIutput(pin_to_circuit, GPIO.LOW) time.sleep(0.1) #Change the pin back to input GPIO.setup(pin_to_circuit, GPIO.IN) #Count until the pin goes high while (GPIO.input(pin_to_circuit) == GPIO.LOW): count += 1 return count #Catch when script is interupted, cleanup correctly try: # Main loop while True: print rc_time(pin_to_circuit) except KeyboardInterrupt: pass finally: GPIO.cleanup()</code>
Can someone merge the applicable parts of these together to read the light sensor and write the output to the database?
Thanks for any assistance.
EDIT:::: Hmmmm code tags aren't working. Only attachments are images. I attached images of the code LOL Sorry.
I am setting up a light/LUX sensor and I need a little help with the coding.
I have the code that reads the DHT22 and writes the output to the database.
I also have code that reads the light sensor and prints the output value to the screen every 1.0 seconds.
Both are working properly
What I need, is code that will read the light sensor and write the value to the database instead of printing it to the screen.
Here is the working code that reads the DHT22 and writes the output to the database...
<php>
<?php function readSensor($sensor) { $output = array(); $return_var = 0; $i=1; exec('sudo /usr/local/bin/loldht '.$sensor, $output, $return_var); while (substr($output[$i],0,1)!="H") { $i++; } $humid=substr($output[$i],11,5); $temp=substr($output[$i],33,5); $db = mysql_connect("localhost","datalogger","datalogger") or die("DB Connect error"); mysql_select_db("datalogger"); $q = "INSERT INTO datalogger VALUES (now(), $sensor, '$temp', '$humid',0)"; mysql_query($q); mysql_close($db); return; } readSensor(9); readSensor(8); readSensor(7); ?>
</php>
Here is the code that reads the light sensor and prints to screen..
<code>
#!/usr/local/bin/python import RPi.GPIO as GPIO import time __author__ = 'Gus (Adapted from Adafruit)' __license__ = "GPL" __maintainer__ = "pimylifeup.com" GPIO.setmode(GPIO.BOARD) #define the pin that goes to the circuit---CHANGE THIS TO 11 pin_to_circuit = 7 def rc_time (pin_to_circuit): count = 0 #Output on the pin for GPIO.setup(pin_to_circuit, GPIO.OUT) GPIutput(pin_to_circuit, GPIO.LOW) time.sleep(0.1) #Change the pin back to input GPIO.setup(pin_to_circuit, GPIO.IN) #Count until the pin goes high while (GPIO.input(pin_to_circuit) == GPIO.LOW): count += 1 return count #Catch when script is interupted, cleanup correctly try: # Main loop while True: print rc_time(pin_to_circuit) except KeyboardInterrupt: pass finally: GPIO.cleanup()</code>
Can someone merge the applicable parts of these together to read the light sensor and write the output to the database?
Thanks for any assistance.
EDIT:::: Hmmmm code tags aren't working. Only attachments are images. I attached images of the code LOL Sorry.