Skip to content

Commit fbee074

Browse files
committed
Replaced MQTT lib with async, watchdog enabled, WifiClient
- MQTT now uses async-mqtt-client to prevent MQTT from blocking - The esp32 watchdog is now enabled - A lot of String variables changed to char - HTTPClient changed to properly use WifiClient lib
1 parent 16ef65e commit fbee074

19 files changed

+208
-158
lines changed

Software/EmonESP/src/debug.h

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#define __DEBUG_H
3131

3232
//#define ENABLE_DEBUG
33+
//#define DEBUG_PORT Serial
3334

3435
#define TEXTIFY(A) #A
3536
#define ESCAPEQUOTE(A) TEXTIFY(A)

Software/EmonESP/src/emoncms.cpp

+9-21
Original file line numberDiff line numberDiff line change
@@ -32,43 +32,31 @@
3232
#include "wifi.h"
3333

3434
//EMONCMS SERVER strings
35-
const char *e_url = "/input/post.json?json=";
35+
const char* e_url = "/input/post.json?json=";
3636
boolean emoncms_connected = false;
3737

3838
unsigned long packets_sent = 0;
3939
unsigned long packets_success = 0;
4040
unsigned long emoncms_connection_error_count = 0;
4141

42-
void emoncms_publish(String data)
42+
static char url[MAX_DATA_LEN+100];
43+
44+
void emoncms_publish(const char * data)
4345
{
4446
// We now create a URL for server data upload
45-
String url = emoncms_path.c_str();
46-
url += e_url;
47-
url += "{";
48-
// Copy across, data length
49-
for (int i = 0; i < data.length(); ++i) {
50-
url += data[i];
51-
}
52-
url += ",psent:";
53-
url += packets_sent;
54-
url += ",psuccess:";
55-
url += packets_success;
56-
url += ",freeram:";
57-
url += String(ESP.getFreeHeap());
58-
url += "}&node=";
59-
url += emoncms_node;
60-
url += "&apikey=";
61-
url += emoncms_apikey;
47+
sprintf(url, "%s%s{%s,psent:%lu,psuccess:%lu,freeram:%lu,rssi:%d}&node=%s&apikey=%s",
48+
emoncms_path.c_str(), e_url, data, packets_sent, packets_success, ESP.getFreeHeap(),
49+
WiFi.RSSI(), emoncms_node.c_str(), emoncms_apikey.c_str());
6250

63-
DBUGS.println(url); delay(10);
51+
DBUGLN(url); delay(10);
6452
packets_sent++;
6553

6654
// Send data to Emoncms server
6755
String result = "";
6856
if (emoncms_fingerprint != 0) {
6957
// HTTPS on port 443 if HTTPS fingerprint is present
7058
DBUGS.println("HTTPS Enabled"); delay(10);
71-
result = get_https(emoncms_fingerprint.c_str(), emoncms_server.c_str(), url, 443);
59+
result = get_http(emoncms_server.c_str(), url, 443, emoncms_fingerprint.c_str());
7260
} else {
7361
// Plain HTTP if other emoncms server e.g EmonPi
7462
DBUGS.println("Plain old HTTP"); delay(10);

Software/EmonESP/src/emoncms.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,6 @@ extern unsigned long packets_success;
4444
//
4545
// data: a comma seperated list of name:value pairs to send
4646
// -------------------------------------------------------------------
47-
void emoncms_publish(String data);
47+
void emoncms_publish(const char * data);
4848

4949
#endif // _EMONESP_EMONCMS_H

Software/EmonESP/src/emonesp.h

+3
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,7 @@
3535

3636
#include "debug.h"
3737

38+
#define ENABLE_WDT
39+
#define MAX_DATA_LEN 4096
40+
3841
#endif // _EMONESP_H

Software/EmonESP/src/energy_meter.cpp

+5-6
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,6 @@ const int period = 1000; //time interval in ms to send data
9999
Otherwise a CT may be backwards */
100100
bool canBeNegative = true;
101101

102-
103-
char result[200];
104102
char measurement[16];
105103

106104
ATM90E32 eic{}; //initialize the IC class
@@ -157,6 +155,9 @@ void energy_meter_setup() {
157155
// -------------------------------------------------------------------
158156
void energy_meter_loop()
159157
{
158+
159+
char * result = input_string;
160+
160161
/*get the current "time" (actually the number of milliseconds since the program started)*/
161162
currentMillis = millis();
162163

@@ -456,11 +457,11 @@ void energy_meter_loop()
456457
dtostrf(eic.GetTotalApparentPower(), 2, 4, measurement);
457458
strcat(result, measurement);
458459

459-
strcat(result, ",PhaseA:");
460+
strcat(result, ",CT1Angle:");
460461
dtostrf(eic.GetPhaseA(), 2, 2, measurement);
461462
strcat(result, measurement);
462463

463-
strcat(result, ",PhaseC:");
464+
strcat(result, ",CT2Angle:");
464465
dtostrf(eic.GetPhaseC(), 2, 2, measurement);
465466
strcat(result, measurement);
466467
#endif
@@ -478,6 +479,4 @@ void energy_meter_loop()
478479

479480
//DBUGS.println(result);
480481

481-
input_string = result;
482-
483482
}

Software/EmonESP/src/http.cpp

+30-35
Original file line numberDiff line numberDiff line change
@@ -28,39 +28,53 @@
2828
#include "emonesp.h"
2929
#include "http.h"
3030

31-
WiFiClientSecure client; // Create class for HTTPS TCP connections get_https()
32-
HTTPClient http; // Create class for HTTP TCP connections get_http()
31+
#define HTTP_TIMEOUT 4
32+
33+
WiFiClient client; // Create class for HTTP TCP connections get_http()
34+
WiFiClientSecure client_ssl; // Create class for HTTPS TCP connections get_https()
35+
36+
static char request[MAX_DATA_LEN+100];
3337

3438
// -------------------------------------------------------------------
35-
// HTTPS SECURE GET Request
39+
// HTTP or HTTPS GET Request
3640
// url: N/A
3741
// -------------------------------------------------------------------
3842

39-
String
40-
get_https(const char *fingerprint, const char *host, String url,
41-
int httpsPort) {
43+
String get_http(const char * host, const char * url, int port, const char * fingerprint) {
44+
WiFiClientSecure * http;
45+
46+
if (fingerprint) {
47+
http = &client_ssl;
48+
} else {
49+
http = (WiFiClientSecure *) &client;
50+
}
51+
4252
// Use WiFiClient class to create TCP connections
43-
if (!client.connect(host, httpsPort)) {
44-
DBUGS.print(host + httpsPort); //debug
53+
if (!http->connect(host, port, HTTP_TIMEOUT*1000)) {
54+
DBUGS.printf("%s:%d\n", host, port); //debug
4555
return ("Connection error");
4656
}
57+
http->setTimeout(HTTP_TIMEOUT);
4758
#ifndef ESP32
4859
#warning HTTPS verification not enabled
49-
if (client.verify(fingerprint, host)) {
60+
if (!fingerprint || http->verify(fingerprint, host)) {
5061
#endif
51-
client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host +
52-
"\r\n" + "Connection: close\r\n\r\n");
62+
sprintf(request, "GET %s HTTP/1.1\r\nHost: %s\r\nConnection: close\r\n\r\n", url, host);
63+
http->print(request);
5364
// Handle wait for reply and timeout
5465
unsigned long timeout = millis();
55-
while (client.available() == 0) {
56-
if (millis() - timeout > 5000) {
57-
client.stop();
66+
while (http->available() == 0) {
67+
if (millis() - timeout > (HTTP_TIMEOUT*1000)) {
68+
http->stop();
5869
return ("Client Timeout");
5970
}
71+
#ifdef ENABLE_WDT
72+
feedLoopWDT();
73+
#endif
6074
}
6175
// Handle message receive
62-
while (client.available()) {
63-
String line = client.readStringUntil('\r');
76+
while (http->available()) {
77+
String line = http->readStringUntil('\r');
6478
DBUGS.println(line); //debug
6579
if (line.startsWith("HTTP/1.1 200 OK")) {
6680
return ("ok");
@@ -73,22 +87,3 @@ get_https(const char *fingerprint, const char *host, String url,
7387
#endif
7488
return ("error " + String(host));
7589
}
76-
77-
// -------------------------------------------------------------------
78-
// HTTP GET Request
79-
// url: N/A
80-
// -------------------------------------------------------------------
81-
String
82-
get_http(const char *host, String url) {
83-
http.begin(String("http://") + host + String(url));
84-
int httpCode = http.GET();
85-
if ((httpCode > 0) && (httpCode == HTTP_CODE_OK)) {
86-
String payload = http.getString();
87-
DBUGS.println(payload);
88-
http.end();
89-
return (payload);
90-
} else {
91-
http.end();
92-
return ("server error: " + String(httpCode));
93-
}
94-
} // end http_get

Software/EmonESP/src/http.h

+3-16
Original file line numberDiff line numberDiff line change
@@ -35,26 +35,13 @@
3535

3636
#include <Arduino.h>
3737
#include <Print.h>
38+
#include <WiFiClient.h> // http GET request
3839
#include <WiFiClientSecure.h> // Secure https GET request
3940

40-
#ifdef ESP32
41-
#include <HTTPClient.h>
42-
#elif defined(ESP8266)
43-
#include <ESP8266HTTPClient.h>
44-
#else
45-
#error Platform not supported
46-
#endif
47-
48-
// -------------------------------------------------------------------
49-
// HTTPS SECURE GET Request
50-
// url: N/A
51-
// -------------------------------------------------------------------
52-
extern String get_https(const char* fingerprint, const char* host, String url, int httpsPort);
53-
5441
// -------------------------------------------------------------------
55-
// HTTP GET Request
42+
// HTTP or HTTPS GET Request
5643
// url: N/A
5744
// -------------------------------------------------------------------
58-
extern String get_http(const char* host, String url);
45+
extern String get_http(const char * host, const char * url, int port=80, const char * fingerprint=NULL);
5946

6047
#endif // _EMONESP_HTTP_H

Software/EmonESP/src/input.cpp

+10-10
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,17 @@
2929
#include "emonesp.h"
3030
#include "input.h"
3131

32-
String input_string = "";
33-
String last_datastr = "";
32+
char input_string[MAX_DATA_LEN] = "";
33+
char last_datastr[MAX_DATA_LEN] = "";
3434

35-
boolean input_get(String& data)
35+
boolean input_get(char * data)
3636
{
3737
boolean gotData = false;
3838

3939
// If data from test API e.g `http://<IP-ADDRESS>/input?string=CT1:3935,CT2:325,T1:12.5,T2:16.9,T3:11.2,T4:34.7`
40-
if (input_string.length() > 0) {
41-
data = input_string;
42-
input_string = "";
40+
if (strlen(input_string) > 0) {
41+
strcpy(data, input_string);
42+
strcpy(input_string, "");
4343
gotData = true;
4444
}
4545
#ifdef USE_SERIAL_INPUT
@@ -54,11 +54,11 @@ boolean input_get(String& data)
5454
if (gotData)
5555
{
5656
// Get rid of any whitespace, newlines etc
57-
data.trim();
57+
//data.trim();
5858

59-
if (data.length() > 0) {
60-
DBUGS.printf("Got '%s'\n", data.c_str());
61-
last_datastr = data;
59+
if (strlen(data) > 0) {
60+
// DBUGS.printf("Got '%s'\n", data.c_str());
61+
strcpy(last_datastr, data);
6262
} else {
6363
gotData = false;
6464
}

Software/EmonESP/src/input.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@
3535
// Support for reading input
3636
// -------------------------------------------------------------------
3737

38-
extern String last_datastr;
39-
extern String input_string;
38+
extern char last_datastr[MAX_DATA_LEN];
39+
extern char input_string[MAX_DATA_LEN];
4040

4141
// -------------------------------------------------------------------
4242
// Read input sent via the web_server or serial.
4343
//
4444
// data: if true is returned data will be updated with the new line of
4545
// input
4646
// -------------------------------------------------------------------
47-
extern boolean input_get(String& data);
47+
extern boolean input_get(char * data);
4848

4949
#endif // _EMONESP_INPUT_H

0 commit comments

Comments
 (0)