Skip to content

Commit

Permalink
Replaced MQTT lib with async, watchdog enabled, WifiClient
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
CircuitSetup committed May 19, 2020
1 parent 16ef65e commit fbee074
Show file tree
Hide file tree
Showing 19 changed files with 208 additions and 158 deletions.
1 change: 1 addition & 0 deletions Software/EmonESP/src/debug.h
Expand Up @@ -30,6 +30,7 @@
#define __DEBUG_H

//#define ENABLE_DEBUG
//#define DEBUG_PORT Serial

#define TEXTIFY(A) #A
#define ESCAPEQUOTE(A) TEXTIFY(A)
Expand Down
30 changes: 9 additions & 21 deletions Software/EmonESP/src/emoncms.cpp
Expand Up @@ -32,43 +32,31 @@
#include "wifi.h"

//EMONCMS SERVER strings
const char *e_url = "/input/post.json?json=";
const char* e_url = "/input/post.json?json=";
boolean emoncms_connected = false;

unsigned long packets_sent = 0;
unsigned long packets_success = 0;
unsigned long emoncms_connection_error_count = 0;

void emoncms_publish(String data)
static char url[MAX_DATA_LEN+100];

void emoncms_publish(const char * data)
{
// We now create a URL for server data upload
String url = emoncms_path.c_str();
url += e_url;
url += "{";
// Copy across, data length
for (int i = 0; i < data.length(); ++i) {
url += data[i];
}
url += ",psent:";
url += packets_sent;
url += ",psuccess:";
url += packets_success;
url += ",freeram:";
url += String(ESP.getFreeHeap());
url += "}&node=";
url += emoncms_node;
url += "&apikey=";
url += emoncms_apikey;
sprintf(url, "%s%s{%s,psent:%lu,psuccess:%lu,freeram:%lu,rssi:%d}&node=%s&apikey=%s",
emoncms_path.c_str(), e_url, data, packets_sent, packets_success, ESP.getFreeHeap(),
WiFi.RSSI(), emoncms_node.c_str(), emoncms_apikey.c_str());

DBUGS.println(url); delay(10);
DBUGLN(url); delay(10);
packets_sent++;

// Send data to Emoncms server
String result = "";
if (emoncms_fingerprint != 0) {
// HTTPS on port 443 if HTTPS fingerprint is present
DBUGS.println("HTTPS Enabled"); delay(10);
result = get_https(emoncms_fingerprint.c_str(), emoncms_server.c_str(), url, 443);
result = get_http(emoncms_server.c_str(), url, 443, emoncms_fingerprint.c_str());
} else {
// Plain HTTP if other emoncms server e.g EmonPi
DBUGS.println("Plain old HTTP"); delay(10);
Expand Down
2 changes: 1 addition & 1 deletion Software/EmonESP/src/emoncms.h
Expand Up @@ -44,6 +44,6 @@ extern unsigned long packets_success;
//
// data: a comma seperated list of name:value pairs to send
// -------------------------------------------------------------------
void emoncms_publish(String data);
void emoncms_publish(const char * data);

#endif // _EMONESP_EMONCMS_H
3 changes: 3 additions & 0 deletions Software/EmonESP/src/emonesp.h
Expand Up @@ -35,4 +35,7 @@

#include "debug.h"

#define ENABLE_WDT
#define MAX_DATA_LEN 4096

#endif // _EMONESP_H
11 changes: 5 additions & 6 deletions Software/EmonESP/src/energy_meter.cpp
Expand Up @@ -99,8 +99,6 @@ const int period = 1000; //time interval in ms to send data
Otherwise a CT may be backwards */
bool canBeNegative = true;


char result[200];
char measurement[16];

ATM90E32 eic{}; //initialize the IC class
Expand Down Expand Up @@ -157,6 +155,9 @@ void energy_meter_setup() {
// -------------------------------------------------------------------
void energy_meter_loop()
{

char * result = input_string;

/*get the current "time" (actually the number of milliseconds since the program started)*/
currentMillis = millis();

Expand Down Expand Up @@ -456,11 +457,11 @@ void energy_meter_loop()
dtostrf(eic.GetTotalApparentPower(), 2, 4, measurement);
strcat(result, measurement);

strcat(result, ",PhaseA:");
strcat(result, ",CT1Angle:");
dtostrf(eic.GetPhaseA(), 2, 2, measurement);
strcat(result, measurement);

strcat(result, ",PhaseC:");
strcat(result, ",CT2Angle:");
dtostrf(eic.GetPhaseC(), 2, 2, measurement);
strcat(result, measurement);
#endif
Expand All @@ -478,6 +479,4 @@ void energy_meter_loop()

//DBUGS.println(result);

input_string = result;

}
65 changes: 30 additions & 35 deletions Software/EmonESP/src/http.cpp
Expand Up @@ -28,39 +28,53 @@
#include "emonesp.h"
#include "http.h"

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

WiFiClient client; // Create class for HTTP TCP connections get_http()
WiFiClientSecure client_ssl; // Create class for HTTPS TCP connections get_https()

static char request[MAX_DATA_LEN+100];

// -------------------------------------------------------------------
// HTTPS SECURE GET Request
// HTTP or HTTPS GET Request
// url: N/A
// -------------------------------------------------------------------

String
get_https(const char *fingerprint, const char *host, String url,
int httpsPort) {
String get_http(const char * host, const char * url, int port, const char * fingerprint) {
WiFiClientSecure * http;

if (fingerprint) {
http = &client_ssl;
} else {
http = (WiFiClientSecure *) &client;
}

// Use WiFiClient class to create TCP connections
if (!client.connect(host, httpsPort)) {
DBUGS.print(host + httpsPort); //debug
if (!http->connect(host, port, HTTP_TIMEOUT*1000)) {
DBUGS.printf("%s:%d\n", host, port); //debug
return ("Connection error");
}
http->setTimeout(HTTP_TIMEOUT);
#ifndef ESP32
#warning HTTPS verification not enabled
if (client.verify(fingerprint, host)) {
if (!fingerprint || http->verify(fingerprint, host)) {
#endif
client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host +
"\r\n" + "Connection: close\r\n\r\n");
sprintf(request, "GET %s HTTP/1.1\r\nHost: %s\r\nConnection: close\r\n\r\n", url, host);
http->print(request);
// Handle wait for reply and timeout
unsigned long timeout = millis();
while (client.available() == 0) {
if (millis() - timeout > 5000) {
client.stop();
while (http->available() == 0) {
if (millis() - timeout > (HTTP_TIMEOUT*1000)) {
http->stop();
return ("Client Timeout");
}
#ifdef ENABLE_WDT
feedLoopWDT();
#endif
}
// Handle message receive
while (client.available()) {
String line = client.readStringUntil('\r');
while (http->available()) {
String line = http->readStringUntil('\r');
DBUGS.println(line); //debug
if (line.startsWith("HTTP/1.1 200 OK")) {
return ("ok");
Expand All @@ -73,22 +87,3 @@ get_https(const char *fingerprint, const char *host, String url,
#endif
return ("error " + String(host));
}

// -------------------------------------------------------------------
// HTTP GET Request
// url: N/A
// -------------------------------------------------------------------
String
get_http(const char *host, String url) {
http.begin(String("http://") + host + String(url));
int httpCode = http.GET();
if ((httpCode > 0) && (httpCode == HTTP_CODE_OK)) {
String payload = http.getString();
DBUGS.println(payload);
http.end();
return (payload);
} else {
http.end();
return ("server error: " + String(httpCode));
}
} // end http_get
19 changes: 3 additions & 16 deletions Software/EmonESP/src/http.h
Expand Up @@ -35,26 +35,13 @@

#include <Arduino.h>
#include <Print.h>
#include <WiFiClient.h> // http GET request
#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
// url: N/A
// -------------------------------------------------------------------
extern String get_https(const char* fingerprint, const char* host, String url, int httpsPort);

// -------------------------------------------------------------------
// HTTP GET Request
// HTTP or HTTPS GET Request
// url: N/A
// -------------------------------------------------------------------
extern String get_http(const char* host, String url);
extern String get_http(const char * host, const char * url, int port=80, const char * fingerprint=NULL);

#endif // _EMONESP_HTTP_H
20 changes: 10 additions & 10 deletions Software/EmonESP/src/input.cpp
Expand Up @@ -29,17 +29,17 @@
#include "emonesp.h"
#include "input.h"

String input_string = "";
String last_datastr = "";
char input_string[MAX_DATA_LEN] = "";
char last_datastr[MAX_DATA_LEN] = "";

boolean input_get(String& data)
boolean input_get(char * data)
{
boolean gotData = false;

// 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`
if (input_string.length() > 0) {
data = input_string;
input_string = "";
if (strlen(input_string) > 0) {
strcpy(data, input_string);
strcpy(input_string, "");
gotData = true;
}
#ifdef USE_SERIAL_INPUT
Expand All @@ -54,11 +54,11 @@ boolean input_get(String& data)
if (gotData)
{
// Get rid of any whitespace, newlines etc
data.trim();
//data.trim();

if (data.length() > 0) {
DBUGS.printf("Got '%s'\n", data.c_str());
last_datastr = data;
if (strlen(data) > 0) {
// DBUGS.printf("Got '%s'\n", data.c_str());
strcpy(last_datastr, data);
} else {
gotData = false;
}
Expand Down
6 changes: 3 additions & 3 deletions Software/EmonESP/src/input.h
Expand Up @@ -35,15 +35,15 @@
// Support for reading input
// -------------------------------------------------------------------

extern String last_datastr;
extern String input_string;
extern char last_datastr[MAX_DATA_LEN];
extern char input_string[MAX_DATA_LEN];

// -------------------------------------------------------------------
// Read input sent via the web_server or serial.
//
// data: if true is returned data will be updated with the new line of
// input
// -------------------------------------------------------------------
extern boolean input_get(String& data);
extern boolean input_get(char * data);

#endif // _EMONESP_INPUT_H

0 comments on commit fbee074

Please sign in to comment.