Strange code behaviour with nrf24, sim800l
good morning all,
i'm trying develop base station tracking different spot in house node sensors sleep of time , wake up, make readings (temp, humidity ecc.) , send them trough nrf24 base station within range.
this station waken nrf24 (interrupt pin irq), writes data file on microsd , send data packet on server via gprs sim800l module.
so far i've been doing test , seems work flawlessy on nodes i've experienced problems in receiving node. think related power supply problems (i had lot of trouble in running sim800l , solved adding 470uf cap before it).
the whole system powered 4 18650 lipos in parallel charged solar panel , tp4056. got random reboot (and yes solved memory issues replacing serial.print f macro). weirdest thing system works 80% without solar panel , struggles lot panel. if replace 4 lipos 1s battery , solar panel works better still unstable , not reliable.
now managed make run whole night , hasnt crashed yet hope give me hint. i'll let see code , ready answer questions. know it's difficult without seeing actual hardware hope come hint or something. in advance.
usually gets stuck @ postdata() method.
here code.
i'm trying develop base station tracking different spot in house node sensors sleep of time , wake up, make readings (temp, humidity ecc.) , send them trough nrf24 base station within range.
this station waken nrf24 (interrupt pin irq), writes data file on microsd , send data packet on server via gprs sim800l module.
so far i've been doing test , seems work flawlessy on nodes i've experienced problems in receiving node. think related power supply problems (i had lot of trouble in running sim800l , solved adding 470uf cap before it).
the whole system powered 4 18650 lipos in parallel charged solar panel , tp4056. got random reboot (and yes solved memory issues replacing serial.print f macro). weirdest thing system works 80% without solar panel , struggles lot panel. if replace 4 lipos 1s battery , solar panel works better still unstable , not reliable.
now managed make run whole night , hasnt crashed yet hope give me hint. i'll let see code , ready answer questions. know it's difficult without seeing actual hardware hope come hint or something. in advance.
usually gets stuck @ postdata() method.
here code.
code: [select]
void setup() /****** setup: runs once ******/
{
//change 10k bootup , gsm connection
delay(4000); //wait start up
serial.begin(115200);
//inizializza la micro sd
if (!sd.begin(2)) {
serial.println(f("inizializzazione della micro sd fallita"));
return;
}
else{
serial.println(f("sd inizializzata corettamente"));
serial.println(f("-----------------------------"));
}
//inizializza la sim800l
serialsim800.begin(9600);
delay(1000);
serial.println(f("sim800l inizializzata corettamente"));
serial.println(f("-----------------------------"));
printf_begin(); //inizializza printf per il debug del nrf24l01
myradio.begin(); //inizializza nrf24l01
myradio.enableackpayload(); //setack payload
myradio.setautoack(true); //set autoack
myradio.setchannel(108); //set channel
myradio.setpalevel(rf24_pa_max); // set pa level
myradio.setdatarate(rf24_250kbps); //set data rate
myradio.maskirq(1,1,0); //mask irq triggers except receive (1 mask, 0 no mask)
myradio.openreadingpipe(1, addresses[0]); // use first entry in array 'addresses' (only 1 right now)
myradio.startlistening();
myradio.printdetails();
serial.println(f(""));
}//--(end setup )---
void loop() /****** loop: runs ******/
{
//wait untill packet arrive
if ( myradio.available()) // check incoming data transmitter
{
while (myradio.available()) // while there data ready
{
serial.println(f(""));
serial.println(f("wake up, packet received"));
delay(1000);
myradio.read( &radiodata, sizeof(radiodata) ); // read incoming data
// data, print it
serial.println(f("ok, dati ricevuti"));
myradio.writeackpayload( 1, &ack, sizeof(ack) );
printf("\nwriting ack: %d", ack);
}
delay(2000);
//leggo la vbat della regina e l'assegno alla variabile
vbatregina = readbattery();
delay(2000);
myfile = sd.open("data.txt", file_write);
// if file opened okay, write it:
if (myfile) {
serial.println(f("writing data.txt..."));
myfile.println();
myfile.print(proprietario);
myfile.print(",");
delay(10);
myfile.print(idregina);
myfile.print(",");
delay(10);
myfile.print(radiodata.idarnia);
myfile.print(",");
delay(10);
myfile.print(radiodata.packetn);
myfile.print(",");
delay(10);
myfile.print(vbatregina);
myfile.print(",");
delay(10);
myfile.print(radiodata.vbat);
myfile.print(",");
myfile.print(radiodata.tin);
myfile.print(",");
delay(10);
myfile.print(radiodata.hum);
myfile.print(",");
delay(10);
myfile.print(radiodata.tex);
myfile.print(",");
delay(10);
myfile.print(radiodata.peso);
myfile.print(",");
delay(10);
// close file:
myfile.close();
serial.println(f("done."));
} else {
// if file didn't open, print error:
serial.println(f("error opening file"));
}
delay(3000);
//post data on server
postdata();
} //end radio available
delay(3000);
serial.println(f("sleeping till interrupt"));
delay(100); //delay allow serial print before sleep
sleep.pwrdownmode(); //set sleep mode
//sleep till interrupt pin equals particular state.
//in case "falling" change 1 0.
sleep.sleepinterrupt(digitalpintointerrupt(interruptpin),falling); //(interrupt number, interrupt state)
}//--(end main loop )---
/*-----( declare user-written functions )-----*/
float readbattery(){
string str = "";
serial.println(f("lettura dati batteria: "));
int percentuale;
float volt;
delay(1000);
serialsim800.println(f("at+cbc"));
delay(400);
while(serialsim800.available()){
str += (char)serialsim800.read();
}
serial.println(str);
if(str.indexof("+cbc") >= 0){
percentuale = str.substring(str.indexof("+cbc:")+8).toint();
volt = str.substring(str.indexof("+cbc:")+11).toint();
volt= volt/1000;
/*
serial.println(f("percentuale batteria: "));
serial.println(percentuale);
serial.println(f("volt : "));
serial.println(volt);
*/
return volt;
}
}
void postdata(){
serialsim800.println(f("at+sapbr=3,1,\"apn\",\"web.coopvoce.it\""));//setting apn, second need fill in local apn server
delay(1000);
showserialdata();
serialsim800.println(f("at+sapbr=1,1"));
delay(3000);
showserialdata();
serialsim800.println(f("at+sapbr=2,1"));
delay(3000);
showserialdata();
serialsim800.println(f("at+httpinit"));
delay(3000);
showserialdata();
serialsim800.println(f("at+httppara=\"cid\",1"));
delay(3000);
showserialdata();
serialsim800.print(f("at+httppara=\"url\",\"bekeeper.altervista.org/insert.php?proprietario="));
serialsim800.print(proprietario);
serialsim800.print(f("&idregina="));
serialsim800.print(idregina);
serialsim800.print(f("&idarnia="));
serialsim800.print(radiodata.idarnia);
serialsim800.print(f("&packetn="));
serialsim800.print(radiodata.packetn);
serialsim800.print(f("&vbatregina="));
serialsim800.print(vbatregina);
serialsim800.print(f("&vbatarnia="));
serialsim800.print(radiodata.vbat);
serialsim800.print(f("&tin="));
serialsim800.print(radiodata.tin);
serialsim800.print(f("&hum="));
serialsim800.print(radiodata.hum);
serialsim800.print(f("&tex="));
serialsim800.print(radiodata.tex);
serialsim800.print(f("&peso="));
serialsim800.print(radiodata.peso);
serialsim800.println(f("\""));
delay(3000);
showserialdata();
serialsim800.println(f("at+httpaction=0"));
delay(3000);
showserialdata();
serialsim800.println(f("at+httpread"));
delay(3000);
showserialdata();
serialsim800.println(f("at+httpterm"));
delay(3000);
showserialdata();
serialsim800.println(f("at+sapbr=0,1"));
delay(3000);
showserialdata();
}
void showserialdata(){
while(serialsim800.available()!=0)
{
serial.write(serialsim800.read());
}
}
why there delay()s when writing file?
why there delay()s in code?
bullshit. use serial.flush() (the time should used) block long needed make sure outgoing serial data buffer empty.
why there delay()s in code?
code: [select]
delay(100); //delay allow serial print before sleep
bullshit. use serial.flush() (the time should used) block long needed make sure outgoing serial data buffer empty.
Arduino Forum > Using Arduino > Programming Questions > Strange code behaviour with nrf24, sim800l
arduino
Comments
Post a Comment