-
I am successfully transmitting 2 smoothed analog values between 2 esp32s using
the esp now protocol. -
There is a delay I've created in the smoothing loop using millis() which I believe
is working correctly. The delay is for reading stability between readings. As mentioned in example below:
https://www.arduino.cc/en/Tutorial/Smoothing -
I am trying to create a delay in the loop on the both the transmitter side and
receiver side since the values are printing really fast.
arduino-tutorial-using-millis-instead-of-delay/
I am using the delay using millis() example on the website mentioned above (it' s a non-blocking code example), but the delay is still transmitting values rapidly rather than holding them back every 3 seconds.
if (millis() > previousMillis + loopDelay) {
previousMillis = millis();
}
I have this code above inside the void loop and it is independent of the millis() code mentioned the 1st time. I am emulating the same example in the website mentioned above but it is not working. Any advice would be helpful. Thanks.
Transmit:
/* Free and open source, CC BY-SA 4.0
https://espressif.com/sites/default/files/documentation/esp-now_user_guide_en.pdf
https://docs.espressif.com/projects/esp-idf/en/stable/api-reference/wifi/esp_now.html
Modified code based on:
https://www.instructables.com/id/ESP32-With-ESP-Now-Protocol/
http://www.arduino.cc/en/Tutorial/Smoothing
*/
//ESP-Now power saving protocol
#include <esp_now.h>
#include <WiFi.h>
//ESP32 adc conversion
#include <driver/adc.h>
// Channel used for connection
#define CHANNEL 1
// Structure with information about the next peer
esp_now_peer_info_t peer;
// Define the number of samples to keep track of. The higher the number, the
// more the readings will be smoothed, but the slower the output will respond to
// the input. Using a constant rather than a normal variable lets us use this
// data to determine the size of the readings array.
const int numReadings = 64;
const uint8_t numSensors = 2;
uint32_t readings[numSensors][numReadings]; //multi-dimensional readings from analog input
uint32_t total[numSensors] = {0}; // the running total
uint32_t readIndex = 0; // the index of the current reading
uint32_t average = 0; // the average
uint8_t EspNowAverage = 0;
// delay to process smoothing readings
uint16_t numReadingsInterval = 1;
uint32_t previousMillis = 0;
//Earth rotates .25 degrees/minute. In 4 minutes Earth rotates 1 degree.
//Add a delay time if necessary to conserve power for solar cell or battery.
uint16_t loopDelay = 3000;
// Mac Address of the peer to which we will send the data
uint8_t peerMacAddress[] = {0x30, 0xAE, 0xA4, 0xF0, 0x54, 0xA0};
void setup() {
Serial.begin(115200);
// initialize all the readings to 0:
for (uint16_t thisReading = 0; thisReading < numReadings; thisReading++) {
for ( int j = 0; j < numSensors; j++) readings[j][thisReading] = 0;
}
// Call the function that initializes the station mode
modeStation();
// Call function that initializes ESPNow
InitESPNow();
// Add the peer
addPeer(peerMacAddress);
// Registers the callback that will inform us about the status of the submission.
// The function that will be executed is onDataSent and is stated below
esp_now_register_send_cb(OnDataSent);
}
//loop function being used to smooth analog input values
// void OnDataSent function sends values over and over again
void loop() {
// Voltage divider analog in pins
adc1_config_width(ADC_WIDTH_12Bit);
adc1_config_channel_atten(ADC1_CHANNEL_6, ADC_ATTEN_DB_11); //Pin34
adc1_config_width(ADC_WIDTH_12Bit);
adc1_config_channel_atten(ADC1_CHANNEL_7, ADC_ATTEN_DB_11); //Pin35
// millis() -->Returns the number of milliseconds passed since the Arduino board began running the current program.
// This number will overflow (go back to zero), after approximately 50 days.
// timing resets to 0 when millis() - previousMillis becomes greater than or equal to numReadingsInterval * numReadings
// then loop proceeds with remaining functions
if (millis() > previousMillis + loopDelay) {
previousMillis = millis();
}
if ( millis() - previousMillis >= numReadingsInterval * numReadings ) {
previousMillis = millis();
// Read the sensor
uint16_t Azimuth = adc1_get_raw(ADC1_CHANNEL_6);
uint16_t Elevation = adc1_get_raw(ADC1_CHANNEL_7);
uint16_t inputPin[ numSensors ] = {Azimuth, Elevation};
uint16_t ai;
for (ai = 0; ai < numSensors ; ai++) {
// subtract the last reading:
total[ ai ] = total[ ai ] - readings[ai][readIndex];
// read from the sensor:
readings[ai][readIndex] = inputPin[ai];
// add the reading to the total:
total[ai] = total[ai] + readings[ai][readIndex];
// calculate the average:
average = total[ai] / numReadings;
// reduce results to 0-255
EspNowAverage = average / 16; // send smoothed average of each sensor input to receiver
readAndSend();
}
// advance to the next position in the array:
readIndex = readIndex + 1;
// if we're at the end of the array...
if (readIndex >= numReadings) {
// ...wrap around to the beginning:
readIndex = 0;
}
}
}
// Function responsible for pin reading and sending of data to the peer
void readAndSend() {
// Read the data of the pin
uint8_t data = EspNowAverage;
// Send the data to the peer
send(&data, peerMacAddress);
}
// Function that serves as a callback to warn us about the sending situation we made
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Success" : "Fail");
}
// Function to initialize the station mode
void modeStation() {
// We put the ESP in station mode
WiFi.mode(WIFI_STA);
// We show on Serial Monitor the Mac Address
// of this ESP when in station mode
Serial.print("Mac Address in Station: ");
Serial.println(WiFi.macAddress());
}
// ESPNow startup function
void InitESPNow() {
// If the initialization was successful
if (esp_now_init() == ESP_OK) {
Serial.println("ESPNow Init Success");
}
// If initialization failed
else {
Serial.println("ESPNow Init Failed");
ESP.restart();
}
}
// Function that adds a new peer through your MAC address
void addPeer(uint8_t *peerMacAddress) {
// We inform the channel
peer.channel = CHANNEL;
// 0 not to use encryption or 1 to use
peer.encrypt = 0;
// Copy array address to structure
memcpy(peer.peer_addr, peerMacAddress, 6);
// Add slave
esp_now_add_peer(&peer);
}
// Function that will send the data to the peer that has the specified mac address
void send(const uint8_t *data, uint8_t *peerMacAddress) {
esp_err_t result = esp_now_send(peerMacAddress, data, sizeof(data));
}