Skip to content
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) #422

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ _If this project has any value for you, please consider [buying me a 🍺](https
<li>Needs just an ESP32, no need for extra hardware. ESP8266 is also supported.</li>
<li>Queries the Altherma for selected values at defined interval.</li>
<li>Converts and formalizes all values in a JSON message sent over MQTT.</li>
<li>Converts and formalizes all values in a JSON message sent over HTTPS.</li>
<li>Easily integrates with Home Assistant's MQTT auto-discovery.</li>
<li>Supports update OverTheAir</li>
<li>Log messages in Serial + MQTT + Screen (m5StickC)</li>
Expand Down Expand Up @@ -64,6 +65,7 @@ If you are using an **ESP8266** select the `nodemcuv2` environement.

3. Edit the file `src/setup.h` as follows:
- enter your wifi and mqtt settings
- if you want to send via HTTPS, update the HTTPS section lower down the file
- select your RX TX GPIO pins connected to the X10A port. *The ESP32 has 3 serial ports. The first one, Serial0 is reserved for ESP<-USB->PC communication and ESP Altherma uses the Serial0 for logging (as any other project would do). So if you open the serial monitor on your PC, you'll see some debug from ESPAltherma. ESP32 can map any GPIO to the serial ports. Do NOT use the main Serial0 GPIOs RX0/TX0.* * The ESP8266 only has 1.5 Serial ports so it uses a software based serial driver. You can choose any pins, but some will stop you from being able to use the console*

For ESP32 try to stick to the RX2/TX2 of your board (probably GPIO16/GPIO17). **For M5StickC or M5StickCPlus, 26 and 36 will automatically be used if you selected the corresponding environment**. For ESP8266 pins 4 & 5 (D2 & D1 on the NodeMCUv2) are known to work well.
Expand Down
52 changes: 52 additions & 0 deletions include/https.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#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

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");
M5.Lcd.print("[HTTPS] POST...\n");
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);
M5.Lcd.printf("[HTTPS] POST... code: %d\n", httpCode);
// 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);
M5.Lcd.println(payload);
}

}
else {
Serial.printf("[HTTPS] POST... failed, error: %s\n", https.errorToString(httpCode).c_str());
M5.Lcd.printf("[HTTPS] POST... failed, error: %s\n", https.errorToString(httpCode).c_str());
}

https.end();
}
}
6 changes: 6 additions & 0 deletions include/mqtt.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include <PubSubClient.h>
#include <EEPROM.h>
#include "restart.h"
#include "https.h"
#include <string.h>

#define MQTT_attr "espaltherma/ATTR"
#define MQTT_lwt "espaltherma/LWT"
Expand Down Expand Up @@ -46,6 +48,10 @@ void sendValues()
strcat(jsonbuff,"]");
#endif
client.publish(MQTT_attr, jsonbuff);
//M5.Lcd.println(jsonbuff); //Optional - prints the data to the screen if required
#ifdef HTTPS
posthttps(jsonbuff);
#endif
#ifdef JSONTABLE
strcpy(jsonbuff, "[{\0");
#else
Expand Down
51 changes: 51 additions & 0 deletions src/setup.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#ifndef SETUP_H //allow for additional .h files to include and compile
#define SETUP_H //allow for additional .h files to include and compile

//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**
Expand Down Expand Up @@ -130,3 +133,51 @@
#ifndef PROTOCOL
#define PROTOCOL 'I'
#endif

//Uncomment this to send HTTPS Messages
// #define HTTPS
// #define HTTP_SERVER "https://emoncms.org/input/post" //emoncms

// // only change the value between the " " leave the rest of the line untouched.
// #define APIKEY "YOUR API KEY" //emoncms
// #define NODE "YOUR NODE NAME" // node in emoncms

// // root certificate of emoncms.org - expires 2038. You can include your own HTTPS server certificate in this format
// // thanks to https://randomnerdtutorials.com/esp32-https-requests/ for the how-to
// #define rootCACertificate \
// "-----BEGIN CERTIFICATE-----\n" \
// "MIIF3jCCA8agAwIBAgIQAf1tMPyjylGoG7xkDjUDLTANBgkqhkiG9w0BAQwFADCB\n" \
// "iDELMAkGA1UEBhMCVVMxEzARBgNVBAgTCk5ldyBKZXJzZXkxFDASBgNVBAcTC0pl\n" \
// "cnNleSBDaXR5MR4wHAYDVQQKExVUaGUgVVNFUlRSVVNUIE5ldHdvcmsxLjAsBgNV\n" \
// "BAMTJVVTRVJUcnVzdCBSU0EgQ2VydGlmaWNhdGlvbiBBdXRob3JpdHkwHhcNMTAw\n" \
// "MjAxMDAwMDAwWhcNMzgwMTE4MjM1OTU5WjCBiDELMAkGA1UEBhMCVVMxEzARBgNV\n" \
// "BAgTCk5ldyBKZXJzZXkxFDASBgNVBAcTC0plcnNleSBDaXR5MR4wHAYDVQQKExVU\n" \
// "aGUgVVNFUlRSVVNUIE5ldHdvcmsxLjAsBgNVBAMTJVVTRVJUcnVzdCBSU0EgQ2Vy\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