-->
Page 1 of 2

How to store serial data to an array to be send to server?

PostPosted: Fri Sep 04, 2020 10:47 pm
by Xtech007
Hi all!
I have the following issue with the code below,
I’m trying to store serial data to an array, then convert all the array to a string and then send it to the server. But I keep getting an error “non class type”

The divine I’m queuing responds one byte at a time. Any suggestions is appreciated!
Code: Select all#include <ESP8266WebServer.h>
int incomingByte[13] ;       //array of data
String page1;
#define mdDeRe 14 // GPIO14 = D5  // sends signal to device.
ESP8266WebServer server(80);

void getdata1 (){
  digitalWrite(mdDeRe, HIGH);   /// enable receiver to receive data
  Serial.write(17);     ///// <<<---- maybe this can be written better
  Serial.write(3);
  Serial.write(0);
  Serial.write(84);
  Serial.write(0);
  Serial.write(4);
  Serial.write(7);
  Serial.write(73);
  Serial.flush();
  digitalWrite(mdDeRe,LOW);    //// enables device to send data


    //// store received bytes into an array /////
   if (Serial.available()) {
      for (int i=0; i<13; i ++){
      incomingByte[i] = Serial.read();
        }
      } 
  /// Here I would like to send the whole array to server
      page1 = incomingByte.toString();
      server.send(200, "text/plane",page1); //sends data to server
   }

Re: How to store serial data to an array to be send to serve

PostPosted: Sun Sep 06, 2020 2:01 am
by Pablo2048
I don't see anything "Advanced" in this, but why you cannot use this:
Code: Select allconst uint8_t message[] = {17, 3, 0, 84, 0, 4, 7, 73};
...
Serial.write(message, sizeof(message));

Also your incomingByte[i] = Serial.read() is wrong - if you have not enough bytes in receive buffer, the Serial.read() method return -1

... Ugh - I hit Submit instead of Preview... So here is the missing part:
Code: Select all  String result;

   if (Serial.available()) {
      for (int i=0; i<13; i ++){
          result.concat((char)Serial.read());
        }
    }
  /// Here I would like to send the whole array to server
    server.send(200, "text/plain", result); //sends data to server

Re: How to store serial data to an array to be send to serve

PostPosted: Sun Sep 06, 2020 11:09 pm
by Xtech007
Thank you for your Response as it has been a great help!
I'm really just learning as I go Along, and thanks to folks like this forum I'm this far.
But now I have a second Issue, with your code
Code: Select all      if (Serial.available()) {
      for (int i=0; i<13; i ++){
          result.concat((char)Serial.read());
        }
    }
  /// Here I would like to send the whole array to server
    server.send(200, "text/plain", result); //sends data to server

This Gives as output on the html file :
(char) }>U)
But the expected output is: (hex) 11 03 08 00 7D 00 3E 00 OD 00 55 14 29
I'm thinking the rest of the bytes were ignored when converted to char. I compared it to ascii chart for example hex 11 = Char [Device control 1], hex 03= char [End of Text], 00= [null] and so on..
I have attached 2 images so you can review what I see on the html side!
response.jpg

and The Response
html.jpg

Re: How to store serial data to an array to be send to serve

PostPosted: Wed Sep 09, 2020 10:04 am
by Pablo2048
This is not very complicated right? You can try for example this:
Code: Select all      if (Serial.available()) {
      for (int i=0; i<13; i ++){
          uint8_t b = Serial.read();
          if (0 != result.length())
            result.concat(" "); // add space separator if we are not the first in line
          if (b < 0x10)
            result.concat("0"); // add leading 0 character if we are smaller than 0x10
          result.concat(String(b, HEX));
        }
    }
  /// Here I would like to send the whole array to server
    server.send(200, "text/plain", result); //sends data to server