Particle electron publish error

Hello Everyone,

I am working on particle electron motion sensing, I have done a coding in particle electron with PIR sensor to sense the motion. customer needs that the device should withstand for months. So I have designed the way to wake up the device when there is a motion detected. I have connected my pir sensor output to A7 pin so that device wakes up if there is a motion. However, they required that the device should publish an event every 6 hours. So that i have differentiated with publish event every 6 hours and publishing variable by adding when sensor value =1 (so that we could find there is a motion).

But in my case, it publishing variable at the same time as publishing event every 6 hours. I need to differentiate in showing output if there is a motion. Kindly anyone advise me please.

const unsigned long TIME_PUBLISH_BATTERY_SEC = 6* 60 * 60;

  System.sleep(A7, RISING, TIME_PUBLISH_BATTERY_SEC);
  delay(500);

  awake = ((analogRead(A7)) != 0);

  Serial.printlnf("awake=%d", awake);
  Particle.variable("SensorData", awake);

First, if you want to know whether the PIR pin was HIGH or LOW at wake, you should not have half a second delay after wake :wink:
Also your use of Particle.variable() seems (as we can’t see your full code) wrong - these are only registered once (usually in setup()) and are not actively pushed by code.

But since the wake latency might be longer than the wake trigger, there have been several threads dealing with the distinction between timed or pin wake
https://community.particle.io/search?q=wake%20time%20pin

2 Likes

I found this code in this community and I have altered to my requirements. But i am getting sensor high at alternative times. Kindly help me to sort this out

// This #include statement was automatically added by the Particle IDE.
#include <Ubidots.h>

#include "application.h"
SYSTEM_THREAD(ENABLED);

#define TOKEN "BBFF-a1aXeKjT3QlzwOnae53ovppXPolR8k" // Put here your Ubidots TOKEN

Ubidots ubidots(TOKEN);
const char *eventName = "HeartBeat";
const char *eventBattery = "LowBattery";

// Various timing constants
const unsigned long MAX_TIME_TO_PUBLISH_MS = 120000; // Only stay awake for 60 seconds trying to connect to the cloud and publish
const unsigned long TIME_AFTER_PUBLISH_MS = 10000; // After publish, wait 4 seconds for data to go out
const unsigned long TIME_AFTER_BOOT_MS = 10000; // At boot, wait 5 seconds before going to sleep again (after coming online)
const unsigned long TIME_PUBLISH_BATTERY_SEC = 5 * 60; // every 4 hours, send a battery update
int count;

 float value;
// Stuff for the finite state machine
enum State { ONLINE_WAIT_STATE, RESET_STATE, RESET_WAIT_STATE, PUBLISH_STATE, SLEEP_STATE, SLEEP_WAIT_STATE, BOOT_WAIT_STATE, LOW_BAT_STATE, ALERT_STATE };
State state = ONLINE_WAIT_STATE;
unsigned long stateTime = 0;
int awake = 0;

void setup() {
	Serial.begin(9600);
	FuelGauge fuel;
    value = fuel.getVCell();
    Particle.variable("SensorData", awake);
}
 
void loop() {

	switch(state) {
	case ONLINE_WAIT_STATE:
		if (Particle.connected()) {
			state = RESET_STATE;
		}
		if (millis() - stateTime > 5000) {
			stateTime = millis();
			Serial.println("waiting to come online");
		}
		break;

	case RESET_STATE:
		Serial.println("resetting sensor");

		if (!(analogRead(A7))) {
			Serial.println("Movement not found");
			state = SLEEP_STATE;
			break;
		}

		state = BOOT_WAIT_STATE;
		break;

	case PUBLISH_STATE:
		if (Particle.connected()) {
		    
			char data[32];
			snprintf(data, sizeof(data), "%d,%.02f,%.02f", awake, value);
			Particle.publish(eventName, data, 60);
			ubidots.add("SENSORDATA", awake);
            ubidots.add("BATTERYLEVEL",value);
            ubidots.sendAll();
			stateTime = millis();
			state = SLEEP_WAIT_STATE;
			while(analogRead(A7)){
			    for(count=0; count ++;){
			        Serial.print(count);
			        snprintf(data, sizeof(data), "%d", count);
			        Particle.publish("Motioncount", data, 60);
			    }
			}
		} else {
			// Haven't come online yet
			if (millis() - stateTime >= MAX_TIME_TO_PUBLISH_MS) {
				// Took too long to publish, just go to sleep
				state = SLEEP_STATE;
			}
		}
		break;

	case SLEEP_WAIT_STATE:
		if (millis() - stateTime >= TIME_AFTER_PUBLISH_MS) {
			state = SLEEP_STATE;
		}
		break;

	case BOOT_WAIT_STATE:
		if (millis() - stateTime >= TIME_AFTER_BOOT_MS) {
			// To publish the battery stats after boot, set state to PUBLISH_STATE
			// To go to sleep immediately, set state to SLEEP_STATE
			state = PUBLISH_STATE;
		}
		break;

	case SLEEP_STATE:
		// Sleep
		System.sleep(A7, RISING, TIME_PUBLISH_BATTERY_SEC, SLEEP_NETWORK_STANDBY);
		awake = ((analogRead(A7)) != 0);
		Serial.printlnf("awake=%d", awake);
		state = PUBLISH_STATE;
		stateTime = millis();
		break;
		
	case LOW_BAT_STATE:
	if(value <= 3.3){
	    state = ALERT_STATE;
	    
	}
	else{
	    state = PUBLISH_STATE;
        ubidots.sendAll();
	 
	}
	 break;
	 
	 case ALERT_STATE:
		if (Particle.connected()) {
			char data1[32];
			snprintf(data1, sizeof(data1), "%d,%.02f", value);
			Particle.publish(eventBattery, data1, 60);
			stateTime = millis();
			state = SLEEP_WAIT_STATE;
		} else {
			// Haven't come online yet
			if (millis() - stateTime >= MAX_TIME_TO_PUBLISH_MS) {
				// Took too long to publish, just go to sleep
				state = SLEEP_STATE;
			}
		}
		break;
	}
		
}

Is this the original code or your adaption of it?

If you could also post a link to the origin of this code that might help since there will probably be some discussion about its behaviour too.

And what does this mean?

That is adaption from original code…

Discussion thread - Using an accelerometer and publishing when a sleeping Photon is moved

code source - https://github.com/rickkas7/AccelWake/blob/master/accelwake_electron.cpp

I am getting sensor value as high every alternate time( 00 1 1 001 1 001 1 ), I am confused about it.

Maybe @rickkas7 can help best with his own code :wink:

2 Likes