Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. My Project
  3. Count car-starts

Count car-starts

Scheduled Pinned Locked Moved My Project
47 Posts 5 Posters 12.8k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Z Zeph

    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.

    mfalkviddM Offline
    mfalkviddM Offline
    mfalkvidd
    Mod
    wrote on last edited by mfalkvidd
    #34

    @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?

    F 1 Reply Last reply
    0
    • mfalkviddM mfalkvidd

      @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?

      F Offline
      F Offline
      flopp
      wrote on last edited by mfalkvidd
      #35

      @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

      mfalkviddM 1 Reply Last reply
      0
      • F flopp

        @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

        mfalkviddM Offline
        mfalkviddM Offline
        mfalkvidd
        Mod
        wrote on last edited by mfalkvidd
        #36

        @flopp said:

        What does that mean exactly? Write to eeprom when power disappear, how is that possible without power?

        Writing is not instantaneous. It might only take a millisecond, but during that time things can happen. So when the write starts, there is enough power. But half-way through the write power is lost. That leaves the eeprom in an unknown state.

        F 1 Reply Last reply
        0
        • mfalkviddM mfalkvidd

          @flopp said:

          What does that mean exactly? Write to eeprom when power disappear, how is that possible without power?

          Writing is not instantaneous. It might only take a millisecond, but during that time things can happen. So when the write starts, there is enough power. But half-way through the write power is lost. That leaves the eeprom in an unknown state.

          F Offline
          F Offline
          flopp
          wrote on last edited by flopp
          #37

          @mfalkvidd
          Ok, I read it "to write as counting voltage drop" but it was "write to eeprom during voltage drop".
          It is now clear, thanks

          1 Reply Last reply
          0
          • tbowmoT Offline
            tbowmoT Offline
            tbowmo
            Admin
            wrote on last edited by
            #38

            @flopp

            A more exotic "hack" could be to tap into the OBD2 interface, and for example check engine RPM to see if motor is running, or not.

            See this link..
            http://mechanics.stackexchange.com/questions/24171/how-to-detect-engine-ignition-on-off-status-using-obd2

            F 1 Reply Last reply
            0
            • tbowmoT tbowmo

              @flopp

              A more exotic "hack" could be to tap into the OBD2 interface, and for example check engine RPM to see if motor is running, or not.

              See this link..
              http://mechanics.stackexchange.com/questions/24171/how-to-detect-engine-ignition-on-off-status-using-obd2

              F Offline
              F Offline
              flopp
              wrote on last edited by
              #39

              @tbowmo
              Something like this?
              This looks really nice, something I want to try later on
              http://youtu.be/kmZ2PUMOKB8

              1 Reply Last reply
              0
              • F Offline
                F Offline
                flopp
                wrote on last edited by
                #40

                My sketch seems to work, only problem is presentation in domoticz

                I will add a buzzer to now when it has reported to gateway

                1 Reply Last reply
                0
                • tbowmoT Offline
                  tbowmoT Offline
                  tbowmo
                  Admin
                  wrote on last edited by
                  #41

                  @flopp

                  There is an howto here http://www.instructables.com/id/Hack-an-ELM327-Cable-to-make-an-Arduino-OBD2-Scann/ looks pretty simple to get going, and read out the RPM's..

                  F 1 Reply Last reply
                  0
                  • tbowmoT tbowmo

                    @flopp

                    There is an howto here http://www.instructables.com/id/Hack-an-ELM327-Cable-to-make-an-Arduino-OBD2-Scann/ looks pretty simple to get going, and read out the RPM's..

                    F Offline
                    F Offline
                    flopp
                    wrote on last edited by
                    #42

                    @tbowmo
                    Nice, I will test in a few weeks

                    1 Reply Last reply
                    0
                    • F Offline
                      F Offline
                      flopp
                      wrote on last edited by
                      #43

                      I have now solve the problem with storing data in Domoticz at correct day.

                      I have another node that send back the same value as it is right now, every 2 hours

                      1 Reply Last reply
                      0
                      • F Offline
                        F Offline
                        flopp
                        wrote on last edited by flopp
                        #44

                        Version 1.12, changes gw.wait when asking for VAR from 5 sec to 1 sec. also added support for Buzzer on D3

                        1 Reply Last reply
                        0
                        • F Offline
                          F Offline
                          flopp
                          wrote on last edited by
                          #45
                          This post is deleted!
                          1 Reply Last reply
                          0
                          • F Offline
                            F Offline
                            flopp
                            wrote on last edited by flopp
                            #46

                            Keep-alive version. I noticed when this Node didn't get any response from Controller it send back 0, which means that this day will be very many starts

                            // Made by Daniel Nilsson
                            // Tested with Domoticz 3.4967
                            // Version 1.1
                            // 2016-05-10
                            
                            //Keep-alive since Domoticz seems to not storing data if not data comes in for 3 hours(user setting)
                            
                            #include <SPI.h>
                            #include <MySensor.h>
                            
                            #define CHILD_ID 0                          // Id of the sensor child
                            #define NODE_ID 7                        // same ID as real Car Counter
                            
                            int Controller;                             // Current start counts from Controller, like Domoticz
                            
                            MySensor gw;
                            MyMessage volumeMsg(CHILD_ID,V_RAIN);
                            MyMessage lastCounterMsg(CHILD_ID,V_VAR1);
                            
                            void setup()
                            {          
                              delay(500);   // wait for radio
                              
                              //Begin
                              gw.begin(incomingMessage, NODE_ID);
                              // Register this device as Rain sensor (will not show in Domoticz until first value arrives)
                              gw.present(CHILD_ID, S_RAIN);       
                            }
                            
                            void loop()
                            { 
                              
                                //Ask for starts from Controller.
                              
                                  Serial.println("Request start counts");
                                  //gw.request(CHILD_ID, V_VAR1);
                                  while (Controller == 0) {
                                    gw.request(CHILD_ID, V_VAR1);
                                    Serial.println("No response from Controller");
                                    gw.wait(60000);
                                    }
                                  gw.wait(5000);
                                  
                            if (!resend((volumeMsg.set(Controller)), 5))return;
                            gw.wait(1000);
                            gw.sleep(120*60000); //less then timeout in Controller
                            }
                            
                            // check if "st:fail" during gw.send, thanks n3ro
                            bool 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){
                                return false;
                              }
                              return true;
                            }
                            
                            //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);   
                              }
                            }
                            
                            1 Reply Last reply
                            1
                            • F Offline
                              F Offline
                              flopp
                              wrote on last edited by flopp
                              #47

                              Version 1.13 changed to fixed NODE_ID

                              // Made by Daniel Nilsson
                              // Tested with Domoticz 3.5721
                              // 2016-12-10
                              
                              #include <SPI.h>
                              #include <MySensor.h>
                              
                              #define CHILD_ID 0                          // Id of the sensor child
                              #define NODE_ID 7                            // 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.13"                // 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()
                              {          
                                pinMode(3,OUTPUT);
                                delay(2*45000);  // Allow time if USB/cigarette plug is powered before you turned the key
                                digitalWrite(3,HIGH);
                                delay(300);
                                digitalWrite(3,LOW);
                                delay(300);
                                digitalWrite(3,HIGH);
                                delay(300);
                                digitalWrite(3,LOW);
                                //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()
                              { 
                                
                                  //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.wait(1000);
                                    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("");
                              
                              if (!resend((volumeMsg.set(starts)), 6))return;
                              if (!resend((lastCounterMsg.set(starts)), 6)) return;
                              
                              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
                              digitalWrite(3,HIGH);
                                delay(900);
                                digitalWrite(3,LOW);
                                
                              while(1){}
                              
                              }
                              
                              // check if "st:fail" during gw.send, thanks n3ro
                              bool 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){
                                  return false;
                                }
                                return true;
                              }
                              
                              //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);   
                                }
                              }
                              
                              1 Reply Last reply
                              0
                              Reply
                              • Reply as topic
                              Log in to reply
                              • Oldest to Newest
                              • Newest to Oldest
                              • Most Votes


                              12

                              Online

                              11.7k

                              Users

                              11.2k

                              Topics

                              113.1k

                              Posts


                              Copyright 2025 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
                              • Login

                              • Don't have an account? Register

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • MySensors
                              • OpenHardware.io
                              • Categories
                              • Recent
                              • Tags
                              • Popular