sandervandegeijn / P1-Meter-ESP8266-MQTT

Software for the ESP8266 that sends P1 smart meter data to MQTT (without CRC checking)
GNU General Public License v3.0
22 stars 7 forks source link

esp8266 board manager ver 2.60 change #5

Open bill-orange opened 5 years ago

bill-orange commented 5 years ago

Recently the ESP8266 board manager ver. 2.6.0 was released. Several changes that had been in the works regarding SoftwareSerial were implemented. As a result the sample code for this project will no longer compile. @dok-net provided this explanation:


Software serial can handle the diagnostics to USB quite well, whereas using the HW UART for the real application is in almost any scenario the superior option. I can’t even begin to imagine why all those libraries never considered

Serial.swap();

Now, if you’re faced with this situation, it’s only fair that I give you the simple change that needs to be done to the sources:

Before, somewhere in the sketch, provided that RX and TX are defines for the RX and TX pin numbers respectively:

SoftwareSerial swSer1(RX, TX); // non-inverted physical protocol, buffer size 256
swSer1.begin(115200);

Now, for quite awhile:

SoftwareSerial swSer1;
swSer1.begin(115200, RX, TX);

Where for the HW UART things would be:

Serial.swap();
Serial.begin(115200);

I hope you see the beauty of it – it makes it simpler to switch to HW UART, and it’s a bit easier on the eye. Plus, switching bitrate at runtime should not involve the constructor!


As a result, I believe that example code should be revised as shown below. Please note, I was not able to test it other than making sure it compiled!

//Infrastructure stuff
const int MAXLINELENGTH = 64;
char telegram[MAXLINELENGTH];
SoftwareSerial mySerial;
constexpr SoftwareSerialConfig swSerialConfig = SWSERIAL_8N1;
constexpr int IUTBITRATE = 115200;
WiFiClient espClient;
PubSubClient client(espClient);

void setup()
{
  Serial.begin(115200);
  mySerial.begin (IUTBITRATE,SERIAL_RX, -1, swSerialConfig,true, MAXLINELENGTH); 
  Serial.println("Booting");
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

  while (WiFi.waitForConnectResult() != WL_CONNECTED)
  {
    Serial.println("Connection Failed! Rebooting...");
    delay(5000);
    ESP.restart();
  }
  // mySerial.begin(115200); removed
  ArduinoOTA.setHostname(hostName);
sandervandegeijn commented 5 years ago

Thanks, I don't have any time the coming weeks to validate your fix. If others post a positive result I'll merge it!

bill-orange commented 5 years ago

I have not retested but it may no longer be necessary. Since my post The serial library has been revised two more time (one still in the release process). The Intent of the changes were to resolve various compatibility issues.

Fingers crossed.