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;
}

}

}

Eric Ardis
  • 13
  • 3

1 Answers1

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").

Juraj
  • 18,264
  • 4
  • 31
  • 49