Count car-starts
-
Solution 1 should be easy to implement by replacing your endless while loops with sending until you get an ack from the controller.
An alternative solution could be to fetch the current time from the controller at startup, and compare the current time to the last time a report was sent. If the time was less than 5 minutes ago, don't do anything. If the time is more than 5 minutes ago, store current time in the eeprom, increment the number of starts and send the new value to the controller.
@mfalkvidd said:
... store current time in the eeprom...How can I store timestamp to EEPROM, it can only handle a value maximum 256 or?
-
@mfalkvidd said:
... store current time in the eeprom...How can I store timestamp to EEPROM, it can only handle a value maximum 256 or?
-
@flopp correct. The value needs to be split into four eeprom "slots"
See http://playground.arduino.cc/Code/EEPROMReadWriteLong for info on how to do that.
@mfalkvidd
thanks -
New version 1.1
// Made by Daniel Nilsson // Tested with Domoticz 2.4440 // 2016-03-12 #include <SPI.h> #include <MySensor.h> #define CHILD_ID 0 // Id of the sensor child #define NODE_ID AUTO // a number or AUTO to let controller assign #define SKETCH_NAME "Car start counter" // Change to a fancy name you like #define SKETCH_VERSION "1.1" // Your version int Controller; // Current start counts from Controller, like Domoticz boolean pcReceived = false; // If we have recieved the start counts from Controller or not int starts; // summary of all starts to be sent to Controller int eeprom; // start counts read from/to be stored in EEPROM MySensor gw; MyMessage volumeMsg(CHILD_ID,V_RAIN); MyMessage lastCounterMsg(CHILD_ID,V_VAR1); void setup() { delay(500); // wait for radio delay(2*60000); // Allow time if USB/cigarett plug is powered before you turned the key //Begin gw.begin(incomingMessage, NODE_ID, false); // Send the Sketch Version Information to the Gateway gw.sendSketchInfo(SKETCH_NAME, SKETCH_VERSION); // Register this device as Rain sensor (will not show in Domoticz until first value arrives) gw.present(CHILD_ID, S_RAIN); Serial.println(""); eeprom = gw.loadState(0); // read EEPROM Serial.print(eeprom); // print EEPROM Serial.println(" starts have not been sent"); Serial.println("add 1 start"); Serial.print(eeprom); Serial.print("+1="); eeprom = eeprom + 1; Serial.println(eeprom); gw.saveState(0,eeprom); // store to EEPROM at position 0 Serial.println(""); Serial.println("Startup completed"); } void loop() { //gw.process(); //See if we have the start counts from Controller - and ask for it if we dont. if (!pcReceived) { Serial.println("Request start counts"); gw.request(CHILD_ID, V_VAR1); //gw.process(); gw.wait(5000); return; } Serial.println(""); eeprom = gw.loadState(0); // read EEPROM Serial.print(eeprom); Serial.println(" starts have not been sent"); Serial.print(Controller); Serial.println(" starts from Controller = "); starts = Controller + eeprom; // total starts Serial.print(eeprom); Serial.print("+"); Serial.print(Controller); Serial.print("="); Serial.println(starts); Serial.print("Send "); Serial.print(starts); Serial.println(" to Controller"); Serial.println(""); resend((volumeMsg.set(starts)), 5); //gw.send(volumeMsg.set(starts)); resend((lastCounterMsg.set(starts)), 5); //gw.send(lastCounterMsg.set(starts)); gw.wait(1000); Serial.println(""); Serial.println("store 0 to EEPROM"); gw.saveState(0,0); // set 0 start to EEPROM, all have been sent Serial.println("sleep"); // mission accomplished while(1){} } // check if "st:fail" during gw.send, thanks n3ro void resend(MyMessage &msg, int repeats) { int repeat = 1; boolean sendOK = false; int repeatdelay = 2000; while ((sendOK == false) and (repeat < repeats)) { if (gw.send(msg)) { sendOK = true; } else { sendOK = false; Serial.print("Error "); Serial.println(repeat); } repeat++; delay(repeatdelay); } if (sendOK == false && repeat == repeats){ loop(); } } //Read if we have a incoming message. void incomingMessage(const MyMessage &message) { if (message.type==V_VAR1) { Controller = message.getULong(); pcReceived = true; Serial.print("Received start counts from Controller: "); Serial.println(Controller); } }thanks to @n3ro for st:fail and @sundberg84 for the hint
@mfalkvidd maybe next version will have timestamp from Domoticz, i am still waiting for an answer if I can send date and time to database. As you can see I have removed while-loop until it successfully sent data to Controller -
New version 1.1
// Made by Daniel Nilsson // Tested with Domoticz 2.4440 // 2016-03-12 #include <SPI.h> #include <MySensor.h> #define CHILD_ID 0 // Id of the sensor child #define NODE_ID AUTO // a number or AUTO to let controller assign #define SKETCH_NAME "Car start counter" // Change to a fancy name you like #define SKETCH_VERSION "1.1" // Your version int Controller; // Current start counts from Controller, like Domoticz boolean pcReceived = false; // If we have recieved the start counts from Controller or not int starts; // summary of all starts to be sent to Controller int eeprom; // start counts read from/to be stored in EEPROM MySensor gw; MyMessage volumeMsg(CHILD_ID,V_RAIN); MyMessage lastCounterMsg(CHILD_ID,V_VAR1); void setup() { delay(500); // wait for radio delay(2*60000); // Allow time if USB/cigarett plug is powered before you turned the key //Begin gw.begin(incomingMessage, NODE_ID, false); // Send the Sketch Version Information to the Gateway gw.sendSketchInfo(SKETCH_NAME, SKETCH_VERSION); // Register this device as Rain sensor (will not show in Domoticz until first value arrives) gw.present(CHILD_ID, S_RAIN); Serial.println(""); eeprom = gw.loadState(0); // read EEPROM Serial.print(eeprom); // print EEPROM Serial.println(" starts have not been sent"); Serial.println("add 1 start"); Serial.print(eeprom); Serial.print("+1="); eeprom = eeprom + 1; Serial.println(eeprom); gw.saveState(0,eeprom); // store to EEPROM at position 0 Serial.println(""); Serial.println("Startup completed"); } void loop() { //gw.process(); //See if we have the start counts from Controller - and ask for it if we dont. if (!pcReceived) { Serial.println("Request start counts"); gw.request(CHILD_ID, V_VAR1); //gw.process(); gw.wait(5000); return; } Serial.println(""); eeprom = gw.loadState(0); // read EEPROM Serial.print(eeprom); Serial.println(" starts have not been sent"); Serial.print(Controller); Serial.println(" starts from Controller = "); starts = Controller + eeprom; // total starts Serial.print(eeprom); Serial.print("+"); Serial.print(Controller); Serial.print("="); Serial.println(starts); Serial.print("Send "); Serial.print(starts); Serial.println(" to Controller"); Serial.println(""); resend((volumeMsg.set(starts)), 5); //gw.send(volumeMsg.set(starts)); resend((lastCounterMsg.set(starts)), 5); //gw.send(lastCounterMsg.set(starts)); gw.wait(1000); Serial.println(""); Serial.println("store 0 to EEPROM"); gw.saveState(0,0); // set 0 start to EEPROM, all have been sent Serial.println("sleep"); // mission accomplished while(1){} } // check if "st:fail" during gw.send, thanks n3ro void resend(MyMessage &msg, int repeats) { int repeat = 1; boolean sendOK = false; int repeatdelay = 2000; while ((sendOK == false) and (repeat < repeats)) { if (gw.send(msg)) { sendOK = true; } else { sendOK = false; Serial.print("Error "); Serial.println(repeat); } repeat++; delay(repeatdelay); } if (sendOK == false && repeat == repeats){ loop(); } } //Read if we have a incoming message. void incomingMessage(const MyMessage &message) { if (message.type==V_VAR1) { Controller = message.getULong(); pcReceived = true; Serial.print("Received start counts from Controller: "); Serial.println(Controller); } }thanks to @n3ro for st:fail and @sundberg84 for the hint
@mfalkvidd maybe next version will have timestamp from Domoticz, i am still waiting for an answer if I can send date and time to database. As you can see I have removed while-loop until it successfully sent data to ControllerGreat work @flopp! Just one comment: You call loop if the 5 resend tries isn't enough. That will re-start the loop. What it also will do is to keep all variables in the loop and resend functions in memory. These will add up over time, which will crash your Arduino after a while. So if you start the engine at work and it takes too long time to reach home, the Arduino will have crashed before it is able to update the gateway. See http://arduino.stackexchange.com/questions/355/how-much-can-i-recurse-how-much-can-i-recurse-how-much-caqfsdrfw for technical details on stack usage and recursion.
You might want to do something like this instead:
bool resend(MyMessage &msg, int repeats) { ... if (sendOK == false && repeat == repeats){ return false; } return true; }and then change all
resend(..., 5);to
if (!resend(..., 5)) return;This will have the same effect, but will not accumulate stuff on the stack.
-
Great work @flopp! Just one comment: You call loop if the 5 resend tries isn't enough. That will re-start the loop. What it also will do is to keep all variables in the loop and resend functions in memory. These will add up over time, which will crash your Arduino after a while. So if you start the engine at work and it takes too long time to reach home, the Arduino will have crashed before it is able to update the gateway. See http://arduino.stackexchange.com/questions/355/how-much-can-i-recurse-how-much-can-i-recurse-how-much-caqfsdrfw for technical details on stack usage and recursion.
You might want to do something like this instead:
bool resend(MyMessage &msg, int repeats) { ... if (sendOK == false && repeat == repeats){ return false; } return true; }and then change all
resend(..., 5);to
if (!resend(..., 5)) return;This will have the same effect, but will not accumulate stuff on the stack.
@mfalkvidd
Thanks for taking time to read my sketch :satisfied:if !(resend((lastCounterMsg.set(starts)), 5)) return;Arduino IDE says
Car_counter.ino.ino: In function 'void resend(MyMessage&, int)': Car_counter.ino:117: error: return-statement with a value, in function returning 'void' [-fpermissive] Car_counter.ino:119: error: return-statement with a value, in function returning 'void' [-fpermissive] expected '(' before '!' token -
@mfalkvidd
this must help, or?void send(); resend((volumeMsg.set(starts)), 5); resend((lastCounterMsg.set(starts)), 5); gw.wait(1000); Serial.println(""); Serial.println("store 0 to EEPROM"); gw.saveState(0,0); // set 0 start to EEPROM, all have been sent Serial.println("sleep"); // mission accomplished while(1){} } // check if "st:fail" during gw.send, thanks n3ro void resend(MyMessage &msg, int repeats) { int repeat = 1; boolean sendOK = false; int repeatdelay = 2000; while ((sendOK == false) and (repeat < repeats)) { if (gw.send(msg)) { sendOK = true; } else { sendOK = false; Serial.print("Error "); Serial.println(repeat); } repeat++; delay(repeatdelay); } if (sendOK == false && repeat == repeats){ void send();; } -
And no, movin the code from loop to send does not help. The problem is that you will get recursive calls. send calls resend which calls send which calls resend which calls send which calls resend....and so on. For each call, the Arduino will store all variables on the stack and memory usage will grow and grow.
-
And no, movin the code from loop to send does not help. The problem is that you will get recursive calls. send calls resend which calls send which calls resend which calls send which calls resend....and so on. For each call, the Arduino will store all variables on the stack and memory usage will grow and grow.
@mfalkvidd
OK, so you have to go back to where it comes from -
@mfalkvidd
Sorry, didnt work. Almost same errorCar_counter_1.2:84: error: expected '(' before '!' token Car_counter_1.2:86: error: expected '(' before '!' token expected '(' before '!' token -
@mfalkvidd
But if I put ! inside ( it worksif (!resend((volumeMsg.set(starts)), 5))return; -
OK, let me see if I understand your overall flow.
You have your node connected to switched 12V power in the car. When you turn on the car, the uC gets power, when you turn off the car it loses power. When the car is cranking, the voltage drops enough that it tends to reset again (or else the cranking position on your key switch actually cuts off auxiliary power used by the uC).
So if you are at home and in range, loop() is called by the Arduino runtime just once; at the end of loop() the uC goes into an infinite busy loop rather than return, staying there until power is lost again. If you are away from home or the packets are lost for other reasons, resend() will cause a return from loop(), so that loop() will be called again by the Arduino runtime, so it will just keep trying until either power is lost, or it makes contact with your wireless network again.
Meanwhile every time it starts, the first byte of eeprom is incremented in setup(), and then loop() tries to fetch VAR1, and send VAR1+eeprom(0) back. If it succeeds, then the first byte of eeprom is zeroed.
Right?
That sounds reasonable. My immediate concern would be inconsistent operation during cranking, if the input voltage went just low enough to make the uC unstable. If your car's wiring cuts the power entirely, not a problem. Likewise there appears to be time for the uC to boot, and fully update eeprom, before losing power during cranking. (It's best not to be writing eeprom as voltage drops). It appears that those are not problems for your system tho.
-
@tbowmo
Yes, but I dont want to jeopardize my cars :)
12v can be handle by the Arduino Pro min on RAW pin@flopp said:
12v can be handle by the Arduino Pro min on RAW pin
Cars go well above 14V during operation (as well as having noise glitches on top of that). If the regulator is only good up to 12v, operation could be marginal. Hence the suggestion from @tbowmo of a couple of diodes in the power feed, to drop a bit of the voltage as seen by the regulator.
-
OK, let me see if I understand your overall flow.
You have your node connected to switched 12V power in the car. When you turn on the car, the uC gets power, when you turn off the car it loses power. When the car is cranking, the voltage drops enough that it tends to reset again (or else the cranking position on your key switch actually cuts off auxiliary power used by the uC).
So if you are at home and in range, loop() is called by the Arduino runtime just once; at the end of loop() the uC goes into an infinite busy loop rather than return, staying there until power is lost again. If you are away from home or the packets are lost for other reasons, resend() will cause a return from loop(), so that loop() will be called again by the Arduino runtime, so it will just keep trying until either power is lost, or it makes contact with your wireless network again.
Meanwhile every time it starts, the first byte of eeprom is incremented in setup(), and then loop() tries to fetch VAR1, and send VAR1+eeprom(0) back. If it succeeds, then the first byte of eeprom is zeroed.
Right?
That sounds reasonable. My immediate concern would be inconsistent operation during cranking, if the input voltage went just low enough to make the uC unstable. If your car's wiring cuts the power entirely, not a problem. Likewise there appears to be time for the uC to boot, and fully update eeprom, before losing power during cranking. (It's best not to be writing eeprom as voltage drops). It appears that those are not problems for your system tho.
@Zeph
Fully correct :) thanks for summarizing.Car A have a built in USB where I, today, take power. I have to test if the voltage will go low so the uC will not get correct power but not 0 volt(restart), I don't know what will happen then. This was your question?
Car B have cigarette plug(12v output) so I use a converter to 5V, I think these converters can handle low input and still have 5V on output.I will test it for a while and count starts in my head(good luck)
Next idea is to have a buzzer to beep when it has successfully sent data to Controller, just as information and if it beeps when I am away from home I have a mistake in the sketch
-
@flopp said:
12v can be handle by the Arduino Pro min on RAW pin
Cars go well above 14V during operation (as well as having noise glitches on top of that). If the regulator is only good up to 12v, operation could be marginal. Hence the suggestion from @tbowmo of a couple of diodes in the power feed, to drop a bit of the voltage as seen by the regulator.
-
OK, let me see if I understand your overall flow.
You have your node connected to switched 12V power in the car. When you turn on the car, the uC gets power, when you turn off the car it loses power. When the car is cranking, the voltage drops enough that it tends to reset again (or else the cranking position on your key switch actually cuts off auxiliary power used by the uC).
So if you are at home and in range, loop() is called by the Arduino runtime just once; at the end of loop() the uC goes into an infinite busy loop rather than return, staying there until power is lost again. If you are away from home or the packets are lost for other reasons, resend() will cause a return from loop(), so that loop() will be called again by the Arduino runtime, so it will just keep trying until either power is lost, or it makes contact with your wireless network again.
Meanwhile every time it starts, the first byte of eeprom is incremented in setup(), and then loop() tries to fetch VAR1, and send VAR1+eeprom(0) back. If it succeeds, then the first byte of eeprom is zeroed.
Right?
That sounds reasonable. My immediate concern would be inconsistent operation during cranking, if the input voltage went just low enough to make the uC unstable. If your car's wiring cuts the power entirely, not a problem. Likewise there appears to be time for the uC to boot, and fully update eeprom, before losing power during cranking. (It's best not to be writing eeprom as voltage drops). It appears that those are not problems for your system tho.
-
@Zeph said:
It's best not to be writing eeprom as voltage drops
This is correct. However, shouldn't the BOD take care of that problem? Might be a good idea to set the BOD to 4.3V i stead of the default 2.7 though?
@mfalkvidd said:
@Zeph said:
It's best not to be writing eeprom as voltage drops
What does that mean exactly? Write to eeprom when power disappear, how is that possible without power?
This is correct. However, shouldn't the BOD take care of that problem? Might be a good idea to set the BOD to 4.3V instead of the default 2.7 though?
Perfect, then it will die sooner I guess