Serial Input with start-end markers - Problem on data received

Hello. I have one mega and 2 uno. Obviously mega is the central unit. Uno1 and Uno2 take some measurements and send them through serial to mega. Mega make some decisions and send data through serial again to Uno2 and other data to Uno1.

Uno2 when sends data to mega arrives perfectly. For example:
B2 REC: -1.0,-1.0,0.00,0.00,91,11.8,34.4,-1.0,7.8,0
And also receives data always perfectly.

The problem is uno1. Sometimes the message arrives correct:
For example: 19.0,50.5,19.0,50.5,19.0,50.5,19.0,50.5
But many times arrives half and wrong: 10,500.0.0,19.0,50.0
Others with the start marker inside: 18.9,49.6,18.9,49<18.9,49.4,18.9,49.4,18.

I 've made a lot of experiment with rate and delays inside the code but i didn’t had a success.

I 've added some code to reset ndx to 0 if i receive start marker again, but then i have the half message.

The code on mega is identical to example from https://forum.arduino.cc/index.php?topic=396450.0
It is huge and complex so it is not necessary to post it all…

Below is attached the code from uno1 that i have the problem.

Mega:

//Serial1 Board1 Receive Connection
const byte numChars1 = 42;
char receivedChars1[numChars1];
char tempChars1[numChars1];
boolean newData1 = false;

void loop() {
board1_receive();
	if (newData1 == true) {
		strcpy(tempChars1, receivedChars1);
		Serial4.print("B1 REC: ");
		Serial4.println(receivedChars1);
		b1_parseData();
		newData1 = false;
    }
}

void board1_receive() {
	static boolean recvInProgress1 = false;
	static byte ndx1 = 0;
	char startMarker1 = '<';
	char endMarker1 = '>';
	char rc1;

	while (Serial1.available() > 0 && newData1 == false) {
		rc1 = Serial1.read();
		if (recvInProgress1 == true) {
			if (rc1 != endMarker1) {
				receivedChars1[ndx1] = rc1;
				ndx1++;
				if (ndx1 >= numChars1) {
					ndx1 = numChars1 - 1;
				}
			}
			else {
				receivedChars1[ndx1] = '\0';
				recvInProgress1 = false;
				ndx1 = 0;
				newData1 = true;
			}
		}
        else if (rc1 == startMarker1) {
			recvInProgress1 = true;
		}
	}
}

//------------------------>

void b1_parseData() {
	char * strtokIndx1; // this is used by strtok() as an index

	strtokIndx1 = strtok(tempChars1,",");
	S1 = atof(strtokIndx1);

	strtokIndx1 = strtok(NULL, ",");
	S2 = atof(strtokIndx1);

	strtokIndx1 = strtok(NULL, ",");
	S3 = atof(strtokIndx1);

	strtokIndx1 = strtok(NULL, ",");
	S4 = atof(strtokIndx1);

	strtokIndx1 = strtok(NULL, ",");
	S5 = atof(strtokIndx1);

	strtokIndx1 = strtok(NULL, ",");
	S6 = atof(strtokIndx1);

	strtokIndx1 = strtok(NULL, ",");
	S7 = atof(strtokIndx1);

	strtokIndx1 = strtok(NULL, ",");
	S8 = atof(strtokIndx1);
}

Board1.ino (8.64 KB)

The Mega program you have posted only receives data from board1. In your next Reply please post the program that receives data from board1 and board2.

Also, please post the Uno code in another Reply so we don't have to download it.

...R

I cannot post code of Uno, exceeds limit…

Mega:

//Serial1 Board1 Receive Connection
const byte numChars1 = 42;
char receivedChars1[numChars1];
char tempChars1[numChars1];
boolean newData1 = false;

//Serial3 Board3 Receive Connection
const byte numChars3 = 48;
char receivedChars3[numChars3];
char tempChars3[numChars3];
boolean newData3 = false;

void loop() {
	board1_receive();
	if (newData1 == true) {
		strcpy(tempChars1, receivedChars1);
		Serial4.print("B1 REC: ");
		Serial4.println(receivedChars1);
		b1_parseData();
		newData1 = false;
    }

	board3_receive();
	if (newData3 == true) {
        strcpy(tempChars3, receivedChars3);
		Serial4.print("B3 REC: ");
		Serial4.println(receivedChars3);
        b3_parseData();
        newData3 = false;
    }
// a lot of code here
// Serial Sends
	board1_send();
	board3_send();
}


void board1_receive() {
	static boolean recvInProgress1 = false;
	static byte ndx1 = 0;
	char startMarker1 = '<';
	char endMarker1 = '>';
	char rc1;

	while (Serial1.available() > 0 && newData1 == false) {
		rc1 = Serial1.read();
		if (recvInProgress1 == true) {
			if (rc1 != endMarker1) {
				receivedChars1[ndx1] = rc1;
				ndx1++;
				if (ndx1 >= numChars1) {
					ndx1 = numChars1 - 1;
				}
			}
			else {
				receivedChars1[ndx1] = '\0'; // terminate the string
				recvInProgress1 = false;
				ndx1 = 0;
				newData1 = true;
			}
		}
        else if (rc1 == startMarker1) {
			recvInProgress1 = true;
		}
	}
}

//------------------------>

void b1_parseData() {
	char * strtokIndx1; // this is used by strtok() as an index

	strtokIndx1 = strtok(tempChars1,",");
	S1 = atof(strtokIndx1);

	strtokIndx1 = strtok(NULL, ",");
	S2 = atof(strtokIndx1);

	strtokIndx1 = strtok(NULL, ",");
	S3 = atof(strtokIndx1);

	strtokIndx1 = strtok(NULL, ",");
	S4 = atof(strtokIndx1);

	strtokIndx1 = strtok(NULL, ",");
	S5 = atof(strtokIndx1);

	strtokIndx1 = strtok(NULL, ",");
	S6 = atof(strtokIndx1);

	strtokIndx1 = strtok(NULL, ",");
	S7 = atof(strtokIndx1);

	strtokIndx1 = strtok(NULL, ",");
	S8 = atof(strtokIndx1);
}

void board3_receive() {
	static boolean recvInProgress3 = false;
	static byte ndx3 = 0;
	char startMarker3 = '<';
	char endMarker3 = '>';
	char rc3;

	while (Serial3.available() > 0 && newData3 == false) {
		rc3 = Serial3.read();
		if (recvInProgress3 == true) {
			if (rc3 != endMarker3) {
				receivedChars3[ndx3] = rc3;
				ndx3++;
				if (ndx3 >= numChars3) {
					ndx3 = numChars3 - 1;
				}
			}
			else {
				receivedChars3[ndx3] = '\0'; // terminate the string
				recvInProgress3 = false;
				ndx3 = 0;
				newData3 = true;
			}
		}
        else if (rc3 == startMarker3) {
			recvInProgress3 = true;
		}
	}
}

//------------------------>

void b3_parseData() {
	char * strtokIndx3;

	strtokIndx3 = strtok(tempChars3,",");
	S11 = atof(strtokIndx3);

	strtokIndx3 = strtok(NULL, ",");
	S12 = atof(strtokIndx3);

	strtokIndx3 = strtok(NULL, ",");
	S31 = atof(strtokIndx3);
	
	strtokIndx3 = strtok(NULL, ",");
	S32 = atof(strtokIndx3);
	
	strtokIndx3 = strtok(NULL, ",");
	S33 = atoi(strtokIndx3);
	
	strtokIndx3 = strtok(NULL, ",");
	S34 = atof(strtokIndx3);
	
	strtokIndx3 = strtok(NULL, ",");
	S35 = atof(strtokIndx3);
	
	strtokIndx3 = strtok(NULL, ",");
	S36 = atof(strtokIndx3);
	
	strtokIndx3 = strtok(NULL, ",");
	S37 = atof(strtokIndx3);
	
	strtokIndx3 = strtok(NULL, ",");
	S56 = atoi(strtokIndx3);
}
//------------------------>

void board1_send() {
	Serial1.print("<");
	Serial1.print(S43);
	Serial1.print(",");
	Serial1.print(S9,1);
	Serial1.print(",");
	Serial1.print(S10,1);
	Serial1.print(",");
	Serial1.print(S11,1);
	Serial1.print(",");
	Serial1.print(S12,1);
	Serial1.print(",");
	Serial1.print(S13,1);
	Serial1.print(",");
	Serial1.print(S14,1);
	Serial1.print(",");
	Serial1.print(S33,1);
	Serial1.print(",");
	Serial1.print(S35,1);
	Serial1.print(",");
	Serial1.print(S36,1);
	Serial1.print(",");
	Serial1.print(S47);
	Serial1.print(",");
	Serial1.print(S56);
	Serial1.print(">");
	Serial1.println();
	Serial4.println("Board1 Data Send");
}

//------------------------>

void board3_send() {
	Serial3.print("<");
	Serial3.print(S51);
	Serial3.print(",");
	Serial3.print(S52);
	Serial3.print(",");
	Serial3.print(S53);
	Serial3.print(",");
	Serial3.print(S54);
	Serial3.print(",");
	Serial3.print(S43);
	Serial3.print(">");
	Serial3.println();
	Serial4.println("Board3 Data Send");
}

I can eliminate the error when the start marker is in the middle of the message, but i don’t know if this is wise to do…

void board1_receive() {
	static boolean recvInProgress1 = false;
	static byte ndx1 = 0;
	char startMarker1 = '<';
	char endMarker1 = '>';
	char rc1;

	while (Serial1.available() > 0 && newData1 == false) {
		rc1 = Serial1.read();
		if (recvInProgress1 == true) {
			if (rc1 != endMarker1) {
				receivedChars1[ndx1] = rc1;
				ndx1++;
				if (ndx1 >= numChars1) {
					ndx1 = numChars1 - 1;
				}
				if (rc1 == startMarker1){		//new
					ndx1=0;				//new
					continue;			//new
				}
			}
			else {
				receivedChars1[ndx1] = '\0'; // terminate the string
				recvInProgress1 = false;
				ndx1 = 0;
				newData1 = true;
			}
		}
        else if (rc1 == startMarker1) {
			recvInProgress1 = true;
		}
	}
}

Uno code from Original Post

// Board 1 (LCD)

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <LCD.h>
#include <SoftwareSerial.h>
#include <DHTesp.h>
#include <SHT31.h>

SoftwareSerial Serial1(2, 3);

//SHT30
SHT31 sht1;
SHT31 sht2;
float C1, C2, C3, C4;

//DHT22
DHTesp dht_tbe;
DHTesp dht_tro;
int tbe = 4;
int tro = 5;
float C5, C6, C7, C8;

//Relay
int relay_alert=6;

//LCD Screen
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); 

// variables to hold the parsed data
float C9, C10, C11, C12, C13, C14, C33, C35, C36;
int C43, C47, C56;

//Serial1 Board2 Receive Connection
const byte numChars = 53;
char receivedChars[numChars];
char tempChars[numChars];
boolean newData = false;

//Global
int l, k;

//============>

void setup() {
  Serial.begin(115200);
  Serial1.begin(9600);

//DHT22
  dht_tbe.setup(tbe, DHTesp::DHT22);
  dht_tro.setup(tro, DHTesp::DHT22);

//SHT31
  Wire.begin();
  sht1.begin(0x44);
  sht2.begin(0x45);
  
//Relay
  pinMode(relay_alert, OUTPUT);
  digitalWrite(relay_alert, HIGH);

//LCD
  lcd.begin(20,4);
  lcd.backlight();

//Global
  l = k = 0;
}

//============>

void loop() {

//info (START)
  Serial1.println();
  Serial1.println("Restart Loop");
  Serial1.print("Loop ");
  Serial1.print(l);
  Serial1.print(" Sec:");
  int time=(int)(millis()/1000);
  Serial1.println(time);

// ********** MEASUREMENTS **********

//SHT31
  C1=sht1.getTemperature();
  C2=sht1.getHumidity();
  C3=sht2.getTemperature();
  C4=sht2.getHumidity();

//DHT22
  C5 = dht_tbe.getTemperature();
  C6 = dht_tbe.getHumidity();
  if(isnan(C5)){C5 = 0;}
  if(isnan(C6)){C6 = 0;}
  C7 = dht_tro.getTemperature();
  C8 = dht_tro.getHumidity();
  if(isnan(C7)){C7 = 0;}
  if(isnan(C8)){C8 = 0;}

  C1=C3=C7=C5;                  //REMOVE LATER
  C2=C4=C8=C6;                  //REMOVE LATER
  
//C9-C10-C11-C12-C13-C14-C33-C34-C35-C47 Receive
  board2_receive();

//ParseData
  if (newData == true) {
    strcpy(tempChars, receivedChars);
    b2_parseData();

    newData = false;
    k = 0;
  }
  else {
    delay_function();
    k++;
  }
  delay(100);
  
//Serial Send
  serial_send();

//Serial Print
  serial_print();
  
//LCD Function
  lcd_function();

//Conditions  
  if(C47 == 1){
    digitalWrite(relay_alert, HIGH);
  }
  else digitalWrite(relay_alert, LOW);
}

//============>

void board2_receive() {
  static boolean recvInProgress = false;
  static byte ndx = 0;
  char startMarker = '<';
  char endMarker = '>';
  char rc;

  while (Serial.available() > 0 && newData == false) {
    rc = Serial.read();

    if (recvInProgress == true) {
      if (rc != endMarker) {
        receivedChars[ndx] = rc;
        ndx++;
        if (ndx >= numChars) {
          ndx = numChars - 1;
        }
      }
      else {
        receivedChars[ndx] = '\0';
        recvInProgress = false;
        ndx = 0;
        newData = true;
      }
    }
    else if (rc == startMarker) {
    recvInProgress = true;
    }
  }
  
  Serial1.print("SR-Data Received:");
  Serial1.println(receivedChars);
}

//============>

void b2_parseData() {

  char * strtokIndx2;

  strtokIndx2 = strtok(tempChars,",");
  C43 = atoi(strtokIndx2);

  strtokIndx2 = strtok(NULL, ",");
  C9 = atof(strtokIndx2);

  strtokIndx2 = strtok(NULL, ",");
  C10 = atof(strtokIndx2);
  
  strtokIndx2 = strtok(NULL, ",");
  C11 = atof(strtokIndx2);
  
  strtokIndx2 = strtok(NULL, ",");
  C12 = atof(strtokIndx2);
  
  strtokIndx2 = strtok(NULL, ",");
  C13 = atof(strtokIndx2);
  
  strtokIndx2 = strtok(NULL, ",");
  C14 = atof(strtokIndx2);
  
  strtokIndx2 = strtok(NULL, ",");
  C33 = atof(strtokIndx2);
  
  strtokIndx2 = strtok(NULL, ",");
  C35 = atof(strtokIndx2);
  
  strtokIndx2 = strtok(NULL, ",");
  C36 = atof(strtokIndx2);
  
  strtokIndx2 = strtok(NULL, ",");
  C47 = atoi(strtokIndx2);
  
  strtokIndx2 = strtok(NULL, ",");
  C56 = atoi(strtokIndx2);
}

//============>

void serial_send(){
  Serial.print("<");
  Serial.print(C1,1);
  Serial.print(",");
  Serial.print(C2,1);
  Serial.print(",");
  Serial.print(C3,1);
  Serial.print(",");
  Serial.print(C4,1);
  Serial.print(",");
  Serial.print(C5,1);
  Serial.print(",");
  Serial.print(C6,1);
  Serial.print(",");
  Serial.print(C7,1);
  Serial.print(",");
  Serial.print(C8,1);
  Serial.print(">");
  Serial.println();
  Serial1.println("Serial Data Send");

}

//============>

void serial_print(){
  Serial1.print("S1=");
  Serial1.print(C1);
  Serial1.print(" C-TLR S2=");
  Serial1.print(C2);
  Serial1.println(" %-HLR");
  Serial1.print("S3=");
  Serial1.print(C3);
  Serial1.print(" C-TKO S4=");
  Serial1.print(C4);
  Serial1.println(" %-HKO");
  Serial1.print("S5=");
  Serial1.print(C5);
  Serial1.print(" C-TBE S6=");
  Serial1.print(C6);
  Serial1.println(" %-HBE");
  Serial1.print("S7=");
  Serial1.print(C7);
  Serial1.print(" C-TRO S8=");
  Serial1.print(C8);
  Serial1.println(" %-HRO");
  Serial1.print("S9=");
  Serial1.print(C9);
  Serial1.print(" C-TCR S10=");
  Serial1.print(C10);
  Serial1.println(" %-HCR");
  Serial1.print("S11=");
  Serial1.print(C11);
  Serial1.print(" C-TBR S12=");
  Serial1.print(C12);
  Serial1.println(" %-HBR");
  Serial1.print("S13=");
  Serial1.print(C13);
  Serial1.print(" C-TOU S14=");
  Serial1.print(C14);
  Serial1.println(" %-HOU");
  Serial1.println();
  Serial1.print("S33=");
  Serial1.print(C33);
  Serial1.println("cm - Oil Level");
  Serial1.print("S35=");
  Serial1.print(C35);
  Serial1.print(" C-TWB S36=");
  Serial1.print(C36);
  Serial1.println(" C-TWS");
  
  Serial1.print("Boiler: ");
  if(C43 == 1){Serial1.println("On");}
  else Serial1.println("Off");
  Serial1.print("Lalert: ");
  if(C47 == 1){Serial1.println("On");}
  else Serial1.println("Off");
  Serial1.println();
  Serial1.print("Water Selection: ");
  if(C56 == 0){Serial1.println("Boiler");}
  else Serial1.println("Sun");
  Serial1.println();
}

//==========>

void lcd_function(){
  Serial1.println("LCD Start");

  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Oil Level");
  lcd.setCursor(0,1);
  lcd.print(C33);
  lcd.setCursor(5,1);
  lcd.print("cm");
  delay(1000);
  
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Boiler Temperature");
  lcd.setCursor(0,1);
  lcd.print(C35);
  lcd.setCursor(4,1);
  lcd.print("\xDF" "C");
  if(C56 == 0){
    lcd.setCursor(7,1);
    lcd.print("<--");
  }
  lcd.setCursor(13,1);
  if(C43 == 1){
    lcd.print("On");
  }
  else lcd.print("Off");
  lcd.setCursor(0,2);
  lcd.print("Solar  Temperature");
  lcd.setCursor(0,3);
  lcd.print(C36);
  lcd.setCursor(4,3);
  lcd.print("\xDF" "C");
  if(C56 == 0){
    lcd.setCursor(7,1);
    lcd.print("<--");
  }
  delay(3000);

  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("LR:");
  lcd.setCursor(4,0);
  lcd.print(C1);
  lcd.setCursor(8,0);
  lcd.print("\xDF" "C");
  lcd.setCursor(12,0);
  lcd.print(C2);
  lcd.setCursor(16,0);
  lcd.print("%");
  lcd.setCursor(0,1);
  lcd.print("KO:");
  lcd.setCursor(4,1);
  lcd.print(C3);
  lcd.setCursor(8,1);
  lcd.print("\xDF" "C");
  lcd.setCursor(12,1);
  lcd.print(C4);
  lcd.setCursor(16,1);
  lcd.print("%");
  lcd.setCursor(0,2);
  lcd.print("BE:");
  lcd.setCursor(4,2);
  lcd.print(C5);
  lcd.setCursor(8,2);
  lcd.print("\xDF" "C");
  lcd.setCursor(12,2);
  lcd.print(C6);
  lcd.setCursor(16,2);
  lcd.print("%");
  lcd.setCursor(0,3);
  lcd.print("RO:");
  lcd.setCursor(4,3);
  lcd.print(C7);
  lcd.setCursor(8,3);
  lcd.print("\xDF" "C");
  lcd.setCursor(12,3);
  lcd.print(C8);
  lcd.setCursor(16,3);
  lcd.print("%");
  delay(3000);
  
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("CR:");
  lcd.setCursor(4,0);
  lcd.print(C9);
  lcd.setCursor(8,0);
  lcd.print("\xDF" "C");
  lcd.setCursor(12,0);
  lcd.print(C10);
  lcd.setCursor(16,0);
  lcd.print("%");
  lcd.setCursor(0,1);
  lcd.print("BR:");
  lcd.setCursor(4,1);
  lcd.print(C11);
  lcd.setCursor(8,1);
  lcd.print("\xDF" "C");
  lcd.setCursor(12,1);
  lcd.print(C12);
  lcd.setCursor(16,1);
  lcd.print("%");
  lcd.setCursor(0,2);
  lcd.print("OU:");
  lcd.setCursor(4,2);
  lcd.print(C13);
  lcd.setCursor(8,2);
  lcd.print("\xDF" "C");
  lcd.setCursor(12,2);
  lcd.print(C14);
  lcd.setCursor(16,2);
  lcd.print("%");
  
  delay(2000);
  
  Serial1.println("LCD END");
}

//==========>

void delay_function(){
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Receiving");
  delay (1000);
  if(k > 10){
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Problem...");
    delay (10000);
  }
}

//=========END

…R

gordona1:
I cannot post code of Uno, exceeds limit..

Strange. I had no problem.

And your Mega code in Reply #2 is not a complete program. Please post the complete program.

...R

As i said it is a bit big program. I removed a lot unnecessary…

#include <SPI.h>
#include <Ethernet2.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <DHTesp.h>
#include <EmonLib.h>
#include <Adafruit_BME280.h>
#include <Max44009.h>
#include <SoftwareSerial.h>
#include <DFRobot_LCD.h>

//Ethernet section
byte mac[] = {0xA8, 0x61, 0x0A, 0xAE, 0x69, 0x69 };
IPAddress ip (192,168,5,252);
IPAddress myDns {192, 168, 5, 240};
IPAddress gateway {192, 168, 5, 254 };                 
IPAddress subnet {255, 255, 255, 0 };  
EthernetClient client;

//LCD Screen
DFRobot_LCD lcd(16,2);

//SoftSerial
SoftwareSerial Serial4(12, 13);

//Temperature & Humidity (House)
float S1, S2, S3, S4, S5, S6, S7, S8;

//Temperature & Humidity DHT22 CR
DHTesp dht_cr;
int temp_cr = 38;
float S9, S10;

//Temperature & Humidity DHT22 BR (Boiler Room)
float S11, S12;

//Temperature & Humidity & Pressure BME280 OUT
Adafruit_BME280 bme;
float S13, S14, S15;

//Lux Section
Max44009 Lux(0x4A);
float S16;

//Power consumption Board 0
float S20, S21, S22, S23, S24, S25, S26, S27, S28;

//Boiler Room Sensors
float S31, S32, S34, S35, S36, S37;
int S33, S51, S52, S53, S54, S56;

//vref
long S40;

//Arduino Temp
OneWire ds1(34);
//uint8_t S41_sensor[8] = { 0x28, 0xAA, 0x7D, 0x1F, 0x58, 0x14, 0x01, 0x79 };
DallasTemperature dsa(&ds1);
float S41;

//Power consumption Rack
EnergyMonitor emon1;
#define current_calibration 17.5
double S42;

//Relay
int relay1 = 27;	//Boiler
int relay2 = 28;	//Ventillation
int relay3 = 35;	//Arduino Fan
int relay4 = 23;	//External Lights
int S43, S44, S45, S46;

//Board 1 Variables
int S47;

//Serial1 Board1 Receive Connection
const byte numChars1 = 42;
char receivedChars1[numChars1];
char tempChars1[numChars1];
boolean newData1 = false;

//Serial2 Board0 Receive Connection
const byte numChars0 = 50;
char receivedChars0[numChars0];
char tempChars0[numChars0];
boolean newData0 = false;

//Serial3 Board3 Receive Connection
const byte numChars3 = 48;
char receivedChars3[numChars3];
char tempChars3[numChars3];
boolean newData3 = false;

//mySQL
byte server[] = { 192, 168, 5, 242 };
 
//Global
int c, k, l;

//Boilers Section
int b;
unsigned long startMillis;
unsigned long currentMillis;
const unsigned long period = 3600000;

//Heat Section
const byte numCharsTP = 20;
char receivedCharsTP[20];
char tempCharsTP[20];
boolean newDataTP = false;
float TLR, TKO, TBE, TRO;

//------------------------>

void setup() {

//Serials
	Serial.begin(9600);
	Serial1.begin(9600);
	Serial2.begin(9600);
	Serial3.begin(9600);
	Serial4.begin(9600);
	
// Ethernet Section
	Ethernet.begin(mac, ip, myDns, gateway, subnet);
	Serial4.print("My IP address: ");
	Serial4.println(Ethernet.localIP());
	delay(500);

//LCD Screen
	lcd.init();

//Temperature & Humidity DHT22 CR
	dht_cr.setup(temp_cr, DHTesp::DHT22);
	
//Temperature & Humidity DHT22 OUT
	bme.begin(0x76);

//DS18B20 Sensors
	dsa.begin();
	
//Relay
	pinMode(relay1, OUTPUT);
	digitalWrite(relay1, HIGH);
	pinMode(relay2, OUTPUT);
	digitalWrite(relay2, HIGH);
	pinMode(relay3, OUTPUT);
	digitalWrite(relay3, HIGH);
	pinMode(relay4, OUTPUT);
	digitalWrite(relay4, HIGH);

//Global
	k = 0;
	l = 0;
    
//Boiler
	b = 0;
	startMillis = millis();  //initial start time

//Power Consumption
	emon1.current(15, current_calibration);

// Vref
	S40 = readVcc();

// Lux
	Lux.setManualMode(0, 188);
	delay(500);
}

//------------------------>

void loop() {
//info (START)
	Serial4.println();
	Serial4.println("Restart Loop");
	Serial4.print("Loop ");
	Serial4.print(l);
	Serial4.print(" Sec:");
	int time=(int)(millis()/1000);
	Serial4.println(time);

// ********** MEASUREMENTS **********

// S1-S2-S3-S4-S5-S6-S7-S8 Temperatures & Humidity House
	board1_receive();
	if (newData1 == true) {
		strcpy(tempChars1, receivedChars1);
		Serial4.print("B1 REC: ");
		Serial4.println(receivedChars1);
		b1_parseData();
		newData1 = false;
    }
	
// S9-S10 Temperature & Humidity DHT22 CR (START)
	S9 = dht_cr.getTemperature();
	S10 = dht_cr.getHumidity();
	if(isnan(S9)){S9 = 0;}
	if(isnan(S10)){S10 = 0;}
	
// S13-S14-S15 Temperature & Humidity & Pressure BME OUT (START)
	S13 = bme.readTemperature();
	S14 = bme.readHumidity();
	S15 = bme.readPressure() / 100.0F;
	if(isnan(S13)){S13 = 0;}
	if(isnan(S14)){S14 = 0;}
	if(isnan(S15)){S15 = 0;}

// S16 Light
	S16 = Lux.getLux() + 30;
	
// S20 Voltage, S21 L1 Current, S22 L2 Current, S23 L3 Current, S24 L Current, S25 pf
	//board0_receive();
	if (newData0 == true) {
        strcpy(tempChars0, receivedChars0);
		Serial4.print("B0 REC: ");
		Serial4.println(receivedChars0);
        b0_parseData();
        newData0 = false;
    }
	S20 = S21 = S22 = S23 = S24 = S25 = S26 = S27 = S28 = -1;		//NOT CONNECTED YET
	
// S31-S32-S33-S34-S35-S36-S37-S56 Boiler Room - Board 3 Receive
	board3_receive();
	if (newData3 == true) {
        strcpy(tempChars3, receivedChars3);
		Serial4.print("B3 REC: ");
		Serial4.println(receivedChars3);
        b3_parseData();
        newData3 = false;
    }
	
// S41 Arduino Temperature (START)
	dsa.requestTemperatures();
	S41 = dsa.getTempCByIndex(0);
	
//	S42 Power Consumption
	S42 = power();
	delay(2);
	
// Temperatures Selection
	get_temp_pref();
	if (newDataTP == true) {
        strcpy(tempCharsTP, receivedCharsTP);
        heatpref_parsedata();
        newDataTP = false;
    }

// ********** CONDITIONS **********

// LED Alert
	if(S42 > 1000){S47=1;}
	if(S42 < 1000){S47=0;}
  
// Fan Section
	if(S41>27){S45=1;}
	if(S41<25){S45=0;}
	
// Ventillation Section
	if(S13<18 && S13>12){
		if(S9>22){S44=1;}
		if(S9<20){S44=0;}
	}
	if(S13<12 && S13>6){
		if(S9>20){S44=1;}
		if(S9<16){S44=0;}
	}
	if(S13<6){
		if(S9>18){S44=1;}
		if(S9<14){S44=0;}
	}
	
//Heat Section
	if(S1<TLR-0.2){S51=1;}
	if(S1>TLR+0.2){S51=0;}
	
	if(S3<TKO-0.2){S52=1;}
	if(S3>TKO+0.2){S52=0;}
	
	if(S5<TBE-0.2){S53=1;}
	if(S5>TBE+0.2){S53=0;}
	
	if(S7<TRO-0.2){S54=1;}
	if(S7>TRO+0.2){S54=0;}
	
// External Lights
	if(S16<10){					//DO
		S46=0;
	}
	if(S16>50){
		S46=1;
	}

Continue from previous post…

// ********** FUNCTIONS **********
// Boiler Section
	boiler_function();

// Relay Status
	relay_arming();
	relay_status();

// mySQL Section
	if(k == 10){
		mysql_data();
		S40 = readVcc();
		k=0;     
	}
	k++;

// Serial Sends
	board1_send();
	board3_send();

// Serial Prints
	serial_prints();
	
// Loop Counter
	if(l==32767){
		l=0;
	}
	l++;

// LCD - Delay
	lcd_function();
	delay_function();
}

//------------------------>

long readVcc() {
//unnecessary
}

//------------------------>

void board1_receive() {
	static boolean recvInProgress1 = false;
	static byte ndx1 = 0;
	char startMarker1 = '<';
	char endMarker1 = '>';
	char rc1;

	while (Serial1.available() > 0 && newData1 == false) {
		rc1 = Serial1.read();
		if (recvInProgress1 == true) {
			if (rc1 != endMarker1) {
				receivedChars1[ndx1] = rc1;
				ndx1++;
				if (ndx1 >= numChars1) {
					ndx1 = numChars1 - 1;
				}
				if (rc1 == startMarker1){		//new
					ndx1=0;						//new
					continue;					//new
				}
			}
			else {
				receivedChars1[ndx1] = '\0'; // terminate the string
				recvInProgress1 = false;
				ndx1 = 0;
				newData1 = true;
			}
		}
        else if (rc1 == startMarker1) {
			recvInProgress1 = true;
		}
	}
}

//------------------------>

void b1_parseData() {
	char * strtokIndx1; // this is used by strtok() as an index

	strtokIndx1 = strtok(tempChars1,",");
	S1 = atof(strtokIndx1);

	strtokIndx1 = strtok(NULL, ",");
	S2 = atof(strtokIndx1);

	strtokIndx1 = strtok(NULL, ",");
	S3 = atof(strtokIndx1);

	strtokIndx1 = strtok(NULL, ",");
	S4 = atof(strtokIndx1);

	strtokIndx1 = strtok(NULL, ",");
	S5 = atof(strtokIndx1);

	strtokIndx1 = strtok(NULL, ",");
	S6 = atof(strtokIndx1);

	strtokIndx1 = strtok(NULL, ",");
	S7 = atof(strtokIndx1);

	strtokIndx1 = strtok(NULL, ",");
	S8 = atof(strtokIndx1);
}

//------------------------>

void board0_receive() {
//NOT YET
}

//------------------------>

void b0_parseData() {
//NOT YET
}

//------------------------>

void board3_receive() {
	static boolean recvInProgress3 = false;
	static byte ndx3 = 0;
	char startMarker3 = '<';
	char endMarker3 = '>';
	char rc3;

	while (Serial3.available() > 0 && newData3 == false) {
		rc3 = Serial3.read();
		if (recvInProgress3 == true) {
			if (rc3 != endMarker3) {
				receivedChars3[ndx3] = rc3;
				ndx3++;
				if (ndx3 >= numChars3) {
					ndx3 = numChars3 - 1;
				}
			}
			else {
				receivedChars3[ndx3] = '\0'; // terminate the string
				recvInProgress3 = false;
				ndx3 = 0;
				newData3 = true;
			}
		}
        else if (rc3 == startMarker3) {
			recvInProgress3 = true;
		}
	}
}

//------------------------>

void b3_parseData() {
	char * strtokIndx3;

	strtokIndx3 = strtok(tempChars3,",");
	S11 = atof(strtokIndx3);

	strtokIndx3 = strtok(NULL, ",");
	S12 = atof(strtokIndx3);

	strtokIndx3 = strtok(NULL, ",");
	S31 = atof(strtokIndx3);
	
	strtokIndx3 = strtok(NULL, ",");
	S32 = atof(strtokIndx3);
	
	strtokIndx3 = strtok(NULL, ",");
	S33 = atoi(strtokIndx3);
	
	strtokIndx3 = strtok(NULL, ",");
	S34 = atof(strtokIndx3);
	
	strtokIndx3 = strtok(NULL, ",");
	S35 = atof(strtokIndx3);
	
	strtokIndx3 = strtok(NULL, ",");
	S36 = atof(strtokIndx3);
	
	strtokIndx3 = strtok(NULL, ",");
	S37 = atof(strtokIndx3);
	
	strtokIndx3 = strtok(NULL, ",");
	S56 = atoi(strtokIndx3);
}

//------------------------>

double power() {
	double Irms = emon1.calcIrms(5588); // Calculate Irms only
	if(Irms < 0.5){
		Irms = 0;
	}
	return (Irms*230);
}

//------------------------>

void get_temp_pref() {
//unnecessary (Connects to web server to get temperature preferences)
}

//------------------------>

void heatpref_parsedata(){
//unnecessary (parses data from webserver)
}

//------------------------>

void boiler_function() {
// unnecessary (huge code to control hot water)
}

//------------------------>

void relay_arming() {
	if(S43==1){digitalWrite(relay1, LOW);}
	else digitalWrite(relay1, HIGH);
	delay(200);
	if(S44==1){digitalWrite(relay2, LOW);}
	else digitalWrite(relay2, HIGH);
	delay(200);
	if(S45==1){digitalWrite(relay3, LOW);}
	else digitalWrite(relay3, HIGH);
	delay(200);
	if(S46==1){digitalWrite(relay4, LOW);}
	else digitalWrite(relay4, HIGH);
	delay(200);
}

//------------------------>

void relay_status() {
// unnecessary (updates webserver with relay status)
}

//------------------------>

void mysql_data() {
//unnecessary (update mysql with all data)
}

//------------------------>

void board1_send() {
	Serial1.print("<");
	Serial1.print(S43);
	Serial1.print(",");
	Serial1.print(S9,1);
	Serial1.print(",");
	Serial1.print(S10,1);
	Serial1.print(",");
	Serial1.print(S11,1);
	Serial1.print(",");
	Serial1.print(S12,1);
	Serial1.print(",");
	Serial1.print(S13,1);
	Serial1.print(",");
	Serial1.print(S14,1);
	Serial1.print(",");
	Serial1.print(S33,1);
	Serial1.print(",");
	Serial1.print(S35,1);
	Serial1.print(",");
	Serial1.print(S36,1);
	Serial1.print(",");
	Serial1.print(S47);
	Serial1.print(",");
	Serial1.print(S56);
	Serial1.print(">");
	Serial1.println();
	Serial4.println("Board1 Data Send");
}

//------------------------>

void board3_send() {
	Serial3.print("<");
	Serial3.print(S51);
	Serial3.print(",");
	Serial3.print(S52);
	Serial3.print(",");
	Serial3.print(S53);
	Serial3.print(",");
	Serial3.print(S54);
	Serial3.print(",");
	Serial3.print(S43);
	Serial3.print(">");
	Serial3.println();
	Serial4.println("Board3 Data Send");
}

//------------------------>

void serial_prints() {
// unnecessary (all the info)
}

//------------------------>

void lcd_function(){
// unnecessary (again a lot of code for lcd )
}

//------------------------>

void delay_function() {
	Serial4.print("Delay 1...");
	delay(1000);
	Serial4.print("2...");
	delay(1000);
	Serial4.print("3...");
	delay(1000);
	Serial4.print("4...");
	delay(1000);
	Serial4.print("5...");
	delay(1000);
	Serial4.print("6...");
	delay(1000);
	Serial4.print("7...");
	delay(1000);
	Serial4.print("8...");
	delay(1000);
	Serial4.print("9...");
	delay(1000);
	//Serial4.print("10...");
	//delay(1000);
}

//------------------------> END

It would be a big help if you can produce a much shorter Mega program that does nothing except receive data from the two Unos and print the received data and parse the received data.

I notice that you have changed the variable names here

void board1_receive() {
	static boolean recvInProgress1 = false;
	static byte ndx1 = 0;
	char startMarker1 = '<';
	char endMarker1 = '>';
	char rc1;

by adding a ‘1’ to the names. That is unnecessary. These are all local variable that are only relevant for this function. A local variable in another function the same name will be treated as completely separate.

You do, of course, need separate names for the global variables such as receivedChars1

I have also noticed that you have some large delay()s in some of the functions called from loop(). They are very likely to upset the serial input. You should be calling the boardX_receive() functions very frequently.

The functions delay() and delayMicroseconds() block the Arduino until they complete.
Have a look at how millis() is used to manage timing without blocking in Several Things at a Time.

And see Using millis() for timing. A beginners guide if you need more explanation.

…R

At first thank you for the answer.

  1. I can try only receive and send, but let me try it in a few days.
  2. I know that they are local variables but in my eyes it is easier for me.
  3. I don't have delays in my functions, except the relay arming, and this is because the relays controls 220v relays, which controls some heavy loads and i noticed that (mostly at the beginning) if they armed simultaneously a fuse might drop. Worst if the relays disarmed at once, well you know...
    I have a large (9 sec) delay at the very end of the loop, because for me it is not necessary for a quicker loop. i just want to take all my measurements, take decisions and every about 90sec to update mysql database. The other 2 (in the future 3) arduino has lcd to produce specific information which takes 8-9 sec to appear all of them. So i believed that it was not necessary to have quicker loops in the main. Allow me to read and think about it..

The code added in replay #4 eliminates the problem completely. I am using millis for timing in other functions. If the large delay causes problems i can fix this by making different things on every loop but keeping receives in any loop..

Please give me some time to try these suggestions..

Thank you.

The 9 second delay is almost certainly the problem. Put the code you want executed at 9 second intervals in a function and call that function at 9 second intervals. Let loop() run constantly so you can call the serial receive functions often enough to prevent buffer overflow.

gordona1:
3. I don't have delays in my functions, except the relay arming, and this is because the relays controls 220v relays, which controls some heavy loads and i noticed that (mostly at the beginning) if they armed simultaneously a fuse might drop. Worst if the relays disarmed at once, well you know...

Timing intervals are often needed. Just don't use the delay() function to cause them.

I have a large (9 sec) delay at the very end of the loop, because for me it is not necessary for a quicker loop.

This is a big conceptual mistake. Your code should ensure that loop() repeats many times per second - like a 100 times or more.

If there is something that only needs to happen at 9 second intervals then you can program for that using millis()

...R

Look at my tutorial on Multi-tasking in Arduino, it includes a loopTimer so you can check the 'delay' in your code
For replacing 'delay's check out my How to Write Timers and Delays in Arduino

Also check out my Text I/O for the Real World which has examples of adding extra RX buffering and shows how to get non-blocking Serial debug so you can see what your code is doing without breaking it due to the delay introduced by the debug prints();

Thank you all for your answers. Let me study your suggestions and redesign my programs.. I will come back to post my progress..

Hi,
I had a similar problem with a work project, my start marker was being corrupted.

My solution was to change the start marker, my problem had to do with the start marker boolean ASCII beginning 0010,
I chose a character that started 0101 and that fixed the problem.
I was not using the UART built software version.

You use “<”, which is 3C hex and 0011 1100

I was using “*”, which is 2A hex and 0010 1010
I changed to “[” which is 5B hex and 0101 1011

Just a thought, it may or may not help.

Tom… :slight_smile:

Hello to all and thank you once again for your interest.

I removed all the delays in my programs. I am using timers now as you suggested. I 've created 1 sec interval and calling every n sec my functions. I left the "receive" inside loop. I did this on both arduinos.

I still have the same problem. Problem disappears completely if i just add:

if (rc1 == startMarker1){
	ndx1=0;
	continue;
}

to reset the receiving progress.

TomGeorge your suggestion is my next priority.

gordona1:
I still have the same problem.

Without seeing the revised programs I can't comment.

...R

The Sender:

// Board 1 (LCD)

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <LCD.h>
#include <SoftwareSerial.h>
#include <DHTesp.h>
#include <SHT31.h>

SoftwareSerial Serial1(2, 3);

//SHT30
SHT31 sht1;
SHT31 sht2;
float C1, C2, C3, C4;

//DHT22
DHTesp dht_tbe;
DHTesp dht_tro;
int tbe = 4;
int tro = 5;
float C5, C6, C7, C8;

//Relay
int relay_alert=6;

//LCD Screen
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); 

// variables to hold the parsed data
float C9, C10, C11, C12, C13, C14, C35, C36;
int C33, C43, C47, C56;

//Serial1 Board2 Receive Connection
const byte numChars = 53;
char receivedChars[numChars];
char tempChars[numChars];
boolean newData = false;

//Global
int l;

//Timers
unsigned long start1;
unsigned long start2;
unsigned long currenttime;

//============>

void setup() {
	Serial.begin(9600);
	Serial1.begin(9600);

//DHT22
	dht_tbe.setup(tbe, DHTesp::DHT22);
	dht_tro.setup(tro, DHTesp::DHT22);

//SHT31
	Wire.begin();
	sht1.begin(0x44);
	sht2.begin(0x45);
	
//Relay
	pinMode(relay_alert, OUTPUT);
	digitalWrite(relay_alert, HIGH);

//LCD
	lcd.begin(20,4);
	lcd.backlight();

//Global
	l =  0;

//Timer
	start1 = start2 = millis();
}

//============>

void loop() {
	currenttime = millis();
	
	if(currenttime - start1 >= 1000){
	// ********** MEASUREMENTS **********
	//SHT31
		C1=sht1.getTemperature();
		C2=sht1.getHumidity();
		C3=sht2.getTemperature();
		C4=sht2.getHumidity();

	//DHT22
		C5 = dht_tbe.getTemperature();
		C6 = dht_tbe.getHumidity();
		if(isnan(C5)){C5 = 0;}
		if(isnan(C6)){C6 = 0;}
		C7 = dht_tro.getTemperature();
		C8 = dht_tro.getHumidity();
		if(isnan(C7)){C7 = 0;}
		if(isnan(C8)){C8 = 0;}

		C1=C3=C7=C5;		//REMOVE LATER
		C2=C4=C8=C6;		//REMOVE LATER
	
	//Conditions	
		if(C47 == 1){
			digitalWrite(relay_alert, HIGH);
		}
		else digitalWrite(relay_alert, LOW);
	
	//LCD1
		if(l == 1){lcd1();}
		if(l == 3){lcd2();}
		if(l == 6){lcd3();}
		if(l == 9){lcd4();}
		if(l == 11){l = 0;}
		
	//Serial Send
		serial_send();

	//Serial Print
		if(l % 2 == 0){
			serial_print();
		}
	
		l++;
		
	//Reset Timer
		start1 = millis();
		
	}
	
//C9-C10-C11-C12-C13-C14-C33-C34-C35-C47 Receive
	board2_receive();

//ParseData
	if (newData == true) {
		Serial.print("SR-Data Received:");
		Serial.println(receivedChars);
		strcpy(tempChars, receivedChars);
		b2_parseData();
		newData = false;
		start2 = millis();
	}
	else {
		if(currenttime - start2 >= 15000){
			lcd_problem();
			start2 = millis();
			//resets mega?
		}
	}
}

//============>

void board2_receive() {
	static boolean recvInProgress = false;
	static byte ndx = 0;
	char startMarker = '<';
	char endMarker = '>';
	char rc;

	while (Serial1.available() > 0 && newData == false) {
		rc = Serial1.read();

		if (recvInProgress == true) {
			if (rc != endMarker) {
				receivedChars[ndx] = rc;
				ndx++;
				if (ndx >= numChars) {
					ndx = numChars - 1;
				}
			}
			else {
				receivedChars[ndx] = '\0';
				recvInProgress = false;
				ndx = 0;
				newData = true;
			}
		}
		else if (rc == startMarker) {
		recvInProgress = true;
		}
	}
}

//============>

void b2_parseData() {

	char * strtokIndx2;

	strtokIndx2 = strtok(tempChars,",");
	C43 = atoi(strtokIndx2);

	strtokIndx2 = strtok(NULL, ",");
	C9 = atof(strtokIndx2);

	strtokIndx2 = strtok(NULL, ",");
	C10 = atof(strtokIndx2);
	
	strtokIndx2 = strtok(NULL, ",");
	C11 = atof(strtokIndx2);
	
	strtokIndx2 = strtok(NULL, ",");
	C12 = atof(strtokIndx2);
	
	strtokIndx2 = strtok(NULL, ",");
	C13 = atof(strtokIndx2);
	
	strtokIndx2 = strtok(NULL, ",");
	C14 = atof(strtokIndx2);
	
	strtokIndx2 = strtok(NULL, ",");
	C33 = atoi(strtokIndx2);
	
	strtokIndx2 = strtok(NULL, ",");
	C35 = atof(strtokIndx2);
	
	strtokIndx2 = strtok(NULL, ",");
	C36 = atof(strtokIndx2);
	
	strtokIndx2 = strtok(NULL, ",");
	C47 = atoi(strtokIndx2);
	
	strtokIndx2 = strtok(NULL, ",");
	C56 = atoi(strtokIndx2);
}

//============>

void serial_send(){
	Serial1.print("<");
	Serial1.print(C1,1);
	Serial1.print(",");
	Serial1.print(C2,1);
	Serial1.print(",");
	Serial1.print(C3,1);
	Serial1.print(",");
	Serial1.print(C4,1);
	Serial1.print(",");
	Serial1.print(C5,1);
	Serial1.print(",");
	Serial1.print(C6,1);
	Serial1.print(",");
	Serial1.print(C7,1);
	Serial1.print(",");
	Serial1.print(C8,1);
	Serial1.print(">");
	Serial1.println();
	Serial.println("Serial Data Send");

}

//============>

void serial_print(){
	// Prints the info
}

//==========>
void lcd1(){
	//1st Screen
}

//==========>
void lcd2(){
	//2nd Screen
}

//==========>
void lcd3(){
	//3rd Screen
}

//==========>
void lcd4(){
	//4th Screen
}

//==========>
void lcd_problem(){
	lcd.clear();
	lcd.setCursor(0,0);
	lcd.print("Problem...");
	Serial.println("Problem.............");
	delay (10000);
}

//=========END

The mega (receiver) - Part1:

#include <SPI.h>
#include <Ethernet2.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <DHTesp.h>
#include <EmonLib.h>
#include <Adafruit_BME280.h>
#include <Max44009.h>
#include <SoftwareSerial.h>
#include <DFRobot_LCD.h>

//Ethernet section
byte mac[] = {0xA8, 0x61, 0x0A, 0xAE, 0x69, 0x69 };
IPAddress ip (192,168,5,252);
IPAddress myDns {192, 168, 5, 240};
IPAddress gateway {192, 168, 5, 254 };                 
IPAddress subnet {255, 255, 255, 0 };  
EthernetClient client;

//LCD Screen
DFRobot_LCD lcd(16,2);

//SoftSerial
SoftwareSerial Serial4(12, 13);

//Temperature & Humidity (House)
float S1, S2, S3, S4, S5, S6, S7, S8;

//Temperature & Humidity DHT22 CR
DHTesp dht_cr;
int temp_cr = 38;
float S9, S10;

//Temperature & Humidity DHT22 BR (Boiler Room)
float S11, S12;

//Temperature & Humidity & Pressure BME280 OUT
Adafruit_BME280 bme;
DHTesp dht_out;				//REMOVE
int temp_out = 33;			//REMOVE
float S13, S14, S15;

//Lux Section
Max44009 Lux(0x4A);
float S16;

//Power consumption Board 0
float S20, S21, S22, S23, S24, S25, S26, S27, S28;

//Boiler Room Sensors
float S31, S32, S34, S35, S36, S37;
int S33, S51, S52, S53, S54, S56;

//vref
long S40;

//Arduino Temp
OneWire ds1(34);
//uint8_t S41_sensor[8] = { 0x28, 0xAA, 0x7D, 0x1F, 0x58, 0x14, 0x01, 0x79 };
DallasTemperature dsa(&ds1);
float S41;

//Power consumption Rack
EnergyMonitor emon1;
#define current_calibration 17.5
int S42;

//Relay
int relay1 = 27;	//Boiler
int relay2 = 28;	//Ventillation
int relay3 = 35;	//Arduino Fan
int relay4 = 23;	//External Lights
int S43, S44, S45, S46;

//Board 1 Variables
int S47;

//Serial1 Board1 Receive Connection
const byte numChars1 = 42;
char receivedChars1[numChars1];
char tempChars1[numChars1];
boolean newData1 = false;

//Serial2 Board0 Receive Connection
const byte numChars0 = 50;
char receivedChars0[numChars0];
char tempChars0[numChars0];
boolean newData0 = false;

//Serial3 Board3 Receive Connection
const byte numChars3 = 48;
char receivedChars3[numChars3];
char tempChars3[numChars3];
boolean newData3 = false;

//mySQL
byte server[] = { 192, 168, 5, 242 };
 
//Global
int c, k, l;

//Boilers Section
int b;
unsigned long startMillis;
unsigned long currentMillis;
const unsigned long period = 3600000;

//Heat Section
const byte numCharsTP = 20;
char receivedCharsTP[20];
char tempCharsTP[20];
boolean newDataTP = false;
float TLR, TKO, TBE, TRO;

//Loop Conditions-Timers
unsigned long timer1;
unsigned long currenttime;

//------------------------>

void setup() {

//Serials
	Serial.begin(9600);
	Serial1.begin(9600);
	Serial2.begin(9600);
	Serial3.begin(9600);
	Serial4.begin(9600);
	
// Ethernet Section
	Ethernet.begin(mac, ip, myDns, gateway, subnet);
	Serial4.print("My IP address: ");
	Serial4.println(Ethernet.localIP());
	delay(500);

//LCD Screen
	lcd.init();

//Temperature & Humidity DHT22 CR
	dht_cr.setup(temp_cr, DHTesp::DHT22);
	
//Temperature & Humidity DHT22 OUT
	//bme.begin(0x76);
	dht_out.setup(temp_out, DHTesp::DHT22);		//REMOVE

//DS18B20 Sensors
	dsa.begin();
	
//Relay
	pinMode(relay1, OUTPUT);
	digitalWrite(relay1, HIGH);
	pinMode(relay2, OUTPUT);
	digitalWrite(relay2, HIGH);
	pinMode(relay3, OUTPUT);
	digitalWrite(relay3, HIGH);
	pinMode(relay4, OUTPUT);
	digitalWrite(relay4, HIGH);

//Global
	k = 0;
	l = 0;
    
//Boiler
	b = 0;
	startMillis = millis();  //initial start time

//Power Consumption
	emon1.current(15, current_calibration);

// Vref
	S40 = readVcc();

// Lux
	//Lux.setManualMode(0, 188);

//Timers
	timer1 = millis();
}

//------------------------>

Part2:

void loop() {
	currenttime =	millis();
	if(currenttime - timer1 >= 2000 ){

	// ********** MEASUREMENTS **********
	

	// ********** CONDITIONS **********

	
	// ********** FUNCTIONS **********
	// Boiler Section
		boiler_function();

	// Relay Status (START)
		relay_arming();
		relay_status();

	// mySQL Section (START)
		if(k == 10){
			mysql_data();
			S40 = readVcc();
			k=0;     
		}
		k++;

	// Serial Sends
		board1_send();
		board3_send();

	// Serial Prints
		serial_prints();
		
	// Loop Counter
		if(l==32767){
			l=0;
		}
		l++;

	// LCD - Delay
		lcd_function();
	
	//Timer
		timer1 = millis();
	}
	
	// S1-S2-S3-S4-S5-S6-S7-S8 Temperatures & Humidity House
	board1_receive();
	if (newData1 == true) {
		strcpy(tempChars1, receivedChars1);
		b1_parseData();
		newData1 = false;
	}
	
	// S20 Voltage, S21 L1 Current, S22 L2 Current, S23 L3 Current, S24 L Current, S25 pf
	//board0_receive();
	if (newData0 == true) {
		strcpy(tempChars0, receivedChars0);
		b0_parseData();
		newData0 = false;
	}
	S20 = S21 = S22 = S23 = S24 = S25 = S26 = S27 = S28 = -1;
	
	// S31-S32-S33-S34-S35-S36-S37-S56 Boiler Room - Board 3 Receive
	board3_receive();
	if (newData3 == true) {
		strcpy(tempChars3, receivedChars3);
		b3_parseData();
		newData3 = false;
	}	
}

//------------------------>

long readVcc() {
  //Unnecessary 
}

//------------------------>

void board1_receive() {
	static boolean recvInProgress1 = false;
	static byte ndx1 = 0;
	char startMarker1 = '<';
	char endMarker1 = '>';
	char rc1;

	while (Serial1.available() > 0 && newData1 == false) {
		rc1 = Serial1.read();
		if (recvInProgress1 == true) {
			if (rc1 != endMarker1) {
				receivedChars1[ndx1] = rc1;
				ndx1++;
				if (ndx1 >= numChars1) {
					ndx1 = numChars1 - 1;
				}
				if (rc1 == startMarker1){		//new
					ndx1=0;						//new
					continue;					//new
				}
			}
			else {
				receivedChars1[ndx1] = '\0'; // terminate the string
				recvInProgress1 = false;
				ndx1 = 0;
				newData1 = true;
			}
		}
        else if (rc1 == startMarker1) {
			recvInProgress1 = true;
		}
	}
}

//------------------------>

void b1_parseData() {
	char * strtokIndx1; // this is used by strtok() as an index

	if(l < 10){
		strtokIndx1 = strtok(tempChars1,",");
		S1 = atof(strtokIndx1);

		strtokIndx1 = strtok(NULL, ",");
		S2 = atof(strtokIndx1);

		strtokIndx1 = strtok(NULL, ",");
		S3 = atof(strtokIndx1);

		strtokIndx1 = strtok(NULL, ",");
		S4 = atof(strtokIndx1);

		strtokIndx1 = strtok(NULL, ",");
		S5 = atof(strtokIndx1);

		strtokIndx1 = strtok(NULL, ",");
		S6 = atof(strtokIndx1);

		strtokIndx1 = strtok(NULL, ",");
		S7 = atof(strtokIndx1);

		strtokIndx1 = strtok(NULL, ",");
		S8 = atof(strtokIndx1);
	}
	else{
		float S1t, S2t, S3t, S4t, S5t, S6t, S7t, S8t;
		
		strtokIndx1 = strtok(tempChars1,",");
		S1t = atof(strtokIndx1);

		strtokIndx1 = strtok(NULL, ",");
		S2t = atof(strtokIndx1);

		strtokIndx1 = strtok(NULL, ",");
		S3t = atof(strtokIndx1);

		strtokIndx1 = strtok(NULL, ",");
		S4t = atof(strtokIndx1);

		strtokIndx1 = strtok(NULL, ",");
		S5t = atof(strtokIndx1);

		strtokIndx1 = strtok(NULL, ",");
		S6t = atof(strtokIndx1);

		strtokIndx1 = strtok(NULL, ",");
		S7t = atof(strtokIndx1);

		strtokIndx1 = strtok(NULL, ",");
		S8t = atof(strtokIndx1);
		
		if(S1t >= S1+0.5){S1 = S1t + 0.3;}
		else if(S1t < S1-0.5){S1 = S1t - 0.3;}
		else S1 = S1t;
		if(S2t >= S2+0.5){S2 = S2t + 0.3;}
		else if(S2t < S2-0.5){S2 = S2t - 0.3;}
		else S2 = S2t;
		if(S3t >= S3+0.5){S3 = S3t + 0.3;}
		else if(S3t < S3-0.5){S3 = S3t - 0.3;}
		else S3 = S3t;
		if(S4t >= S4+0.5){S4 = S4t + 0.3;}
		else if(S4t < S4-0.5){S4 = S4t - 0.3;}
		else S4 = S4t;
		if(S5t >= S5+0.5){S5 = S5t + 0.3;}
		else if(S5t < S5-0.5){S5 = S5t - 0.3;}
		else S5 = S5t;
		if(S6t >= S6+0.5){S6 = S6t + 0.3;}
		else if(S6t < S6-0.5){S6 = S6t - 0.3;}
		else S6 = S6t;
		if(S7t >= S7+0.5){S7 = S7t + 0.3;}
		else if(S7t < S7-0.5){S7 = S7t - 0.3;}
		else S7 = S7t;
		if(S8t >= S8+0.5){S8 = S8t + 0.3;}
		else if(S8t < S8-0.5){S8 = S8t - 0.3;}
		else S8 = S8t;
		
	}
}

//------------------------>

void board0_receive() {
	//Not working for now
}
//------------------------>
void b0_parseData() {
	//Not working for now
}
//------------------------>
void board3_receive() {
	static boolean recvInProgress3 = false;
	static byte ndx3 = 0;
	char startMarker3 = '<';
	char endMarker3 = '>';
	char rc3;

	while (Serial3.available() > 0 && newData3 == false) {
		rc3 = Serial3.read();
		if (recvInProgress3 == true) {
			if (rc3 != endMarker3) {
				receivedChars3[ndx3] = rc3;
				ndx3++;
				if (ndx3 >= numChars3) {
					ndx3 = numChars3 - 1;
				}
			}
			else {
				receivedChars3[ndx3] = '\0'; // terminate the string
				recvInProgress3 = false;
				ndx3 = 0;
				newData3 = true;
			}
		}
        else if (rc3 == startMarker3) {
			recvInProgress3 = true;
		}
	}
}
//------------------------>
void b3_parseData() {
	char * strtokIndx3;

	strtokIndx3 = strtok(tempChars3,",");
	S11 = atof(strtokIndx3);

	strtokIndx3 = strtok(NULL, ",");
	S12 = atof(strtokIndx3);

	strtokIndx3 = strtok(NULL, ",");
	S31 = atof(strtokIndx3);
	
	strtokIndx3 = strtok(NULL, ",");
	S32 = atof(strtokIndx3);
	
	strtokIndx3 = strtok(NULL, ",");
	S33 = atoi(strtokIndx3);
	
	strtokIndx3 = strtok(NULL, ",");
	S34 = atof(strtokIndx3);
	
	strtokIndx3 = strtok(NULL, ",");
	S35 = atof(strtokIndx3);
	
	strtokIndx3 = strtok(NULL, ",");
	S36 = atof(strtokIndx3);
	
	strtokIndx3 = strtok(NULL, ",");
	S37 = atof(strtokIndx3);
	
	strtokIndx3 = strtok(NULL, ",");
	S56 = atoi(strtokIndx3);
}
//------------------------>
double power() {
	//Unnecessary 
}
//------------------------>
void get_temp_pref() {
	//Unnecessary 
}
//------------------------>
void heatpref_parsedata(){
	//Unnecessary 
}
//------------------------>
void boiler_function() {
	//Unnecessary 
}
//------------------------>
void relay_arming() {
	if(S43==1){digitalWrite(relay1, LOW);}
	else digitalWrite(relay1, HIGH);
	delay(200);
	if(S44==1){digitalWrite(relay2, LOW);}
	else digitalWrite(relay2, HIGH);
	delay(200);
	if(S45==1){digitalWrite(relay3, LOW);}
	else digitalWrite(relay3, HIGH);
	delay(200);
	if(S46==1){digitalWrite(relay4, LOW);}
	else digitalWrite(relay4, HIGH);
	delay(200);
}
//------------------------>
void relay_status() {
	//Unnecessary 	
}
//------------------------>
void mysql_data() {
	//Unnecessary 
}
//------------------------>
void board1_send() {
	Serial1.print("<");
	Serial1.print(S43);
	Serial1.print(",");
	Serial1.print(S9,1);
	Serial1.print(",");
	Serial1.print(S10,1);
	Serial1.print(",");
	Serial1.print(S11,1);
	Serial1.print(",");
	Serial1.print(S12,1);
	Serial1.print(",");
	Serial1.print(S13,1);
	Serial1.print(",");
	Serial1.print(S14,1);
	Serial1.print(",");
	Serial1.print(S33,1);
	Serial1.print(",");
	Serial1.print(S35,1);
	Serial1.print(",");
	Serial1.print(S36,1);
	Serial1.print(",");
	Serial1.print(S47);
	Serial1.print(",");
	Serial1.print(S56);
	Serial1.print(">");
	Serial1.println();
}
//------------------------>
void board3_send() {
	Serial3.print("<");
	Serial3.print(S51);
	Serial3.print(",");
	Serial3.print(S52);
	Serial3.print(",");
	Serial3.print(S53);
	Serial3.print(",");
	Serial3.print(S54);
	Serial3.print(",");
	Serial3.print(S43);
	Serial3.print(">");
	Serial3.println();
}
//------------------------>
void serial_prints() {
	//
}
//------------------------>
void lcd_function(){
	//Unnecessary 
}
//------------------------> END