-
-
Notifications
You must be signed in to change notification settings - Fork 123
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
Include support for HTTPS POST of data to HTTPS Server (e.g. emoncms.org for heatpumpmonitor.org) #2 #428
base: main
Are you sure you want to change the base?
Include support for HTTPS POST of data to HTTPS Server (e.g. emoncms.org for heatpumpmonitor.org) #2 #428
Changes from all commits
0f639f0
75bf5af
26cc16c
9c0360b
009c59b
3f3422b
5ddd215
d8ff873
e19971f
5d8b526
1cd4d81
1d2cea5
2ee13ba
d63d402
b36eceb
7a9f995
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
#include <HttpClient.h> | ||
#include <WiFiClientSecure.h> | ||
#include "setup.h" | ||
#ifdef ARDUINO_M5Stick_C_Plus2 | ||
#include <M5StickCPlus2.h> | ||
#elif ARDUINO_M5Stick_C_Plus | ||
#include <M5StickCPlus.h> | ||
#elif ARDUINO_M5Stick_C | ||
#include <M5StickC.h> | ||
#endif | ||
|
||
char jsonbuffhttps[MAX_MSG_SIZE] = "{\0"; | ||
|
||
|
||
void posthttps(char* httpsbuff) | ||
{ | ||
String httpRequestData = "node=" + String(NODE) + "&data=" + String(httpsbuff) + "&apikey=" + APIKEY; //Build the Emoncms data string | ||
|
||
static WiFiClientSecure client; | ||
if (!client) { | ||
client.setCACert(rootCACertificate); | ||
} | ||
|
||
// setup https | ||
HTTPClient https; | ||
|
||
if (https.begin(client, HTTP_SERVER)) { | ||
Serial.print("[HTTPS] POST...\n"); | ||
#ifdef ARDUINO_M5Stick_C_Plus2 | ||
M5.Lcd.print("[HTTPS] POST...\n"); | ||
#endif | ||
https.addHeader("Content-Type", "application/x-www-form-urlencoded"); | ||
|
||
// start connection and send HTTP header | ||
int httpCode = https.POST(httpRequestData); | ||
// httpCode will be negative on error | ||
if (httpCode > 0) { | ||
// HTTP header has been send and Server response header has been handled | ||
Serial.printf("[HTTPS] POST... code: %d\n", httpCode); | ||
#ifdef ARDUINO_M5Stick_C_Plus2 | ||
M5.Lcd.printf("[HTTPS] POST... code: %d\n", httpCode); | ||
#endif | ||
// file found at server | ||
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) { | ||
// print server response payload | ||
String payload = https.getString(); | ||
Serial.println(payload); | ||
#ifdef ARDUINO_M5Stick_C_Plus2 | ||
M5.Lcd.println(payload); | ||
#endif | ||
} | ||
|
||
} | ||
else { | ||
Serial.printf("[HTTPS] POST... failed, error: %s\n", https.errorToString(httpCode).c_str()); | ||
#ifdef ARDUINO_M5Stick_C_Plus2 | ||
M5.Lcd.printf("[HTTPS] POST... failed, error: %s\n", https.errorToString(httpCode).c_str()); | ||
#endif | ||
} | ||
|
||
https.end(); | ||
} | ||
} | ||
|
||
|
||
void sendValuesHTTPS() | ||
{ | ||
Serial.printf("Sending values via HTTPS.\n"); | ||
#ifdef ARDUINO_M5Stick_C_Plus2 | ||
//Add Power values | ||
// getBatteryVoltage returns battery voltage [mV] as an int16_t | ||
float batteryVoltage = (float) M5.Power.getBatteryVoltage() / 1000; // convert to V as a float | ||
snprintf(jsonbuffhttps + strlen(jsonbuffhttps),MAX_MSG_SIZE - strlen(jsonbuffhttps) , "\"%s\":\"%.3gV\",", "M5BatV", batteryVoltage); | ||
#elif ARDUINO_M5Stick_C | ||
//Add M5 APX values | ||
snprintf(jsonbuffhttps + strlen(jsonbuffhttps),MAX_MSG_SIZE - strlen(jsonbuffhttps) , "\"%s\":\"%.3gV\",\"%s\":\"%gmA\",", "M5VIN", M5.Axp.GetVinVoltage(),"M5AmpIn", M5.Axp.GetVinCurrent()); | ||
snprintf(jsonbuffhttps + strlen(jsonbuffhttps),MAX_MSG_SIZE - strlen(jsonbuffhttps) , "\"%s\":\"%.3gV\",\"%s\":\"%gmA\",", "M5BatV", M5.Axp.GetBatVoltage(),"M5BatCur", M5.Axp.GetBatCurrent()); | ||
snprintf(jsonbuffhttps + strlen(jsonbuffhttps),MAX_MSG_SIZE - strlen(jsonbuffhttps) , "\"%s\":\"%.3gmW\",", "M5BatPwr", M5.Axp.GetBatPower()); | ||
#endif | ||
snprintf(jsonbuffhttps + strlen(jsonbuffhttps),MAX_MSG_SIZE - strlen(jsonbuffhttps) , "\"%s\":\"%ddBm\",", "WifiRSSI", WiFi.RSSI()); | ||
snprintf(jsonbuffhttps + strlen(jsonbuffhttps),MAX_MSG_SIZE - strlen(jsonbuffhttps) , "\"%s\":\"%d\",", "FreeMem", ESP.getFreeHeap()); | ||
jsonbuffhttps[strlen(jsonbuffhttps) - 1] = '}'; | ||
|
||
posthttps(jsonbuffhttps); | ||
#ifdef ARDUINO_M5Stick_C_Plus2 | ||
M5.Lcd.println(jsonbuffhttps); //Optional - prints the data to the screen if required | ||
#endif | ||
|
||
// wipes the jsonbuffhttps back to {null | ||
strcpy(jsonbuffhttps, "{\0"); | ||
|
||
} | ||
|
||
|
||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Extra whitespace here too |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
#ifndef SETUP_H | ||
#define SETUP_H | ||
|
||
//Setup your credentials and mqtt info here: | ||
//only change the value between the " " leave the rest of the line untouched. | ||
#define WIFI_SSID "SSID"//**Your SSID here** | ||
|
@@ -10,6 +13,8 @@ | |
//#define WIFI_PRIMARY_DNS 8, 8, 8, 8 //A DNS address is needed, even if it's not used | ||
//#define WIFI_SECONDARY_DNS 8, 8, 4, 4 //A DNS address is needed, even if it's not used | ||
|
||
#define MQTT //We want to send MQTT messages to a MQTT Server (comment out if we don't) | ||
|
||
#define MQTT_SERVER "192.168.1.4"//**IP address here of your MQTT server** | ||
#define MQTT_USERNAME ""//leave empty if not set (bad!) | ||
#define MQTT_PASSWORD ""//leave empty if not set (bad!) | ||
|
@@ -130,3 +135,51 @@ | |
#ifndef PROTOCOL | ||
#define PROTOCOL 'I' | ||
#endif | ||
|
||
// HTTPS FUNCTIONS ------------------- | ||
// Uncomment the below 'define HTTPS' to send HTTPS Messages | ||
//#define HTTPS | ||
#if defined (HTTPS) | ||
#define HTTP_SERVER "https://emoncms.org/input/post" //emoncms | ||
#define APIKEY " " //emoncms | ||
#define NODE "emontx" // node in emoncms | ||
|
||
// root certificate of emoncms.org - expires 2038 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Obviously if they change who provides their cert chain... Their current certs are 3 months... What's the error handling like if the cert no longer validates? |
||
#define rootCACertificate \ | ||
"-----BEGIN CERTIFICATE-----\n" \ | ||
"MIIF3jCCA8agAwIBAgIQAf1tMPyjylGoG7xkDjUDLTANBgkqhkiG9w0BAQwFADCB\n" \ | ||
"iDELMAkGA1UEBhMCVVMxEzARBgNVBAgTCk5ldyBKZXJzZXkxFDASBgNVBAcTC0pl\n" \ | ||
"cnNleSBDaXR5MR4wHAYDVQQKExVUaGUgVVNFUlRSVVNUIE5ldHdvcmsxLjAsBgNV\n" \ | ||
"BAMTJVVTRVJUcnVzdCBSU0EgQ2VydGlmaWNhdGlvbiBBdXRob3JpdHkwHhcNMTAw\n" \ | ||
"MjAxMDAwMDAwWhcNMzgwMTE4MjM1OTU5WjCBiDELMAkGA1UEBhMCVVMxEzARBgNV\n" \ | ||
"BAgTCk5ldyBKZXJzZXkxFDASBgNVBAcTC0plcnNleSBDaXR5MR4wHAYDVQQKExVU\n" \ | ||
"aGUgVVNFUlRSVVNUIE5ldHdvcmsxLjAsBgNVBAMTJVVTRVJUcnVzdCBSU0EgQ2Vy\n" \ | ||
"dGlmaWNhdGlvbiBBdXRob3JpdHkwggIiMA0GCSqGSIb3DQEBAQUAA4ICDwAwggIK\n" \ | ||
"AoICAQCAEmUXNg7D2wiz0KxXDXbtzSfTTK1Qg2Hi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||
"ATiUFJVDBwn7YKnuHKsSjKCaXqeYalltiz8I+8jRRa8YFWSQEg9zKC7F4iRO/Fjs\n" \ | ||
"8PRF/iKz6y+O0tlFYQXBl2+odnKPi4w2r78NBc5xjeambx9spnFixdjQg3IM8WcR\n" \ | ||
"iQycE0xyNN+81XHfqnHd4blsjDwSXWXavVcStkNr/+XeTWYRUc+ZruwXtuhxkYze\n" \ | ||
"Sf7dNXGiFSeUHM9h4ya7b6NnJSFd5t0dCy5oGzuCr+yDZ4XUmFF0sbmZgIn/f3gZ\n" \ | ||
"XHlKYC6SQK5MNyosycdiyA5d9zZbyuAlJQG03RoHnHcAP9Dc1ew91Pq7P8yF1m9/\n" \ | ||
"qS3fuQL39ZeatTXaw2ewh0qpKJ4jjv9cJ2vhsE/zB+4ALtRZh8tSQZXq9EfX7mRB\n" \ | ||
"VXyNWQKV3WKdwrnuWih0hKWbt5DHDAff9Yk2dDLWKMGwsAvgnEzDHNb842m1R0aB\n" \ | ||
"L6KCq9NjRHDEjf8tM7qtj3u1cIiuPhnPQCjY/MiQu12ZIvVS5ljFH4gxQ+6IHdfG\n" \ | ||
"jjxDah2nGN59PRbxYvnKkKj9\n" \ | ||
"-----END CERTIFICATE-----" | ||
#endif //HTTPS | ||
|
||
#endif //SETUP_H |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like this line and the one above it should be indented a couple of spaces