@sweebee I already built several Battery PIR’s according to your sample in MySensors v1.5.4. Mine are presented here.
Since a few month’s I’m running MySensors V2.0 and those PIR’s work great.
Now I’ve built a new PIR with a MySensors v2.0 sketch but this one doesn’t seem to work.
The Serial Monitor does not give any info (blank screen).
Below my sketch. Could you verify the sketch please?
I can’t seem to find out what is wrong:
// Enable debug prints
// #define MY_DEBUG
// Enable and select radio type attached
#define MY_RADIO_NRF24
//#define MY_RADIO_RFM69
#include <SPI.h>
#include <MySensors.h>
#include <readVcc.h>
// ********** CONFIG **********************************
#define NODE_ID AUTO // ID of node
#define CHILD_ID 1 // ID of sensor
#define PIR_PIN 3 // Pin connected to the PIR
#define MIN_V 2000 // empty voltage (0%)
#define MAX_V 3200 // full voltage (100%)
// ****************************************************
MyMessage msg(CHILD_ID, V_TRIPPED);
int oldBatteryPcnt = -1;
int sentValue = -1;
int forceSend = 0;
void setup()
{
pinMode(PIR_PIN, INPUT);
}
void presentation()
{
sendSketchInfo("PIR Sensor", "1.3");
present(CHILD_ID, S_MOTION);
}
void loop()
{
// Get PIR
sendPir();
// Send batterylevel
sendBattery();
// Sleep until something happens with the sensor
sleep(PIR_PIN-2, CHANGE);
}
// FUNCTIONS
void sendPir()
{
int value = digitalRead(PIR_PIN); // Get value of PIR
if (value != sentValue) { // If status of PIR has changed
resend(msg.set(value), 5); // Send PIR status to gateway
sentValue = value;
}
}
void sendBattery() // Send battery percentage to GW
{
forceSend++;
int batteryPcnt = min(map(readVcc(), MIN_V, MAX_V, 0, 100), 100); // Get VCC and convert to percentage
if (batteryPcnt != oldBatteryPcnt || forceSend >= 20) { // If battery percentage has changed
sendBatteryLevel(batteryPcnt); // Send battery percentage to gateway
oldBatteryPcnt = batteryPcnt;
forceSend = 0;
}
}
void resend(MyMessage &msg, int repeats) // Resend messages if not received by GW
{
int repeat = 0;
int repeatDelay = 0;
boolean ack = false;
while ((ack == false) and (repeat < repeats)) {
if (send(msg)) {
ack = true;
} else {
ack = false;
repeatDelay += 100;
}
repeat++;
delay(repeatDelay);
}
}