Detect movement by Electron RSSI project Success!


#1

Sorry about the formatting don’t know how to indent 4 spaces.
Any suggestions or ideas for code improvement?
UDP data usage is tiny compared to publish.
ESP2866 receives the packet and calls Pushover to notify my Android.

define R0 replyBuffer[0]
IPAddress rem()
char replyBuffer[]="Hi";  //[0] used for P,C
char packetBuffer[255];
UDP udp;
FuelGauge fuel;
int i,j,k,v;
boolean wasc;

void send(){
udp.beginPacket(rem,2390);  //default for ESP2866
udp.write(replyBuffer);
udp.endPacket();
}
void red(){
RGB.control(true);
RGB.color(255,0,0); 
delay(100);
RGB.control(false);
}
void setup(){
Serial.begin(115200); 
pinMode(C0,INPUT_PULLUP);  //GND to trigger
while(!Cellular.ready()) Particle.process();  //process needed?
//Serial.println("Begin");
Particle.variable("j",j);  //RSSI 0-50
Particle.variable("v",v);  //voltage*100
udp.begin(2390);  //for Rx
send();

while(udp.parsePacket()==0) Particle.process();  //or use safe mode to program OTA
int len=udp.read(packetBuffer,255);
packetBuffer[len]=0;
//Serial.println(packetBuffer);
//Cellular.off(); delay(1000);  //off needed
//System.sleep(SLEEP_MODE_DEEP,999999);  
//can GND RST pin to trigger, save power 55ma in loop(), cost 5k data
}
void loop(){
k=wasc=0;
for(j=0;j<10;j++){ red(); delay(100); }  //2s 10 blinks
for(j=0;j<20;j++){  //20s could miss C0 trigger fixed
  CellularSignal sig=Cellular.RSSI();
  k+=sig.rssi;
  if(digitalRead(C0)==0)wasc=1;
  delay(1000);
}
k+=2000;  //0-50
k/=20;
j=k;
Serial.print("k="); Serial.println(k);  //2 lines remain + begin
while(abs(j-k)<=12){  //modify 8-12 if nobody around 15-20 detect driving only
  CellularSignal sig=Cellular.RSSI();
  j=sig.rssi+100;  //0-50 shared
  v=fuel.getVCell()*100;  //shared
  red();
  if(Time.second()<20)
  if(Time.minute()%20==0){R0='P'; break;}  //Ping needs 1m delay every 20m fixed above
  if(digitalRead(C0) ==0){R0='C'; break;}  //C0 trigger
  if(wasc)               {R0='C'; break;} 
  Particle.process();  //delay does not allow OTA or variable() reliably
  delay(3000-100);  //100 red
  Serial.println(j-k);
}  //<=12
send();
if(Time.minute()%20<18)  //can send 5x in 2 mins was missing alert without if
if(R0!='P')
  delay(2*60*1000UL);  //wait 2 mins optional for C0 trigger, disable P, many >12 driving
R0='H';  //back to default
}  //loop

#2

@moors7 How did you do that? For next time.


#3

Like this :smile:


#4
3 of em

#5

So how are you applying this detect movement by RSSI change?

I would assume RSSI will change even without movement under various different local and atmospheric conditions.


#6

I think that @RWB is correct–you can have:

  • RSSI that changes even though you didn’t move
  • RSSI that doesn’t change even though you did move

For the latter case, you can be moving along a signal strength contour line and make a complete circle around the tower and never see RSSI change or you could be really close to the tower and moving several hundred yards, even away from the tower, does not lower your apparent signal strength because the front-end of the radio is saturated.

These are unlikely but possible so you have to figure out if they are OK in your use case.


#7

This code looks for a range of RSSI values that represent not changing. If you’re only looking to see when the module is moving in a car I use <=15. If nobody is walking nearby and you want to detect theft when the modules moves at all you can use <=5. The code will store the average value over 20 seconds. Your results will depend on topography and the type of building/car. It works for me in these 2 cases. The value only changes ±1 over an hours time. Probably it is difficult to code when the signal is weak to begin with. Then you can only detect when there is a complete loss, or moving closer to the tower. This code will detect a timeout when the signal goes missing for any reason. Perhaps someone unplugged the LiPo.


#8

RSSI certainly cannot measure distance from a tower. As you said. But if nobody is near it will be affected by someone walking past. If the orientation of the antenna is changed the output is obvious. By using 15, all changes are ignored until you start driving in a car.


#9

It may be stable for now but I don’t see it working out over the long haul. Only time will tell though.