You are absolutely right.
I also had to change byte to int in this line:
static int ndx = 0;
After changing these two lines, the code works as expected.
Here is the final code for the receiver:
const int numCharsSlave = 700;
char receivedSlaveChars[numCharsSlave];
// Data received from sensor system
int sensor_rain = 0;
float sensor_light = 0.00;
float sensor_hum = 0.00;
float sensor_temp = 0.00;
// Data received from the Internet
float API_rain = 0.00;
float API_temp = 0.00;
float API_tempFeels = 0.00;
int API_hum = 0;
char API_wind[60] = "";
char API_weather[60] = "";
char API_text_tomorrow[160] = "";
char API_text_tomorrow_night[160] = "";
boolean newData = false; // invert this on Slave arduino
static boolean recvInProgress = false;
static int ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
void setup() {
Serial.begin(9600);
Serial3.begin(9600);
}
void loop() {
recvWithStartEndMarkers();
if (newData == true) {
parseData();
showParsedData();
newData = false;
}
}
void recvWithStartEndMarkers() {
while (Serial3.available() > 0 && newData == false) {
rc = Serial3.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedSlaveChars[ndx] = rc;
ndx++;
if (ndx >= numCharsSlave) {
ndx = numCharsSlave - 1;
}
}
else {
receivedSlaveChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
void parseData() { // split the data into its parts
char * strtokIndx; // this is used by strtok() as an index
strtokIndx = strtok(receivedSlaveChars, "*"); // get the first part - the string
sensor_rain = atoi(strtokIndx);
strtokIndx = strtok(NULL, "*");
sensor_light = atof(strtokIndx);
strtokIndx = strtok(NULL, "*");
sensor_hum = atof(strtokIndx);
strtokIndx = strtok(NULL, "*");
sensor_temp = atof(strtokIndx);
strtokIndx = strtok(NULL, "*");
API_rain = atof(strtokIndx);
strtokIndx = strtok(NULL, "*");
API_temp = atof(strtokIndx);
strtokIndx = strtok(NULL, "*");
API_tempFeels = atof(strtokIndx);
strtokIndx = strtok(NULL, "*");
API_hum = atoi(strtokIndx);
strtokIndx = strtok(NULL, "*");
strcpy(API_wind, strtokIndx);
strtokIndx = strtok(NULL, "*");
strcpy(API_weather, strtokIndx);
strtokIndx = strtok(NULL, "*");
strcpy(API_text_tomorrow, strtokIndx);
strtokIndx = strtok(NULL, "*");
strcpy(API_text_tomorrow_night, strtokIndx);
}
void showParsedData() {
Serial.print("Rain sensor: ");
Serial.println(sensor_rain);
Serial.print("Sensor light: ");
Serial.println(sensor_light);
Serial.print("Sensor humidity: ");
Serial.println(sensor_hum);
Serial.print("Sensor temperature: ");
Serial.println(sensor_temp);
Serial.print("API rain: ");
Serial.println(API_rain);
Serial.print("API temperature: ");
Serial.println(API_temp);
Serial.print("API temperature feels: ");
Serial.println(API_tempFeels);
Serial.print("API humidity: ");
Serial.println(API_hum);
Serial.print("API wind: ");
Serial.println(API_wind);
Serial.print("API weather: ");
Serial.println(API_weather);
Serial.print("API Text tomorrow: ");
Serial.println(API_text_tomorrow);
Serial.print("API Text tomorrow night: ");
Serial.println(API_text_tomorrow_night);
}
Final code for sender:
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11);//rx tx
// Data received from sensor system
int sensor_rain = 1111;
float sensor_light = 60000.22;
float sensor_hum = 100.33;
float sensor_temp = 100.44;
// Data received from the Internet
float API_rain = 100.55;
float API_temp = 100.66;
float API_tempFeels = 100.77;
int API_hum = 8888;
char API_wind[60] = "12345678901234567890123456789012345678901234567890123456789";
char API_weather[60] = "12345678901234567890123456789012345678901234567890123456789";
char API_text_tomorrow[160] = "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789";
char API_text_tomorrow_night[160] = "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789";
void setup() {
Serial.begin(9600);
mySerial.begin(9600);
Serial.println(sizeof(sensor_rain)+sizeof(sensor_light)+sizeof(sensor_hum)+sizeof(sensor_temp)+sizeof(API_rain)+sizeof(API_temp)+sizeof(API_tempFeels)+sizeof(API_hum)+sizeof(API_wind)+sizeof(API_weather)+sizeof(API_text_tomorrow)+sizeof(API_text_tomorrow_night));
}
void loop() {
sendDataToMaster ();
delay(15000);
}
void sendDataToMaster () {
mySerial.print('<');
mySerial.print(sensor_rain);
mySerial.print('*');
mySerial.print(sensor_light);
mySerial.print('*');
mySerial.print(sensor_hum);
mySerial.print('*');
mySerial.print(sensor_temp);
mySerial.print('*');
mySerial.print(API_rain);
mySerial.print('*');
mySerial.print(API_temp);
mySerial.print('*');
mySerial.print(API_tempFeels);
mySerial.print('*');
mySerial.print(API_hum);
mySerial.print('*');
mySerial.print(API_wind);
mySerial.print('*');
mySerial.print(API_weather);
mySerial.print('*');
mySerial.print(API_text_tomorrow);
mySerial.print('*');
mySerial.print(API_text_tomorrow_night);
mySerial.print('>');
}
And this is the output:
Rain sensor: 1111
Sensor light: 60000.22
Sensor humidity: 100.33
Sensor temperature: 100.44
API rain: 100.55
API temperature: 100.66
API temperature feels: 100.77
API humidity: 8888
API wind: 12345678901234567890123456789012345678901234567890123456789
API weather: 12345678901234567890123456789012345678901234567890123456789
API Text tomorrow: 123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
API Text tomorrow night: 123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
Thanks everyone very much for your help and patience. I clearly could not have done this without you guys. I appreciate it much.