sensor data sent to server; acknowledgement

4 years 1 month ago #2422 by vickeyhort
Hi. I have a sensor node which sends light level, door activity and pir sensor activity to EasyIOT server using mysensors driver. I am using 1.4 version of mysensors library.

I want to use acknowledgement option to make sure that value has been sent by sensor node and received by server, as If communication fails, node should try again and again to send that value to sensor unless server received the value.

I have no idea how to implement this. May someone please help me with this? Here is my sketch.

#include <SPI.h>
#include <MySensor.h>

#define CHILD_ID_LIGHT 0
#define CHILD_ID_DOOR 1
#define CHILD_ID_PIR 2
#define CHILD_ID_LED 3
#define CHILD_ID_SAVER 4

#define LDR_PIN A0
#define DOOR_PIN 2
#define PIR_PIN 3
#define LED_PIN 4 // Relay 1 is attached
#define SAVER_PIN 5 // Relay 2 is attached

#define INTERRUPT DOOR_PIN-2

unsigned long SLEEP_TIME = 1200000; // Sleep time 1200000 i.e. 20 minutes (in milliseconds)

MySensor gw;

boolean activity = false;
boolean LEDSwitch = false;
boolean SaverSwitch = false;
boolean nightSwitch = false;

MyMessage msgLDR(CHILD_ID_LIGHT, V_LIGHT_LEVEL);
MyMessage msgDoor(CHILD_ID_DOOR, V_TRIPPED);
MyMessage msgPir(CHILD_ID_PIR, V_TRIPPED);

void setup()
{
gw.begin(NULL, AUTO, false, 1);
gw.sendSketchInfo("BedRoom", "1.0");

pinMode(DOOR_PIN,INPUT);
pinMode(PIR_PIN,INPUT);
pinMode(LED_PIN, OUTPUT);
pinMode(SAVER_PIN, OUTPUT);

digitalWrite(DOOR_PIN, HIGH);
digitalWrite(PIR_PIN, HIGH);
digitalWrite(LED_PIN, HIGH);
digitalWrite(SAVER_PIN, HIGH);

gw.present(CHILD_ID_LIGHT, S_LIGHT_LEVEL);
gw.present(CHILD_ID_DOOR, S_DOOR);
gw.present(CHILD_ID_PIR, S_MOTION);

int LightLevel;
for(int i = millis(); i < (millis() + 5000); i++){
LightLevel = (100 - ((1023-analogRead(LDR_PIN))/10.23));
}
if(LightLevel < 20){
gw.send(msgLDR.set(LightLevel));
nightSwitch = true;
}
else {
gw.send(msgLDR.set(LightLevel));
nightSwitch = false;
}
}

void loop()
{
if(nightSwitch){ //When night switch is on
int wake;
wake = gw.sleep(INTERRUPT,FALLING, SLEEP_TIME);
if(wake == 1){
if(!activity){
gw.send(msgDoor.set("1"));
gw.send(msgDoor.set("0"));
activity = true;
if((!LEDSwitch) && (!SaverSwitch)){
pirswitch();
}
else{ //activity finished
activityoff();
}
}
}
else{ //Timer wake up
int LightLevel;
if((!LEDSwitch) && (!SaverSwitch)){
for(int i = millis(); i < (millis() + 5000); i++){
LightLevel = (100 - ((1023-analogRead(LDR_PIN))/10.23));
}
}
else{
for(int i = millis(); i < (millis() + 1000); i++){
LightLevel = (100 - ((1023-analogRead(LDR_PIN))/10.23));
}
}

if((LightLevel < 10) && ((!LEDSwitch) && (SaverSwitch))){
Serial.println("Light level is less than 50%, Turning LED on");
digitalWrite(LED_PIN, LOW);
LEDSwitch = true;
SLEEP_TIME = 7200000; // Sleep time changed to 2 hours
}
else if((LightLevel > 20) && ((!LEDSwitch) && (!SaverSwitch))){
gw.send(msgLDR.set(LightLevel));
nightSwitch = false;
SLEEP_TIME = 7200000; // Sleep time changed to 2 hours
gw.sleep(SLEEP_TIME);
}
else if((LightLevel > 5) && (nightSwitch) && ((!LEDSwitch) && (!SaverSwitch))){
gw.send(msgLDR.set(LightLevel));
SLEEP_TIME = 600000; // Sleep time changed to 10 minutes
}
}
}
else { //When night switch is off
int LightLevel;
for(int i = millis(); i < (millis() + 5000); i++){
LightLevel = (100 - ((1023-analogRead(LDR_PIN))/10.23));
}
if(LightLevel < 20){
gw.send(msgLDR.set(LightLevel));
nightSwitch = true;
SLEEP_TIME = 7200000; // Sleep time changed to 2 hours
gw.sleep(INTERRUPT,FALLING, SLEEP_TIME);
}
else if((LightLevel < 30) && (!nightSwitch)){
SLEEP_TIME = 600000; // Sleep time changed to 10 minutes
gw.sleep(SLEEP_TIME);
}
else if((LightLevel < 50) && (!nightSwitch)){
SLEEP_TIME = 1800000; // Sleep time changed to 30 minutes
gw.sleep(SLEEP_TIME);
}
}
}

void pirswitch()
{
for(int i = millis(); i < (millis() + 5000); i++){ //Check PIR for activity 10 times with delay of half second
boolean tripped = digitalRead(PIR_PIN) == HIGH;
Serial.println(i);
if(tripped){
gw.send(msgPir.set(tripped?"1":"0"));
Serial.println("Motion detected");
//activity on function
activityon();
Serial.println("Loop terminated");
break;
}
}
delay(3000);
activity = false;
}

void activityon()
{
//turn saver on
digitalWrite(SAVER_PIN, LOW);
SaverSwitch = true;
Serial.println("Energy saver turned on");
int LightLevel;
for(int i = millis(); i < (millis() + 1000); i++){ //Check if energy saver is on
LightLevel = (100 - ((1023-analogRead(LDR_PIN))/10.23));
}
if(LightLevel < 10){
Serial.println("Light level is less than 50%, Turning LED on");
digitalWrite(LED_PIN, LOW);
LEDSwitch = true;
}
else{
SLEEP_TIME = 1000;
Serial.println("Sleep time changed to second");
}
}

void activityoff()
{
delay(3000);
//turn off saver or LED
digitalWrite(SAVER_PIN, HIGH);
digitalWrite(LED_PIN, HIGH);
Serial.println("Activity finished");
SaverSwitch = false;
LEDSwitch = false;
activity = false;
SLEEP_TIME = 7200000;
Serial.println("Sleep time changed to 2 hours");
}
The following user(s) said Thank You: osalval

Please Log in or Create an account to join the conversation.

4 years 4 weeks ago #2427 by EasyIoT

vickeyhort wrote: Hi. I have a sensor node which sends light level, door activity and pir sensor activity to EasyIOT server using mysensors driver. I am using 1.4 version of mysensors library.

I want to use acknowledgement option to make sure that value has been sent by sensor node and received by server, as If communication fails, node should try again and again to send that value to sensor unless server received the value.

I have no idea how to implement this. May someone please help me with this? Here is my sketch.

#include <SPI.h>
#include <MySensor.h>

#define CHILD_ID_LIGHT 0
#define CHILD_ID_DOOR 1
#define CHILD_ID_PIR 2
#define CHILD_ID_LED 3
#define CHILD_ID_SAVER 4

#define LDR_PIN A0
#define DOOR_PIN 2
#define PIR_PIN 3
#define LED_PIN 4 // Relay 1 is attached
#define SAVER_PIN 5 // Relay 2 is attached

#define INTERRUPT DOOR_PIN-2

unsigned long SLEEP_TIME = 1200000; // Sleep time 1200000 i.e. 20 minutes (in milliseconds)

MySensor gw;

boolean activity = false;
boolean LEDSwitch = false;
boolean SaverSwitch = false;
boolean nightSwitch = false;

MyMessage msgLDR(CHILD_ID_LIGHT, V_LIGHT_LEVEL);
MyMessage msgDoor(CHILD_ID_DOOR, V_TRIPPED);
MyMessage msgPir(CHILD_ID_PIR, V_TRIPPED);

void setup()
{
gw.begin(NULL, AUTO, false, 1);
gw.sendSketchInfo("BedRoom", "1.0");

pinMode(DOOR_PIN,INPUT);
pinMode(PIR_PIN,INPUT);
pinMode(LED_PIN, OUTPUT);
pinMode(SAVER_PIN, OUTPUT);

digitalWrite(DOOR_PIN, HIGH);
digitalWrite(PIR_PIN, HIGH);
digitalWrite(LED_PIN, HIGH);
digitalWrite(SAVER_PIN, HIGH);

gw.present(CHILD_ID_LIGHT, S_LIGHT_LEVEL);
gw.present(CHILD_ID_DOOR, S_DOOR);
gw.present(CHILD_ID_PIR, S_MOTION);

int LightLevel;
for(int i = millis(); i < (millis() + 5000); i++){
LightLevel = (100 - ((1023-analogRead(LDR_PIN))/10.23));
}
if(LightLevel < 20){
gw.send(msgLDR.set(LightLevel));
nightSwitch = true;
}
else {
gw.send(msgLDR.set(LightLevel));
nightSwitch = false;
}
}

void loop()
{
if(nightSwitch){ //When night switch is on
int wake;
wake = gw.sleep(INTERRUPT,FALLING, SLEEP_TIME);
if(wake == 1){
if(!activity){
gw.send(msgDoor.set("1"));
gw.send(msgDoor.set("0"));
activity = true;
if((!LEDSwitch) && (!SaverSwitch)){
pirswitch();
}
else{ //activity finished
activityoff();
}
}
}
else{ //Timer wake up
int LightLevel;
if((!LEDSwitch) && (!SaverSwitch)){
for(int i = millis(); i < (millis() + 5000); i++){
LightLevel = (100 - ((1023-analogRead(LDR_PIN))/10.23));
}
}
else{
for(int i = millis(); i < (millis() + 1000); i++){
LightLevel = (100 - ((1023-analogRead(LDR_PIN))/10.23));
}
}

if((LightLevel < 10) && ((!LEDSwitch) && (SaverSwitch))){
Serial.println("Light level is less than 50%, Turning LED on");
digitalWrite(LED_PIN, LOW);
LEDSwitch = true;
SLEEP_TIME = 7200000; // Sleep time changed to 2 hours
}
else if((LightLevel > 20) && ((!LEDSwitch) && (!SaverSwitch))){
gw.send(msgLDR.set(LightLevel));
nightSwitch = false;
SLEEP_TIME = 7200000; // Sleep time changed to 2 hours
gw.sleep(SLEEP_TIME);
}
else if((LightLevel > 5) && (nightSwitch) && ((!LEDSwitch) && (!SaverSwitch))){
gw.send(msgLDR.set(LightLevel));
SLEEP_TIME = 600000; // Sleep time changed to 10 minutes
}
}
}
else { //When night switch is off
int LightLevel;
for(int i = millis(); i < (millis() + 5000); i++){
LightLevel = (100 - ((1023-analogRead(LDR_PIN))/10.23));
}
if(LightLevel < 20){
gw.send(msgLDR.set(LightLevel));
nightSwitch = true;
SLEEP_TIME = 7200000; // Sleep time changed to 2 hours
gw.sleep(INTERRUPT,FALLING, SLEEP_TIME);
}
else if((LightLevel < 30) && (!nightSwitch)){
SLEEP_TIME = 600000; // Sleep time changed to 10 minutes
gw.sleep(SLEEP_TIME);
}
else if((LightLevel < 50) && (!nightSwitch)){
SLEEP_TIME = 1800000; // Sleep time changed to 30 minutes
gw.sleep(SLEEP_TIME);
}
}
}

void pirswitch()
{
for(int i = millis(); i < (millis() + 5000); i++){ //Check PIR for activity 10 times with delay of half second
boolean tripped = digitalRead(PIR_PIN) == HIGH;
Serial.println(i);
if(tripped){
gw.send(msgPir.set(tripped?"1":"0"));
Serial.println("Motion detected");
//activity on function
activityon();
Serial.println("Loop terminated");
break;
}
}
delay(3000);
activity = false;
}

void activityon()
{
//turn saver on
digitalWrite(SAVER_PIN, LOW);
SaverSwitch = true;
Serial.println("Energy saver turned on");
int LightLevel;
for(int i = millis(); i < (millis() + 1000); i++){ //Check if energy saver is on
LightLevel = (100 - ((1023-analogRead(LDR_PIN))/10.23));
}
if(LightLevel < 10){
Serial.println("Light level is less than 50%, Turning LED on");
digitalWrite(LED_PIN, LOW);
LEDSwitch = true;
}
else{
SLEEP_TIME = 1000;
Serial.println("Sleep time changed to second");
}
}

void activityoff()
{
delay(3000);
//turn off saver or LED
digitalWrite(SAVER_PIN, HIGH);
digitalWrite(LED_PIN, HIGH);
Serial.println("Activity finished");
SaverSwitch = false;
LEDSwitch = false;
activity = false;
SLEEP_TIME = 7200000;
Serial.println("Sleep time changed to 2 hours");
}


For ack is responsibele one bit in protocol. But I'm not sure if on server side ack response is implemented.
The following user(s) said Thank You: osalval

Please Log in or Create an account to join the conversation.

4 years 4 weeks ago #2430 by osalval
That is the question, not only to be sure in the Node side, this Controller do not have a way to know if a Node is Alive, that is, in the Controller side.

Please Log in or Create an account to join the conversation.

Time to create page: 0.271 seconds

Forum latest

  • No posts to display.