0

I'm trying to use a SIM800L and Ethernet shield in one program. Separately they work perfectly, but they do not work together.

When I put the following code in the Setup method, the SIM800L can't receive an SMS.

Ethernet.begin(mac, ip, gateway, subnet);
server.begin();

There is a SIM800L example to receive SMS that works perfectly:

    #include <SoftwareSerial.h>
    SoftwareSerial sim800(10,11);  // (Rx,Tx  > Tx,Rx) 

    char incomingByte; 
    String sms;

    //define relay pins
    int relay_1 = 8;

    void setup() 
    {
      //Initial state of the relay
      pinMode(relay_1, OUTPUT);
      digitalWrite(relay_1, HIGH); 
      
      Serial.begin(9600);
      sim800.begin(9600); 

     while(!sim800.available()){
        sim800.println("AT");
        delay(1000); 
        Serial.println("Connecting...");
        }
      Serial.println("Connected!");  

      sim800.println("AT+CMGF=1");  //Set SMS to Text Mode 
      delay(1000);  
      sim800.println("AT+CNMI=1,2,0,0,0");  //Procedure to handle newly arrived 
     messages(command name in text: new message indications to TE) 
     }

    void loop()
    {  
      if(sim800.available()){
       delay(100);
      // Serial Buffer
       while(sim800.available()){
        incomingByte = sim800.read();
        sms += incomingByte; 
        }
        delay(10);  
        Serial.println(sms);
        sms.toUpperCase(); // Uppercase the Received Message

        //turn RELAY_1 ON or OFF
        if (sms.indexOf("RELAY1 OFF") > -1){
          digitalWrite(relay_1, HIGH);
          }
         if (sms.indexOf("RELAY1 ON") > -1){
          digitalWrite(relay_1, LOW);
          }

        delay(1000);}
        //clear sms
        sms = "";
      }

There is an Ethernet shield web server example that also works:

     #include <SPI.h>
     #include <Ethernet.h>

     int relay_1  = 8;

     int pos = 0; 
     byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };   //physical mac address
     byte ip[] = { 192, 168, 1, 178 };                      // ip in lan (that's what you 
     need to use in your browser. ("192.168.1.178")
     byte gateway[] = { 192, 168, 1, 1 };                   // internet access via router
     byte subnet[] = { 255, 255, 255, 0 };                  //subnet mask
     EthernetServer server(80);                             //server port     
     String readString;

     void setup() {
      // Open serial communications and wait for port to open:
      Serial.begin(9600);
       while (!Serial) {
       ; // wait for serial port to connect. Needed for Leonardo only
      }
      pinMode(relay_1 , OUTPUT);
      digitalWrite(relay_1 , LOW);

     // start the Ethernet connection and the server:
      Ethernet.begin(mac, ip, gateway, subnet);
      server.begin();
      Serial.print("server is at ");
      Serial.println(Ethernet.localIP());
      }


      void loop() {
      // Create a client connection
      EthernetClient client = server.available();
      if (client) {
        while (client.connected()) {   
          if (client.available()) {
            char c = client.read();
            //read char by char HTTP request
            if (readString.length() < 100) {
              //store characters to string
            readString += c;
            //Serial.print(c);
         }

         //if HTTP request has ended
         if (c == '\n') {          
           Serial.println(readString); //print to serial monitor for debuging
     
           client.println("HTTP/1.1 200 OK"); //send new page
           client.println("Content-Type: text/html");
           client.println();     
           client.println("<HTML>");
           client.println("<HEAD>");
           client.println("<meta name='apple-mobile-web-app-capable' content='yes' />");
           client.println("<meta name='apple-mobile-web-app-status-bar-style' 
           content='black-translucent' />");
           client.println("<TITLE>ethernet shield server</TITLE>");
           client.println("</HEAD>");
           client.println("<BODY>");
           client.println("<H1>Ethernet project Project</H1>");
           client.println("<hr />");
           client.println("<br />");  
           client.println("<H2>Arduino with Ethernet Shield</H2>");
           client.println("<br />");  
           client.println("<a href=\"/?relay1on\"\">Turn On LED</a>");
           client.println("<a href=\"/?relay1off\"\">Turn Off LED</a><br />");   
           client.println("</BODY>");
           client.println("</HTML>");
     
           delay(1);
           //stopping client
           client.stop();
           //controls the Arduino if you press the buttons
           if (readString.indexOf("?relay1on") >0){
               digitalWrite(led, HIGH);
           }
           if (readString.indexOf("?relay1off") >0){
               digitalWrite(led, LOW);
           }
            //clearing string for next read
            readString="";  
           
         }
       }
    }
    }
    }

How can I combine them in one functioning program?

JRE
  • 67,678
  • 8
  • 104
  • 179
vito
  • 111
  • 2
  • 1
    You've chosen to use pins 10 and 11 for your software serial communications with the SIM800L. Those two pins are part of the SPI bus used to communicate with the Ethernet controller. There might be something else wrong, but that's a conflict you need to fix. – JRE Apr 17 '22 at 23:16
  • @JRE, how do you know what Arduino model it is? – Juraj Apr 18 '22 at 09:05
  • @JRE Which pin can I use? I am using Arduino Mega – vito Apr 18 '22 at 09:11
  • on Mega use hardware Serial. Mega has 4 of them. (pin 10 is CS pin of the Ethernet shield) – Juraj Apr 19 '22 at 04:59

0 Answers0