Help needed on a sketch...
-
Right, 1800000 = 30min, sorry.
-
@rvendrame Hi, It seems to work now... THANKS! Another question, I'm trying to measure the level of a 9v battery in the same sketch as well, using this:
Insert Code Here ``#include <SPI.h> #include <MySensor.h> #define CHILD_ID_LIGHT 0 #define LIGHT_SENSOR_ANALOG_PIN 1 #define BATTERY_SENSE_PIN A1 // select the input pin for the battery sense point #define MIN 300 // Liquid #define MAX 700 // Air int sensorPin = A0; // select the input pin for the potentiometer int sensorValue = 0; // variable to store the value coming from the sensor boolean metric = true; MySensor gw; MyMessage msg(CHILD_ID_LIGHT, V_LIGHT_LEVEL); int lastLightLevel; int oldBatteryPcnt = 0; int battLoop =0; void setup(){ Serial.begin(9600); gw.begin(); // Send the sketch version information to the gateway and Controller gw.sendSketchInfo("Mobile1", "1.16"); // Register all sensors to gateway (they will be created as child devices) gw.present(CHILD_ID_LIGHT, S_LIGHT_LEVEL); } void loop(){ // read the value from the sensor: sensorValue = analogRead(sensorPin); delay(1000); int lastsensorValue = sensorValue; Serial.println(sensorValue); int sendValue = map(sensorValue, 1023, 250, 0, 100); gw.send( msg.set( sendValue ) ); // get the battery Voltage int batteryValue = analogRead(BATTERY_SENSE_PIN); Serial.println(batteryValue); // 1M, 470K divider across battery and using internal ADC ref of 1.1V // Sense point is bypassed with 0.1 uF cap to reduce noise at that point // ((1e6+470e3)/470e3)*1.1 = Vmax = 3.44 Volts // 3.44/1023 = Volts per bit = 0.003363075 // vout = (sensorvalue x 3.3) / 1023 //vin = vout / (R2/(R1+R2)); float batteryV = ( batteryValue * 0.0088 ) / 0.8; // divide R2/(R1+R2) int batteryPcnt = batteryValue / 10 ; if ( batteryPcnt > 100 ) batteryPcnt = 100; Serial.print("Battery Voltage: "); Serial.print(batteryV); Serial.println(" V"); Serial.print("Battery percent: "); Serial.print(batteryPcnt); Serial.println(" %"); if (oldBatteryPcnt != batteryPcnt) { // Power up radio after sleep gw.sendBatteryLevel(batteryPcnt); oldBatteryPcnt = batteryPcnt; gw.sleep(300000); }Using this sketch causes the battery check into some kind of loop and not sending it back correctly to domoticz. Log shows:
Battery Voltage: 1.68 V
Battery percent: 15 %
384
send: 1-1-0-0 s=0,c=1,t=23,pt=2,l=2,st=ok:82
153
Battery Voltage: 1.68 V
Battery percent: 15 %
385
send: 1-1-0-0 s=0,c=1,t=23,pt=2,l=2,st=ok:82
153
Battery Voltage: 1.68 V
Battery percent: 15 %
384Any ideas on this?
-
Just put the gw.sleep out of the if {}
-
Ok, I'm almost where I want to be... Feeding my pro tinket 5v on a 9v battery I've managed to get the correct voltage reading. Which is aprox 9.10v on a new battery. The only thing I can't get to work is the % which I need to send to Domoticz .
I'm using this code, being pretty sure that the formula is correct but whatever I try, the battery % is shown as 0 on every voltage level. Am I missing something... ?
// number of analog samples to take per reading #define NUM_SAMPLES 10 int sum = 0; // sum of samples taken unsigned char sample_count = 0; // current sample number float voltage = 0.0; // calculated voltage #include <SPI.h> #include <MySensor.h> #define CHILD_ID_LIGHT 0 #define LIGHT_SENSOR_ANALOG_PIN 1 #define BATTERY_SENSE_PIN A1 // select the input pin for the battery sense point #define MIN 300 // Liquid #define MAX 700 // Air // number of analog samples to take per reading #define NUM_SAMPLES 10 int sensorPin = A0; // select the input pin for the potentiometer int sensorValue = 0; // variable to store the value coming from the sensor MySensor gw; MyMessage msg(CHILD_ID_LIGHT, V_LIGHT_LEVEL); void setup(){ Serial.begin(9600); gw.begin(); // Send the sketch version information to the gateway and Controller gw.sendSketchInfo("Mobile1", "1.16"); // Register all sensors to gateway (they will be created as child devices) gw.present(CHILD_ID_LIGHT, S_LIGHT_LEVEL); } void loop(){ // read the value from the sensor: sensorValue = analogRead(sensorPin); delay(1000); int lastsensorValue = sensorValue; Serial.println(sensorValue); int sendValue = map(sensorValue, 1023, 250, 0, 100); gw.send( msg.set( sendValue ) ); gw.sleep(3000); // take a number of analog samples and add them up while (sample_count < NUM_SAMPLES) { sum += analogRead(A1); sample_count++; delay(10); } // calculate the voltage // use 5.0 for a 5.0V ADC reference voltage // 5.015V is the calibrated reference voltage voltage = ((float)sum / (float)NUM_SAMPLES * 4.92) / 1024.0; int Vmax = 9.10; int Vmin =4.92; int spanning = voltage * 11.714; int batteryPcnt = ((spanning-Vmin)/(Vmax-Vmin))*100; if ( batteryPcnt > 100 ) batteryPcnt = 100; // send voltage for display on Serial Monitor // voltage multiplied by 11 when using voltage divider that // divides by 11. 11.132 is the calibrated voltage divide // value Serial.print(voltage * 11.714); Serial.println (" V"); sample_count = 0; sum = 0; Serial.print("Battery percent: "); Serial.print(batteryPcnt); Serial.println(" %"); } -
Did you try to define batteryPcnt as 'float' instead 'int'?
-
it means gw.send() only accepts a integer --- So you need to set batteryPcnt back to int.
-
I use this:
// Report battery level to controller gw.sendBatteryLevel( bat_level ); -
Hi there,
I'm having another sketch problem.. In order to try and save battery power on my moisture sensors I'm trying to use a IO pin as power by setting it high for some time, let the sensor take a measurement, send the results to the gateway, setting the IO pin low again, let the node go to sleep for some time .
This works only one time, the node presents itself to the gateway, sends data but after that it's not seen again after the expected sleep time.
My guess is that somehow after puting the IO pin low it doesn't come back up in a high state. The strange thing is that when I measure the current on the pins of my moisture sensor with a multimeter the current goes from 0 to aprox 4.27V in the given time, so actually that indicates the pin is going from high to low as needed, but somehow the radio doesn't come back up... This is my code:#include <SPI.h> #include <MySensor.h> #define CHILD_ID_LIGHT 0 #define LIGHT_SENSOR_ANALOG_PIN 1 #define MIN 300 // Liquid #define MAX 700 // Air int sensorPin = A0; // select the input pin for the potentiometer int sensorValue = 0; // variable to store the value coming from the sensor int power =4; MySensor gw; MyMessage msg(CHILD_ID_LIGHT, V_HUM); int lastLightLevel; void setup(){ pinMode(power, OUTPUT); digitalWrite(power, HIGH); delay(3000); Serial.begin(9600); gw.begin(); // Send the sketch version information to the gateway and Controller gw.sendSketchInfo("Mobile Sensor 1", "Kruiden"); // Register all sensors to gateway (they will be created as child devices) gw.present(CHILD_ID_LIGHT, S_HUM); delay (2000); } void loop(){ // read the value from the sensor: digitalWrite(power, HIGH); delay (5000); sensorValue = analogRead(sensorPin); delay(1000); int lastsensorValue = sensorValue; Serial.println(sensorValue); int sendValue = map(sensorValue, 0, 842, 0, 100); gw.send( msg.set( sendValue ) ); delay(10000); digitalWrite(power, LOW); delay (2000); gw.sleep(60000); }Does anyone have any clue..?