Juan_Antonio’s ESP32 examples, Need Help

Hi,

I am using one of Juan_Antonio’s ESP32 examples.
Example #5 - App sends on/off LED12 and LED14. WiFi. WebServer. Static IP.

The example works fine, but I need to also use ESP-Now

When I add the needed code for ESP-Now, the example still works, but not the ESP-now.

If I comment out WiFi.begin(ssid, password); then ESP-Now works fine but not the example code.

I also tried Example #4 in Soft Access Point mode,
it uses: WiFi.softAP(ssid, password);
instead of: WiFi.begin(ssid, password);

Using that method they both work fine together, but then I can not send Emails, because I am not connected to the router.

I need to use Example #5, because I need to be connected to WiFi to send emails when certain events are fired, and I need ESP-Now to send data to other ESP32 boards.

How can I get both to work at the same time?
Dose anyone have any ideas?

Thanks,
Mike.

ESP32 code:

#include <WiFi.h>
#include <esp_now.h>
#define LED2  2 
String LedState = "";

const char* ssid = "MyWiFi";
const char* password = "123456789";

// Setting Static IP.
        IPAddress local_IP(192, 168, 0, 115);
        IPAddress gateway(192, 168, 1, 1);
        IPAddress subnet(255, 255, 255, 0); 
        IPAddress primaryDNS(8, 8, 8, 8); //opcional 
        IPAddress secondaryDNS(8, 8, 4, 4); //opcional

WiFiServer server(80); // Port 80

uint8_t NowAddress[] = {0x20, 0xAE, 0xA4, 0x20, 0x0D, 0x20};

typedef struct now_struct {
  int d1 = 123; // fake test data
  int d2 = 456; // fake test data
  } 
now_struct;
now_struct NowData;


void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {  
 if (status == 0) {Serial.println("Delivery Status: ok");}
  else {Serial.println("Delivery Status: fail");}
}


void setup() {
  Serial.begin(115200);
  pinMode(LED2, OUTPUT);

  // Setting Static IP.
  if (!WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS)) {
    Serial.println("Error in configuration.");
  }
  
  // Set device as a Wi-Fi Station
  WiFi.mode(WIFI_STA);

  // Connect WiFi net.
  Serial.println();
  Serial.print("Connecting with ");
  Serial.println(ssid);
  
  WiFi.begin(ssid, password);

  // *** Temporarily disabled for testing ***
  //while (WiFi.status() != WL_CONNECTED) {
    //delay(500);
    //Serial.print(".");
  //}
    
  Serial.println("Connected to WiFi.");

  
  // Init ESP-NOW
  if (esp_now_init() != ESP_OK) {
    Serial.println("Error initializing ESP-NOW");
    return;
  }

  // register for Call back to get the status of Trasnmitted packet  
  esp_now_register_send_cb(OnDataSent);

  // Register peer  
  esp_now_peer_info_t peerInfo;
  memcpy(peerInfo.peer_addr, NowAddress, 6);
  peerInfo.channel = 1;  
  peerInfo.encrypt = false;
  
  // Add peer        
  if (esp_now_add_peer(&peerInfo) != ESP_OK){
    Serial.println("Failed to add peer");
    return;
  }

  // Start Web Server.
  server.begin();
  Serial.println("Web Server started.");
 
  // This is IP
  Serial.print("This is IP to connect to the WebServer: ");
  Serial.print("http://");
  Serial.println(WiFi.localIP());
}
 
void loop() {
  delay(1000);
  esp_now_send(0, (uint8_t *) &NowData, sizeof(NowData));

  WiFiClient client = server.available();
  if (!client) {
    return;
  }
   
  Serial.print("New client: ");
  Serial.println(client.remoteIP());
   
  // Read the information sent by the client.
  String req = client.readStringUntil('\r');
  Serial.println(req);

  // Make the client's request.
       if (req.indexOf("on12") != -1) {digitalWrite(LED2, HIGH); LedState = "LED2 ON";}
       if (req.indexOf("off12") != -1){digitalWrite(LED2, LOW); LedState = "LED2 OFF";}
       
  // Return Led status.
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html");
  client.println(""); //  Comillas importantes.
  client.println(LedState);

  client.flush();
  client.stop();
  Serial.println("Client disconnected.");
    
}

Hi @m_casky , I have not used the ESP-Now protocol, I will look at it.

Edit:
In this example, ESP32 is a SoftAP (network: 192.168.4.1)
Mobile is a Station of ESP32 (192.168.4.2)

Router is network: 192.168.1.1
ESP32 is a Client of Router, 192.168.1.3

Maybe it's some idea.

http://kio4.com/arduino/122_Wemos_PuntoAcceso_Estacion.htm

You have to use WiFi.mode(WIFI_AP_STA) for mixed mode but i don't know how is it work together with ESP-now. I've never used ESP-now.

I think in this example, the use of Delay() may cause issues because it blocks everything. Using Milliseconds elapsed is the way to go.

Hi Zol,
I have already tried that, if I use WiFi.mode(WIFI_AP_STA); then ESP-Now works fine, but not the WiFi code.

Mike.

Hi ChrisWard,
Yes I know using Delay(), is not a good idea, this only a test code. I tried a pure ESP-Now demo with the Delay() for testing and it is ok to use for that purpose.

Mike.

Have you looked into this?: https://randomnerdtutorials.com/esp32-esp-now-wi-fi-web-server/
Maybe it gives some idea.

Thanks Zol,
Yes I have seen that site, in fact that is where I got the original codes for ESP-Now.

The link you provided though may help, but I don't want to serve a web page from the ESP32, but rather control the ESP32 by setting Web1 URL and using Web1 get, to do things in the app according to the response received.

Maybe I can hack that code in the link to make it work, I’ll give it a try.

Thanks,
Mike.

Funny thing…

If I add WiFi.disconnect(); right after WiFi.begin(ssid, password); in setup() then ESP-Now will work.

But of course my WiFi code will not work.

So my thought was to change the code in the loop() to this:

void loop() {

WiFiClient client = server.available();
if (!client) {
return;
}

Serial.print("New client: ");
Serial.println(client.remoteIP());

// Read the information sent by the client.
String req = client.readStringUntil('\r');
Serial.println(req);

// Make the client's request.
if (req.indexOf("on12") != -1) {digitalWrite(LED2, HIGH); LedState = "LED2 ON";}
if (req.indexOf("off12") != -1){digitalWrite(LED2, LOW); LedState = "LED2 OFF";}

// Return Led status.
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println(""); // Comillas importantes.
client.println(LedState);

WiFi.disconnect(); delay(1000);

if (WiFi.status() != WL_CONNECTED) {

Serial.println("WiFi disconnected");
Serial.println("Sending ESP-Now data");

esp_now_send(0, (uint8_t *) &NowData, sizeof(NowData));

delay(1000);

client.flush();
client.stop();
Serial.println("Client disconnected.");
WiFi.begin(ssid, password);
server.begin();

}

I thought this should work, but it doesn’t.

It’s funny calling WiFi.disconnect(); in setup, ESP-Now will work but not in the code above.

I will have to keep searching for a solution.

Mike.

You dont have to serve a webpage for use webserver. It will handle the HTTP requests while Wifi TCP server handle the ESP-now.

Still can't get this working, I have trided many ways, any other suggestions?

The problem seems to be with WiFi.begin(ssid, password);
Thanks,
Mike.

Have you tried it with the async webserver example? I dont have time to test it, but according to the example it should work.

Yes I did, I even tride a ESP8266 in many different ways.
Still it does not work.

Thanks for all your help.
Mike.

If you dig up into the problem you found that the problem is probably with the WiFi channel.
Read these: https://github.com/espressif/arduino-esp32/issues/878
https://www.esp32.com/viewtopic.php?t=14542
You have to set the same channel for everything i suppose. Hope this help

One issue there is the time WiFi connection/disconnection might take. You have judged one second to be enough but that may not always be true. What if you made WiFi disconnect and WifI connect separate procedures with the main loop awaiting their return?

What exactly is your App + Hardware for (what is the goal/purpose), and is it only for your own use? There may be other solutions that fit the requirement better.

Thanks for the link Zol.

I tried the suggestions in link, especially the part to make sure the ESP32 is persistent is not storing prior WiFi settings:

void WiFiReset() {
WiFi.persistent(false);
WiFi.disconnect();
WiFi.mode(WIFI_OFF);
}

I also tried setting every thing to the same channel using:
#define WIFI_CHANNEL 10

WiFi.mode(WIFI_AP_STA );
WiFi.begin(ssid, password,WIFI_CHANNEL);

esp_now_peer_info_t peerInfo;
peerInfo.channel = WIFI_CHANNEL;
peerInfo.encrypt = false;
memcpy(peerInfo.peer_addr, NowAddress, 6);

if (esp_now_add_peer(&peerInfo) != ESP_OK) {return;}

Still dose not work.

Thanks,
Mike.

Thanks Chris.
I tried using a separate procedure, with various delay times for connect and disconnect. I also used Milliseconds elapsed, so as not to block any code.

I also tried stopping and restarting ESP-Now in the procedure.

I also tried returning Boolean values to the Loop(); according to the Wifi disconnect and reconnect state, as well as for the ESP-Now state.

No luck getting it to work.

Thanks Mike.

IPs do not match

    IPAddress local_IP(192, 168, 0, 115);
    IPAddress gateway(192, 168, 1, 1);

I tried:
IPAddress local_IP(192, 168, 1, 115);
IPAddress gateway(192, 168, 1, 1);
but it would not connect to my router.

do you mean i should change it to:
IPAddress local_IP(192, 168, 0, 115);
IPAddress gateway(192, 168, 0, 1);

I am vey new to Wifi, and am not sure what you mean?

This works for me...

#include <WiFi.h>
#include <esp_now.h>
#define LED2 2
String LedState = "inicio";

const char* ssid = "Name_of_Router";
const char* password = "Password_net_my_router";

// My net Router is 192.168.1.1

// Setting Static IP.
IPAddress local_IP(192, 168, 1, 115);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress primaryDNS(8, 8, 8, 8); //opcional
IPAddress secondaryDNS(8, 8, 4, 4); //opcional

In Browser
// http://192.168.1.115/?=off12