How to comparison with Arduino console data ?

Hello,

I have created PHP pages that connected MySQL and Arduino can use GET request from PHP. I want the UID data from GET request that displayed on Arduino console can comparison? someone said use Serial.read, but I don’t know how to do, please give me example, thanks.

PHP code

<?php

include("db.php");

$uid = $_GET['uid'];

$result = mysqli_query($conn, "select * FROM user WHERE uid = '$uid'");


while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) {
    $posts[] = $row['uid'];
}

echo "$uid";

Ardunio code

#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(74,125,232,128);  // numeric IP for Google (no DNS)
char server[] = "xxxx.com";    // name address for Google (using DNS)

// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 1, 177);

// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // start the Ethernet connection:

    Ethernet.begin(mac, ip);

  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("connecting...");

  // if you get a connection, report back via serial:
  if (client.connect(server, 80)) {
    Serial.println("connected");
    // Make a HTTP request:
    client.print("GET /getdata.php?");
    client.print("uid="); 
    client.print("F070AAAA");
    client.println(" HTTP/1.1");
    client.println("Host: xxxx.com");
    client.println("Connection: close");
    client.println();
  } else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
}

void loop() {

  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }


  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();

    // do nothing forevermore:
    while (true);
  }
}

Console displayed

connecting...
connected
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Tue, 30 Jan 2018 03:36:00 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: close

f
F070AAAA







0


disconnecting.

as your text is recieved fom the GET you store it in a char array and can then process it as required
this assumes the last line recieved is the data required

 // connectLoop controls the hardware fail timeout
  int connectLoop = 0;
  while(client.connected())
  {
    while(client.available())
    {
      inChar = client.read();
      Serial.write(inChar);
      if(inChar < ' ') index=0; else data[index++]=inChar;
      data[index]=0;
      // set connectLoop to zero if a packet arrives
      connectLoop = 0;
    }
    connectLoop++;
    // if more than 10000 milliseconds since the last packet
    if(connectLoop > 10000)
    {
      // then close the connection from this end.
      Serial.println();
      Serial.println(F("Timeout"));
      client.stop();
    }
    // this is a delay for the connectLoop timing
    delay(1);
  }
// proces data[] ......................

Hello horace,

Thanks you for helping me.

After compiled your code with my code (within loop), that displaying the following error, maybe have to define something, right ?

Error

error: 'index' was not declared in this scope

       data[index]=0;

            ^

exit status 1
'inChar' was not declared in this scope

Full code

#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(74,125,232,128);  // numeric IP for Google (no DNS)
char server[] = "winlogin.marvelconnect.com";    // name address for Google (using DNS)

// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 1, 177);

// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // start the Ethernet connection:

    Ethernet.begin(mac, ip);

  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("connecting...");

  // if you get a connection, report back via serial:
  if (client.connect(server, 80)) {
    Serial.println("connected");
    // Make a HTTP request:
    client.print("GET /getdata.php?");
    client.print("uid="); 
    client.print("F070FAA4");
    client.println(" HTTP/1.1");
    client.println("Host: winlogin.marvelconnect.com");
    client.println("Connection: close");
    client.println();
  } else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
}

void loop() {

  int connectLoop = 0;
  while(client.connected())
  {
    while(client.available())
    {
      inChar = client.read();
      Serial.write(inChar);
      if(inChar < ' ') index=0; else data[index++]=inChar;
      data[index]=0;
      // set connectLoop to zero if a packet arrives
      connectLoop = 0;
    }
    connectLoop++;
    // if more than 10000 milliseconds since the last packet
    if(connectLoop > 10000)
    {
      // then close the connection from this end.
      Serial.println();
      Serial.println(F("Timeout"));
      client.stop();
    }
    // this is a delay for the connectLoop timing
    delay(1);
  }


  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();

    // do nothing forevermore:
    while (true);
  }
}

you need to define data, index, inchaar

#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(74,125,232,128);  // numeric IP for Google (no DNS)
char server[] = "winlogin.marvelconnect.com";    // name address for Google (using DNS)

// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 1, 177);

// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // start the Ethernet connection:

    Ethernet.begin(mac, ip);

  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("connecting...");

  // if you get a connection, report back via serial:
  if (client.connect(server, 80)) {
    Serial.println("connected");
    // Make a HTTP request:
    client.print("GET /getdata.php?");
    client.print("uid=");
    client.print("F070FAA4");
    client.println(" HTTP/1.1");
    client.println("Host: winlogin.marvelconnect.com");
    client.println("Connection: close");
    client.println();
  } else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
}

  char data[100]={0};   // << add these
  int index=0;
  
void loop() {

  int connectLoop = 0;
  while(client.connected())
  {
    while(client.available())
    {
      char inChar = client.read();
      Serial.write(inChar);
      if(inChar < ' ') index=0; else data[index++]=inChar;
      data[index]=0;
      // set connectLoop to zero if a packet arrives
      connectLoop = 0;
    }
    connectLoop++;
    // if more than 10000 milliseconds since the last packet
    if(connectLoop > 10000)
    {
      // then close the connection from this end.
      Serial.println();
      Serial.println(F("Timeout"));
      client.stop();
    }
    // this is a delay for the connectLoop timing
    delay(1);
  }


  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();

    // do nothing forevermore:
    while (true);
  }
}

Also I added

char inChar;

After compiled the code, I get nothing.

connecting...
connected

disconnecting.

leiw:
Also I added

char inChar;

After compiled the code, I get nothing.

connecting...

connected

disconnecting.

I think Serial.write(inChar); will display something on console, right ?

post your latest code

#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(74,125,232,128);  // numeric IP for Google (no DNS)
char server[] = "winlogin.marvelconnect.com";    // name address for Google (using DNS)

// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 1, 177);

// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // start the Ethernet connection:

    Ethernet.begin(mac, ip);

  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("connecting...");

  // if you get a connection, report back via serial:
  if (client.connect(server, 80)) {
    Serial.println("connected");
    // Make a HTTP request:
    client.print("GET /getdata.php?");
    client.print("uid="); 
    client.print("F070FAA4");
    client.println(" HTTP/1.1");
    client.println("Host: winlogin.marvelconnect.com");
    client.println("Connection: close");
    client.println();
  } else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
}

char inChar;
char data[100]={0};   // << add these
int index=0;

void loop() {

  int connectLoop = 0;
  while(client.connected())
  {
    while(client.available())
    {
      inChar = client.read();
      Serial.write(inChar);
      if(inChar < ' ') index=0; else data[index++]=inChar;
      data[index]=0;
      // set connectLoop to zero if a packet arrives
      connectLoop = 0;
    }
    connectLoop++;
    // if more than 10000 milliseconds since the last packet
    if(connectLoop > 10000)
    {
      // then close the connection from this end.
      Serial.println();
      Serial.println(F("Timeout"));
      client.stop();
    }
    // this is a delay for the connectLoop timing
    delay(1);
  }


  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();

    // do nothing forevermore:
    while (true);
  }
}

running yoou code on a arduino Mega with an ethernet shield I get

connecting...
connected
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Tue, 30 Jan 2018 10:25:31 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: close

f
F070FAA4







0


disconnecting.

the only line I changed was

    Ethernet.begin(mac);//, ip);

to use DHCP

After run it again, I also get same result.

connecting...
connected
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Tue, 30 Jan 2018 10:34:40 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: close

f
F070FAA4







0


disconnecting.

OK, now I use data[index] to do comparison, right ? sorry, I am newbie of Arduino, not much experience, thanks.

the text of the last line recieved is in data - to compare use strcmp() e.g.

if(strcmp(data, "F070FAA4"))   ......

Hello,

After using strcmp comparison the values, the result is UID comparison not completed, I added the strcmp comparison after connectLoop > 10000, please see full code below.

#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(74,125,232,128);  // numeric IP for Google (no DNS)
char server[] = "winlogin.marvelconnect.com";    // name address for Google (using DNS)

// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 1, 177);

// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // start the Ethernet connection:

    Ethernet.begin(mac, ip);

  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("connecting...");

  // if you get a connection, report back via serial:
  if (client.connect(server, 80)) {
    Serial.println("connected");
    // Make a HTTP request:
    client.print("GET /getdata.php?");
    client.print("uid="); 
    client.print("F070FAA4");
    client.println(" HTTP/1.1");
    client.println("Host: winlogin.marvelconnect.com");
    client.println("Connection: close");
    client.println();
  } else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
}

char inChar;
char data[100]={0};   // << add these
int index=0;

void loop() {

  int connectLoop = 0;
  while(client.connected())
  {
    while(client.available())
    {
      inChar = client.read();

      if(inChar < ' ') index=0; else data[index++]=inChar;
      data[index]=0;

      // set connectLoop to zero if a packet arrives
      connectLoop = 0;
    }
    connectLoop++;
    // if more than 10000 milliseconds since the last packet
    if(connectLoop > 10000)
    {
      // then close the connection from this end.
      Serial.println();
      Serial.println(F("Timeout"));
      client.stop();
    }
    // this is a delay for the connectLoop timing
    delay(1);
  }

  if(strcmp(data, "F070FAA4") == 0)
  {
    Serial.print("UID comparison completed");
  } else {
    Serial.print("UID comparison not completed");
  }

  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();

    // do nothing forevermore:
    while (true);
  }
}

I have trying to print data values to console, but the result very strange.

connecting...
connected
HHTHTTHTTPHTTP/HTTP/1HTTP/1.HTTP/1.1HTTP/1.1 HTTP/1.1 2HTTP/1.1 20HTTP/1.1 200HTTP/1.1 200 HTTP/1.1 200 OHTTP/1.1 200 OKSSeSerServServeServerServer:Server: Server: nServer: ngServer: ngiServer: nginServer: nginxServer: nginx/Server: nginx/1Server: nginx/1.Server: nginx/1.1Server: nginx/1.12Server: nginx/1.12.Server: nginx/1.12.2DDaDatDateDate:Date: Date: WDate: WeDate: WedDate: Wed,Date: Wed, Date: Wed, 3Date: Wed, 31Date: Wed, 31 Date: Wed, 31 JDate: Wed, 31 JaDate: Wed, 31 JanDate: Wed, 31 Jan Date: Wed, 31 Jan 2Date: Wed, 31 Jan 20Date: Wed, 31 Jan 201Date: Wed, 31 Jan 2018Date: Wed, 31 Jan 2018 Date: Wed, 31 Jan 2018 0Date: Wed, 31 Jan 2018 03Date: Wed, 31 Jan 2018 03:Date: Wed, 31 Jan 2018 03:2Date: Wed, 31 Jan 2018 03:29Date: Wed, 31 Jan 2018 03:29:Date: Wed, 31 Jan 2018 03:29:4Date: Wed, 31 Jan 2018 03:29:43Date: Wed, 31 Jan 2018 03:29:43 Date: Wed, 31 Jan 2018 03:29:43 GDate: Wed, 31 Jan 2018 03:29:43 GMDate: Wed, 31 Jan 2018 03:29:43 GMTCCoConContConteContenContentContent-Content-TContent-TyContent-TypContent-TypeContent-Type:Content-Type: Content-Type: tContent-Type: teContent-Type: texContent-Type: textContent-Type: text/Content-Type: text/hContent-Type: text/htContent-Type: text/htmContent-Type: text/htmlContent-Type: text/html;Content-Type: text/html; Content-Type: text/html; cContent-Type: text/html; chContent-Type: text/html; chaContent-Type: text/html; charContent-Type: text/html; charsContent-Type: text/html; charseContent-Type: text/html; charsetContent-Type: text/html; charset=Content-Type: text/html; charset=UContent-Type: text/html; charset=UTContent-Type: text/html; charset=UTFContent-Type: text/html; charset=UTF-Content-Type: text/html; charset=UTF-8TTrTraTranTransTransfTransfeTransferTransfer-Transfer-ETransfer-EnTransfer-EncTransfer-EncoTransfer-EncodTransfer-EncodiTransfer-EncodinTransfer-EncodingTransfer-Encoding:Transfer-Encoding: Transfer-Encoding: cTransfer-Encoding: chTransfer-Encoding: chuTransfer-Encoding: chunTransfer-Encoding: chunkTransfer-Encoding: chunkeTransfer-Encoding: chunkedCCoConConnConneConnecConnectConnectiConnectioConnectionConnection:Connection: Connection: cConnection: clConnection: cloConnection: closConnection: closefFF0F07F070F070FF070FAF070FAAF070FAA40
disconnecting.

do the test after disconnection, e.g.

  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
  if(strcmp(data, "F070FAA4") == 0)
  {
    Serial.print("UID comparison completed");
  } else {
    Serial.print("UID comparison not completed");
  }

    // do nothing forevermore:
    while (true);
  }

Your result is UID comparison completed ?

My result is UID comparison not completed.

connecting...
connection failed

disconnecting.
UID comparison not completed

the connection to host winlogin.marvelconnect.com seems very intermittent
runing your code of post #7 most the time it either fails to connect or returns nothing
every now and then it prints the expected result?

horace:
the connection to host winlogin.marvelconnect.com seems very intermittent
runing your code of post #7 most the time it either fails to connect or returns nothing
every now and then it prints the expected result?

I tried many times, still UID comparison not completed.

for some reason the first attempt at client.connect() usually fails???
this is a modification of one of my ethernet programs which loops doing GETs to a web page

 /*
   HTTP GET method from http://playground.arduino.cc/Code/WebClient

*/

// modifications to original
// 7/10/2017 update IP address and web page information
// 8/10/2017 replaced sprintf() with strcat() calls to build client data

#define SPRINTF       // comment out to use strcat() 

#include <SPI.h>
#include <Ethernet.h>

// this must be unique
byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

// change to your network settings
IPAddress ip(192,168,1,177);
IPAddress gateway(192, 168, 1, 254);
IPAddress subnet(255, 255, 255, 0);

// change to your server
IPAddress server(192,185,16,85);

//Change to your domain name for virtual servers
char serverName[] = "winlogin.marvelconnect.com";

// change to your server's port
int serverPort = 80;

EthernetClient client;

// set this to the number of milliseconds delay between GET requests
#define delayMillis 30000UL
unsigned long thisMillis = 0;
unsigned long lastMillis = delayMillis;

void setup() {
   Serial.begin(115200);
   // disable SD SPI
   pinMode(4,OUTPUT);
   digitalWrite(4,HIGH);

   // Start ethernet
   Serial.println(F("Starting ethernet..."));
   //Ethernet.begin(mac, ip, gateway, gateway, subnet);
   // If using dhcp, comment out the line above 
   // and uncomment the next 2 lines plus the Ethernet.maintain call in loop

   if(!Ethernet.begin(mac)) Serial.println(F("failed"));
   else Serial.print(F("IP "));
   Serial.println(Ethernet.localIP());
   delay(2000);
   Serial.println(F("Ready"));
}

void loop()
{
   Ethernet.maintain();   // If using dhcp to get an IP, uncomment line
   // send data every delayMillis 
   thisMillis = millis();
   if(thisMillis - lastMillis > delayMillis)
   {
      // GET the page
      if(!getPage(serverPort, serverName, "/getdata.php?uid=F070FAA4")) Serial.print(F("Fail "));
      else { Serial.print(F("Pass ")); lastMillis=millis(); }
   }   
}

char inChar;
char data[100]={0};   // << add these
int index=0;

// HTTP GET from page on server serverName using  ipBuf thisPort 
byte getPage(int thisPort, const char* serverName, const char *page)
{
  int inChar;
  char outBuf[128]="GET ";
  Serial.print(F("\n\nconnecting to "));
    Serial.println(serverName);
  // connect to server
  if(client.connect(serverName, thisPort) == 1)
  {
    Serial.println(F("connected"));
 #ifdef SPRINTF   
     // form HTTP GET and send it - replace sprint() with strcat()
     sprintf(outBuf,"GET %s HTTP/1.1",page);
     client.println(outBuf);
     Serial.println(outBuf);
     // form  server and send it
     sprintf(outBuf,"Host: %s",serverName);
     client.println(outBuf);
#else
    client.println(strcat(strcat(outBuf, page), " HTTP/1.1"));
    Serial.println(outBuf);
    outBuf[0]=0;
    client.println(strcat(strcat(outBuf, "Host: "), serverName));
 #endif
    Serial.println(outBuf);
    client.println(F("Connection: close\r\n"));
  } 
  else
  {
    Serial.println(F("failed"));
    return 0;
  }

  // connectLoop controls the hardware fail timeout
  int connectLoop = 0;
  while(client.connected())
  {
    while(client.available())
    {
      inChar = client.read();
      if(inChar < ' ') index=0; else data[index++]=inChar;
      data[index]=0;
      Serial.write(inChar);
      // set connectLoop to zero if a packet arrives
      connectLoop = 0;
    }
    connectLoop++;
    // if more than 10000 milliseconds since the last packet
    if(connectLoop > 10000)
    {
      // then close the connection from this end.
      Serial.println();
      Serial.println(F("Timeout"));
      client.stop();
    }
    // this is a delay for the connectLoop timing
    delay(1);
  }
  Serial.println();
  Serial.println(F("disconnecting."));
  // close client end
  client.stop();
  if(strcmp(data, "F070FAA4") == 0)
  {
    Serial.print("UID comparison completed\n");
  } else {
    Serial.print("UID comparison not completed\n");
  }

  return 1;
}

a run gives

Starting ethernet...
IP 192.168.1.177
Ready


connecting to winlogin.marvelconnect.com
failed
Fail 

connecting to winlogin.marvelconnect.com
connected
GET /getdata.php?uid=F070FAA4 HTTP/1.1
Host: winlogin.marvelconnect.com
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Wed, 31 Jan 2018 09:33:22 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: close

f
F070FAA4







0


disconnecting.
UID comparison not completed
Pass

it then loops doing the GET commands OK
after the transmission of F070FAA4 the server appears to send some blank lines then 0 - should it be doing this?

  1. I don't know why the first connection is fails.

  2. some blank lines then 0 maybe console problem ?

I think it must be you php script
when I use GET with my server I get a single line reply

Starting ethernet connection 1 failed connections  0 data fails  0
IP 192.168.1.177
Ethernet Ready
connecting to XXXXXXXXXXX
connected
GET /get.php?uid=1 HTTP/1.1
Host: XXXXXXXXX

HTTP/1.1 200 OK
Content-Type: text/html
Etag: "14051367-1517400941;;;"
X-Litespeed-Cache: miss
Content-Length: 41
Date: Wed, 31 Jan 2018 12:15:41 GMT
Accept-Ranges: bytes
Server: LiteSpeed
Connection: close

{1,2,3,4,5}
disconnecting.

I then parse the data using string tokeniser strtok()

Why {1,2,3,4,5} and it is 8 numbers.