1

I'm trying to get one esp to send integers to a second as a part of a simple remote control, but after multiple days I can't get it to work. The transmitter seems like it's connecting and sending properly as best I can tell, and the receiver is sending some kind of response, but the function I attached with server.on() never gets called.

The code is basically copy and pasted from a tutorial, the only thing I changed was removing code to display the read data to an LCD and display to serial instead.

This is the receiver's code.

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>



const char *ssid = "controllerReceiver";
const char *password = "password";

ESP8266WebServer server(80);



void setup() {
  Serial.begin(115200);

  WiFi.softAP(ssid, password);
  IPAddress myIP = WiFi.softAPIP();

  server.on("/data/", HTTP_GET, handleSentVar);
  server.begin();
  Serial.println("server online");
}

void loop() {
  server.handleClient();

}

void handleSentVar() {
  Serial.println("Handling");
  if (server.hasArg("x_value")) { // this is the variable sent from the client
    int readingInt = server.arg("x_value").toInt();
    Serial.println(readingInt);
    server.send(200, "text/html", "Data received");
  }

}

And here is the transmitter

#include <ESP8266WiFi.h>



const char *ssid = "controllerReceiver";
const char *password = "password";

int outputValue = 999;        

void setup() {
  
  Serial.begin(115200);
  delay(10);

  // Explicitly set the ESP8266 to be a WiFi-client
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    Serial.println("connecting...");
    delay(500);
  }

}

void loop() {

  char intToPrint[5];
  itoa(outputValue, intToPrint, 10); //integer to string conversion for OLED library

  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  const char * host = "192.168.4.1";
  const int httpPort = 80;

  if (!client.connect(host, httpPort)) {
    Serial.println("connection failed");

    return;
  }

  // We now create a URI for the request. Something like /data/?sensor_reading=123
  String url = "/data/";
  url += "x_value=";
  url += intToPrint;

  // This will send the request to the server
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "Connection: close\r\n\r\n");
  unsigned long timeout = millis();
  while (client.available() == 0) {
    if (millis() - timeout > 5000) {
      Serial.println(">>> Client Timeout !");

      client.stop();
      return;
    }
  }

}
1
  • Running a whole webserver + client is waaay overkill for a remote control. Use UDP packets instead.
    – Majenko
    Feb 24 at 18:53

1 Answer 1

1

Replace "/data/" with "/data" and url += "x_value=" with url += "?x_value=".

The HTTP GET parameters are separated from the path by "?". The on function is executed for a path ("/data").

3
  • If you sub server.on("/data/", don't you need the trailing slash in the request also? some web hosts tack that on for you, but the ESP doesn't.
    – dandavis
    Feb 27 at 5:27
  • @dandavis, no..
    – Juraj
    Feb 27 at 5:51
  • I just tested it by on()ing /test and /test/ with different functions, and they are indeed discrete. This is why there's repetitive stuff like server.on("/read/", getReading); server.on("/read", getReading); in my old sketches. I now use a macro that subs both the plain and slashed path to make it easier and would have been pleased+surprised to find out it was useless. Maybe async server is different...
    – dandavis
    Feb 27 at 6:00

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.