Count car-starts



  • Latest version 1.13
    I want to store how many times I start my car each day, i will not use it to anything just for fun.
    I was thinking about using S_RAIN since that value will be reseted every night, which I want. One value for each day. I dont care that it will be showing mm and so on, I will just look at the number.
    I am using Domoticz and I can get it to work as long as I am at home.
    I start my Arduino, it receives VAR1 from Domoticz, Arduino add 1 to the received value and sent it back.
    My problem now is that I want to store how many times my car has been started away from my house where I dont have any connection to my gateway/domoticz.
    scenario: I start my car at home it will add 1 to the received number, I drive away to work, turn of my car. A few hours later I start my car, it will NOT receive VAR1 from Domoticz, so it has to store 1 to EPROM. When I get back home it needs to receive VAR1 and add 1 and send it back again.
    Is it possible to get an indication that Arduino didnt receive VAR1 from Domoticz, if yes, then I can write a row that it will store 1 to EPROM and if i start my car many times away from home it just read from EPROM and add 1 to EPROM.
    I can set it up that it try to send data to Domoticz every minute so as soon as I come home it will send data and then go to sleep forever. If the number will be send the day after it is not a problem this is just for fun.
    Sorry folks if this is difficult to read, i will try to write it better tomorrow i have tried to do this for hours without any solution, now I need to sleep.
    Thanks for all help
    good night


  • Mod

    Cool ida, @flopp

    What you are describing should work. You might want to consider what happens if your car is away during the night, or when you go for a week-long road trip. The sensor is not aware of time unless you connect a rtc so you might get 50 starts reported on the day you arrive home. That might not happen very often though, so it might be easier to ignore that aspect of the problem.



  • @mfalkvidd
    Thanks.
    I also had that in my mind and to start with I will see how many times that may happen and if needed I will add an RTC but then I have 2 options

    1. On midnight set eprom to 0(zero)
    2. Add time and date to the variable that I send to Domoticz(but it can't handle date and time) this means maybe I have to change to other Controller;(

    So it must then be option 1.


  • Hardware Contributor

    @flopp As long as you have it powered you dont need to save it into eeprom (its offcourse safer) but you can just use a int variable?
    You could use a battery sensor or how do you plan to detect the start of the car?



  • @sundberg84

    I will not use battery.
    As soon as the arduino start it will just send 1 and each time it starts and cannot send to Domoticz it will add 1 to eprom.

    Is it possible to get some kind of indication that it fails to send the data to Domoticz?


  • Hardware Contributor

    You have st:fail if you dont get ack back from your gateway or repeater.
    gw.send() returns false when no ack was received.

    This can mean though that you can reach the destination, but the ack fails... so its not 100% if you are just on the range limit.

    I saw some threads about code where you could use this to determine (and resend or in your case save to eeprom) actions.

    http://forum.mysensors.org/topic/1424/resend-if-st-fail/5
    http://forum.mysensors.org/topic/2189/serial-api-noack-when-sending-with-ack-failed/2

    (Ok, i hijacked those links from @mfalkvidd in another thread hehe).



  • @sundberg84
    Thanks that could be something to implement.

    Will look to it later.

    Maybe a start would be to write to eprom every time even if it succefully send the data and later on take care of st:fail.

    I know that eprom only can handle 100.000 writes but arduino pro mini is cheap so I can change the hardware when it fails to write to eprom


  • Hardware Contributor

    10start / day? * 300 days/year? = 3000/year
    10000/30000 = 3 years life... if you use those cheap pro mini clones im note sure the eeprom will fail first.



  • @sundberg84
    Hehe even more I think

    100000/3000=33 years

    I think in both cases eprom is fine but arduino will damage first

    I will start testing a code in a few hours
    I update here later



  • This post is deleted!


  • I have had some small problems with my sketch.

    I have 2 cars, A and B.

    A, as soon as I put in the key without turning it, USB port gets power. When I start the car, USB port losses power for a short while and Arduino restarts. This means that I will send data twice. Possible solution for this is to delay for 1-4 minutes but then I will not send anything when I am at home.

    B, when I unlock the car from remote control it power USB for ~30 seconds. This will result in same problem as car A.

    two things I want to implement to sketch.

    1. Send data as soon as I come home, so I can track exact date when I was using car. Not very important.
    2. Only send data when I actually use the car, not when I power Arduino ON. To solve this I have two solutins, GPS or Gyro. Gyro is the cheap and easy part so I will go for that at the moment. GPS should be perfect, then I can control that it should send data when I start to move and also send as soon as I come home.

  • Mod

    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
    I used While just to not try to send data all the time, since it will only be able to send when I leave my house and not away from my house and my thought was not to use MCU so much maybe this is not a problem. I will test to not sleep until it have sent the data successful in this way it will send data within a few seconds as soon as I reach my house.

    Perfect idea about fetch time, yes this is definitely a solution for my USB-power-on problem

    Thanks


  • Admin

    A solution could also be to hack into the wire harness of the car, and detect when the starter motor gets powered up by turning the key. It's 12v so a simple voltage divider could do it. (and perhaps a couple of diodes from arduino pin to GND and vcc for extra protection.)



  • @tbowmo
    Yes, but I dont want to jeopardize my cars :)
    12v can be handle by the Arduino Pro min on RAW pin



  • @mfalkvidd said:
    ... store current time in the eeprom...

    How can I store timestamp to EEPROM, it can only handle a value maximum 256 or?


  • Mod

    @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


  • Mod

    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.


Log in to reply
 

Looks like your connection to MySensors Forum was lost, please wait while we try to reconnect.