Juan_Antonio’s ESP32 examples, Need Help

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

Thank you,
like I said if i use:
IPAddress local_IP(192, 168, 1, 115);
IPAddress gateway(192, 168, 1, 1);
it wont connect to my router.

I will try your eample with:
IPAddress local_IP(192, 168, 0, 115);
IPAddress gateway(192, 168, 0, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress primaryDNS(8, 8, 8, 8); //opcional
IPAddress secondaryDNS(8, 8, 4, 4); //opcional

and let you know if it works.
Mike.

You must know the gateway, ssid and password of your Router.

Run in Windows

ipconfig to get gateway

ok, it shows 0.xx in all fields
so this should be my settings right?

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

https://www.google.com/search?q=ipconfig+how+get+gateway&sxsrf=ALeKk02dJivnngBpAucADG1Qk5DTDQ7Itw:1603645661199&source=lnms&tbm=isch&sa=X&ved=2ahUKEwjao-zPndDsAhWHZd8KHX11DdwQ_AUoA3oECBAQBQ&biw=1280&bih=611

Looks like Juan has identified the cause of your main issue.

Note that using procedures that return a value negate the use of using delays - the script continues when the procedure has finished it's task, be that 1 second or 1 year.

ok using ipconfig i get this as the gateway:
192.168.0.1

so i changed my setings to this:
IPAddress local_IP(192, 168, 0, 115);
IPAddress gateway(192, 168, 0, 2);

I can turn the led on/off fine, but ESP-Now still reports:
Delivery Status: fail
Mike.

Areyou saying that ESP-Now is reporting:
Delivery Status: ok or that you can just control the led?

I can controll the led fine it is ESP-Now that keeps reporting:
Delivery Status: fail
Thanks mike.

The Web Server worked for me, the ESP32_NOW I have not tried it because I have never used it.

Try this with Master - Slave

Ok i have no problem with the web server working, it is getting ESP-now to work with it.

Thank you very much for all your time.
Mike.

If you have the kindness and the time, it should only take a few min.

If you have a 2 ESP32 boards or a ESP32 and a ESP8266

If you load my original code at the top of my post into a ESP32

And load this code into another ESP32 to be the ESP-Now receiver:

#include <WiFi.h>
#include <esp_wifi.h>
#include <esp_now.h>

uint8_t CustomMacAddress = {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 setup() {
Serial.begin(115200);

WiFi.mode(WIFI_STA);
esp_wifi_set_mac(ESP_IF_WIFI_STA, &CustomMacAddress[0]);

if (esp_now_init() != ESP_OK) {return;}

esp_now_register_recv_cb(OnDataRecv);

}

void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
memcpy(&NowData, incomingData, sizeof(NowData));

Serial.print("Bytes received: ");
Serial.println(len);

Serial.print("D1: "); Serial.println(NowData.d1);
Serial.print("D2: "); Serial.println(NowData.d2);

Serial.println();
}

void loop() {

}

or if you don't have another ESP32 load this code into a ESP8266

#include <ESP8266WiFi.h>
#include <espnow.h>

uint8_t CustomMacAddress = {0x20, 0xAE, 0xA4, 0x20, 0x0D, 0x20};

typedef struct now_struct {
int d1;
int d2;
}
now_struct;
now_struct NowData;

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

WiFi.mode(WIFI_STA);
wifi_set_macaddr(STATION_IF, &CustomMacAddress[0]);

if (esp_now_init() != 0) {return;}

esp_now_set_self_role(ESP_NOW_ROLE_SLAVE);
esp_now_register_recv_cb(OnDataRecv);

}

void OnDataRecv(uint8_t * mac, uint8_t *incomingData, uint8_t len) {
memcpy(&NowData, incomingData, sizeof(NowData));

Serial.print("Bytes received: ");
Serial.println(len);

Serial.print("D1: "); Serial.println(NowData.d1);
Serial.print("D2: "); Serial.println(NowData.d2);
Serial.println();
}

void loop() {

}

Then watch the serial monitor on the ESP32 sender you will see:
Delivery Status: fail

Comment out the WiFi.begin(ssid, password);

Then reload it you will see:
Delivery Status: ok

Thanks,
Mike.

Thanks Chris,
My main issue is not getting the server to work and control the LED, it is getting ESP-Now to work with it.

Yes, I am aware the script continues when the procedure has finished.

That is why I used elapsedMillis.h (a more advanced way to use none-blocking delays than Milliseconds elapsed)
And switch (Case) as a sort of State Machine to go threw each step one at a time, before allowing the next chunk of code to run.

Thank you for your input.

Mike.

That's good practise :sunglasses:

Having done that, with Procedures, I can't see how your setup would fail given that you can successfully run each comms individually - your idea of switching between them should work.