Hallo Leute,
ich habe folgendes Problem. Ich habe mir einen Bodenfeuchtigkeitssensor aus einem koplanaren Kondensator gebastelt.
Dazu hab ich ein kleinen Codeschnipsel mit wiringPi geschrieben um an den GPIO Pins abzufragen wann der Kondensator geladen ist und die Zeit die er dafür braucht zu messen.
Das ganze funktioniert auch einwandfrei. Das Problem ist aber, dass das ganze nur funktioniert, wenn vorher das selbe programm in python geschrieben laufen lasse.
Ich zeige euch am besten mal die Programme:
#!/usr/bin/python
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
#Define function to measure charge time
def RC_Analog(Pin):
i = 0
counter = 0
while i<10:
#Discharge capacitor
GPIO.setup(Pin, GPIO.OUT)
GPIO.output(Pin, GPIO.LOW)
time.sleep(0.1) #in seconds, suspends execution.
GPIO.setup(Pin, GPIO.IN)
#Count loops until voltage across capacitor reads high on GPIO
while (GPIO.input(Pin)==GPIO.LOW):
counter=counter+1
i += 1
counter /= 10
return counter
#Main program loop
while True:
reading = RC_Analog(25) #store counts in a variable
counter = 0
while (reading < 10.00):
counter = counter + 1
if counter >= 50:
break
if reading < 300:
print('Not enough water for your plants to survive! Please water now.')
else:
print('Your plants are safe and healthy, yay!')
GPIO.cleanup()
file.close()
Alles anzeigen
Und die Funktion in C++ die von einem anderen Programm aufgerufen wird.
double CountChargingTime(int pin)
{
list<double> charging_times;
double charges = 0.0;
using namespace std::chrono;
pinMode(pin, OUTPUT);
for(int i=0 ; i < 20; ++i)
{
pinMode(pin, OUTPUT);
digitalWrite(pin, LOW);
delay(2020);
pinMode(pin, INPUT);
high_resolution_clock::time_point t1 = high_resolution_clock::now();
while(digitalRead(pin) == LOW)
{
}
high_resolution_clock::time_point t2 = high_resolution_clock::now();
duration<double> time_span = duration_cast<duration<double>>(t2-t1);
charging_times.push_back(time_span.count());
}
for(list<double>::iterator it=charging_times.begin(); it!=charging_times.end(); ++it)
charges += *it;
double average= charges/charging_times.size();
return average;
}
Alles anzeigen
das komische daran ist, dass von dem c++ Programm auch andere GPIOs genutzt werden. Die funktionieren allerdings einwandfrei. Es ist immer der Pin vom Sensor der spinnt. Habe auch schon den Pin des Sensors mal geändert. Selbes problem. es klappt wirklich immer erst wenn ich das pythonscript ausgeführt habe. Nach nem reboot geht es dann nicht mehr. Führe natürlich beide immer mit root rechten aus.
Jemand ne Idee?
Gruß
Sc0rpe