Skip to content

Instantly share code, notes, and snippets.

@NiteBlock
Last active July 23, 2019 08:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NiteBlock/f165c13037b824c4745f75125a0dfbe4 to your computer and use it in GitHub Desktop.
Save NiteBlock/f165c13037b824c4745f75125a0dfbe4 to your computer and use it in GitHub Desktop.
Arduino code that lets you accsess heroku site
#include <Arduino.h>
#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266HTTPClient.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
#include <WiFiClientSecureBearSSL.h>
// Fingerprint expires soon, needs to be updated
const uint8_t fingerprint[20] = {0x08, 0x3b, 0x71, 0x72, 0x02, 0x43, 0x6E, 0xCA, 0xED, 0x42, 0x86, 0x93, 0xBA, 0x7E, 0xDF, 0x81, 0xC4, 0xBC, 0x62, 0x30};
#define DHTPIN 2
#define DHTTYPE DHT11
DHT_Unified dht(DHTPIN, DHTTYPE);
ESP8266WiFiMulti WiFiMulti;
String idList[4] = {"5d3050c453c0120a78d6282a", "5d304e7653c0120a78d62824", "5d3051bf53c0120a78d62833", "5d31ab50d1590f38248ab204"};
void setup()
{
dht.begin();
randomSeed(123);
Serial.begin(115200);
// Serial.setDebugOutput(true);
Serial.println();
Serial.println();
Serial.println();
for (uint8_t t = 4; t > 0; t--)
{
Serial.printf("[SETUP] WAIT %d...\n", t);
Serial.flush();
delay(1000);
}
WiFi.mode(WIFI_STA);
WiFiMulti.addAP("Be_Curious", "pulpoparacomer");
}
void loop()
{
// wait for WiFi connection
if ((WiFiMulti.run() == WL_CONNECTED))
{
std::unique_ptr<BearSSL::WiFiClientSecure>client(new BearSSL::WiFiClientSecure);
client->setFingerprint(fingerprint);
HTTPClient https;
Serial.print("[HTTPS] begin...\n");
if (https.begin(*client, "https://platform-device.herokuapp.com/save-data"))
{ // HTTPS
Serial.print("[HTTPS] POST...\n");
https.addHeader("Content-Type", "application/json");
sensors_event_t event;
dht.temperature().getEvent(&event);
int number = event.temperature;
int httpCode = https.POST("{\"deviceId\" : \""+String(idList[0])+"\", \"value\" : " + String(number) + "}");
if (httpCode > 0)
{
Serial.printf("[HTTPS] GET... code: %d\n", httpCode);
// file found at server
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY)
{
StaticJsonDocument<808> doc;
String payload = https.getString();
Serial.println(payload);
}
}
else
{
Serial.printf("[HTTPS] GET... failed, error: %s\n", https.errorToString(httpCode).c_str());
}
https.end();
}
else
{
Serial.printf("[HTTPS] Unable to connect\n");
}
}
Serial.println("Wait 1s before next round...");
delay(900);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment