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);
}
}