[Solved] ESP32 "server.on" problems

Hi there. I have a home lighting setup with an ESP8266 (WeMos D1 mini) as master controller. Several web pages in SPIFFS, websockets, the lot. All working fine and fast. Added a NodeMCU (ESP8266) +PIR sensor to the setup (device #5 to the WeMos AP). Here is essence of the loop(). Setup is just connection/printing stuff.

void loop() {
  pirState = digitalRead(pirPin);
  if (pirState != pirPreviousState) { // if changed
    String url = "/sensors/";
    if (pirState) url += "?PIRon", Serial.print("Motion detected, ");
    else url += "?PIRoff", Serial.println("Motion stopped.");
    client.connect(host, httpPort); // 192.168.4.1  80
    client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n");
    client.stop();
    pirPreviousState = pirState; // update
  }
}

The WeMos server GET code is simply this.

server.on("/sensors/", HTTP_GET, handleSensors);
server.begin();

It runs the handleSensors() function, where the PIR info is processed. Don't know if I have done it the right way, but it's working fine on an ESP8266.

Then I tried to "upgrade" to an ESP32. The rest of the ported code works fine, except for this "server.on" part. The handleSensors() function is never executed.

I should tell I'm a self-taught coding noob. I borrow pieces of code from others, and Frankenstein them together. Leo..

Then I tried to "upgrade" to an ESP32. The rest of the ported code works fine, except for this "server.on" part. The handleSensors() function is never executed.

Don't post snippets (Snippets R Us!)

the issue might be elsewhere.Are you sure you are using an appropriate PIR pin on the ESP32 ? Are you connected correctly to the WiFi ? do you see any request being sent ?

as a side note, don't use the comma operator in your if, that's not a good practice, use a block notation {} to group things together in a compound statement and try to avoid the String class esp. when it's easy to not use it

  pirState = digitalRead(pirPin);

  if (pirState != pirPreviousState) { // if state has changed
    pirPreviousState = pirState;       // update state
    client.connect(host, httpPort);    // 192.168.4.1  80
    client.print(F("GET /sensors/"));
    if (pirState == HIGH) {
      client.print(F("?PIRon"));
      Serial.println(F("Motion detected, "));
    } else {
      client.print(F("?PIRoff"));
      Serial.println(F("Motion stopped."));
    }
    client.print(F(" HTTP/1.1\r\n Host: "));
    client.println(host);
    client.println(F("Connection: close\r\n"));
    client.stop();
  }

J-M-L:
Don’t post snippets (Snippets R Us!)

the issue might be elsewhere.Are you sure you are using an appropriate PIR pin on the ESP32 ? Are you connected correctly to the WiFi ? do you see any request being sent ?

as a side note, don’t use the comma operator in your if, that’s not a good practice

Thanks for replying.

A snippet for now, because I’m sure the problem is there.
As said, the main code and the sensor code works fine with an ESP8266 as master.
I’m coding long enough to not make mistakes in declarations, pin numbers, and hardware any more.

I’m also sure the PIR sensor connects to the master,
because I keep track of the number of connected clients with WiFi.softAPgetStationNum();

The print statements were just tacked on today to trouble-shoot, and will be removed when it’s all working.

The F-macro is not going to make significant difference here.
This is a very small loop for an ESP8266.

I just found out that if I change on the master

server.on("/sensors/", HTTP_GET, handleSensors);
to
server.on("/sensors", HTTP_GET, handleSensors);

I DO have a response…

handlefileRead /sensors/ File Not Found: /sensors/index.html

Seems like it’s looking for a file, not executing the function.
Leo…

Maybe cross reference the datasheets. I think you might find a very small important (change) between the two datasheets.

Before diving into that, I just want to know the correct and most simple way for a GET request. Most examples on the net are adding web pages and call-back crap that I don't need.

I basically want the sensor/client to advertise a pin change, and the master/server to trigger/run a function. I don't even know if I'm using the correct way for the ESP8266, but the ESP32 seems more picky there. Leo..

Marked the thread as [Solved].

Changed url += "?PIRon" to url += "?PIRon="

Same for the PIRoff

The added '=' seems to run the function on the master, both ESP32 and ESP8266. Leo..

Working code for one wireless PIR sensor.

Lots of ways to skin a cat.
Here I prepare the whole string in a const char.
Leo…

#include <ESP8266WiFi.h>
WiFiClient client;
const byte pir1Pin = D5;
boolean pir1State, pir1PreviousState;
const char *PIR1on = "GET /sensors/?PIR1on= HTTP/1.1\r\nHost: 192.168.4.1\r\nConnection: close\r\n\r\n";
const char *PIR1off = "GET /sensors/?PIR1off= HTTP/1.1\r\nHost: 192.168.4.1\r\nConnection: close\r\n\r\n";

void setup() {
  WiFi.mode(WIFI_STA);
  WiFi.begin("Aurora", "Zealandis");
}

void loop() {
  pir1State = digitalRead(pir1Pin);
  if (pir1State != pir1PreviousState) {
    client.connect("192.168.4.1", 80);
    if (pir1State) client.print(PIR1on);
    else client.print(PIR1off);
    client.stop();
    pir1PreviousState = pir1State;
  }
  delay(1); // auto modem sleep
}

// server side code
// server.on("/sensors/", HTTP_GET, handleSensors); // runs handleSensors function