Hello all, I hope I'm posting in the right location.
Hardware:
Arduino mega
Yun Shield
I'm running into a problem with the below code as the YunClient client = server.accept(); seems to always return a 0 value. My assumption is that the client is failing.
can anyone please help? I just don't know what I'm missing.
#include <Console.h>
#include <Bridge.h>
#include <YunServer.h>
#include <YunClient.h>
YunServer server;
String name;
void setup() {
// Initialize Console and wait for port to open:
Bridge.begin();
Console.begin();
server.listenOnLocalhost();
server.begin();
// Wait for Console port to connect
while (!Console);
Console.println("Whats your name");
}
void loop() {
YunClient client = server.accept();
Console.print(client); // return 0 I'm assuming it should return 1 as the if (client) fails to execute
// There is a new client?
if (client) {
//readStringUntil() "consumes" the string.
//this means that if command was led/20, now
//it's just 20.
String command = client.readStringUntil('/');
Serial.println(command);
delay(20);
if (command == "led") {
//convert the value from string to int.
int value = client.parseInt();
analogWrite(11, value);
}
client.stop();
}
if (Console.available() > 0) {
char c = Console.read(); // read the next char received
// look for the newline character, this is the last character in the string
if (c == '\n') {
//print text with the name received
Console.print("Hi ");
Console.print(name);
Console.println("! Nice to meet you!");
Console.println();
// Ask again for name and clear the old name
Console.println("Hi, what's your name?");
name = ""; // clear the name string
}
else { // if the buffer is empty Cosole.read() returns -1
name += c; // append the read char from Console to the name string
}
} else {
delay(100);
}
}