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