Can not compile on Arduino Nano ESP 32
-
@eiten
It is the SoilMoistSensor.ino skecth from MySensor example.*// Enable debug prints to serial monitor
#define MY_DEBUG
//#define MY_GATEWAY_ESP32// Enable and select radio type attached
//#define MY_RADIO_RF24
//#define MY_RADIO_NRF5_ESB
#define MY_RADIO_RFM69
//#define MY_RADIO_RFM95#include <math.h> // Conversion equation from resistance to %
#include <MySensors.h>// Setting up format for reading 3 soil sensors
#define NUM_READS (int)10 // Number of sensor reads for filtering
#define CHILD_ID 0MyMessage msg(CHILD_ID, V_LEVEL);
uint32_t SLEEP_TIME = 30000; // Sleep time between reads (in milliseconds)long buffer[NUM_READS];
int idx;/// @brief Structure to be used in percentage and resistance values matrix to be filtered (have to be in pairs)
typedef struct {
int moisture; //!< Moisture
long resistance; //!< Resistance
} values;const long knownResistor = 4700; // Constant value of known resistor in Ohms
int supplyVoltage; // Measured supply voltage
int sensorVoltage; // Measured sensor voltagevalues valueOf[NUM_READS]; // Calculated moisture percentages and resistances to be sorted and filtered
int i; // Simple index variable
void setup()
{
// initialize the digital pins as an output.
// Pin 6,7 is for sensor 1
// initialize the digital pin as an output.
// Pin 6 is sense resistor voltage supply 1
pinMode(6, OUTPUT);// initialize the digital pin as an output. // Pin 7 is sense resistor voltage supply 2 pinMode(7, OUTPUT);}
void presentation()
{
sendSketchInfo("Soil Moisture Sensor Reverse Polarity", "1.0");
present(CHILD_ID, S_MOISTURE);
}void loop()
{measure(6,7,1); Serial.print ("\t"); Serial.println (average()); long read1 = average(); measure(7,6,0); Serial.print ("\t"); Serial.println (average()); long read2= average(); long sensor1 = (read1 + read2)/2; Serial.print ("resistance bias =" ); Serial.println (read1-read2); Serial.print ("sensor bias compensated value = "); Serial.println (sensor1); Serial.println (); //send back the values send(msg.set((int32_t)ceil(sensor1))); // delay until next measurement (msec) sleep(SLEEP_TIME);}
void measure (int phase_b, int phase_a, int analog_input)
{
// read sensor, filter, and calculate resistance value
// Noise filter: median filterfor (i=0; i<NUM_READS; i++) { // Read 1 pair of voltage values digitalWrite(phase_a, HIGH); // set the voltage supply on delayMicroseconds(25); supplyVoltage = analogRead(analog_input); // read the supply voltage delayMicroseconds(25); digitalWrite(phase_a, LOW); // set the voltage supply off delay(1); digitalWrite(phase_b, HIGH); // set the voltage supply on delayMicroseconds(25); sensorVoltage = analogRead(analog_input); // read the sensor voltage delayMicroseconds(25); digitalWrite(phase_b, LOW); // set the voltage supply off // Calculate resistance // the 0.5 add-term is used to round to the nearest integer // Tip: no need to transform 0-1023 voltage value to 0-5 range, due to following fraction long resistance = (knownResistor * (supplyVoltage - sensorVoltage ) / sensorVoltage) ; delay(1); addReading(resistance); Serial.print (resistance); Serial.print ("\t"); }}
// Averaging algorithm
void addReading(long resistance)
{
buffer[idx] = resistance;
idx++;
if (idx >= NUM_READS) {
idx = 0;
}
}long average()
{
long sum = 0;
for (int cnt = 0; cnt < NUM_READS; cnt++) {
sum += buffer[cnt];
}
return (long)(sum / NUM_READS);
}* -
Your board seems to use USBCDC to communicate with the PC. So in the file
/hal/architecture/ESP32/MyHwESP32.cpp, line 30, you have to changeMY_SERIALDEVICE.begin(MY_BAUD_RATE, SERIAL_8N1);toMY_SERIALDEVICE.begin(MY_BAUD_RATE);.
Furthermore, as ESP32 implements the functionsleep(uint32_t)iself, you need to change line 147 in the sketch fromsleep(SLEEP_TIME);tosleep(SLEEP_TIME, false);.Hope this helps!
Regards, Ei
-
As an additional info, sleep will not work on the ESP in version 2.3.2. Try the developement branch from the github if you need sleep, there I implemented this.
-
@ctodor of course:
https://github.com/mysensors/MySensors/tree/development does contain sleep code for ESP. You can download the library here as a zip file. -
@eiten
Sorry for the late response, but I didn't had time to work on it until today.Yes, I was able to compile but not to upload. After "uploading", the Arduino NANO ESP 32 disconects from the PC.
I must reset the board in order to be able to upload another sketch. -
OK, so I suppose the build flags are incorrect. Do you use PlatformIO? Then, you could try:
build_flags = -D ARDUINO_USB_MODE=1 -D ARDUINO_USB_CDC_ON_BOOT=1If you are on the Arduino IDE, you have to set
Tools -> USB-Mode -> CDC-Mode. Ore something similar, I don't have the Arduino IDE installed ATM. IIRC, monitor speed must be set to 460800. -
OK, so I suppose the build flags are incorrect. Do you use PlatformIO? Then, you could try:
build_flags = -D ARDUINO_USB_MODE=1 -D ARDUINO_USB_CDC_ON_BOOT=1If you are on the Arduino IDE, you have to set
Tools -> USB-Mode -> CDC-Mode. Ore something similar, I don't have the Arduino IDE installed ATM. IIRC, monitor speed must be set to 460800.@eiten Yep, I was able to upload the sketch after I've set Tools -> USB-Mode -> CDC-Mode.
Thank you for your help.Well, I think I rushed with the conclusion.
Somehow, the bord is now in infinite boot loop:0;255;3;0;14;Gateway startup complete.
0;255;0;0;18;2.3.2
ESP-ROM:esp32s3-20210327
Build:Mar 27 2021
rst:0x8 (TG1WDT_SYS_RST),boot:0x2b (SPI_FAST_FLASH_BOOT)
Saved PC:0x4200d223
SPIWP:0xee
mode:DIO, clock div:1
load:0x3fce3808,len:0x44c
load:0x403c9700,len:0xbe4
load:0x403cc700,len:0x2a68
entry 0x403c98d4repetes over and over
-
That's strange. Is it exactly the code from above? It seems, it is in gateway mode, but in your sketch, you commented out the gateway option...
@eiten You are right. It is not the same sketch. The previuos sketch seems to work fine (I used it just to make sure I am able to compile and upload).
Now I'm trying to upload a sketch where the ardunio runs as gateway and this is what a need , a gateway that receives messages from a few magnetic door sensors.
But please, don't waste your time with me. I think I'm going to write my simple protocol to do such a simple task.Thank you very much for your effort.
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login