Is there any way I can increase the buffer size?
The slave (Arduino Uno) is sending the data to the master (Arduino Mega).
Or is there any other library which could send these variables from the Uno to the Mega.
Variables needed to be transmitted:
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;
float API_temp;
float API_tempFeels;
int API_hum;
char API_wind[55];
char API_weather[30];
char API_text_tomorrow[150];
char API_text_tomorrow_night[150];
Master code:
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11);//rx tx
const byte numCharsSlave = 32;
char receivedSlaveChars[numCharsSlave];
char tempSlaveChars[numCharsSlave]; // temporary array for use when parsing
// variables to hold the parsed data
char messageFromSlave[numCharsSlave] = {0};
// 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;
float API_temp;
float API_tempFeels;
int API_hum;
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;
unsigned long currentMillis1 = 0;
unsigned long previousMillis1 = 0;
long interval1 = 150; // 150 milliseconds
void setup() {
Serial.begin(9600);
mySerial.begin(9600);
}
void loop() {
recvWithStartEndMarkers();
if (newData == true) {
strcpy(tempSlaveChars, receivedSlaveChars);// this temporary copy is necessary to protect the original data because strtok() used in parseData() replaces the commas with \0
parseData();
showParsedData();
newData = false;
}
}
void recvWithStartEndMarkers() {
while (mySerial.available() > 0 && newData == false) {
rc = mySerial.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(tempSlaveChars, ","); // get the first part - the string
strtokIndx = strtok(NULL, ",");
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);
}
Slave code:
#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] = "Strong wind";
char API_weather[30] = "The sky is clear";
char API_text_tomorrow[200] = "Good weather tomorrow";
char API_text_tomorrow_night[200] = "Shit weather tomorrow night";
void setup() {
Serial.begin(9600);
mySerial.begin(9600);
}
void loop() {
sendDataToMaster ();
delay(15000);
}
void sendDataToMaster () {
mySerial.print('<');
mySerial.print("Slave text");
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(">");
}
These are the codes I am using. Apart from not being able transmit the required amount of variables, it works fine.