Skip to content

Instantly share code, notes, and snippets.

@benevpi
Created October 3, 2019 10:13
Show Gist options
  • Save benevpi/30162f994251c33b5c0d1afb1620f552 to your computer and use it in GitHub Desktop.
Save benevpi/30162f994251c33b5c0d1afb1620f552 to your computer and use it in GitHub Desktop.
weather station
#include <ArduinoJson.h>
#include <WiFi.h>
#include <FastLED.h>
#define NUM_BOARDS 1# define NUM_LEDS 64 * NUM_BOARDS
# define DATA_PIN 18# define CLOCK_PIN 5
CRGB matrix[NUM_LEDS];
CRGB rain_colour;
CRGB temp_colour;
CRGB blank_colour;
CRGB cloud_colour;
CRGB wind_colour;
// WiFi network name and password:
const char * networkName = "Redbrick";
const char * networkPswd = "100heads>1";
// Internet domain to request from:
const char * hostDomain = "api.openweathermap.org";
const int hostPort = 80;
const int BUTTON_PIN = 0;
const int LED_PIN = 5;
//String rain;
void setup() {
// Initilize hardware:
Serial.begin(115200);
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(LED_PIN, OUTPUT);
rain_colour[0] = 0;
rain_colour[1] = 0;
rain_colour[2] = 100;
blank_colour[0] = 0;
blank_colour[1] = 0;
blank_colour[2] = 0;
LEDS.addLeds < APA102, DATA_PIN, CLOCK_PIN, BGR > (matrix, NUM_LEDS);
//LEDS.setBrightness(32);
// Connect to the WiFi network (see function below loop)
connectToWiFi(networkName, networkPswd);
digitalWrite(LED_PIN, LOW); // LED off
Serial.print("heap: ");
Serial.println(ESP.getFreeHeap());
Serial.print("Press button 0 to connect to ");
Serial.println(hostDomain);
}
void loop() {
requestURL(hostDomain, hostPort); // Connect to server
delay(1000000);
}
void connectToWiFi(const char * ssid,
const char * pwd) {
int ledState = 0;
printLine();
Serial.println("Connecting to WiFi network: " + String(ssid));
WiFi.begin(ssid, pwd);
while (WiFi.status() != WL_CONNECTED) {
// Blink LED while we're connecting:
digitalWrite(LED_PIN, ledState);
ledState = (ledState + 1) % 2; // Flip ledState
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("WiFi connected!");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
void requestURL(const char * host, uint8_t port) {
// printLine();
Serial.println("Connecting to domain: " + String(host));
// Use WiFiClient class to create TCP connections
WiFiClient client;
if (!client.connect(host, port)) {
Serial.println("connection failed");
return;
}
Serial.println("Connected!");
printLine();
Serial.print("heap: ");
Serial.println(ESP.getFreeHeap());
// This will send the request to the server
client.print((String)
"GET /data/2.5/forecast?q=bristol&appid=fb19be8ac48cdff616fbc7f54ff30a19 HTTP/1.1\r\n" +
"Host: " + String(host) + "\r\n" +
"Connection: close\r\n\r\n");
unsigned long timeout = millis();
while (client.available() == 0) {
if (millis() - timeout > 5000) {
Serial.println(">>> Client Timeout !");
client.stop();
return;
}
}
Serial.print("heap: ");
Serial.println(ESP.getFreeHeap());
String line = "";
while (client.available()) {
Serial.print("heap: ");
Serial.println(ESP.getFreeHeap());
line = client.readStringUntil('\r');
if (line.indexOf('{') >= 0) {
Serial.println(line);
Serial.println("parsingValues");
//create a json buffer where to store the json data
Serial.print("heap before doc: ");
Serial.println(ESP.getFreeHeap());
DynamicJsonDocument json_doc(60000);
Serial.print("heap before deserialization: ");
Serial.println(ESP.getFreeHeap());
DeserializationError json_error = deserializeJson(json_doc, line);
Serial.print("heap: ");
Serial.println(ESP.getFreeHeap());
// json_error = jsonBuffer.parseObject(line);
if (json_error) {
Serial.println("parseing failed");
Serial.println(json_error.c_str());
//return;
} else {
// Note -- we only want 16 items from the list of forecasts.
for (int i = 0; i < 16; i++) {
String rain = json_doc["list"][i]["rain"]["3h"];
if (rain != "null") {
rain_colour[2] = min(max(2, int(rain.toInt() * 10)), 255);
if (i < 8) {
matrix[56 + i] = rain_colour;
} else {
matrix[24 + (i - 8)] = rain_colour;
}
} else {
if (i < 8) {
matrix[56 + i] = blank_colour;
} else {
matrix[24 + (i - 8)] = blank_colour;
}
}
}
FastLED.show();
//now let's get the temperature
for (int i = 0; i < 16; i++) {
String temp_str = json_doc["list"][i]["main"]["temp"];
//note -- temp in kelvin, let's convert to celcius
float temp = temp_str.toFloat() - 273.15;
//frost!
if (temp < 0) {
temp_colour[0] = 10;
temp_colour[1] = 10;
temp_colour[2] = 10;
} else {
temp_colour[2] = max(40 - (temp * float(2.5)), float(0.0));
temp_colour[0] = max((temp * 3) - 30, float(0.0));
}
if (i < 8) {
matrix[48 + i] = temp_colour;
} else {
matrix[16 + (i - 8)] = temp_colour;
}
}
//cloudiness -- percentage
for (int i = 0; i < 16; i++) {
String cloud_str = json_doc["list"][i]["clouds"]["all"];
int cloud = cloud_str.toInt();
cloud_colour[0] = 5;
cloud_colour[1] = 5;
cloud_colour[2] = cloud / 20;
if (i < 8) {
matrix[40 + i] = cloud_colour;
} else {
matrix[8 + (i - 8)] = cloud_colour;
}
}
///let's have wind as the last option
for (int i = 0; i < 16; i++) {
String wind_str = json_doc["list"][i]["wind"]["speed"];
int wind = wind_str.toInt();
wind_colour[0] = 0;
wind_colour[1] = min(wind, 50);
wind_colour[2] = 0;
if (i < 8) {
matrix[32 + i] = wind_colour;
} else {
matrix[i - 8] = wind_colour;
}
}
FastLED.show();
}
}
}
}
void printLine() {
Serial.println();
for (int i = 0; i < 30; i++)
Serial.print("-");
Serial.println();
}
void sun() {}
void cloud() {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment