Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <ESP8266mDNS.h>
- #include <WiFiUdp.h>
- #include <ArduinoOTA.h>
- #include <ESP8266WiFi.h>
- #include <Adafruit_NeoPixel.h>
- #define PIN D1
- Adafruit_NeoPixel strip = Adafruit_NeoPixel(14, PIN, NEO_GRBW + NEO_KHZ800);
- long startMillis; // will store last time animation was updated
- long interval = 60000; // interval at which to refresh (milliseconds)
- // Server, file, port
- //https://api.openweathermap.org/data/2.5/weather?q=london,uk&appid=apiKey
- const String apiKey = "key";
- const String city = "London,uk";
- const char hostname[] = "api.openweathermap.org";
- const String url = "/data/2.5/weather?q=" + city + "&appid=" + apiKey;
- const int port = 80;
- int condition = 0; //0 error, 1 sunny, 2 cloudy, 3 rainy, 4 snowy, 5 thunder, 6 night
- int code; //these are what we read from the JSON
- int dt;
- int sunrise;
- int sunset;
- // Timeout
- unsigned long timeout = 3000; // ms
- // WiFi Client
- WiFiClient client;
- const char* ssid = "wifi_name";
- const char* password = "wifi_pass";
- void setup() {
- Serial.begin(115200);
- Serial.println("Booting");
- WiFi.mode(WIFI_STA);
- WiFi.begin(ssid, password);
- while (WiFi.waitForConnectResult() != WL_CONNECTED) {
- Serial.println("Connection Failed! Rebooting...");
- delay(5000);
- ESP.restart();
- }
- ArduinoOTA.setHostname("CloudLight");
- ArduinoOTA.onStart([]() {
- String type;
- if (ArduinoOTA.getCommand() == U_FLASH)
- type = "sketch";
- else // U_SPIFFS
- type = "filesystem";
- // NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
- Serial.println("Start updating " + type);
- });
- ArduinoOTA.onEnd([]() {
- Serial.println("\nEnd");
- });
- ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
- Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
- });
- ArduinoOTA.onError([](ota_error_t error) {
- Serial.printf("Error[%u]: ", error);
- if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
- else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
- else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
- else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
- else if (error == OTA_END_ERROR) Serial.println("End Failed");
- });
- ArduinoOTA.begin();
- Serial.println("Ready");
- Serial.print("IP address: ");
- Serial.println(WiFi.localIP());
- strip.begin();
- strip.show(); // Initialize all pixels to 'off'
- colorWipe(strip.Color(20, 0, 0), 50); // Red
- delay(1000);
- colorWipe(strip.Color( 0, 0, 0), 10); // Blue
- randomSeed(analogRead(0));
- // Show we are connected
- Serial.println("Starting!");
- }
- void loop() {
- ArduinoOTA.handle();
- unsigned long timestamp;
- // Establish TCP connection
- Serial.print("Connecting to ");
- Serial.println(hostname);
- if ( !client.connect(hostname, port) ) {
- Serial.println("Connection failed");
- }
- // Send GET request
- String req = "GET " + url + " HTTP/1.1\r\n" +
- "Host: " + hostname + "\r\n" +
- "Connection: close\r\n" +
- "\r\n";
- client.print(req);
- // Wait for response from server
- delay(500);
- timestamp = millis();
- while ( !client.available() && (millis() < timestamp + timeout) ) {
- delay(1);
- }
- // Parse the info (in order! we aren't really parsing it more scrolling through until we find something interesting)
- if ( client.find("\"id\":") ) {
- code = client.parseInt();
- Serial.print("Condition code: ");
- Serial.println(code);
- client.find("\"dt\":");
- dt = client.parseInt();
- client.find("\"sunrise\":");
- sunrise = client.parseInt();
- if (client.find("\"sunset\":")) {
- sunset = client.parseInt();
- } else {
- Serial.println("Couldn't read the time and sunrise info so forcing daytime");
- dt = 5;
- sunrise = 1;
- sunset = 10;
- }
- } else {
- code = 999;
- }
- processCode();
- Serial.print("Condition decided as ");
- Serial.println(condition);
- Serial.println("out of 0 error, 1 sunny, 2 cloudy, 3 rainy, 4 snowy, 5 thunder, 6 night");
- // Flush receive buffer
- while ( client.available() ) {
- client.find('}'); //just skipping through to the end of the response!
- }
- // Close TCP connection
- client.stop();
- Serial.println("Connection closed");
- switch (condition) {
- case 0:
- colorSet(strip.Color(80, 0, 0)); // error red
- startMillis = millis();
- while (millis() - startMillis < interval/2) { //let's not wait the whole time!
- delay(100);
- ArduinoOTA.handle();
- }
- break;
- case 1:
- colorSet(strip.Color(250, 180, 0, 0)); // Sunny yellow
- startMillis = millis();
- while (millis() - startMillis < interval) {
- delay(100);
- ArduinoOTA.handle();
- }
- break;
- case 2:
- colorSet(strip.Color(80, 160, 120, 100)); // Cloudy
- startMillis = millis();
- while (millis() - startMillis < interval) {
- delay(100);
- ArduinoOTA.handle();
- }
- break;
- case 3:
- startMillis = millis();
- while (millis() - startMillis < interval) {
- rain();
- ArduinoOTA.handle();
- }
- break;
- case 4:
- startMillis = millis();
- while (millis() - startMillis < interval) {
- snow();
- ArduinoOTA.handle();
- }
- break;
- case 5:
- startMillis = millis();
- while (millis() - startMillis < interval) {
- thunder();
- ArduinoOTA.handle();
- }
- break;
- case 6:
- colorSet(strip.Color(100, 40, 100)); // night
- startMillis = millis();
- while (millis() - startMillis < interval) {
- delay(100);
- ArduinoOTA.handle();
- }
- break;
- default:
- colorSet(strip.Color(80, 0, 0)); // error red
- startMillis = millis();
- while (millis() - startMillis < interval) {
- delay(100);
- ArduinoOTA.handle();
- }
- break;
- }
- }
- void processCode() {
- //0 error, 1 sunny, 2 cloudy, 3 rainy, 4 snowy, 5 thunder, 6 night
- switch (code) {
- case 200 ... 299:
- condition = 5;//thunder;
- break;
- case 300 ... 399:
- condition = 3;//drizzle;
- break;
- case 500 ... 599:
- condition = 3;//rain;
- break;
- case 600 ... 699:
- condition = 4;//snow;
- break;
- case 700 ... 799:
- condition = 2;//atmospherics;
- break;
- case 800:
- condition = 1;//clear;
- if (sunrise <= dt && dt <= sunset) {
- condition = 1;
- } else {
- condition = 6;
- }
- break;
- case 801 ... 899:
- condition = 2;//clouds;
- break;
- default:
- condition = 0; //error
- break;
- }
- }
- void rain() {
- uint16_t offi = random(7, 13);
- uint16_t offi2 = random(7, 13);
- while (offi2 == offi) {
- offi2 = random(7, 13);
- }
- for (uint16_t i = 0; i < strip.numPixels(); i++) {
- if (i == offi | i == offi2) {
- strip.setPixelColor(i, strip.Color(000, 000, 00));
- } else {
- strip.setPixelColor(i, strip.Color(0, 150, 255));
- }
- }
- strip.show();
- delay(130);
- }
- void snow() {
- uint16_t offi = random(7, 13);
- uint16_t offi2 = random(7, 13);
- while (offi2 == offi) {
- offi2 = random(7, 13);
- }
- for (uint16_t i = 0; i < strip.numPixels(); i++) {
- if (i == offi | i == offi2) {
- strip.setPixelColor(i, strip.Color(000, 000, 00));
- } else {
- strip.setPixelColor(i, strip.Color(70, 140, 105, 80));
- }
- }
- strip.show();
- delay(130);
- }
- void thunder() {
- uint16_t offi = random(0, 14);
- for (uint16_t i = 0; i < strip.numPixels(); i++) {
- if (i == offi && random(20) < 1) {
- strip.setPixelColor(i, strip.Color(255, 255, 255, 255));
- } else {
- strip.setPixelColor(i, strip.Color(50, 100, 75, 20));
- }
- }
- strip.show();
- delay(50);
- }
- // Fill the dots one after the other with a color
- void colorWipe(uint32_t c, uint8_t wait) {
- for (uint16_t i = 0; i < strip.numPixels(); i++) {
- strip.setPixelColor(i, c);
- strip.show();
- delay(wait);
- }
- }
- void colorSet(uint32_t c) {
- for (uint16_t i = 0; i < strip.numPixels(); i++) {
- strip.setPixelColor(i, c);
- }
- strip.show();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement