0

I'm working on a simple home automation project.
This is part of the code related to the GSM module I'm using in my project.
The code seems a bit messy but you can just skip to the question without even checking the code.

#include <SoftwareSerial.h>  
SoftwareSerial gsm(8, 9);  
String sender;  
byte slaveAddr = 1;    
String validSender = "132xxx\","; // i replaced it with my number  


void setup(){  
Serial.begin(9600);  
gsm.begin(9600);  
}  


void loop(){   
while (gsm.available()) {    
    byte data = gsm.read();    
    Serial.write(data);  
    evaluateMessage(data);  
  }  
}  


void evaluateMessage( byte data ) {    
  gsmBuffer[gsmBufPos++] = data;  

  switch ( gsmState ) {  
    case skipData: {  
        if ( data == '"' ) {  
          memset(gsmBuffer, 0, sizeof(gsmBuffer));  
          gsmBufPos = 0;            
        }   
        break;  
      }  
    case readPhoneNumber: {  
        if ( data == ',' ) {  
          sender = gsmBuffer;  
          gsmState = readDate;  
          memset(gsmBuffer, 0, sizeof(gsmBuffer));  
          gsmBufPos = 0;            
        }  
        break;  
      }  
    case readDate: {  
        if ( data == '\n' ) {  
          gsmState = readContent;          
          memset(gsmBuffer, 0, sizeof(gsmBuffer));  
          gsmBufPos = 0;            
        }  
        break;  
      }  
    case readContent: {  
        if ( data == '\n' ) {  
          checkSmsContent();  
          memset(gsmBuffer, 0, sizeof(gsmBuffer));  
          gsmState = skipData;  
          gsmBufPos = 0;  
          sender = "";  
        }  
        break;  
      }  
  }  
}  


void checkSmsContent(){  
  if ( sender == validSender ){  
    Serial.println("Valid Sender");  
    if ( gsmBuffer[0] == 'H' && gsmBuffer[1] == '1' ){        
        Wire.beginTransmission(slaveAddr);  
        Wire.write("H1");  
        Wire.endTransmission(slaveAddr);  
        Serial.println("Status Sent");  
      } else if ( gsmBuffer[0] == 'H' && gsmBuffer[1] == '0'){  
        Wire.beginTransmission(slaveAddr);  
        Wire.write("H0");  
        Wire.endTransmission(slaveAddr);  
      }  
    }  
    delay(1000);  
  }      


I have two atmega28p s connected in I2C. This is the code for the master   Arduino. Here's how it works. I send "H1" to or "H0" to the sim card int the gsm module and this is sent back to the I2C slave Arduino to turn light on or off respectively. I've proper code int the slave that does this and the I2C   connection is correct.  

<b>Here comes the problem.</b> When I upload the code for the first time it works fine.  
I send "H1" to turn the light on and "H0" to turn it off. There is also some text on the LCD part of the larger code ( the complete project).  
Then I'll unplug the power supply and plug it back and wait until the module connects to the network ( assume light has gone off). This time neither "H1" nor "H0" does work.  

I did this multiple times yet doesn't work. Now I'll upload the following sketch to the master ( where the gsm is hooked up to ) and open the Serial monitor to see what is happening.  


#include <SoftwareSerial.h>  
SoftwareSerial gsm ( 8, 9);  
void setup() {  
  Serial.begin(9600);  
  gsm.begin(9600);  
}  

void loop() {  
  while(Serial.available()){  
    gsm.write(Serial.read());  
  }  
  while( gsm.available()){  
    Serial.write(gsm.read());  
  }  

}  

I upload this code to the master, the Arduino IDE says "done uploading". But the text from the previous code ( the code for the complete project ) still is displayed on the LCD ( I guess this should disappear since the above code has nothing to do with the LCD).

When I test the AT commands none of them Work and when I send some text from my phone number to gsm the inverted question mark ('⸮') displays in the Serial port.
I'll unplug the power source and plug it back, wait until the gsm connects to the network. Then the text on the LCD disappears( as expected ) and the AT commands work.

I'll upload the previous code (the complete project code) and send "H1" or "H0" and it works fine.

The problem comes when I unplug the source ( light goes off ) it doesn't work unless I unplug the power supply and re-upload the code. What's the problem?
Thanks in advance!

  • 1
    please use the {} button to format the code ...... also, format the code with correct indentation – jsotola Feb 24 at 4:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.