Loading files
/**
BasicHTTPSClient.ino
Created on: 14.10.2018
*/
#include <Arduino.h>
#include <ArduinoJson.h>
#include <ArduinoJson.hpp>
#include <WiFi.h>
#include <WiFiMulti.h>
#include "certs.h"
#include "secret.h"
#include <HTTPClient.h>
#include <WiFiClientSecure.h>
// Not sure if WiFiClientSecure checks the validity date of the certificate.
// Setting clock just to be sure...
void setClock() {
configTime(0, 0, "pool.ntp.org");
Serial.print(F("Waiting for NTP time sync: "));
time_t nowSecs = time(nullptr);
while (nowSecs < 8 * 3600 * 2) {
delay(500);
Serial.print(F("."));
yield();
nowSecs = time(nullptr);
}
Serial.println();
struct tm timeinfo;
gmtime_r(&nowSecs, &timeinfo);
Serial.print(F("Current time: "));
Serial.print(asctime(&timeinfo));
}
WiFiMulti WiFiMulti;
void setup() {
Serial.begin(115200);
// Serial.setDebugOutput(true);
Serial.println();
Serial.println();
Serial.println();
WiFi.mode(WIFI_STA);
WiFiMulti.addAP(NETWORK_SSID, NETWORK_PASSWORD);
// wait for WiFi connection
Serial.print("Waiting for WiFi to connect...");
while ((WiFiMulti.run() != WL_CONNECTED)) {
Serial.print(".");
}
Serial.println(" connected");
setClock();
}
void loop() {
WiFiClientSecure* client = new WiFiClientSecure;
if (client) {
client->setCACert(cert_ISRG_Root_X1);
{
// Add a scoping block for HTTPClient https to make sure it is destroyed before WiFiClientSecure *client is
HTTPClient https;
// void addHeader(const String& name, const String& value, bool first = false, bool replace = true);
Serial.print("[HTTPS] begin...\n");
if (https.begin(*client, "https://noisemeter.webcomand.com/ws/put")) { // HTTPS
Serial.print("[HTTPS] POST...\n");
// start connection and send HTTP header
DynamicJsonDocument doc(2048);
doc["parent"] = "/Bases/nm1";
doc["data"]["type"] = "comand";
doc["data"]["version"] = "1.0";
doc["data"]["contents"][0]["Type"] = "Noise";
doc["data"]["contents"][0]["Min"] = 0;
doc["data"]["contents"][0]["Max"] = 99;
doc["data"]["contents"][0]["DeviceID"] = "nicknyetest2";
// Serialize JSON document
String json;
serializeJson(doc, json);
// void addHeader(const String& name, const String& value, bool first = false, bool replace = true);
https.addHeader("Authorization", String("Token ") + API_TOKEN);
https.addHeader("Content-Type", "application/json");
https.addHeader("Content-Length", String(json.length()));
https.addHeader("User-Agent", "ESP32");
int httpCode = https.POST(json);
// int POST(uint8_t * payload, size_t size);
// int POST(String payload);
// 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);
// file found at server
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
String payload = https.getString();
Serial.println(payload);
}
} else {
Serial.printf("[HTTPS] POST... failed, error: %s\n", https.errorToString(httpCode).c_str());
}
https.end();
} else {
Serial.printf("[HTTPS] Unable to connect\n");
}
// End extra scoping block
}
delete client;
} else {
Serial.println("Unable to create client");
}
Serial.println();
Serial.println("Waiting 1 min before the next round...");
delay(60000);
}b8134799cThis file is pinned to the selected release. Download the original to open it in your design tool.