I've been exploring Arduino for a year, but am new to the dark arts of library development. I have spent some time looking at tutorials on writing libraries, but adapting one is beyond me at the moment.
The project is using a MonkMakes Plant Monitor with an Adafruit ESP32-S3 feather board.
The challenge is that the plant monitor has Software Serial coded into the library, and it does not compile for the ESP32. The board has rx & tx pins available as Serial1 - I hope those are the right terms!
Trying to replace SoftwareSerial in the library throws compilation errors - currently: Compilation error: no matching function for call to 'PlantMonitor::PlantMonitor(HardwareSerial*)'
The attempted library adaptions are:
PlantMonitorRob.cpp
Here is an example of declaring/defining serial port 2, assigning RX, TX pins and echoing to Serial on the ESP32.
Plenty of other examples (for Serial1, etc.) can be found on the web (e.g. search for "define serial ports ESP32"). Just about any free GPIO pins can be used for TX and RX.
//DEV MODULE selected for ESP32-CAM
// works as expected with UART2, RX=14, TX=15
#include <HardwareSerial.h>
HardwareSerial SerialPort(2); // use UART2
void setup()
{
Serial.begin(115200);
while (!Serial);
Serial.println("Serial test");
SerialPort.begin(115200, SERIAL_8N1, 14, 15);
}
void loop()
{
while (Serial.available()) {
SerialPort.write(Serial.read());
}
while (SerialPort.available()) {
Serial.write(SerialPort.read());
}
}
Thank you @Delta_G and @noiasca I think I'm almost there. After compiling the following error is flagged:
In file included from D:\Data\Arduino\sketches\plant monitor\20240901_simple_v01\20240901_simple_v01.ino:1:
d:\Data\Arduino\libraries\plant_monitor/PlantMonitorRob.h: In constructor 'PlantMonitor::PlantMonitor(Stream&)':
d:\Data\Arduino\libraries\plant_monitor/PlantMonitorRob.h:25:48: error: expected '{' at end of input
25 | PlantMonitor(Stream &stream) : stream(stream);
| ^
Using library plant_monitor in folder: D:\Data\Arduino\libraries\plant_monitor (legacy)
exit status 1
Compilation error: exit status 1
The PlantMonitorRob,h file is:
#ifndef PlantMonitorRob_h // updated to avoid library conflicts
#define PlantMonitorRob_h
#include "Arduino.h"
class PlantMonitor
{
private:
Stream& stream;
public:
int getWater();
float getTemp();
float getHumidity();
void ledOn();
void ledOff();
void begin(); // Add a begin method that you can call from setup
PlantMonitor(Stream &stream) : stream(stream);
};
#endif
// based on https://github.com/monkmakes/mm_plant_monitor
#ifndef PlantMonitorRob_h // updated to avoid library conflicts
#define PlantMonitorRob_h
#include "Arduino.h"
class PlantMonitor
{
private:
Stream& stream;
public:
int getWater();
float getTemp();
float getHumidity();
void ledOn();
void ledOff();
void begin(); // Add a begin method that you can call from setup
PlantMonitor(Stream &stream) : stream(stream) {}
};
#endif
stream doesn't have a begin function - hence you can't use begin on the stream reference.
I just have commented it: