0

In my Uno code, I access to a page in my server and retrieve that value and make decision upon that value.

I get this kind of response,

Connected to server
on
on
off
off
on
off
[WiFiEsp] TIMEOUT: 116

Disconnecting from server.     

Which should just be on or off.

This is my code for testing :

#include "WiFiEsp.h"

// Emulate Serial1 on pins 6/7 if not present
#ifndef HAVE_HWSERIAL1
#include "SoftwareSerial.h"
SoftwareSerial Serial1(2,3); // RX, TX
#endif

char ssid[] = "RPi";            // your network SSID (name)
char pass[] = "raspberry";        // your network password
int status = WL_IDLE_STATUS;     // the Wifi radio's status

char server[] = "192.168.50.1";

// Initialize the Ethernet client object
WiFiEspClient client;

int c =0;
int testLED = 12;


void setup()
{
  pinMode(testLED, OUTPUT);
  // initialize serial for debugging
  Serial.begin(9600);
  // initialize serial for ESP module
  Serial1.begin(9600);
  // initialize ESP module
  WiFi.init(&Serial1);

  // check for the presence of the shield
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    // don't continue
    while (true);
  }

  // attempt to connect to WiFi network
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network
    status = WiFi.begin(ssid, pass);
  }

  // you're connected now, so print out the data
  Serial.println("You're connected to the network");

  printWifiStatus();

  Serial.println();
  Serial.println("Starting connection to server...");
  // if you get a connection, report back via serial
  if (client.connect(server, 80)) {
    Serial.println("Connected to server");
    // Make a HTTP request
    client.println("GET /simple.txt HTTP/1.1");
    client.println("Host: localhost");
    client.println("Connection: close");
    client.println();
  }
}

void loop()
{
  // if there are incoming bytes available
  // from the server, read them and print them
  if (client.available()) {
     c = client.read();   

     switch(c){
          case '0':
                 Serial.println("off");
                 digitalWrite(testLED, LOW);
                 delay(2500);
                 break;

          case '1':
                 Serial.println("on");
                 digitalWrite(testLED, HIGH);
                 delay(2500);
                 break;
      }
        }

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

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


void printWifiStatus()
{
  // print the SSID of the network you're attached to
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength
  long rssi = WiFi.RSSI();
  Serial.print("Signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

My simple.txt file just has 1.

Why this code outputs a gibberish value? Is it a problem with the way I retrive data? Raspberry pi is working as my network server.

Thanks in advance.

  • 1
    What's with the huge delays? – Majenko Dec 28 '17 at 10:31
  • @Majenko I put delay just for testing LED. It was not giving the result even without delay() – Sachith Dec 28 '17 at 11:00
  • why are you using http if you connect to your raspberry? – Juraj Dec 28 '17 at 11:45
  • @Juraj I connect to Raspberry-pi's web server. – Sachith Dec 28 '17 at 11:47
1

When you send a GET request in HTTP you get a HTTP response back, not just the contents of the file you requested.

For instance, your request may result in a response of:

HTTP/1.1 200 OK
Date: Thu, 28 Dec 2017 11:02:21 GMT
Server: Apache/2.4.25 (Ubuntu)
Last-Modified: Wed, 05 Apr 2017 09:40:30 GMT
ETag: "2c39-54c682ee19688"
Accept-Ranges: bytes
Content-Length: 3
Vary: Accept-Encoding
Connection: close
Content-Type: text/plain

1

As you can see there's plenty of occurrences of 0 and 1 in there to trigger your LED.

You need to parse the incoming data better. Read it line by line and only start reacting to the content after the blank line that indicates the division between header and body.

  • Yes, I understood that. But how to parse those unwanted data from the response. – Sachith Dec 28 '17 at 11:15
  • Keep a flag that says if you are in the header or not. The blank line tells you to change the flag, the flag tells you whether to interpret the data as commands. All the rest you ignore. If you want to read character by character then react to the end of line character and count the characters in a line. If the count is (not including the EOL characters) 0 then you have a blank line. You don't care what is in the header. – Majenko Dec 28 '17 at 11:17
  • Can you provide me an example or a link to do these task. I just began Arduino – Sachith Dec 28 '17 at 11:27
  • Discard everything up to a blank line. Then process that. – Nick Gammon Dec 29 '17 at 7:09
0

I found the answer for my question. @Majenko's answer helped me a lot.

I removed HTTP response given by the server and just changed few line in my original code. Also added # in-front of the number in my server's simple.txt file.

Also this arduino-forum post

void loop()
{
  // if there are incoming bytes available
  // from the server, read them and print them
while(client.available() > 0)
{
  char c = client.read();
  if(c == '#')
  {
     while(client.available() == 0) { } // Do nothing while we wait for the next value
     c = client.read();
     if(c == '1'){
        Serial.println("On");
        digitalWrite(testLED, HIGH);
        delay(2500);
     }
     else if(c == '0'){
        Serial.println("Off");
        digitalWrite(testLED, LOW);
        delay(2500);
     }
  }
}


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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.