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