Search code examples
arduinowifi

Testing Arduino WiFiServer message


I need to make a WiFiServer on a Arduino to allow me to send him commands to execute. But there is a problem, when I send him a commande, he test the command but the test is ever false.

The message is correctly send and received.

There is the code:

#include <WiFi.h>

#define PORT 80
#define LED 8

char ssid[] = "trvl";
char pass[] = "ninja837";

String message;
int status = WL_IDLE_STATUS;

WiFiServer server(PORT);

void setup() {
  Serial.begin(9600);
  Serial.println("========================");
  Serial.println("TCP Serveur - Starting..");
  Serial.println("========================");

  pinMode(LED, OUTPUT);

  Serial.print("Try to connect to ssid: ");
  Serial.println(ssid);

  status = WiFi.begin(ssid, pass);
  if(status != WL_CONNECTED) {
    Serial.println("Couldn't connect to wifi network");
  }else{
    server.begin();
    Serial.print("Connected to wifi. Address: ");
    IPAddress myAddress = WiFi.localIP();
    Serial.println(myAddress);
  }


  Serial.println("Ready to use!");
  Serial.println();
}

void loop() {
  WiFiClient client = server.available();

  if(client) {
    if(client.available() > 0) {
      char ch = client.read();
      if(ch != '\n') {
        message += ch;
      }else{
        Serial.print("New message: '");
        Serial.print(message);
        Serial.println("'");

        //digitalWrite(LED, !digitalRead(LED));

        if(message.equals("on")) {
          digitalWrite(LED, HIGH);
          Serial.println("on");
        }else if(message.equals("off")) {
          digitalWrite(LED, LOW);
          Serial.println("off");
        }

        client.println(digitalRead(LED));
        client.flush();

        Serial.print("led: ");
        Serial.println(digitalRead(LED));

        message = "";
      }
    }
  }
}

And here is the monitor of Arduino

Thanks for you help

(Sorry for my english ^^')


Solution

  • Forgot to use the message.trim() to remove the \r