// Aquarium Weather
// vgmlr
// NodeMCU Code
// ESP8266 Weather Credit:
// https://github.com/ThingPulse/esp8266-weather-station
// Weather
#include "ESP8266WiFi.h"
#include "JsonListener.h"
#include "OpenWeatherMapCurrent.h"
#include
#include "SoftwareSerial.h"
// Serial to Arduino
SoftwareSerial com(D6, D5); // Rx, Tx
// Open Weather API
OpenWeatherMapCurrent client;
// API
String OPEN_WEATHER_MAP_APP_ID = "api number";
// Weather Address
String OPEN_WEATHER_MAP_LOCATION_ID = "location id";
// Language
String OPEN_WEATHER_MAP_LANGUAGE = "en";
// Metric?
boolean IS_METRIC = true;
// WIFI
const char* ESP_HOST_NAME = "esp-" + ESP.getFlashChipId();
const char* WIFI_SSID = "network name";
const char* WIFI_PASSWORD = "network password";
WiFiClient wifiClient;
void connectWifi() {
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.println(WIFI_SSID);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Connected");
Serial.println(WiFi.localIP());
}
// Weather Values
int emit; // current time --> unix epoch
int sunr; // sunrise
int suns; // sunset
int clou; // cloud percent
int wind; // wind speed m/s
String main; // conditions
// Values to Send
int light;
int cloud;
int event;
int wind_speed;
String sequence;
// reset
int mulligan = D7;
void setup() {
// for testing
delay(5000);
digitalWrite(mulligan, HIGH);
delay(500);
pinMode(mulligan, OUTPUT);
Serial.begin(115200);
while (!Serial) {
delay(1);
}
delay(500);
// Arduino
com.begin(9600);
connectWifi();
}
void loop() {
local_weather();
delay(1000);
time_position();
cloud_position();
wind_position();
event_position();
aggregate();
delay(1000);
transmit();
// Transmit Serial Every 30 Seconds
// For 1 Hour Then Reset
for (int ditto = 0; ditto < 120; ditto++) {
transmit();
delay(30000);
}
// reset
// bcz -> NodeMCU n' such
digitalWrite(mulligan, LOW);
delay(1000);
}
void local_weather() {
// Get the weather report
OpenWeatherMapCurrentData data;
client.setLanguage(OPEN_
WEATHER_MAP_LANGUAGE);
client.setMetric(IS_METRIC);
client.updateCurrentById(&data, OPEN_WEATHER_MAP_APP_ID, OPEN_WEATHER_MAP_LOCATION_ID);
// Convert Data to Values
emit = data.observationTime;
sunr = data.sunrise;
suns = data.sunset;
main = String(data.main);
wind = data.windSpeed;
clou = data.clouds;
// Print Data
Serial.println(emit);
Serial.println(sunr);
Serial.println(suns);
Serial.println(main);
Serial.println(wind);
Serial.println(clou);
Serial.println();
}
void time_position() {
// before sunrise
if (emit < sunr) {
light = 0;
// after sunset
} else if (emit > suns) {
light = 0;
// after sunrise before sunset
} else if (emit > sunr && emit < suns) {
// maths ugh
int total_dif = suns - sunr;
int since = emit - sunr;
float percent = (float)(since * 100) / total_dif;
percent = round(percent);
light = percent;
} else {
// error allowance
light = 0;
}
Serial.println(light);
}
void cloud_position() {
cloud = clou;
Serial.println(cloud);
}
void wind_position() {
wind_speed = round(wind);
Serial.println(wind_speed);
}
void event_position() {
// OpenWeather Conditions --> https://openweathermap.org/weather-conditions
// Selecting the [main] following: 1 Thunderstorm, 2 Drizzle, 3 Rain, 4 Snow, 5 Fog, 6 Clouds
if (main.indexOf("Thunderstorm") >= 0) {
event = 1;
} else if (main.indexOf("Drizzle") >= 0) {
event = 2;
} else if (main.indexOf("Rain") >= 0) {
event = 3;
} else if (main.indexOf("Snow") >= 0) {
event = 4;
} else if (main.indexOf("Fog") >= 0) {
event = 5;
} else if (main.indexOf("Clouds") >= 0) {
event = 6;
} else {
event = 7;
}
Serial.println(event);
}
void aggregate() {
// Combine values to one string to send
// Done to ease serial limitations
// Example: 0800500052 [10]
// 080 = 80% of day
// 050 = 50% clouds
// 005 = 5 mph wind
// 2 = Drizzle (see event_position)
String light_str = String(light);
if (light < 100 && light > 9) {
light_str = String("0" + light_str);
} else if (light < 10) {
light_str = String("00" + light_str);
} else {
light_str = String(100);
}
String cloud_str = String(cloud);
if (cloud < 100 && cloud > 9) {
cloud_str = String("0" + cloud_str);
} else if (cloud < 10) {
cloud_str = String("00" + cloud_str);
} else {
cloud_str = String(100);
}
String wind_str = String(wind_speed);
if (wind_speed < 100 && wind_speed > 9) {
wind_str = String("0" + wind_str);
} else if (wind_speed < 10) {
wind_str = String("00" + wind_str);
} else {
wind_str = String(100);
}
String event_str = String(event);
sequence = String(light_str + cloud_str + wind_str + event_str);
Serial.println(sequence);
Serial.println("");
}
void transmit() {
com.println(sequence);
}