Union of two programmes

Good morning everyone, to speed things up I’ll give you a brief summary of the problem I have. I have two programs that if loaded separately work and do what I want, the problem now is to put them together in a single program. The first program is used to read incoming messages with the Sim800L, the other sends a signal to a solar controller to turn it on and off. The idea would be to send a message ON or OFF and Arduino turns the controller on and off.

I tried to do that:

//-------------------------------------------
#include <ModbusMaster.h>

#define MAX485_DE      10
#define MAX485_RE_NEG  11

// instantiate ModbusMaster object
ModbusMaster node;

void preTransmission()
{
  digitalWrite(MAX485_RE_NEG, 1);
  digitalWrite(MAX485_DE, 1);
}

void postTransmission()
{
  digitalWrite(MAX485_RE_NEG, 0);
  digitalWrite(MAX485_DE, 0);
}
//----------------------------------------
char incomingByte; 
String inputString;
int relay = 13; // Output for Relay Control
void setup() {
      //--------------------------------------------
       pinMode(MAX485_RE_NEG, OUTPUT);
  pinMode(MAX485_DE, OUTPUT);
  // Init in receive mode
  digitalWrite(MAX485_RE_NEG, 0);
  digitalWrite(MAX485_DE, 0);

  // Modbus communication runs at 115200 baud
  Serial2.begin(115200);

  // Modbus slave ID 1
  node.begin(1, Serial2);
  // Callbacks allow us to configure the RS485 transceiver correctly
  node.preTransmission(preTransmission);
  node.postTransmission(postTransmission);
  //-----------------------------------------------------
      pinMode(relay, OUTPUT);
      digitalWrite(relay, LOW); // Initial state of the relay
      Serial.begin(9600);
      Serial1.begin(9600); 

     while(!Serial1.available()){
        Serial1.println("AT");
        delay(1000); 
        Serial.println("Connecting...");
        }
      Serial.println("Connected!");  
      Serial1.println("AT+CMGF=1");  //Set SMS to Text Mode 
      delay(1000);  
      Serial1.println("AT+CNMI=1,2,0,0,0");  //Procedure to handle newly arrived messages(command name in text: new message indications to TE) 
      delay(1000);
      Serial1.println("AT+CMGL=\"REC UNREAD\""); // Read Unread Messages
     }
     bool state = true;

void loop() {  
  if(Serial1.available()){
      delay(100);

      // Serial Buffer
      while(Serial1.available()){
        incomingByte = Serial1.read();
        inputString += incomingByte; 
        }

        delay(10);      

        Serial.println(inputString);
        inputString.toUpperCase(); // Uppercase the Received Message

        //turn RELAY ON or OFF
        if (inputString.indexOf("ON") > -1){
          digitalWrite(relay, HIGH);
          uint8_t result;
          uint16_t data[6];
  
           // Toggle the coil at address 0x0002 (Manual Load Control)
          result = node.writeSingleCoil(0x0002, state);
          state = !state;
          delay(1000);
          }
         if (inputString.indexOf("OFF") > -1){
          digitalWrite(relay, LOW);
          uint8_t result;
          uint16_t data[6];
  
           // Toggle the coil at address 0x0002 (Manual Load Control)
          result = node.writeSingleCoil(0x0002, state);
          state = !state;
          delay(1000);
          }          

        delay(50);

        //Delete Messages & Save Memory
        if (inputString.indexOf("OK") == -1){
        Serial1.println("AT+CMGDA=\"DEL ALL\"");

        delay(1000);}

        inputString = "";
  }
}

As you can see, I only joined the two programs and I inserted the command to turn on the controller under ON and OFF, unfortunately this does not work …

This is what I read on the Serial Monitor:

Connecting...
Connected!
AT

OK
AT+CMGF=1

OK
AT+CNMI=1,2,0,0,0

OK
AT+CMGL="REC U

In Attachment I put the two programs so if someone wants to help me has the two programs working. Hoping for help I wish everyone a good day :slight_smile:

MegaLetturaSMS.ino (1.56 KB)

AccendiSpegniMega2560.ino (2.2 KB)

You can’t use Serial for both Modbus and debugging. Here I have moved Modbus to Serial2. You will have to move the wiring of your Modbus interface chip.

#include <ModbusMaster.h>


#define MAX485_DE      10
#define MAX485_RE_NEG  11


// instantiate ModbusMaster object
ModbusMaster node;


char incomingByte;
String inputString;


void setup()
{
  pinMode(MAX485_RE_NEG, OUTPUT);
  pinMode(MAX485_DE, OUTPUT);
  
  // Init in receive mode
  digitalWrite(MAX485_RE_NEG, 0);
  digitalWrite(MAX485_DE, 0);


  // Modbus communication runs at 115200 baud
  Serial2.begin(115200);


  // Modbus slave ID 1
  node.begin(1, Serial2);
  // Callbacks allow us to configure the RS485 transceiver correctly
  node.preTransmission(preTransmission);
  node.postTransmission(postTransmission);


  Serial.begin(9600);
  Serial1.begin(9600);


  while (!Serial1.available())
  {
    Serial1.println("AT");
    delay(1000);
    Serial.println("Connecting...");
  }
  Serial.println("Connected!");
  Serial1.println("AT+CMGF=1");  //Set SMS to Text Mode
  delay(1000);
  Serial1.println("AT+CNMI=1,2,0,0,0");  //Procedure to handle newly arrived messages(command name in text: new message indications to TE)
  delay(1000);
  Serial1.println("AT+CMGL=\"REC UNREAD\""); // Read Unread Messages
}


void loop()
{
  if (Serial1.available())
  {
    delay(100);


    // Serial Buffer
    while (Serial1.available())
    {
      incomingByte = Serial1.read();
      inputString += incomingByte;
    }


    delay(10);


    Serial.println(inputString);
    inputString.toUpperCase(); // Uppercase the Received Message


    //turn RELAY ON or OFF
    if (inputString.indexOf("ON") > -1)
    {
      node.writeSingleCoil(0x0002, HIGH);
    }
    
    if (inputString.indexOf("OFF") > -1)
    {
      node.writeSingleCoil(0x0002, LOW);
    }


    delay(50);


    //Delete Messages & Save Memory
    if (inputString.indexOf("OK") == -1)
    {
      Serial1.println("AT+CMGDA=\"DEL ALL\"");


      delay(1000);
    }


    inputString = "";
  }
}








/*!
  We're using a MAX485-compatible RS485 Transceiver.
  Rx/Tx is hooked up to the hardware serial port at 'Serial'.
  The Data Enable and Receiver Enable pins are hooked up as follows:
*/




void preTransmission()
{
  digitalWrite(MAX485_RE_NEG, 1);
  digitalWrite(MAX485_DE, 1);
}


void postTransmission()
{
  digitalWrite(MAX485_RE_NEG, 0);
  digitalWrite(MAX485_DE, 0);
}

Thank you so much for your help, I tried the program and it works :slight_smile: