Error_Code=0;
while (1) {
Serial2.print("AT+CLTS=1\r\n"); // Get local timestamp
Read_Response_WaitFor("OK",SIMWaitTime);
//Serial.println(Receive_buffer);
delay(Delay_between_commands);
Serial2.print("AT+CCLK?\r\n");
Read_Response_WaitFor("OK",SIMWaitTime);
//Serial.println(Receive_buffer);
delay(Delay_between_commands);
if (Process_DateandTime(false)) {Error_Code=4;}
Serial2.print("AT+CSSLCFG=\"SSLVERSION\",0,3\r\n");
Read_Response_WaitFor("OK",SIMWaitTime);
Serial.println(Receive_buffer);
delay(Delay_between_commands);
Serial2.print("AT+CNACT=0,1\r\n");
Read_Response_WaitFor("OK",SIMWaitTime);
Serial.println(Receive_buffer);
delay(Delay_between_commands);
Serial2.print("AT+CNACT?\r\n");
Read_Response_WaitFor("OK",SIMWaitTime);
Serial.println(Receive_buffer);
delay(Delay_between_commands);
if (!GET_IP_Address ())
{
Serial.println("No IP address");
Serial.println("Program stalled");
Error_Code=2;
break;
}
Serial2.print("AT+SMCONF=\"URL\",\"mqtt3.thingspeak.com\",\"1883\"\r\n");
Read_Response_WaitFor("OK",SIMWaitTime);
Serial.println(Receive_buffer);
delay(Delay_between_commands);
Serial2.print("AT+SMCONF=\"KEEPTIME\",60\r\n");
Read_Response_WaitFor("OK",SIMWaitTime);
Serial.println(Receive_buffer);
delay(Delay_between_commands);
Serial2.print("AT+SMCONF=\"CLEANSS\",1\r\n");
Read_Response_WaitFor("OK",SIMWaitTime);
Serial.println(Receive_buffer);
delay(Delay_between_commands);
Serial2.print("AT+SMCONF=\"QOS\",0\r\n");
Read_Response_WaitFor("OK",SIMWaitTime);
Serial.println(Receive_buffer);
delay(Delay_between_commands);
Serial2.print("AT+SMCONF=\"RETAIN\",0\r\n");
Read_Response_WaitFor("OK",SIMWaitTime);
Serial.println(Receive_buffer);
delay(Delay_between_commands);
Serial2.print("AT+SMCONF=\"SUBHEX\",0\r\n");
Read_Response_WaitFor("OK",SIMWaitTime);
Serial.println(Receive_buffer);
delay(Delay_between_commands);
Serial.println("AT+SMCONF=\"CLIENTID\",\""+String(SPIFFS_Data.ClientId)+"\"\r\n");
Serial2.print("AT+SMCONF=\"CLIENTID\",\""+String(SPIFFS_Data.ClientId)+"\"\r\n");
Read_Response_WaitFor("OK",SIMWaitTime);
Serial.println(Receive_buffer);
delay(Delay_between_commands);
Serial.println("AT+SMCONF=\"USERNAME\",\""+String(SPIFFS_Data.Username)+"\"\r\n");
Serial2.print("AT+SMCONF=\"USERNAME\",\""+String(SPIFFS_Data.Username)+"\"\r\n");
Read_Response_WaitFor("OK",SIMWaitTime);
Serial.println(Receive_buffer);
delay(Delay_between_commands);
Serial.println("AT+SMCONF=\"PASSWORD\",\""+ String(SPIFFS_Data.Password)+ "\"\r\n");
Serial2.print("AT+SMCONF=\"PASSWORD\",\""+ String(SPIFFS_Data.Password)+ "\"\r\n");
Read_Response_WaitFor("OK",SIMWaitTime);
Serial.println(Receive_buffer);
delay(Delay_between_commands);
Serial2.print("AT+SMCONN\r\n"); // Check MQTT connection
Read_Response_WaitFor("OK",SIMWaitTime);
Serial.println(Receive_buffer);
delay(Delay_between_commands);
if(strstr(Receive_buffer,"ERROR") != NULL)
{
Serial.println("SMCONN ERROR " );
Error_Code=3;
break;
}
else
{
Serial.println("We have a MQTT connection");
String F1="field"+String(SPIFFS_Data.FieldNr)+"=";
String F2="&field"+String(SPIFFS_Data.FieldNr+1)+"=";
String F3="&field"+String(SPIFFS_Data.FieldNr+2)+"=";
String F4="&field"+String(SPIFFS_Data.FieldNr+3)+"=";
String payload; payload=F1+String(int(pressure))+F2+String(int(tmp))+F3+String(int(hum))+F4+String(int(BatVoltage*100))+"&"+Date_payload;
String Mqtt_Message;
int Payload_Length;
Payload_Length=payload.length();
Serial.print(payload);
Serial.print (" ");
Serial.println(Payload_Length);
//Mqtt_Message="AT+SMPUB=\"channels/"+ ChannelId+"/publish/fields/field1\","+String(Payload_Length)+",1,1\r\n"; //https://nl.mathworks.com/help/thingspeak/publishtoachannelfieldfeed.html
Mqtt_Message="AT+SMPUB=\"channels/"+ String(SPIFFS_Data.ChannelId)+"/publish\","+String(Payload_Length)+",1,1\r\n"; //https://nl.mathworks.com/help/thingspeak/publishtoachannelfeed.html
Serial.println(Mqtt_Message);
Serial2.print(Mqtt_Message);//Serial2.print("AT+SMPUB=\"channels/888617/publish/fields/field1\",1,1,1\r\n"); // lengte moet goed zijn
delay(Delay_between_commands);
Serial2.print(payload);
Read_Response_WaitFor("OK",20000); //SIMWaitTime);
Serial.println(Receive_buffer);
break;
}
} //end while
Serial.print("Done with error code : ");
Serial.println(Error_Code);
if (Error_Code==0)
{
Serial.print("Going to sleep for ");
Serial.print(SPIFFS_Data.Sleep_Period);
Serial.print(" seconds");
esp_sleep_enable_timer_wakeup(SPIFFS_Data.Sleep_Period * uS_TO_S_FACTOR);
esp_deep_sleep_start();
}
else
{
Serial.print("Going to sleep for ");
Serial.print(Sleep_Period_After_Error);
Serial.print(" seconds");
esp_sleep_enable_timer_wakeup(Sleep_Period_After_Error * uS_TO_S_FACTOR);
esp_deep_sleep_start();
}
bool GET_IP_Address () {
char *p0;
char *p1;
int span;
p0=strstr(Receive_buffer,"CNACT: ");
if (p0!=NULL)
{
p0=p0+strlen("CNACT: ");
p1=strstr(p0,"\n");
if (p1!=NULL)
{
span=min(19,p1-p0);
strncpy(IP_buffer, p0, span);
IP_buffer[span]='\0';
Serial.print("IP_buffer : ");
Serial.println(IP_buffer);
Serial.print(" : ");
Serial.println(strlen(IP_buffer));
if (strlen(IP_buffer) >15) {return true;} else {return false;} // 0,1,"10.236.109.95"
}
else
{
return (false);
Serial.println("No IP address");
}
}
else
{
return (false);
}
}
bool Process_DateandTime(bool PSUTZ) {
char *Process_ptr;
bool Date_Error;
Date_Error=false;
//Process_ptr=strstr(Receive_buffer,"CCLK:");
if (PSUTZ) {Process_ptr=strstr(Receive_buffer,"PSUTTZ:");}
else {Process_ptr=strstr(Receive_buffer,"CCLK:");}
if (Process_ptr!=NULL)
{
Serial.println(Process_ptr);
strcpy(date_time_char_array,Process_ptr);
Serial.println(date_time_char_array);
int k=0;
for (int i=0; i<strlen(date_time_char_array); i++)
{
if (strchr(allowed_char,date_time_char_array[i])!=NULL)
{
date_time_char_array_processed[k]=date_time_char_array[i];
k++;
}
}
Serial.println(date_time_char_array_processed);
Process_ptr=date_time_char_array_processed+1;
Serial.println(Process_ptr);
while (1)
{
Process_ptr=strtok(Process_ptr,"/,:"); // 23/04/14,11:56:48,+08,1
if (Process_ptr==NULL) {DateTime_Error=true;break;}
Thisday.Year=atoi(Process_ptr);
Process_ptr=strtok(NULL,"/,:"); // 23/04/14,11:56:48,+08,1
if (Process_ptr==NULL) {DateTime_Error=true;break;}
Thisday.Month=atoi(Process_ptr);
Process_ptr=strtok(NULL,"/,:"); // 23/04/14,11:56:48,+08,1
if (Process_ptr==NULL) {DateTime_Error=true;break;}
Thisday.Day=atoi(Process_ptr);
Process_ptr=strtok(NULL,"/,:"); // 23/04/14,11:56:48,+08,1
if (Process_ptr==NULL) {DateTime_Error=true;break;}
Thisday.Hour=atoi(Process_ptr);
Process_ptr=strtok(NULL,"/,:"); // 23/04/14,11:56:48,+08,1
if (Process_ptr==NULL) {DateTime_Error=true;break;}
Thisday.Minute=atoi(Process_ptr);
Process_ptr=strtok(NULL,"/,:"); // 23/04/14,11:56:48,+08,1
if (Process_ptr==NULL) {DateTime_Error=true;break;}
Thisday.Second=atoi(Process_ptr);
Process_ptr=strtok(NULL,"/,:"); // 23/04/14,11:56:48,+08,1
if (Process_ptr==NULL) {DateTime_Error=true;break;}
Thisday.Timezone=atoi(Process_ptr);
Thisday.Hour=Thisday.Hour+(Thisday.Timezone/4);
break;
}
Serial.println();
Serial.print(Thisday.Year);
Serial.print("/");
Serial.print( Thisday.Month);
Serial.print("/");
Serial.print( Thisday.Day);
Serial.print(",");
Serial.print(Thisday.Hour);
Serial.print(":");
Serial.print( Thisday.Minute);
Serial.print(":");
Serial.print( Thisday.Second);
Serial.print(",");
Serial.println( Thisday.Timezone);
Date_payload="created_at=20"+String(Thisday.Year)+"-"+Int_to_2_Digits(Thisday.Month)+"-"+Int_to_2_Digits(Thisday.Day)+" "+Int_to_2_Digits(Thisday.Hour)+":"+Int_to_2_Digits(Thisday.Minute)+":"+Int_to_2_Digits(Thisday.Second);
Serial.println(Date_payload);
//Serial.println("Done with it");
//while (1) {delay(300);}
if (Thisday.Year<23) {Date_Error=true; }
if (Thisday.Month<0 or Thisday.Month>12 ) {Date_Error=true; }
if (Thisday.Day<0 or Thisday.Month>31 ) {Date_Error=true; }
if (Thisday.Hour<0 or Thisday.Hour>24 ) {Date_Error=true; }
if (Thisday.Minute<0 or Thisday.Minute>60 ) {Date_Error=true; }
if (Thisday.Second<0 or Thisday.Second>60 ) {Date_Error=true; }
}
else
{
//strcpy(fnameJPG,"AA00000001012011.JPG");
Date_Error=true;
}
return Date_Error;
}
bool Read_Response_WaitFor(char* StopCharArray,long time_out) { // false als niet gevonden
int k=0;
long wait_until;
char ccc;
//while (!Serial2.available() and millis()<wait_until) {delay(10);}
time_taken=millis();
wait_until=time_taken+time_out;
while (millis()<wait_until)
{
while (Serial2.available())
{
ccc=Serial2.read();
//Serial.print(ccc);
if (k<buffer_size-1)
{
Receive_buffer[k]=ccc;
k++;
}
}
Receive_buffer[k]=NULL;
if (strstr(Receive_buffer,StopCharArray)!=NULL)
{
//Serial.println("OK found");
// SIM7600_Error=0;
Receive_buffer[k]=NULL;
return false;
}
}
//if (k>=buffer_size-1) {SIM7600_Error=1; strcpy(Receive_buffer,SIM7600_Error_list[SIM7600_Error].c_str());return;}
//if (millis()>=wait_until) {SIM7600_Error=2; strcpy(Receive_buffer,SIM7600_Error_list[SIM7600_Error].c_str()); return;}
Receive_buffer[k]=NULL;
time_taken=millis()-time_taken;
Serial.println();
Serial.print("1] This operation took ");
Serial.print(time_taken);
Serial.println(" millisec");
return true; //if (strstr(Receive_buffer,"ERROR")!=NULL) {return true;} else {return false;}
}