Hello everyone
I'm currently working on a project which uses Arduino Cloud as a GUI for ESP32 boards connected using ESPNOW. I'm having trouble getting the Dashboard to connect. As far as I can tell, everything seems to be correct and the Dashboard was working earlier this week with a simpler program, but now even the simple program doesn't connect to the dashboard. As far as I can tell, all the settings are the same. The network credentials are correct, I have the Cloud Agent opened as an administrator, the code looks/uploads fine, and even the Serial Monitor on the Cloud once I upload my program tells me everything is working and that WiFi is connected.
I've attached my code below, please forgive any formatting issues, this is my first time uploading to this forum.
This connection issue is very frustrating not least because it was working earlier . Please advise.
Thank you, Jason
#include "thingProperties.h"
#include <esp_now.h>
#include <WiFi.h>
// Analog pins for voltage sensors (car presence detection)
#define ANALOG_IN_PIN1 33
#define ANALOG_IN_PIN2 32
#define ANALOG_IN_PIN3 35
#define ANALOG_IN_PIN4 34
#define OUT1 14
#define OUT2 27
#define OUT3 26
#define OUT4 25
#define REF_VOLTAGE 3.3
#define ADC_RESOLUTION 4096.0
#define R1 30000.0 // Resistor values in voltage sensor (in ohms)
#define R2 7500.0
String success;
int pwm1;
int pwm2;
int pwm3;
int pwm4;
float prox1;
float prox2;
float prox3;
float prox4;
float bats[] = { 101, 101, 101, 101 }; //array of batteries, in order of car 1,2,3,4
int sortedIDs[] = { 1, 2, 3, 4 }; //array of carIDs, will be sorted on data receive
float carPresence[] = { 10, 10, 10, 10 }; //array of proximity sensor statuses;
uint8_t broadcastAddressCar1[] = {0x08, 0x3A, 0x8D, 0x23, 0x1A, 0x00};
uint8_t broadcastAddressCar2[] = {0x1C, 0x69, 0x20, 0x82, 0xBA, 0x9C};
uint8_t broadcastAddressCar3[] = {0xCC, 0x7B, 0x5C, 0xF8, 0x5D, 0x08};
uint8_t broadcastAddressCar4[] = {0x1C, 0x69, 0x20, 0x89, 0xE0, 0x58};
typedef struct struct_message {
int pwm1; //pwm level of cars, determined after sorting
int pwm2;
int pwm3;
int pwm4;
float prox1; //proximity sensor
float prox2;
float prox3;
float prox4;
} struct_message;
struct_message myData;
typedef struct receive_message {
float bat; //battery percentage of incoming data's car
int carID; //ID of incoming data's car
} receive_message;
receive_message receiveData;
esp_now_peer_info_t peerInfo;
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
Serial.print("\r\nLast Packet Send Status:\t");
Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
if (status == 0) {
//success = "Delivery Success :)";
} else {
success = "Delivery Fail :(";
}
}
void OnDataRecv(const uint8_t *mac, const uint8_t *incomingData, int len) {
memcpy(&receiveData, incomingData, sizeof(receiveData));
//Serial.println("Data Received.");
if (receiveData.carID == 1) { //take the battery of incoming car and assign to proper place
bats[0] = receiveData.bat;
gUICar1Bat = receiveData.bat;
} else if (receiveData.carID == 2) {
bats[1] = receiveData.bat;
gUICar2Bat = receiveData.bat;
} else if (receiveData.carID == 3) {
bats[2] = receiveData.bat;
gUICar3Bat = receiveData.bat;
} else {
bats[3] = receiveData.bat;
gUICar4Bat = receiveData.bat;
}
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
// Set device as a Wi-Fi Station
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
WiFi.mode(WIFI_STA);
analogSetAttenuation(ADC_11db); // Set ADC range to 3.3V
// Init ESP-NOW
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
return;
}
// Once ESPNow is successfully Init, we will register for Send CB to
// get the status of Trasnmitted packet
esp_now_register_send_cb(OnDataSent);
// Register peer
peerInfo.channel = 0;
peerInfo.encrypt = false;
// Add peer
memcpy(peerInfo.peer_addr, broadcastAddressCar1, 6);
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
Serial.println("Failed to add peer");
return;
}
memcpy(peerInfo.peer_addr, broadcastAddressCar2, 6);
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
Serial.println("Failed to add peer");
return;
}
memcpy(peerInfo.peer_addr, broadcastAddressCar3, 6);
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
Serial.println("Failed to add peer");
return;
}
memcpy(peerInfo.peer_addr, broadcastAddressCar4, 6);
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
Serial.println("Failed to add peer");
return;
}
esp_now_register_recv_cb(esp_now_recv_cb_t(OnDataRecv));
}
void loop() {
// put your main code here, to run repeatedly:
ArduinoCloud.update();
float adc_1 = analogRead(ANALOG_IN_PIN1);
float adc_2 = analogRead(ANALOG_IN_PIN2);
float adc_3 = analogRead(ANALOG_IN_PIN3);
float adc_4 = analogRead(ANALOG_IN_PIN4);
float voltage_adc1 = (adc_1 * REF_VOLTAGE) / ADC_RESOLUTION;
prox1 = voltage_adc1 * (R1 + R2) / R2;
float voltage_adc2 = (adc_2 * REF_VOLTAGE) / ADC_RESOLUTION;
prox2 = voltage_adc2 * (R1 + R2) / R2;
float voltage_adc3 = (adc_3 * REF_VOLTAGE) / ADC_RESOLUTION;
prox3 = voltage_adc3 * (R1 + R2) / R2;
float voltage_adc4 = (adc_4 * REF_VOLTAGE) / ADC_RESOLUTION;
prox4 = voltage_adc4 * (R1 + R2) / R2;
carPresence[0] = prox1;
carPresence[1] = prox2;
carPresence[2] = prox3;
carPresence[3] = prox4;
if (prox1 < 1) {
analogWrite(OUT1,255);
} else{
analogWrite(OUT1,0);
}
if (prox2 < 1) {
analogWrite(OUT2,255);
} else{
analogWrite(OUT2,0);
}
if (prox3 < 1) {
analogWrite(OUT3,255);
} else{
analogWrite(OUT3,0);
}
if (prox4 < 1) {
analogWrite(OUT4,255);
} else{
analogWrite(OUT4,0);
}
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3 - i; j++) {
if (bats[j] > bats[j + 1]) {
int intTemp = sortedIDs[j]; //sort ID array in order of lowest battery to highest
sortedIDs[j] = sortedIDs[j + 1];
sortedIDs[j + 1] = intTemp;
}
}
}
int count = 0; //number of cars
for (int i = 0; i < 4; i++) {
if (carPresence[i] < 1) { //count number of cars
count += 1;
}
}
//logic for assigning PWMs
if (count == 0) { //case where no cars; don't assign pwm
pwm1 = 0;
pwm2 = 0;
pwm3 = 0;
pwm4 = 0;
} else if (count == 1) { //case where we have one car, it gets 100
int PWMS[] = { 255, 0, 0, 0 };
//sort again to put PWM into correct location in array
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3 - i; j++) {
if (sortedIDs[j] > sortedIDs[j + 1]) {
int intTemp = sortedIDs[j]; //sort ID array back into order of 1,2,3,4
sortedIDs[j] = sortedIDs[j + 1];
sortedIDs[j + 1] = intTemp;
int temp2 = PWMS[j]; //sort PWM array
PWMS[j] = PWMS[j + 1];
PWMS[j + 1] = temp2;
}
}
}
pwm1 = PWMS[0];
pwm2 = PWMS[1];
pwm3 = PWMS[2];
pwm4 = PWMS[3];
} else if (count == 2) { //case where we have two cars, both get 100
int PWMS[] = { 255, 255, 0, 0 };
//sort again to put PWM into correct location in array
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3 - i; j++) {
if (sortedIDs[j] > sortedIDs[j + 1]) {
int intTemp = sortedIDs[j]; //sort ID array back into order of 1,2,3,4
sortedIDs[j] = sortedIDs[j + 1];
sortedIDs[j + 1] = intTemp;
int temp2 = PWMS[j]; //sort PWM array
PWMS[j] = PWMS[j + 1];
PWMS[j + 1] = temp2;
}
}
}
pwm1 = PWMS[0];
pwm2 = PWMS[1];
pwm3 = PWMS[2];
pwm4 = PWMS[3];
} else if (count == 3) { //case with 3 cars: lowest battery gets 100%, rest get 50%
int PWMS[] = { 255, 128, 128, 0 };
//sort again to put PWM into correct location in array
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3 - i; j++) {
if (sortedIDs[j] > sortedIDs[j + 1]) {
int intTemp = sortedIDs[j]; //sort ID array back into order of 1,2,3,4
sortedIDs[j] = sortedIDs[j + 1];
sortedIDs[j + 1] = intTemp;
int temp2 = PWMS[j]; //sort PWM array
PWMS[j] = PWMS[j + 1];
PWMS[j + 1] = temp2;
}
}
}
pwm1 = PWMS[0];
pwm2 = PWMS[1];
pwm3 = PWMS[2];
pwm4 = PWMS[3];
} else if (count == 4) {
int PWMS[] = { 255, 128, 64, 64};
//sort again to put PWM into correct location in array
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3 - i; j++) {
if (sortedIDs[j] > sortedIDs[j + 1]) {
int intTemp = sortedIDs[j]; //sort ID array back into order of 1,2,3,4
sortedIDs[j] = sortedIDs[j + 1];
sortedIDs[j + 1] = intTemp;
int temp2 = PWMS[j]; //sort PWM array
PWMS[j] = PWMS[j + 1];
PWMS[j + 1] = temp2;
}
}
}
pwm1 = PWMS[0];
pwm2 = PWMS[1];
pwm3 = PWMS[2];
pwm4 = PWMS[3];
}
myData.pwm1 = pwm1;
myData.pwm2 = pwm2;
myData.pwm3 = pwm3;
myData.pwm4 = pwm4;
myData.prox1 = prox1;
myData.prox2 = prox2;
myData.prox3 = prox3;
myData.prox4 = prox4;
Serial.print("bat 1 is ");
Serial.println(bats[0]);
Serial.print("bat 2 is ");
Serial.println(bats[1]);
Serial.print("bat 3 is ");
Serial.println(bats[2]);
Serial.print("bat 4 is ");
Serial.println(bats[3]);
esp_err_t result = esp_now_send(0, (uint8_t *)&myData, sizeof(myData));
if (result == ESP_OK) {
//Serial.println("Sent with success");
} else {
Serial.println("Error sending the data");
}
delay(500);
}