Hello fellow coders.
I'm having a bit of a problem getting data from my serial port into a string.
I am trying to build a function (Right now it sits in the loop() function, but it will be moved out and into its own) that detects when a esp8266 send data to the arduino. Then it will isolate the connection ID, the length of the data string and the data itself in separat variables. After collecting these data, i can shut down the connection to the connected client.
I'ved come a long way. My code reads the connection ID and the length but i need to save the rest of the data into a string, så the function can pass it back with return.
This is my working code:
#include <LiquidCrystal.h>
// initialize the LCD library with the numbers of the interface pins
LiquidCrystal lcd(15, 14, 5, 4, 3, 2);
char incomingByte; //Used to save incoming byte from serial
int digits; //Used to keep track of numbers of digits in connection ID and data length
int grabedData; //Used to hold the composed number of connection ID and data length
int connectionId; //Used to hold connection ID
int dataLength; //Used to hold data length
///////////////////////////////////////////////////////////////////
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(20, 4);
//adjust brightness of LCD backlight
analogWrite(10, 255);
serialSetup(115200); //Setup serial for USB and ESP8266
setupESP8266(); //Setup esp8266 as server on port 5483
}
///////////////////////////////////////////////////////////////////
void loop() {
if (Serial1.available() > 0) { //Check is data is available
delay(200); //Allow buffer to fill up
if (Serial1.find("+IPD,")) { //Locate +IPD, in incoming data and read from that point
//Find connection ID
digits = 0; //Ensure digits variable is 0
while (Serial1.available() > 0) { //Run code in loop as long as data exist in buffer
incomingByte = Serial1.read(); //Read one byte from buffer
if (incomingByte == 44) { //If byte contains the ascii ","
break; //...Break free of loop even if data still available
}
digits++; //Increase digits to hold track of number of digits in connection ID
if (digits == 2) { //Run next line of code if first digit is allready read
grabedData *= 10; //Move first digit to tens place making it possible to add second number in ones place
}
grabedData += incomingByte - 48; //Subtract 48 from read number due to ascii number 0 is the 48'th number i table
}
connectionId = grabedData; //Save result in its own variable
grabedData = 0; //Reset grabedData to 0
//Find data length
digits = 0; //Ensure digits variable is 0
while (Serial1.available() > 0) { //Run code in loop as long as data exist in buffer
incomingByte = Serial1.read(); //Read one byte from buffer
if (incomingByte == 58) { //If byte contains the ascii ":"
break; //...Break free of loop even if data still available
}
digits++; //Increase digits to hold track of number of digits in data length
if (digits == 2) { //Run next line of code if first digit is allready read
grabedData *= 10; //Move first digit to tens place making it possible to add second number in ones place
}
grabedData += incomingByte -48; //Subtract 48 from read number due to ascii number 0 is the 48'th number i table
}
dataLength = grabedData; //Save result in its own variable
grabedData = 0; //Reset grabedData to 0
//Print remaining data
if (Serial1.available() > 0) {
lcd.clear();
lcd.setCursor(0,2);
if (dataLength > 20) {
dataLength = 20;
}
for (int x = 0; x < dataLength; x++) {
incomingByte = Serial1.read();
lcd.print(incomingByte);
}
}
lcd.setCursor(0,0);
lcd.print("Connection ID: ");
lcd.print(connectionId);
lcd.setCursor(0,1);
lcd.print("Data length: ");
lcd.print(dataLength);
}
}
}
///////////////////////////////////////////////////////////////////
void serialSetup(long baudRate) {
Serial.begin(baudRate); //open serial port to computer
Serial1.begin(baudRate); //open serial port to esp8266
//Wait for esp8266 to get ready
while(true) { //Start endless loop
if (Serial1.available() > 0) { //Check for data in buffer
if (Serial1.find("ready")) { //Look for "ready" in buffer
break; //Break loop when found
}
}
}
}
///////////////////////////////////////////////////////////////////
void setupESP8266() {
//Setup esp8266
lcd.clear();
lcd.print("Setup initiated");
sendCommand("AT+RESTORE",1000); //Restore esp8266 to factory settings
sendCommand("AT+CWMODE=3",1000); //Setup esp8266 as Access Point
sendCommand("AT+CWSAP=\"ESP8266\",\"ABCDEFGH\",1,0,4,0",2500); //SSID "ESP8266" - Password "ABCDEFGH" - Channel 1 - Open network - 4 connection allow at a time - Broadcast SSID
sendCommand("AT+CIPMUX=1",1000); //Allow multiple connections
sendCommand("AT+CIPSERVER=1,5483",2500); //Setup TCP server on port 5483
if (Serial1.available() > 0) { //Check for leftover data in buffer
while(Serial1.available() > 0) { //Enter read loop if data present to clear out data
Serial1.read();
}
}
lcd.clear();
lcd.print("SYSTEM READY");
}
///////////////////////////////////////////////////////////////////
void sendCommand(String command, int delayTime) {
Serial1.println(command); //Sends command to esp8266
delay(delayTime); //Wait for command to execute
}
The bit I'm looking for help with is:
if (Serial1.available() > 0) {
lcd.clear();
lcd.setCursor(0,2);
if (dataLength > 20) {
dataLength = 20;
}
for (int x = 0; x < dataLength; x++) {
incomingByte = Serial1.read();
lcd.print(incomingByte);
}
}
lcd.setCursor(0,0);
lcd.print("Connection ID: ");
lcd.print(connectionId);
lcd.setCursor(0,1);
lcd.print("Data length: ");
lcd.print(dataLength);
How can I save the remaining data from the serialbuffer into a string to pass back with return? Right now it just print the remaining data to the LCD.