Your Code that I edit for ESP32 only and I try Verify ..
and it will error at this line
"esp_wifi_set_mac(ESP_IF_WIFI_AP, &GatewayMac[0]);"
`
#define SKETCH_NAME "ESP-NOW GATEWAY"
#define SKETCH_VERSION "2022-01-01"
#define SKETCH_ABOUT "ESP-Now Gateway template code and demonstrator of simultaneous ESP-Now and WiFi."
/*
- This is a template code for an ESP-Now Gateway for ESP8266 and ESP32. It assumes Arduino IDE.
- Note. ESP8266 and ESP32 use different WiFi implementations why you must set the appropriate
- #define USE_ESP8266 or USE_ESP32. Please remember to also set the appropriate BOARD in the
- ARDUINO IDE to find the correct include files.
- The Gateway receives messages on the Espressif proprietary protocol ESP-Now. It also provides a simple WEB server
- displaying the received temperature data from all sensors. This is to demonstrate a single ESP8266 is capable of
- communicating on ESP-Now and 2.4 GHz Wifi simoultaneously. One restriction apply, this only works on WiFi channel 1
- and you must therefore set the router to "fixed" channel 1.
- In order to perform the demonstration you will need:
-
- 1x ESP8266 or ESP32 programmed with this sketch, acting as Gateway (to the WiFi/internet), and
-
- 1-20x ESP8266 or ESP32 programmed with the espnow_sensor sketch and equipped with a temperature sensor
- (or anything else you connect and modify the code for).
- This is a simple demo sketch but fully working. It demostrates simultaneous WiFi and ESP-Now on ESP8266 and ESP32.
- You can add other Internet services like MQTT, Blynk, ThingSpeak etc. (I have been running this for 1.5+ years with
- those services.)
- AUTHOR:
- Jonas Byström, https://github.com/jonasbystrom
- CREDITS:
- Code related to ESP-Now is partly based on code from:
-
- With important info from:
-
- And great info from:
-
#include <esp_now.h>
#include <esp_wifi.h>
#include <WiFi.h>
#include <WiFiMulti.h>
#include <WebServer.h>
#ifndef LED_BUILTIN
#define LED_BUILTIN 5 // Set this to the built-in LED number of your ESP32 board
#endif
// ------------------------------------------------------------------------------------------
// ESP-NOW SYSTEM CONFIGS
// ------------------------------------------------------------------------------------------
//
// This is the MAC address to be installed (sensors shall then send to this MAC address)
uint8_t GatewayMac[] = {0x02, 0x10, 0x11, 0x12, 0x13, 0x14};
/* Note, the Gateway listens on this MAC address. All sensors shall send to this MAC address.
* You can set any MAC address of your choice according to this table of "free-to-use local MAC addresses":
* {0x02, any, any, any, any, any}
* {0x06, any, ...}
* {0x0A, any, ...}
* {0x0E, any, ...}
*
* Further, it would be possible to use the built-in HW-MAC address of the ESP8266. But, in case you would need to
* change the ESP (for any reasons) you would then need to update ALL sensors to the same new MAC address.
* It is therefore better to use a soft MAC defined by code. This will be installed in any new ESP HW you may use.
* Just remeber to set a new MAC address for every new system (gateway+sensors) you install in paralell. *
*/
// ESP-Now message format. Sensor data is transmitted using this struct.
typedef struct sensor_data_t {
int unit; // Unit no to identy which sensor is sending
float temp; // Temperature (C)
float humidity; // Humidity (%)
float baro_pressure; // Barometric pressure (hPa)
int lux; // Light sensor data (lux)
float Vbat; // Battery voltage level (V)
char ID[80]; // Any clear text to identify the unit
int wakeTimeMS; // Sensor wake time until sending data
unsigned long updated; // Epoch time when received by Gateway. Set by gateway/receiver. (Not used by sensor, but part of struct for convenience reasons.)
} sensor_data_t;
// ------------------------------------------------------------------------------------------
// ESP-NOW GATEWAY CONFIGS
// ------------------------------------------------------------------------------------------
// Router WiFi Credentials (runnning on 2.4GHz and Channel=1)
#define SOFTAP_SSID "...YOUR SSID Network Name..."
#define SOFTAP_PASS "...YOUR Network Password ..."
#define UNITS 20 // No of esp-now sensor units supported to receive from. ESP-Now has a maximum of 20
// ------------------------------------------------------------------------------------------
// GLOBALS
// ------------------------------------------------------------------------------------------
WiFiMulti wifiMulti;
sensor_data_t bufSensorData; // buffer for incoming data
sensor_data_t sensorData[UNITS+1]; // buffer for all sensor data
// ------------------------------------------------------------------------------------------
// ESP-NOW functions
// ------------------------------------------------------------------------------------------
//
// This is a callback function from ESP (?), anyway (!IMPORTANT)
//
// (Note. I noted now, 1 1/2 years later, when i clean this up and retest a gateway with ESP32 - this initVariant
// seems not to be called by ESP routines anymore. I have as a fix called this routine manually in the setup.
// This could of course (?) be done also for ESP8266. But i dont want to touch that code since it has been working
// for 1.5 years now.)
//
void initVariant()
{
WiFi.mode(WIFI_AP);
esp_wifi_set_mac(ESP_IF_WIFI_AP, &GatewayMac[0]); //ESP32 code
}
// Callback when data is received from any Sender
void OnDataRecv(const uint8_t *mac_addr, const uint8_t *data, int data_len)
{
digitalWrite (LED_BUILTIN, !HIGH); // Led ON
char macStr[24];
snprintf(macStr, sizeof(macStr), " %02x:%02x:%02x:%02x:%02x:%02x",
mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
Serial.print("\nData received from: "); Serial.println(macStr);
memcpy(&bufSensorData, data, sizeof(bufSensorData));
// Print data
Serial.print ("ID: ");
Serial.print (bufSensorData.ID);
Serial.println ("");
Serial.print ("Unit: ");
Serial.print (bufSensorData.unit);
Serial.print (" Temp: ");
Serial.print (bufSensorData.temp);
Serial.print (" Humidity: ");
Serial.print (bufSensorData.humidity);
Serial.print (" Baro: ");
Serial.print (bufSensorData.baro_pressure);
Serial.print (" Lux: ");
Serial.print (bufSensorData.lux);
Serial.println ("");
Serial.print ("Vbat: ");
Serial.print (bufSensorData.Vbat);
Serial.print (" Wake: ");
Serial.print (bufSensorData.wakeTimeMS);
Serial.println ("");
// Store data
int i = bufSensorData.unit;
if ( (i >= 1) && (i <= UNITS) ) {
memcpy(&sensorData[i], data, sizeof(bufSensorData));
};
digitalWrite (LED_BUILTIN, !LOW); // Led OFF}
}
// ------------------------------------------------------------------------------------
// WEB server functions
// ------------------------------------------------------------------------------------
WebServer server(80);
void handleRoot() {
digitalWrite (LED_BUILTIN, !HIGH); // Led ON
// Create a (very) simple response, just to demonstrate
String msg;
msg = "ESP-Now Gateway \n";
for (int i=1; i<=UNITS; i++) {
String str2 = (i<10)?("0"+String(i)):(String(i)); // Make i to 2 pos string
msg += String("Unit: "+str2+" Temp: "+String(sensorData[i].temp)+" C\n");
}
server.send(200, "text/plain", msg );
Serial.println ("Web server call:" + msg);
digitalWrite (LED_BUILTIN, !LOW); // Led OFF}
}
void handleNotFound() {
String message = "File Not Found\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET) ? "GET" : "POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for (uint8_t i = 0; i < server.args(); i++) {
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
server.send(404, "text/plain", message);
}
// ------------------------------------------------------------------------------------
void setup()
// ------------------------------------------------------------------------------------
{
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite (LED_BUILTIN, !HIGH); // Led ON (Most often not working on ESP32)
// Init Serial
Serial.begin(115200);
while (!Serial) {};
delay(100); // Needed for some boards
Serial.println("\n\n");
// Print sketch intro ---------------------------
Serial.println();
Serial.println("===========================================");
Serial.println(SKETCH_NAME);
Serial.println(SKETCH_VERSION);
Serial.println(SKETCH_ABOUT);
Serial.println("===========================================");
// initVariant() seems (?) not to be called anymore for ESP32. It used to ...? Anyway, just call it from here.
// (And of course, we could maybe just move all the code here, also for ESP8266. Some other day ...)
initVariant();
// Connect to WiFi ------------------------------
Serial.print("Connecting to WiFi ");
// Set device in AP mode to begin with
WiFi.mode(WIFI_AP_STA); // AP and STA is required (!IMPORTANT)
wifiMulti.addAP(SOFTAP_SSID, SOFTAP_PASS); // I use wifiMulti ... just by habit, i guess ....
while (wifiMulti.run() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
// Come here - we are connected
Serial.println(" Done");
// Print WiFi data
Serial.println("Set as AP_STA station.");
Serial.print ("SSID: "); Serial.println(WiFi.SSID());
Serial.print ("Channel: "); Serial.println(WiFi.channel());
Serial.print ("IP address: "); Serial.println(WiFi.localIP());
delay(1000);
// Initialize ESP-Now ---------------------------
// Config gateway AP - set SSID and channel
int channel = WiFi.channel();
if (WiFi.softAP(SOFTAP_SSID, SOFTAP_PASS, channel, 1)) {
Serial.println("AP Config Success. AP SSID: " + String(SOFTAP_SSID));
} else {
Serial.println("AP Config failed.");
}
// Print MAC addresses
Serial.print("AP MAC: "); Serial.println(WiFi.softAPmacAddress());
Serial.print("STA MAC: "); Serial.println(WiFi.macAddress());
// Init ESP-Now
#define ESPNOW_SUCCESS ESP_OK
if (esp_now_init() == ESPNOW_SUCCESS) {
Serial.println("ESP - Now Init Success");
} else {
Serial.println("ESP - Now Init Failed");
ESP.restart(); // just restart if we cant init ESP-Now
}
// ESP-Now is now initialized. Register a callback fcn for when data is received
esp_now_register_recv_cb(OnDataRecv);
// Set web server callback functions
server.on("/", handleRoot);
server.onNotFound(handleNotFound);
// Start web server
server.begin();
Serial.print("WEB server started on SSID: "); Serial.print (WiFi.SSID()); Serial.print (" with IP: "); Serial.println(WiFi.localIP());
digitalWrite (LED_BUILTIN, !LOW); // Led OFF
}
// ------------------------------------------------------------------------------------
void loop()
// ------------------------------------------------------------------------------------
{
server.handleClient();
}
`
When I compiled it has error below
Error
Arduino: 1.8.19 (Windows Store 1.8.57.0) (Windows 10), Board: "NodeMCU-32S, 80MHz, 921600, None"
C:\Users\NECGEN~1\AppData\Local\Temp\arduino_modified_sketch_785761\sketch_aug11d.ino: In function 'void initVariant()':
sketch_aug11d:138:50: error: cannot convert 'esp_interface_t' to 'wifi_interface_t'
esp_wifi_set_mac(ESP_IF_WIFI_AP, &GatewayMac[0]); //ESP32 code
In file included from C:\Users\NECGEN~1\AppData\Local\Temp\arduino_modified_sketch_785761\sketch_aug11d.ino:59:
C:\Users\NECgen4UEFI\Documents\ArduinoData\packages\esp32\hardware\esp32\2.0.4/tools/sdk/esp32/include/esp_wifi/include/esp_wifi.h:659:45: note: initializing argument 1 of 'esp_err_t esp_wifi_set_mac(wifi_interface_t, const uint8_t*)'
esp_err_t esp_wifi_set_mac(wifi_interface_t ifx, const uint8_t mac[6]);
Multiple libraries were found for "WiFi.h"
Used: C:\Users\NECgen4UEFI\Documents\ArduinoData\packages\esp32\hardware\esp32\2.0.4\libraries\WiFi
Not used: C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.57.0_x86__mdqgnx93n4wtt\libraries\WiFi
exit status 1
cannot convert 'esp_interface_t' to 'wifi_interface_t'
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
...
I found some fix this issue, but I dont' know how to use this fix to edit with sketch. please help me,thank you.
I am a newbe to learn about esp-now
--