0

I'm trying Sim800l and ESP8066 As my serial communication to Mega r3. when i try sim800l code side, something like this: which i refined the code a little bit. In Serial monitor handshakes are ok. but when new sms arrives it doesn't show the right content. Why with same code I see different results?


void setup()
{
Serial.begin(9600);
Serial1.begin(9600);
Serial.println("Starting..."); 
delay(1000);
Serial1.println("AT");
pollSms();
Serial1.println("AT+CMGF=1"); 
pollSms();
Serial1.println("AT+CNMI=1,2,0,0,0");
pollSms();
}
void loop()
{
pollSms();
}
void pollSms()
{
delay(500);
while (Serial.available()) 
{
Serial1.write(Serial.read());
}
while(Serial1.available()) 
{
Serial.write(Serial1.read());
}
}

everything is just fine but when I try

     #include <SPI.h>//Communicate via WiFi
     #include <DHT.h>
     #include <avr/sleep.h> 
     #include <avr/interrupt.h> 
     #include <WiFi.h>  
/* Libraries */


/* Variables */
     char id[] = "id";//id of Wifi network
     char pass[] = "password";//password of Wifi network
     const char* host = "esp8266sd";
     float humadity1 , temprature1=25;//Solon 1
     float humadity2 , temprature2;//Solon 2
     int Soil_humadity1,Soil_humadity2,Soil_humadity3;
     char c;
     String WiFi_instruction="";
     String SMSmessage="";
     String number, Answer;
     boolean valid;
     #define TWO_HRS 7200000
     unsigned long startTime1,startTime2;    
/* Variables */

/* AM2301 */
     #define DHTTYPE DHT21   // It defines which kind of humadity and temprature sensor is connecter (AM2301)
     DHT dht1(DHTPIN1, DHTTYPE); 
     DHT dht2(DHTPIN2, DHTTYPE); 
/* AM2301 */

/* Android App */
     WiFiServer server(80);
/* Android App */

/* Valid numbers */
     String num1="9016057005";
     String num2="9026057005";//change it 
/* Valid numbers */


void setup() 
{
/* Pins Settings */
 

/* Serial communications */ 
  Serial.begin(9600); // setup baudrate for transition to "serial monitor"
  Serial1.begin(9600);//Sim800l Serial Communication
  Serial3.begin(9600);//ESP8266 Serial Communication
  /* SMS Settings */
  Serial.println("Start!...");
  Serial1.println("AT");            //Send AT command it will says "OK" if everything is fine
  Serialcom();                      //description below
  Serial1.println("AT+CMGF=0");     // Configuring TEXT mode
  Serialcom();
  Serial1.println("AT+IPR=9600"); 
  Serial1.println("AT+CNMI=1,2,0,0,0");
  Serialcom();
  /* SMS Settings */
/* Serial communications */
  
/* Timers */
  startTime1 = millis();
  startTime2 = millis();
/* Timers */

/* Sensors */
  dht1.begin();        //setup humadity and temprature sensor: Solon 1
  dht2.begin();       //setup humadity and temprature sensor: Solon 2
  humadity1 = dht1.readHumidity();
  temprature1 = dht1.readTemperature();
  humadity2 = dht2.readHumidity();
  temprature2 = dht2.readTemperature();
/* Sensors */

}//setup ended
 
void loop() 
{
  char x;
  
/* Receiving instruction from WiFi' serial */
    while (Serial3.available()) 
   {
     x=Serial3.read();
     WiFi_instruction.concat(x);       
   } 

Serial.println("WiFi instructions"); 
     /* Check for any instruction*/
     WiFi_instruction="";// WiFi_instruction is empty now.
   }//if WiFi_instruction.length()!-0
/* Receiving instruction from WiFi' serial */

/* Yl-69 Sensors */
     Soil_humadity1=digitalRead(YL1);//If it return 1, soil is ok otherwise not
     Soil_humadity2=digitalRead(YL2);
     Soil_humadity3=digitalRead(YL3);
/* Yl-69 Sensors */

/* AS2301 Sensors's Data */   
float temp_temprature1,temp_temprature2,temp_humadity1,temp_humadity2;
temp_temprature1= dht1.readTemperature();
temp_temprature2= dht2.readTemperature();
temp_humadity1=dht1.readHumidity();
temp_humadity2=dht2.readHumidity();

//Whether data is NaN or 0 or some none sense increment-decrement, new numbers won't be considered

   if(isnan(temp_temprature1) ||isnan(temp_temprature2)||
      isnan(temp_humadity1)||isnan(temp_humadity2))
 {
      /* Do nothing */
        
 }
   else
 {
   if(temp_temprature1!=0 && 
     temp_temprature2!=0&& 
     temp_humadity1!=0 &&  
     temp_humadity2!=0)
      {
      if(abs(dht1.readHumidity()-humadity1)<10 
      && abs(dht2.readHumidity()-humadity2)<10
      && abs(dht1.readTemperature()-temprature1)<10
      && abs(dht2.readTemperature()-temprature2)<10) 
            {
      Serial.print(" Sensor values have been changed ");
      humadity1 = dht1.readHumidity();
      temprature1 = dht1.readTemperature();
      humadity2 = dht2.readHumidity();
      temprature2 = dht2.readTemperature();
            }
      }
 }
/* AS2301 Sensors's Data */ 

/* Check for any SMS */


          while(Serial1.available()) 
          {
          x=Serial1.read();
          SMSmessage.concat(x);
          }
        Serial.println("SMS:"); 
        Serial.println(SMSmessage);
      if(SMSmessage.length()>0)
      {
 
       int valid1=SMSmessage.indexOf(num1);
       int valid2=SMSmessage.indexOf(num2);
       if(valid1==0 || valid2==0)
       {
       Serial.println("number is valid");  
       valid=true;
       }
       /*Extranct the number to andwer */
       if(valid1==0)
       number=num1;
       if(valid2==0)
       number=num2;
       /*Extranct the number to andwer */
/*Check for any sms */

/* Answering via SMS */
//If there is the Code for a specific Instruction it return 1 otherwise returns -1
   if(valid)
   {
   inst00=SMSmessage.indexOf(inst0);
   inst01_CHECK=SMSinst01=SMSmessage.indexOf(inst1);//automatic
   inst02_CHECK=SMSinst02=SMSmessage.indexOf(inst2);
   inst03_CHECK=SMSinst03=SMSmessage.indexOf(inst3);
   inst04_CHECK=SMSinst04=SMSmessage.indexOf(inst4);
   if(SMSinst02 ==-1 && SMSinst03==-1 && SMSinst04==-1)// if none of the non-automatic in selected it mast remain automatic
    SMSinst01=0; 
   inst05_CHECK=SMSinst05=SMSmessage.indexOf(inst5);//automatic
   inst06_CHECK=SMSinst06=SMSmessage.indexOf(inst6);
   inst07_CHECK=SMSinst07=SMSmessage.indexOf(inst7);
   inst08_CHECK=SMSinst08=SMSmessage.indexOf(inst8);
   if(SMSinst06 ==-1 && SMSinst07==-1 && SMSinst08==-1)// if none of the non-automatic in selected it mast remain automatic
    SMSinst05=0; 
   inst09_CHECK=SMSinst09=SMSmessage.indexOf(inst9);//automatic
   inst010_CHECK=SMSinst010=SMSmessage.indexOf(inst10);
   inst011_CHECK=SMSinst011=SMSmessage.indexOf(inst11);
   if(SMSinst010 ==-1 && SMSinst011==-1)// if none of the non-automatic in selected it mast remain automatic
    SMSinst09=0; 
   inst012_CHECK=SMSinst012=SMSmessage.indexOf(inst12);//automatic
   inst013_CHECK=SMSinst013=SMSmessage.indexOf(inst13);
   inst014_CHECK=SMSinst014=SMSmessage.indexOf(inst14);
   if(SMSinst013 ==-1 && SMSinst014==-1)// if none of the non-automatic in selected it mast remain automatic
    inst012=0; 
   inst015_CHECK=SMSinst015=SMSmessage.indexOf(inst15);//automatic
   inst016_CHECK=SMSinst016=SMSmessage.indexOf(inst16);
   inst017_CHECK=SMSinst017=SMSmessage.indexOf(inst17);
   if(SMSinst016 ==-1 && SMSinst017==-1)// if none of the non-automatic in selected it mast remain automatic
    SMSinst015=0; 
   inst018_CHECK=SMSinst018=SMSmessage.indexOf(inst18);//automatic
   inst019_CHECK=SMSinst019=SMSmessage.indexOf(inst19);
   inst020_CHECK=SMSinst020=SMSmessage.indexOf(inst20);
   if(SMSinst019 ==-1 && SMSinst020==-1)// if none of the non-automatic in selected it mast remain automatic
    SMSinst018=0; 
   inst021_CHECK=SMSinst021=SMSmessage.indexOf(inst21);//automatic
   inst022_CHECK=SMSinst022=SMSmessage.indexOf(inst22);
   inst023_CHECK=SMSinst023=SMSmessage.indexOf(inst23);
   if(SMSinst022 ==-1 && SMSinst023==-1)// if none of the non-automatic in selected it mast remain automatic
    SMSinst021=0; 
   inst024_CHECK=SMSinst024=SMSmessage.indexOf(inst24);//automatic
   inst025_CHECK=SMSinst025=SMSmessage.indexOf(inst25);
   inst026_CHECK=SMSinst026=SMSmessage.indexOf(inst26);
   if(SMSinst025 ==-1 && SMSinst026==-1)// if none of the non-automatic in selected it mast remain automatic
    SMSinst024=0; 
   inst027_CHECK=SMSinst027=SMSmessage.indexOf(inst27);//automatic
   inst028_CHECK=SMSinst028=SMSmessage.indexOf(inst28);
   inst029_CHECK=SMSinst029=SMSmessage.indexOf(inst29);
   if(SMSinst028 ==-1 && SMSinst029==-1)// if none of the non-automatic in selected it mast remain automatic
    SMSinst027=0; 
  SMSmessage="";//SMSmessage now is empty until next sms
   }//if valid
     }// if SMS.length()
Serial.println("SMS instructions"); 
if(inst00)
{
  String statSoil1,statSoil2,statSoil3;
  /* YL, Soil's humadity Sensors results */
  

}//Loop ends


/*Functions */
/*

 void Serialcom()
{
delay(500);
while(Serial.available()) // IDE serial l serial Serial1
{
Serial1.write(Serial.read());//Forward what Serial received to Software Serial Port
}
while(Serial1.available()) //serialSerial1 l serial dial IDE
{

Serial.write(Serial1.read());//Forward what Software Serial received to Serial Port
}

}
/*Functions */
  • If your problem is sms related, reduce your code to only involve on sms, you will then find what's wrong with the code. BTW, don't use String and String concatenation, learn to use [cstring](http://www.cplusplus.com/reference/cstring/) functions. The way you do `while(Serial1.available()) {x=Serial1.read(); SMSmessage.concat(x); }` caused heap fragmentation and will soon crash your program. – hcheung Oct 21 '20 at 01:20
  • tnx. now code is working. I add a couple of delays and it worked fine. I'm going to add Cstrings. – maryam mohammadbagheri Oct 25 '20 at 13:42

0 Answers0