lasselukkari / aWOT

Arduino web server library.
MIT License
283 stars 41 forks source link

Can't get a response from any request sent to aWOT server #145

Closed seva-luchianov closed 1 year ago

seva-luchianov commented 1 year ago

I'm just trying to write the simplest ping command. Using nodeJS and axios to send data to the board:

setInterval(async () => {
    console.log("ping");
    let res = await axios.get('http://192.168.0.150/ping');
    console.log("RES:", res);
}, 3000);

And here is my arduino code (uno wifi rev 2)

#include <aWOT.h>

char ssid[] = "";
char pass[] = "";
int status = WL_IDLE_STATUS;

WiFiServer server(80);
Application app;

void setup() {
  Serial.begin(9600);
  connect_WiFi();
  printWifiStatus();

  app.get("/ping", &ping);
  server.begin();
}

void loop() {
  WiFiClient client = server.available();
  if (client.connected()) {
    app.process(&client);
  }
}

void ping(Request &req, Response &res) {
  Serial.println("pong");
  res.sendStatus(200);
}

void connect_WiFi() {
  // attempt to connect to Wifi network:
  while (status != WL_CONNECTED) {
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);
    // wait 5 seconds for connection:
    delay(5000);
  }
}

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

  // print your board'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");
}

Board connects to wifi, and for each ping sent it logs "pong" to serial out. But in nodeJS no response is ever received. I've tried everything I can think of

res.status(200);
res.print("sent");

I've tried using res.end(); and res.flush(); in any order, I've tried setting any and all headers with no success. I've tried sending both get and post using xhr from the browser as well. Ultimately I need this to work with a nodeJS backed but I'm open to any library that isn't axios if that's the issue... All examples on https://awot.net make it look like it just works. What am I doing wrong?

lasselukkari commented 1 year ago

I just tested your code with an ESP8266 and it works for me.

Screenshot 2023-04-02 at 10 40 46

Do you get the response with a browser?

lasselukkari commented 1 year ago

Call client.stop(); after the app.process(&client). Your wifi lib may need that. Some wifi libraries stop the client automatically in the destructor, some don't.


  if (client.connected()) {
    app.process(&client);
    client.stop();
  }
lasselukkari commented 1 year ago

I just noticed that the ARDUINO UNO WiFi REV2 is the same board as MKR WiFi 1010 with a different form factor. Last time I checked the wifi client and server libraries for the board were garbage. I would seriously consider using some other development board.

seva-luchianov commented 1 year ago

I wasn't able to get a response from anything, browser or nodejs. I'll try calling client.stop() as you mentioned! Do you have an suggestions for a specific development board? My only requirements are: Wifi enabled 5v rail 2 pwm pins and 1 5v output pin

seva-luchianov commented 1 year ago

client.stop() made this work as you said! Incredible how I've never seen that in any example code, even on Arduino's own website with examples specifically for the UNO Rev2. Really appreciate you helping out a newbie, hopefully someone else in the future can find this thread when they run into the same issue.

lasselukkari commented 1 year ago

Nice that you got it working. With the 5V output requirement your choices are a lot more limited. Almost everything uses 3.3V nowadays. A logic level converter could be one option if you can't change your design otherwise.

If you like the library please give it star here on github.