Skip to content

Commit

Permalink
MQTT Fixes & cleanup
Browse files Browse the repository at this point in the history
- Add RSSI value to MQTT #22 - thanks @fredkehler!
- Fix for MQTT reconnect - was set to 50000ms (14 min) for reconnect instead of 5000ms (5 seconds). This was blocking everything else if MQTT disconnected, potentially causing freezing.  Now it will try to reconnect continously for 5 seconds, then every 10 seconds.
- Moved some includes to .h files for consistency
- Fixed some typos in web interface
- Added default voltage calibration value for meter v1.4 rev3. (it is much lower because of lower value resistors for voltage divider (20k and 10k).
  • Loading branch information
CircuitSetup committed Feb 21, 2020
1 parent 6dec8bd commit 9c575cf
Show file tree
Hide file tree
Showing 25 changed files with 89 additions and 83 deletions.
20 changes: 12 additions & 8 deletions Software/EmonESP/src/data/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ <h2>3. MQTT</h2>
</p>
<p><b>Username:</b><br>
<input data-bind="textInput: config.mqtt_user" type="text"><br/>
<span class="small-text">Leave blank for authentication</span>
<span class="small-text">Leave blank for no authentication</span>
</p>
<p><b>Password:</b><br>
<input data-bind="textInput: config.mqtt_pass" type="password"><br/>
Expand Down Expand Up @@ -210,9 +210,13 @@ <h2>7. Calibration Settings</h2>
<p>
<b>Voltage:</b><br>
<input type="text" value="voltage_cal" data-bind="textInput: config.voltage_cal"><br>
<span class="small-text">37106 - 9v AC Transformer - Jameco 157041 (default)
<span class="small-text">37106 - 9v AC Transformer - Jameco 157041
<br>38302 - 9v AC Transformer - Jameco 112336
<br>29462 - 12v AC Transformer - Jameco 167151</span><br>
<br>29462 - 12v AC Transformer - Jameco 167151
<br>For meter versions >= 1.4 rev3:
<br>3920 - 9v AC Transformer - Jameco 157041 (default)
</span><br>

</p>
<p>
<b>CT1:</b><br>
Expand All @@ -231,15 +235,15 @@ <h2>7. Calibration Settings</h2>
<p>
<b>Frequency:</b><br>
<input type="text" data-bind="textInput: config.freq_cal"><br>
<span class="small-text">4485 for 60 Hz (North America Default)
<br>389 for 50 hz (rest of the world)</span>
<span class="small-text">60 Hz - North America: 4231 (Default)
<br>50 Hz - Europe: 135</span>
</p>
<p>
<b>CT Gain (PGA):</b><br>
<input type="text" data-bind="textInput: config.gain_cal"><br>
<span class="small-text"> 0 for 0-99A (1x)
<br>21 for 100A (2x) (default)
<br>42 for 100-200A (4x)</span>
<span class="small-text"> 1x: 0
<br>2x: 21 (default)
<br>4x: 42</span>
<br><br>

<button id="save_cal" data-bind="click: saveCal, text: (saveCalFetching() ? 'Saving' : (saveCalSuccess() ? 'Saved' : 'Save')), disable: saveCalFetching">Save</button><br>
Expand Down
2 changes: 1 addition & 1 deletion Software/EmonESP/src/energy_meter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
// Declaration for SSD1306 display connected using software SPI (default case):
#define OLED_DC 0
#define OLED_CS 16
#define OLED_RESET 2 //17
#define OLED_RESET 17
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &SPI, OLED_DC, OLED_RESET, OLED_CS);
#endif

Expand Down
10 changes: 5 additions & 5 deletions Software/EmonESP/src/energy_meter.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@
For meter <= v1.3:
42080 - 9v AC Transformer - Jameco 112336
32428 - 12v AC Transformer - Jameco 167151
For meter > v1.4:
For meter = v1.4:
37106 - 9v AC Transformer - Jameco 157041
38302 - 9v AC Transformer - Jameco 112336
29462 - 12v AC Transformer - Jameco 167151
For Meters > v1.4 purchased after 11/1/2019 and rev.3
7611 - 9v AC Transformer - Jameco 157041
For Meters >= v1.4 rev.3
3920 - 9v AC Transformer - Jameco 157041
*/
#define VOLTAGE_GAIN 37106
#define VOLTAGE_GAIN 3920

/*
10170 - SCT-006 20A/25mA
Expand All @@ -75,7 +75,7 @@
#define CURRENT_GAIN_CT2 39473

#ifdef SOLAR_METER
#define VOLTAGE_GAIN_SOLAR 37106
#define VOLTAGE_GAIN_SOLAR 3920
#define SOLAR_GAIN_CT1 39473
#define SOLAR_GAIN_CT2 39473
#endif
Expand Down
11 changes: 0 additions & 11 deletions Software/EmonESP/src/http.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,6 @@
#include "emonesp.h"
#include "http.h"

#include <Print.h>
#include <WiFiClientSecure.h> // Secure https GET request

#ifdef ESP32
#include <HTTPClient.h>
#elif defined(ESP8266)
#include <ESP8266HTTPClient.h>
#else
#error Platform not supported
#endif

WiFiClientSecure client; // Create class for HTTPS TCP connections get_https()
HTTPClient http; // Create class for HTTP TCP connections get_http()

Expand Down
10 changes: 10 additions & 0 deletions Software/EmonESP/src/http.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@
// -------------------------------------------------------------------

#include <Arduino.h>
#include <Print.h>
#include <WiFiClientSecure.h> // Secure https GET request

#ifdef ESP32
#include <HTTPClient.h>
#elif defined(ESP8266)
#include <ESP8266HTTPClient.h>
#else
#error Platform not supported
#endif

// -------------------------------------------------------------------
// HTTPS SECURE GET Request
Expand Down
17 changes: 11 additions & 6 deletions Software/EmonESP/src/mqtt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@
#include "emonesp.h"
#include "mqtt.h"
#include "config.h"

#include <Arduino.h>
#include <PubSubClient.h> // MQTT https://github.com/knolleary/pubsubclient PlatformIO lib: 89
#include <WiFiClient.h>
#include "wifi.h"

WiFiClient espClient; // Create client for MQTT
PubSubClient mqttclient(espClient); // Create client for MQTT
Expand All @@ -44,6 +41,7 @@ int i = 0;

// -------------------------------------------------------------------
// MQTT Connect
// Called only when MQTT server field is populated
// -------------------------------------------------------------------
boolean mqtt_connect()
{
Expand All @@ -57,8 +55,8 @@ boolean mqtt_connect()
String strID = String(ESP.getChipId());
#endif


if (mqtt_user.length() == 0) {
//allows for anonymous connection
if (mqttclient.connect(strID.c_str())) { // Attempt to connect
DBUGS.println("MQTT connected");
mqttclient.publish(mqtt_topic.c_str(), "connected"); // Once connected, publish an announcement..
Expand Down Expand Up @@ -122,9 +120,16 @@ void mqtt_publish(String data)
if (int(data[i]) == 0) break;
}

// send esp free ram
String ram_topic = mqtt_topic + "/" + mqtt_feed_prefix + "freeram";
String free_ram = String(ESP.getFreeHeap());
mqttclient.publish(ram_topic.c_str(), free_ram.c_str());

// send wifi signal strength
long rssi = WiFi.RSSI();
String rssi_S = String(rssi);
String rssi_topic = mqtt_topic + "/" + mqtt_feed_prefix + "rssi";
mqttclient.publish(rssi_topic.c_str(), rssi_S.c_str());
}

// -------------------------------------------------------------------
Expand All @@ -137,7 +142,7 @@ void mqtt_loop()
if (!mqttclient.connected()) {
long now = millis();
// try and reconnect continuously for first 5s then try again once every 10s
if ( (now < 50000) || ((now - lastMqttReconnectAttempt) > 100000) ) {
if ( (now < 5000) || ((now - lastMqttReconnectAttempt) > 10000) ) {
lastMqttReconnectAttempt = now;
if (mqtt_connect()) { // Attempt to reconnect
lastMqttReconnectAttempt = 0;
Expand Down
2 changes: 2 additions & 0 deletions Software/EmonESP/src/mqtt.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
// -------------------------------------------------------------------

#include <Arduino.h>
#include <PubSubClient.h> // MQTT https://github.com/knolleary/pubsubclient PlatformIO lib: 89
#include <WiFiClient.h>

// -------------------------------------------------------------------
// Perform the background MQTT operations. Must be called in the main
Expand Down
9 changes: 0 additions & 9 deletions Software/EmonESP/src/ota.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,6 @@
#include "wifi.h"
#include "http.h"

#include <FS.h>

#include <ArduinoOTA.h> // local OTA update from Arduino IDE
#ifdef ESP32
#include <Update.h> // remote OTA update from server
#elif defined(ESP8266)
#include <ESP8266httpUpdate.h> // remote OTA update from server
#endif


// -------------------------------------------------------------------
//OTA UPDATE SETTINGS
Expand Down
2 changes: 2 additions & 0 deletions Software/EmonESP/src/ota.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
// -------------------------------------------------------------------

#include <Arduino.h>
#include <FS.h>
#include <ArduinoOTA.h> // local OTA update from Arduino IDE
#ifdef ESP32
#include <Update.h> // remote OTA update from server
#elif defined(ESP8266)
Expand Down
Binary file modified Software/EmonESP/src/src.ino.esp32.bin
Binary file not shown.
Binary file added Software/EmonESP/src/src.ino.esp32_metering.bin
Binary file not shown.
Binary file modified Software/EmonESP/src/src.spiffs.bin
Binary file not shown.
2 changes: 1 addition & 1 deletion Software/EmonESP/src/web_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ static const char _DUMMY_PASSWORD[] PROGMEM = "_DUMMY_PASSWORD";

#define TEXTIFY(A) #A
#define ESCAPEQUOTE(A) TEXTIFY(A)
String currentfirmware = "2.5.5"; //ESCAPEQUOTE(BUILD_TAG);
String currentfirmware = "2.5.7"; //ESCAPEQUOTE(BUILD_TAG);

void dumpRequest(AsyncWebServerRequest *request) {
if (request->method() == HTTP_GET) {
Expand Down
18 changes: 10 additions & 8 deletions Software/EmonESP/src_solar/data/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ <h2>3. MQTT</h2>
</p>
<p><b>Username:</b><br>
<input data-bind="textInput: config.mqtt_user" type="text"><br/>
<span class="small-text">Leave blank for authentication</span>
<span class="small-text">Leave blank for no authentication</span>
</p>
<p><b>Password:</b><br>
<input data-bind="textInput: config.mqtt_pass" type="password"><br/>
Expand Down Expand Up @@ -210,9 +210,11 @@ <h2>7. Calibration Settings</h2>
<span>Values for voltage and current calibration. <br>For more information on setting custom values for calibration see the <a href="https://github.com/CircuitSetup/Split-Single-Phase-Energy-Meter">Energy Meter Github page.</a> The default values are used unless other values are entered here.</span><br><br>
<p>
<b>Voltage:</b><br>
<span class="small-text">37106 - 9v AC Transformer - Jameco 157041 (default)
<span class="small-text">37106 - 9v AC Transformer - Jameco 157041
<br>38302 - 9v AC Transformer - Jameco 112336
<br>29462 - 12v AC Transformer - Jameco 167151</span><br>
<br>29462 - 12v AC Transformer - Jameco 167151
<br>For meter versions >= 1.4 rev3:
<br>3920 - 9v AC Transformer - Jameco 157041 (default)
<input type="text" value="voltage_cal" data-bind="textInput: config.voltage_cal"><br>
</p>
<p>
Expand Down Expand Up @@ -241,15 +243,15 @@ <h2>7. Calibration Settings</h2>
<p>
<b>Frequency:</b><br>
<input type="text" data-bind="textInput: config.freq_cal"><br>
<span class="small-text">4485 for 60 Hz (North America Default)
<br>389 for 50 hz (rest of the world)</span>
<span class="small-text">60 Hz - North America: 4231 (Default)
<br>50 Hz - Europe: 135</span>
</p>
<p>
<b>CT Gain (PGA):</b><br>
<input type="text" data-bind="textInput: config.gain_cal"><br>
<span class="small-text"> 0 for 0-10A (1x)
<br>21 for 100A (2x) (default)
<br>42 for 100-200A (4x)</span>
<span class="small-text"> 1x: 0
<br>2x: 21 (default)
<br>4x: 42</span>
<br><br>

<button data-bind="click: saveCal, text: (saveCalFetching() ? 'Saving' : (saveCalSuccess() ? 'Saved' : 'Save')), disable: saveCalFetching">Save</button><br>
Expand Down
4 changes: 2 additions & 2 deletions Software/EmonESP/src_solar/energy_meter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
// Declaration for SSD1306 display connected using software SPI (default case):
#define OLED_DC 0
#define OLED_CS 16
#define OLED_RESET 2 //17
#define OLED_RESET 17
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &SPI, OLED_DC, OLED_RESET, OLED_CS);
#endif

Expand Down Expand Up @@ -105,7 +105,7 @@ char measurement[16];

ATM90E32 eic{}; //initialize the IC class
#ifdef SOLAR_METER
ATM90E32 eic_solar {};
ATM90E32 eic_solar{};
#endif

// -------------------------------------------------------------------
Expand Down
8 changes: 4 additions & 4 deletions Software/EmonESP/src_solar/energy_meter.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@
37106 - 9v AC Transformer - Jameco 157041
38302 - 9v AC Transformer - Jameco 112336
29462 - 12v AC Transformer - Jameco 167151
For Meters > v1.4 purchased after 11/1/2019 and rev.3
7611 - 9v AC Transformer - Jameco 157041
For Meters >= v1.4 rev.3
3920 - 9v AC Transformer - Jameco 157041
*/
#define VOLTAGE_GAIN 37106
#define VOLTAGE_GAIN 3920

/*
10170 - SCT-006 20A/25mA
Expand All @@ -75,7 +75,7 @@
#define CURRENT_GAIN_CT2 39473

#ifdef SOLAR_METER
#define VOLTAGE_GAIN_SOLAR 37106
#define VOLTAGE_GAIN_SOLAR 3920
#define SOLAR_GAIN_CT1 39473
#define SOLAR_GAIN_CT2 39473
#endif
Expand Down
12 changes: 0 additions & 12 deletions Software/EmonESP/src_solar/http.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,6 @@
#include "emonesp.h"
#include "http.h"

#include <Print.h>
#include <WiFiClientSecure.h> // Secure https GET request

#ifdef ESP32
#include <HTTPClient.h>
#elif defined(ESP8266)
#include <ESP8266HTTPClient.h>
#else
#error Platform not supported
#endif


WiFiClientSecure client; // Create class for HTTPS TCP connections get_https()
HTTPClient http; // Create class for HTTP TCP connections get_http()

Expand Down
10 changes: 10 additions & 0 deletions Software/EmonESP/src_solar/http.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@
// -------------------------------------------------------------------

#include <Arduino.h>
#include <Print.h>
#include <WiFiClientSecure.h> // Secure https GET request

#ifdef ESP32
#include <HTTPClient.h>
#elif defined(ESP8266)
#include <ESP8266HTTPClient.h>
#else
#error Platform not supported
#endif

// -------------------------------------------------------------------
// HTTPS SECURE GET Request
Expand Down
Loading

0 comments on commit 9c575cf

Please sign in to comment.