Pith - nodemcu_egg_status
nodemcu_egg_status/nodemcu_egg_status_ssl.ino [2.3 kb]
Modified: 23:14:08 89 026 (16 Jun 026)
39 Days Ago
// remote egg status (ssl)
// pkvi

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <WiFiClientSecureBearSSL.h>

// set local
const char* ssid = "ssid";
const char* password = "pass";
// set get url
// text shd be 000000000 format (rgb)
const char* dest = "https://domain.com/dump/egg.txt";

WiFiClient wifiClient;

#include "FastLED.h"
#define NLEDS 24
#define LTYPE WS2812B
#define CORDER GRB
CRGB leds[NLEDS];
const int neo_pin = D6;

const long hours = 12;
const long hold = (hours * 60 * 60 * 1000);

int txtvalue;
String eggvalue = "000000150";

void n_one (int r, int g, int b) {
    leds[0] = CRGB (r,g,b);
    FastLED.show();
}

void n_fade (int r, int g, int b) {
  for (int v = 0; v < NLEDS; v++) {
    leds[v] = CRGB (r,g,b);
    FastLED.show();
    delay(50);
  }
}

void n_show (int r, int g, int b) {
  for (int v = 0; v < NLEDS; v++) {
    leds[v] = CRGB (r,g,b);
    FastLED.show();
  }
}

void setup() {
  Serial.begin(115200);
  FastLED.addLeds<LTYPE, neo_pin, CORDER>(leds, NLEDS).setCorrection(TypicalLEDStrip);
  n_show(0, 0, 0); // clear
  // load notification
  n_fade(255, 0, 255);
  delay(10);
  n_fade(0, 0, 0);
  delay(10);
  n_show(0, 0, 0);
}

void loop() {
  // run get
  get_status();
  // parse and convert to int
  int red = eggvalue.substring(0, 3).toInt();
  int green = eggvalue.substring(3, 6).toInt();
  int blue = eggvalue.substring(6).toInt();
  // send to fastled
  n_fade(red, green, blue);
  n_show(red, green, blue);
  // wait for next get
  delay(hold);
}

void get_status () {

  WiFi.mode(WIFI_STA);
  delay(100);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    n_one(255, 0, 0);
    delay(500);
    n_one(0, 0, 0);
    delay(500);
  }

  if (WiFi.status() == WL_CONNECTED) {
    n_one(0, 255, 0);

    std::unique_ptr<BearSSL::WiFiClientSecure> secureClient(new BearSSL::WiFiClientSecure);
    secureClient->setInsecure();
    HTTPClient http;

    if (http.begin(*secureClient, dest)) {
      http.setFollowRedirects(HTTPC_FORCE_FOLLOW_REDIRECTS);
      txtvalue = http.GET();

    if (txtvalue == HTTP_CODE_OK) {
      n_one(0, 0, 0);
      n_one(0, 0, 255);
      eggvalue = http.getString();
      eggvalue.trim();
    } else {
      n_one(0, 0, 0);
      n_one(255, 0, 0);  
      delay(500);
    }

    http.end();
    }
  }

  delay(1000);
  WiFi.mode(WIFI_OFF);
}
Updates
Wedge - Android 126.026
Wedge - Linux 124.026
Shim - Android 117.026
Miter - 114.026
Kerf - Android 112.026
Dev
TVShow (227) 'CSA'
TVShow (228) 'APT'
TVProgram (83) 'BXT'
Miter Update(s)
Peen (Messaging)

Menu
Calendar
Project Tin (024/029)
Miter
RSS Feed
User Avatar
@vgmlr
=SUM(parts)