Reputation: 1
I'm building a project where I use 2 ESP8266 and 1 ESP32 devices, where I collect data with sensors on ESP8266's and send those gathered data to ESP32. Then, this ESP32 device sends an HTTP request (w/ HTTPClient) to my Node.js Web Server, with the data received, which will eventually be processed there and saved to the database.
I'm having a problem though; when I use some mock data, and use only ESP32 without ESP-NOW and any other connection with other devices, I get status code 200, which indicates that my HTTP Request is sent successfully. And when I use ESP-NOW to establish conenction between ESP devices, and not send any HTTP Request, I can successfully send and receive data.
However, when I use ESP-NOW to send and receive data, and send the data using HTTPClient request to my Web server on ESP32, I get status code -1 (refused connection).
I have also used painlessmesh library to send/receive data between ESP devices, but that gave me the same exact issue. I'm using Wi-Fi mode STA on my ESP32 device, which I connect it to my router to connect to the Internet.
Upvotes: 0
Views: 1285
Reputation: 723
If you send the HTTP request when the onReceive() callback function is called, you will get a Status Code -1 because the wifi built-in card is not available to make requests until the callback execution is over. Also note that the WiFi channel used by the sender needs to match the receiver WiFi channel as Marcel mentioned.
I have discussed this issue here: https://github.com/m1cr0lab-esp32/esp-now-network-and-wifi-gateway/issues/6.
A simple fix would be to use a bool variable and send the HTTP requests in your loop function:
bool sendHTTPReq = false;
void onReceive(const uint8_t *mac_addr, const uint8_t *data, int len) {
Serial.printf("received: %3u from %02x:%02x:%02x:%02x:%02x:%02x\n",
(uint8_t) *data,
mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]
);
sendHTTPReq = true;
} // You can use boolean variables to trigger wifi-related events in the loop function following onReceive() call.
void loop() {
if (sendHTTPReq) {
// Send your request here
}
}
Code to configure a sender and receiver communicating with ESP-NOW while being connected to a WiFi gateway is available on my Github repository: https://github.com/lukalafaye/ESP-NOW-WiFi-Gateway.
Upvotes: 1
Reputation: 1
It is possible to run a web server and esp_now in the same esp32. Check my code here https://github.com/Servayejc/esp_now_web_server for the server https://github.com/Servayejc/esp_now_web_server for the sender This code make also automatic pairing of the nodes of esp_now. This code is based on randomnerdtutorial…
Upvotes: 0
Reputation: 23565
You cannot use ESP-NOW and WiFi in parallel at the same time; it's either-or. Exception: if you use the same channel for both (may not be possible)!
Option 1
You can alternate between the two protocols. So, as soon as the ESP-NOW transmissions from node 1 has completed (data fully received) you turn off ESP-NOW, connect to WiFi, publish via MQTT and then reverse. Needless to say that while your ESP32 is on WiFi you cannot receive data via ESP-NOW from either node 1 or node 2.
Option 2
Use some sort of gateway between ESP-NOW and WiFi. This product for example contains two ESP32 (connected over UART), one for ESP-NOW, one for WiFi: https://thingpulse.com/product/espgateway/. Disclaimer: I am a ThingPulse co-founder.
Upvotes: 2