Hi, i'm currently trying to send data from my ATMega to the ESP8266. The problem is that :
- Esp doesn't receive the data if the string is too long unless i increase the delay on my mega's code to be longer.
- This is a small part of a bigger code where there is a process going on in the background requiring arduino to continously be working with no delay as there is a timer involving millis() in play.
3.I also noticed when coding both that they both share COM3, do i have to reinstall my CH340 Driver?
So I'm looking for help to solve these 3 issues, any help will be appreciated. Sorry if my questions are inaccurate and random as im a beginner and learning as i go. Any help is appreciated
Code for ESP:
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
String PHValue, h2sValue,volume,kirimState,kirimState2,processReps,coolantType,coolantTypeValue;
float PHValueFloat,h2sValueFloat,volumeValueFloat;
int processRepsInt;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
}
String splitString(String data, char separator, int index)
{
int found = 0;
int strIndex[] = { 0, -1 };
int maxIndex = data.length() - 1;
for (int i = 0; i <= maxIndex && found <= index; i++) {
if (data.charAt(i) == separator || i == maxIndex) {
found++;
strIndex[0] = strIndex[1] + 1;
strIndex[1] = (i == maxIndex) ? i+1 : i;
}
}
return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
}
void loop() {
// put your main code here, to run repeatedly:
if(Serial.available()){
String msg = "";
while(Serial.available()){
msg += char(Serial.read());
delay(50);
}
Serial.print("Esp's received msg");
Serial.println(msg);
//kirim
PHValue = splitString(msg, ',', 0);
h2sValue = splitString(msg, ',', 1);
processReps = splitString(msg, ',', 2);
//kirim2
volume = splitString(msg, ',', 3);
coolantType= splitString(msg,',',4);
PHValueFloat=PHValue.toFloat();
h2sValueFloat=h2sValue.toFloat();
processRepsInt=processReps.toInt();
volumeValueFloat=volume.toFloat();
coolantTypeValue=coolantType;
Serial.print(msg);
}
}
Code for Mega:
float PHValue_Test, h2sValue_Test,volume_Test;
int processReps_Test;
String coolantType_Test;
int x=0;
String kirim = "";
void setup(){
Serial.begin(9600);
Serial3.begin(115200);
}
void loop(){
PHValue_Test = 3.4;
h2sValue_Test = 2.3;
volume_Test =400;
processReps_Test=10;
coolantType_Test="Type S";
kirim = "";
kirim += PHValue_Test;
kirim += ",";
kirim +=h2sValue_Test;
kirim +=",";
kirim +=processReps_Test;
kirim +=",";
kirim +=volume_Test;
kirim += ",";
kirim +=coolantType_Test;
kirim +=",";
Serial.print("Mega Sent:");
Serial.println(kirim);
Serial3.println(kirim);
if(Serial3.available()){
String msg="";
while(Serial3.available()){
msg+=char(Serial3.read());
delay(50);
}
Serial.println(msg);
}
delay(10000);
}