Add code to get and parse solar event data to syncronise lights with Inle Lake solar activity

This commit is contained in:
Robert Marshall 2018-09-19 22:16:24 +01:00
parent 948962b8a4
commit 590ecc6de9

View file

@ -7,6 +7,9 @@
#include <OneWire.h>
#include <DallasTemperature.h>
#include <FastLED.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>
#include <time.h>
#define MQTT_SERVER "192.168.1.3"
#define SSID "GCHQ Surveillance Van"
@ -23,6 +26,11 @@
#define VOLTAGE_OFFSET 0.03
#define SOLAR_EVENT_URL "http://api.sunrise-sunset.org/json?lat=20.548103&lng=96.916835&formatted=0"
#define TIMEZONE_OFFSET 23400 // 6.5 hours in seconds
//#define TIME_SHIFT 5400 // 1.5 hours
#define NTP_POOL "uk.pool.ntp.org"
void mqttCallback(char *topic, byte *payload, unsigned int length);
Adafruit_ADS1115 ads;
@ -37,6 +45,7 @@ static const uint8_t sunriseLength = 30;
static const uint8_t heatIndexMax = 240;
static const float lightChangeInterval = ((float)(sunriseLength * 60) / heatIndexMax) * 1000;
static uint8_t heatIndex = 0;
int _lightsOnStartTime, _lightsOnEndTime, _lightsOffStartTime, _lightsOffEndTime;
float pHStep = (PH_7_VOLTAGE - PH_4_VOLTAGE) / 3;
@ -71,6 +80,54 @@ void reconnect() {
waitForMQTT();
}
String getSolarEventJson(){
waitForWiFi();
HTTPClient http;
http.begin(SOLAR_EVENT_URL);
int httpCode = http.GET();
String result = "";
if (httpCode > 0)
result = http.getString();
http.end();
return result;
}
int getEpoch(const char *str) {
struct tm time;
int year, month;
sscanf(str, "%d-%d-%dT%d:%d:%d+00:00", &year, &month, &time.tm_mday, &time.tm_hour, &time.tm_min, &time.tm_sec);
time.tm_year = year - 1900;
time.tm_mon = month - 1;
return mktime(&time);
}
int adjustForTimezone(int epoch){
return epoch + TIMEZONE_OFFSET;
}
void parseEvents(String json){
StaticJsonBuffer<1024> jsonBuffer;
JsonObject &root = jsonBuffer.parseObject(json);
if (!root.success()) {
Serial.println("Could not parse json");
return;
}
const char *astronomicalTwilightBegin = root["results"]["astronomical_twilight_begin"];
const char *sunrise = root["results"]["sunrise"];
const char *sunset = root["results"]["sunset"];
const char *astronomicalTwilightEnd = root["results"]["astronomical_twilight_end"];
_lightsOnStartTime = adjustForTimezone(getEpoch(astronomicalTwilightBegin));
_lightsOnEndTime = adjustForTimezone(getEpoch(sunrise));
_lightsOffStartTime = adjustForTimezone(getEpoch(sunset));
_lightsOffEndTime = adjustForTimezone(getEpoch(astronomicalTwilightEnd));
Serial.println(_lightsOffEndTime);
}
void setup() {
Serial.begin(115200);
@ -83,6 +140,12 @@ void setup() {
WiFi.begin(SSID, PASSWORD);
reconnect();
delay(2000);
configTime(0, 0, NTP_POOL);
String json = getSolarEventJson();
parseEvents(json);
FastLED.addLeds<WS2811, LIGHT_PIN, GRB>(leds, LED_COUNT).setCorrection(TypicalSMD5050);//.setTemperature(Tungsten40W);
Serial.print("Light interval: ");