Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improving the binding to the Serial object and fix SoftwareSerial bug #20

Merged
merged 5 commits into from
Mar 11, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/HardwareSerial/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

[platformio]
src_dir = ./
lib_dir = ../../../

[env]
framework = arduino
Expand Down
2 changes: 1 addition & 1 deletion examples/OLED_64x48/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@

[platformio]
src_dir = ./
lib_dir = ../../../

[env]
framework = arduino
lib_deps = ESP8266_SSD1306

[env:d1_mini]
platform = espressif8266
lib_deps = ${env.lib_deps}, EspSoftwareSerial@>=6.7.1
board = d1_mini

[env:mhetesp32minikit]
Expand Down
1 change: 1 addition & 0 deletions examples/SoftwareSerial/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.vscode
.pio
.pioenvs
.piolibdeps
Expand Down
11 changes: 10 additions & 1 deletion examples/SoftwareSerial/SoftwareSerial.ino
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,18 @@
#include <SoftwareSerial.h>
#endif
#include <PMserial.h> // Arduino library for PM sensors with serial interface

#if !defined(PMS_RX) && !defined(PMS_TX)
const uint8_t PMS_RX = 10, PMS_TX = 11;
constexpr auto PMS_RX = 10;
constexpr auto PMS_TX = 11;
#endif

#ifndef ESP32
SoftwareSerial SoftSerial1(PMS_RX, PMS_TX);
SerialPM pms(PMS5003, SoftSerial1); // PMSx003, RX, TX
#else
SerialPM pms(PMS5003, PMS_RX, PMS_TX); // PMSx003, RX, TX
#endif

void setup()
{
Expand All @@ -19,6 +27,7 @@ void setup()
Serial.println(PMS_RX);
Serial.print(F(" TX:"));
Serial.println(PMS_TX);

pms.init();
}

Expand Down
13 changes: 6 additions & 7 deletions examples/SoftwareSerial/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -10,40 +10,39 @@

[platformio]
src_dir = ./
lib_dir = ../../../
default_envs =

[env]
framework = arduino

[env:uno]
; ATmega328, 5V/16MHz
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please leave the comments like this one, I find them useful to keep track of what each env support

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately PlatformIO erases comments when you change a file from GUI. I hope they ever fix this bug.

Copy link
Contributor Author

@heX16 heX16 Mar 11, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can add back these comments at a later stage

You can use custom fields in environment sections. Just add custom_ prefix.

platformio/platformio-core#3862 (comment)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I'll do that whenever I came around to add the comments back

platform = atmelavr
board = uno

[env:mini168_3V3]
; ATmega168, 3.3V/8MHz
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

platform = atmelavr
board = pro8MHzatmega168

[env:mini328_3V3]
; ATmega328, 3.3V/8MHz
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...

platform = atmelavr
board = pro8MHzatmega328

[env:esp01]
; ESP8266, 512kB flash
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...

platform = espressif8266
lib_deps = EspSoftwareSerial@>=6.7.1
board = esp01
build_flags = -D PMS_RX=2 -D PMS_TX=0

[env:d1_mini]
; ESP8266, 4096kB flash
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...

platform = espressif8266
lib_deps = EspSoftwareSerial@>=6.7.1
board = d1_mini
build_flags = -D PMS_RX=D7 -D PMS_TX=D6

[env:esp32minikit]
platform = espressif32
board = mhetesp32minikit
build_flags = -D PMS_RX=23 -D PMS_TX=19

[env:diecimilaatmega328]
board = diecimilaatmega328
platform = atmelavr
6 changes: 2 additions & 4 deletions examples/debug/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

[platformio]
src_dir = ./
lib_dir = ../../../

[env]
framework = arduino
Expand Down Expand Up @@ -48,21 +49,18 @@ board = maple_mini_b20
[env:esp01]
; esp8266 (512Kb)
platform = espressif8266
lib_deps = EspSoftwareSerial@>=6.7.1
board = esp01

[env:aqmon]
; esp8266 (1Mb)
platform = espressif8266
lib_deps = EspSoftwareSerial@>=6.7.1
board = esp01_1m
upload_resetmethod = nodemcu
build_flags = -D USE_HWSERIAL

[env:d1_mini]
; esp8266 (4Mb)
platform = espressif8266
lib_deps = EspSoftwareSerial@>=6.7.1
board = d1_mini
build_flags = -D PMS_RX=D7 -D PMS_TX=D6

Expand All @@ -80,4 +78,4 @@ board = m5stack-core-esp32
[env:mkrwifi1010]
platform = atmelsam
board = mkrwifi1010
build_flags = -D USE_HWSERIAL1
build_flags = -D USE_HWSERIAL1
3 changes: 3 additions & 0 deletions src/PMserial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ void SerialPM::wake() {

SerialPM::STATUS SerialPM::trigRead()
{
if (hwSerial == false)
static_cast<SoftwareSerial *>(uart)->listen(); // when you want to listen on a port, explicitly select it. (https://www.arduino.cc/en/Tutorial/LibraryExamples/TwoPortReceive)

while (uart->available())
{
uart->read(); // empty the RX buffer
Expand Down
6 changes: 4 additions & 2 deletions src/PMserial.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,18 @@ class SerialPM
float temp, rhum, hcho;
};
};

#ifdef HAS_HW_SERIAL
SerialPM(PMS sensor, HardwareSerial &serial) : pms(sensor)
{
uart = &serial;
hwSerial = true;
}
#endif

#ifdef HAS_SW_SERIAL
SerialPM(PMS sensor, uint8_t rx, uint8_t tx) : pms(sensor)
SerialPM(PMS sensor, Stream &serial) : pms(sensor)
{
SoftwareSerial serial(rx, tx);
uart = &serial;
hwSerial = false;
}
Expand All @@ -98,6 +99,7 @@ class SerialPM
hwSerial = true;
}
#endif

void init();
#define PMS_ERROR_TIMEOUT "Sensor read timeout"
#define PMS_ERROR_PMS_TYPE "Wrong PMSx003 sensor type"
Expand Down