Advertisement
Guest User

WeatherLamp

a guest
Jun 15th, 2019
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <ESP8266WiFi.h>
  2. #include <ESP8266mDNS.h>
  3. #include <WiFiUdp.h>
  4. #include <ArduinoOTA.h>
  5.  
  6. #include <ESP8266WiFi.h>
  7. #include <Adafruit_NeoPixel.h>
  8.  
  9. #define PIN D1
  10. Adafruit_NeoPixel strip = Adafruit_NeoPixel(14, PIN, NEO_GRBW + NEO_KHZ800);
  11.  
  12.  
  13.  
  14.  
  15. long startMillis;        // will store last time animation was updated
  16. long interval = 60000;          // interval at which to refresh (milliseconds)
  17.  
  18. // Server, file, port
  19. //https://api.openweathermap.org/data/2.5/weather?q=london,uk&appid=apiKey
  20. const String apiKey = "key";
  21. const String city = "London,uk";
  22. const char hostname[] = "api.openweathermap.org";
  23. const String url = "/data/2.5/weather?q=" + city + "&appid=" + apiKey;
  24. const int port = 80;
  25. int condition = 0; //0 error, 1 sunny, 2 cloudy, 3 rainy, 4 snowy, 5 thunder, 6 night
  26. int code; //these are what we read from the JSON
  27. int dt;
  28. int sunrise;
  29. int sunset;
  30.  
  31. // Timeout
  32. unsigned long timeout = 3000;  // ms
  33.  
  34. // WiFi Client
  35. WiFiClient client;
  36.  
  37.  
  38.  
  39. const char* ssid = "wifi_name";
  40. const char* password = "wifi_pass";
  41.  
  42. void setup() {
  43.   Serial.begin(115200);
  44.   Serial.println("Booting");
  45.   WiFi.mode(WIFI_STA);
  46.   WiFi.begin(ssid, password);
  47.   while (WiFi.waitForConnectResult() != WL_CONNECTED) {
  48.     Serial.println("Connection Failed! Rebooting...");
  49.     delay(5000);
  50.     ESP.restart();
  51.   }
  52.  
  53.  
  54.   ArduinoOTA.setHostname("CloudLight");
  55.  
  56.  
  57.   ArduinoOTA.onStart([]() {
  58.     String type;
  59.     if (ArduinoOTA.getCommand() == U_FLASH)
  60.       type = "sketch";
  61.     else // U_SPIFFS
  62.       type = "filesystem";
  63.  
  64.     // NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
  65.     Serial.println("Start updating " + type);
  66.   });
  67.   ArduinoOTA.onEnd([]() {
  68.     Serial.println("\nEnd");
  69.   });
  70.   ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
  71.     Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
  72.   });
  73.   ArduinoOTA.onError([](ota_error_t error) {
  74.     Serial.printf("Error[%u]: ", error);
  75.     if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
  76.     else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
  77.     else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
  78.     else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
  79.     else if (error == OTA_END_ERROR) Serial.println("End Failed");
  80.   });
  81.   ArduinoOTA.begin();
  82.   Serial.println("Ready");
  83.   Serial.print("IP address: ");
  84.   Serial.println(WiFi.localIP());
  85.  
  86.  
  87.  
  88.  
  89.   strip.begin();
  90.   strip.show(); // Initialize all pixels to 'off'
  91.  
  92.   colorWipe(strip.Color(20,   0,   0), 50); // Red
  93.   delay(1000);
  94.   colorWipe(strip.Color(  0,   0, 0), 10); // Blue
  95.   randomSeed(analogRead(0));
  96.  
  97.   // Show we are connected
  98.   Serial.println("Starting!");
  99. }
  100.  
  101. void loop() {
  102.   ArduinoOTA.handle();
  103.  
  104.   unsigned long timestamp;
  105.  
  106.   // Establish TCP connection
  107.   Serial.print("Connecting to ");
  108.   Serial.println(hostname);
  109.   if ( !client.connect(hostname, port) ) {
  110.     Serial.println("Connection failed");
  111.   }
  112.  
  113.   // Send GET request
  114.   String req = "GET " + url + " HTTP/1.1\r\n" +
  115.                "Host: " + hostname + "\r\n" +
  116.                "Connection: close\r\n" +
  117.                "\r\n";
  118.   client.print(req);
  119.  
  120.   // Wait for response from server
  121.   delay(500);
  122.   timestamp = millis();
  123.   while ( !client.available() && (millis() < timestamp + timeout) ) {
  124.     delay(1);
  125.   }
  126.  
  127.   // Parse the info (in order! we aren't really parsing it more scrolling through until we find something interesting)
  128.   if ( client.find("\"id\":") ) {
  129.     code = client.parseInt();
  130.     Serial.print("Condition code: ");
  131.     Serial.println(code);
  132.     client.find("\"dt\":");
  133.     dt = client.parseInt();
  134.     client.find("\"sunrise\":");
  135.     sunrise = client.parseInt();
  136.     if (client.find("\"sunset\":")) {
  137.       sunset = client.parseInt();
  138.     } else {
  139.       Serial.println("Couldn't read the time and sunrise info so forcing daytime");
  140.       dt = 5;
  141.       sunrise = 1;
  142.       sunset = 10;
  143.     }
  144.   } else {
  145.     code = 999;
  146.   }
  147.  
  148.   processCode();
  149.   Serial.print("Condition decided as ");
  150.   Serial.println(condition);
  151.   Serial.println("out of 0 error, 1 sunny, 2 cloudy, 3 rainy, 4 snowy, 5 thunder, 6 night");
  152.  
  153.   // Flush receive buffer
  154.   while ( client.available() ) {
  155.     client.find('}'); //just skipping through to the end of the response!
  156.   }
  157.  
  158.   // Close TCP connection
  159.   client.stop();
  160.   Serial.println("Connection closed");
  161.  
  162.   switch (condition) {
  163.     case 0:
  164.       colorSet(strip.Color(80, 0, 0)); // error red
  165.       startMillis = millis();
  166.       while (millis() - startMillis < interval/2) { //let's not wait the whole time!
  167.         delay(100);
  168.         ArduinoOTA.handle();
  169.  
  170.       }
  171.       break;
  172.  
  173.     case 1:
  174.       colorSet(strip.Color(250, 180, 0, 0)); // Sunny yellow
  175.       startMillis = millis();
  176.       while (millis() - startMillis < interval) {
  177.         delay(100);
  178.         ArduinoOTA.handle();
  179.  
  180.       }
  181.       break;
  182.  
  183.     case 2:
  184.       colorSet(strip.Color(80, 160, 120, 100)); // Cloudy
  185.       startMillis = millis();
  186.       while (millis() - startMillis < interval) {
  187.         delay(100);
  188.         ArduinoOTA.handle();
  189.  
  190.       }
  191.       break;
  192.  
  193.     case 3:
  194.       startMillis = millis();
  195.       while (millis() - startMillis < interval) {
  196.         rain();
  197.         ArduinoOTA.handle();
  198.  
  199.       }
  200.       break;
  201.  
  202.     case 4:
  203.       startMillis = millis();
  204.       while (millis() - startMillis < interval) {
  205.         snow();
  206.         ArduinoOTA.handle();
  207.  
  208.       }
  209.       break;
  210.  
  211.     case 5:
  212.       startMillis = millis();
  213.       while (millis() - startMillis < interval) {
  214.         thunder();
  215.         ArduinoOTA.handle();
  216.  
  217.       }
  218.       break;
  219.  
  220.     case 6:
  221.       colorSet(strip.Color(100, 40, 100)); // night
  222.       startMillis = millis();
  223.       while (millis() - startMillis < interval) {
  224.         delay(100);
  225.         ArduinoOTA.handle();
  226.  
  227.       }
  228.       break;
  229.     default:
  230.       colorSet(strip.Color(80, 0, 0)); // error red
  231.       startMillis = millis();
  232.       while (millis() - startMillis < interval) {
  233.         delay(100);
  234.         ArduinoOTA.handle();
  235.  
  236.       }
  237.       break;
  238.  
  239.   }
  240.  
  241. }
  242.  
  243.  
  244. void processCode() {
  245.   //0 error, 1 sunny, 2 cloudy, 3 rainy, 4 snowy, 5 thunder, 6 night
  246.   switch (code) {
  247.     case 200 ... 299:
  248.       condition = 5;//thunder;
  249.       break;
  250.     case 300 ... 399:
  251.       condition = 3;//drizzle;
  252.       break;
  253.     case 500 ... 599:
  254.       condition = 3;//rain;
  255.       break;
  256.     case 600 ... 699:
  257.       condition = 4;//snow;
  258.       break;
  259.     case 700 ... 799:
  260.       condition = 2;//atmospherics;
  261.       break;
  262.     case 800:
  263.       condition = 1;//clear;
  264.       if (sunrise <= dt && dt <= sunset) {
  265.         condition = 1;
  266.       } else {
  267.         condition = 6;
  268.       }
  269.       break;
  270.     case 801 ... 899:
  271.       condition = 2;//clouds;
  272.       break;
  273.     default:
  274.       condition = 0; //error
  275.       break;
  276.   }
  277. }
  278.  
  279.  
  280.  
  281. void rain() {
  282.   uint16_t offi = random(7, 13);
  283.   uint16_t offi2 = random(7, 13);
  284.   while (offi2 == offi) {
  285.     offi2 = random(7, 13);
  286.   }
  287.  
  288.   for (uint16_t i = 0; i < strip.numPixels(); i++) {
  289.     if (i == offi | i == offi2) {
  290.       strip.setPixelColor(i, strip.Color(000, 000, 00));
  291.  
  292.     } else {
  293.  
  294.       strip.setPixelColor(i, strip.Color(0, 150, 255));
  295.     }
  296.   }
  297.  
  298.   strip.show();
  299.   delay(130);
  300.  
  301.  
  302. }
  303.  
  304. void snow() {
  305.   uint16_t offi = random(7, 13);
  306.   uint16_t offi2 = random(7, 13);
  307.   while (offi2 == offi) {
  308.     offi2 = random(7, 13);
  309.   }
  310.  
  311.   for (uint16_t i = 0; i < strip.numPixels(); i++) {
  312.     if (i == offi | i == offi2) {
  313.       strip.setPixelColor(i, strip.Color(000, 000, 00));
  314.  
  315.     } else {
  316.  
  317.       strip.setPixelColor(i, strip.Color(70, 140, 105, 80));
  318.     }
  319.   }
  320.  
  321.   strip.show();
  322.   delay(130);
  323.  
  324.  
  325. }
  326.  
  327. void thunder() {
  328.   uint16_t offi = random(0, 14);
  329.  
  330.  
  331.   for (uint16_t i = 0; i < strip.numPixels(); i++) {
  332.     if (i == offi && random(20) < 1) {
  333.       strip.setPixelColor(i, strip.Color(255, 255, 255, 255));
  334.  
  335.     } else {
  336.  
  337.       strip.setPixelColor(i, strip.Color(50, 100, 75, 20));
  338.     }
  339.   }
  340.  
  341.   strip.show();
  342.   delay(50);
  343.  
  344.  
  345. }
  346.  
  347. // Fill the dots one after the other with a color
  348. void colorWipe(uint32_t c, uint8_t wait) {
  349.   for (uint16_t i = 0; i < strip.numPixels(); i++) {
  350.     strip.setPixelColor(i, c);
  351.     strip.show();
  352.     delay(wait);
  353.   }
  354. }
  355.  
  356. void colorSet(uint32_t c) {
  357.   for (uint16_t i = 0; i < strip.numPixels(); i++) {
  358.     strip.setPixelColor(i, c);
  359.   }
  360.   strip.show();
  361.  
  362. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement