Receiver:
const byte numCharsSlave = 500;
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[55] = "";
char API_weather[30] = "";
char API_text_tomorrow[150] = "";
char API_text_tomorrow_night[150] = "";
boolean newData = false; // invert this on Slave arduino
static boolean recvInProgress = false;
static byte 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);
}
Transmitter:
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11);//rx tx
// Data received from sensor system
int sensor_rain = 1;
float sensor_light = 2.22;
float sensor_hum = 3.33;
float sensor_temp = 4.44;
// Data received from the Internet
float API_rain = 5.55;
float API_temp = 6.66;
float API_tempFeels = 7.77;
int API_hum = 8;
char API_wind[55] = "123456789012345678901234567890123456789012345678901234";
char API_weather[30] = "12345678901234567890123456789";
char API_text_tomorrow[150] = "1234567890123456789012345678901234567890123456789012341234567890123456789012345678901234567890123456789012341234567890123456789012345678901234567890";
char API_text_tomorrow_night[150] = "1234567890123456789012345678901234567890123456789012341234567890123456789012345678901234567890123456789012341234567890123456789012345678901234567890";
void setup() {
Serial.begin(9600);
mySerial.begin(9600);
}
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(">");
}