Good morning all,
i'm trying to develop a base station tracking different spot in my house with node sensors that sleep most of the time and then wake up, make the readings (temp, humidity ecc.) and send them trough a nrf24 to a base station within range.
This station basically is waken up by the nrf24 (interrupt pin IRQ), writes the data to a file on a microSD and then send the data packet on my server via Gprs with a Sim800L module.
So far i've been doing some test and everything seems to work flawlessy on my nodes but i've experienced some problems in the receiving node. I think they are related to power supply problems (i had a lot of trouble in running the sim800l and i solved by adding a 470uf cap just before it).
The whole system is powered by 4 18650 lipos in parallel charged by a solar panel and tp4056. I got some random reboot (and yes i already solved my memory issues by replacing serial.print with the F macro). The weirdest thing is that the system works i would say 80% good without the solar panel and it struggles a lot with the panel. If i replace the 4 lipos with a 1s battery and solar panel it works better but still very unstable and not reliable.
Now i managed to make it run for a whole night and it hasnt crashed yet but i hope you could give me a hint. I'll let you see my code and i am ready to answer all your questions. I know it's really difficult without seeing the actual hardware but i hope you could come up with a hint or something. Thanks in advance.
Usually it gets stuck at the PostData() method.
Here is the code.
void setup() /****** SETUP: RUNS ONCE ******/
{
//change to 10k for bootup and gsm connection
delay(4000); //wait for everything to 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 the PA Level
myRadio.setDataRate(RF24_250KBPS); //set data rate
myRadio.maskIRQ(1,1,0); //mask all IRQ triggers except for receive (1 is mask, 0 is no mask)
myRadio.openReadingPipe(1, addresses[0]); // Use the first entry in array 'addresses' (Only 1 right now)
myRadio.startListening();
myRadio.printDetails();
Serial.println(F(""));
}//--(end setup )---
void loop() /****** LOOP: RUNS CONSTANTLY ******/
{
//wait untill a packet arrive
if ( myRadio.available()) // Check for incoming data from transmitter
{
while (myRadio.available()) // While there is data ready
{
Serial.println(F(""));
Serial.println(F("WAKE UP, PACKET RECEIVED"));
delay(1000);
myRadio.read( &radioData, sizeof(radioData) ); // read the incoming data
// DO something with the data, like 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 the file opened okay, write to it:
if (myFile) {
Serial.println(F("Writing to 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 the file:
myFile.close();
Serial.println(F("done."));
} else {
// if the file didn't open, print an error:
Serial.println(F("ERROR opening the file"));
}
delay(3000);
//post data on server
postData();
} //END Radio available
delay(3000);
Serial.println(F("Sleeping Till Interrupt"));
delay(100); //delay to allow serial to fully print before sleep
sleep.pwrDownMode(); //set sleep mode
//Sleep till interrupt pin equals a particular state.
//In this case "falling" change from 1 to 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 the APN, the second need you fill in your 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());
}
}