Hi all,
First off, i am a noob at this (programming with arduino) and would like some advise or checkup for improving my code i've put together. (was intentionally to work with micropython, but esp-now not yet implemented)
The project i am working on is: waterlevel measurement with esp-now
for this project i have used 2 esp32 dev board and one relay board, the 2 esp32 boards are connected to each other with esp-now, the 'master' send a measured distance using the ultrasonic sensor HR-SR04 and then the 'slave' uses this value to make some of the relays on or off.
The project itself work with this code but i think i could use some tweaking:
Sender code:
#include <esp_now.h>
#include <WiFi.h>
uint8_t broadcastAddress[] = {0x24, 0x62, 0xAB, 0xD5, 0x15, 0x30}; //mac-adress ontvanger
int trigPin = 16; // Trigger
int echoPin = 17; // Echo
int redled = 2; // Connection ok
long duration, cm, inches;
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_STA);
Serial.println();
Serial.println(WiFi.macAddress());
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
return;
}
// register peer
esp_now_peer_info_t peerInfo;
memcpy(peerInfo.peer_addr, broadcastAddress, 6);
peerInfo.channel = 0;
peerInfo.encrypt = false;
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
Serial.println("Failed to add peer");
return;
}
//Define inputs and outputs
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(redled, OUTPUT);
}
void loop() {
// The sensor is triggered by a HIGH pulse of 10 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
digitalWrite(trigPin, redled, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the signal from the sensor: a HIGH pulse whose
// duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
// Convert the time into a distance
cm = (duration / 2) / 29.1; // Divide by 29.1 or multiply by 0.0343
inches = (duration / 2) / 74; // Divide by 74 or multiply by 0.0135
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(5000);
{
// send data
esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &cm, sizeof(int));
if (result == ESP_OK) {
Serial.println("Sent with success");
digitalWrite(redled, HIGH);
}
else {
Serial.println("Error sending the data");
digitalWrite(redled, LOW);
}
}
}
Receiver code:
#include <esp_now.h>
#include <WiFi.h>
int high = 16; // Low level
int low = 17; // High level
int fault = 18; // Wrong reading
void onReceiveData(const uint8_t *mac, const uint8_t *cm, int len) {
Serial.print("Received from MAC: ");
for (int i = 0; i < 6; i++) {
Serial.printf("%02X", mac[i]);
if (i < 5)Serial.print(":");
}
int * messagePointer = (int*)cm;
Serial.println();
Serial.println(*messagePointer);
pinMode(high, OUTPUT);
pinMode(low, OUTPUT);
pinMode(fault, OUTPUT);
digitalWrite(high, LOW);
digitalWrite(low, LOW);
if (*cm < 15)
digitalWrite(high, HIGH);
else
digitalWrite(high, LOW);
if (*cm > 30)
digitalWrite(low, HIGH);
else
digitalWrite(low, LOW);
if (*cm == 0)
digitalWrite(fault, HIGH);
else
digitalWrite(fault, LOW);
}
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_STA);
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
return;
}
esp_now_register_recv_cb(onReceiveData);
pinMode(high, OUTPUT);
pinMode(low, OUTPUT);
}
void loop() {}}
Like i said this works but if the sender fails the receiver does not notice this, is there a way to program this ? Also i don't think it runs stable, first boot takes a while and need the restart sometimes...