Advertisement
peregrin5

The_Weather_Men_Weather_Station_P2-11-8-18

Nov 8th, 2018
361
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 11.59 KB | None | 0 0
  1. #include <Adafruit_NeoPixel.h>
  2. #include <Adafruit_GFX.h>    // Core graphics library
  3. #include <Adafruit_ST7735.h> // Hardware-specific library for ST7735
  4. #include <SPI.h>
  5. #include <Wire.h>
  6. #include <AdafruitIO.h>
  7. #include <Adafruit_MQTT.h>
  8. #include <LiquidCrystal.h>
  9. #include "config.h"
  10.  
  11. #define PIN 5
  12. #define NUM_LEDS 24
  13. #define LED_PER_SEG 6
  14. #define CYCLE_INT (1 * 1000 * 60UL)  // Intervals of 2 minute
  15.  
  16. #define TFT_CS     21
  17. #define TFT_RST    22
  18. #define TFT_DC     19
  19. #define TFT_SCLK   23
  20. #define TFT_MOSI   18
  21.  
  22.  
  23. Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RST);
  24.  
  25.  
  26. // LCD Initialize
  27. const int rs = 13, en = 12, d4 = 14, d5 = 27, d6 = 26, d7 = 25;
  28. LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
  29.  
  30. AdafruitIO_Feed *hightemp = io.feed("hightemp"); // set up the 'hightemp' feed
  31. AdafruitIO_Feed *precipitation = io.feed("precipitation"); // set up the 'precipitation' feed
  32. AdafruitIO_Feed *hourlytime = io.feed("time"); // set up the 'hourlytime' feed
  33.  
  34. Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRBW + NEO_KHZ800);
  35.  
  36. int weatherState;
  37. uint16_t pixStart = 0;
  38. unsigned long fadeint = 0;
  39. int r = 0, g = 0, b = 0, rbit = 0;
  40. boolean reversefade = 0;
  41. static unsigned long patternInterval = 0;  // initialize such that a reading is due the first time through loop()
  42. int minutes = 0;
  43. int hourlyInt = 0;
  44. unsigned long oneMinuteCounter = 0;
  45. int brightness = 200;
  46.  
  47.  
  48. void setup() {
  49.   Serial.begin(115200);
  50.   strip.setBrightness(brightness);
  51.   strip.begin();
  52.  
  53.   strip.show();
  54.   strip.clear();  // Initialize all pixels to 'off'
  55.  
  56.   Serial.print("Connecting to Adafruit IO");
  57.   io.connect();
  58.   lcd.begin(16, 2);
  59.   lcd.setCursor(0, 0);
  60.   lcd.print("Connecting...");
  61.  
  62. // Starting TFT
  63. tft.initR(INITR_BLACKTAB);
  64. tft.fillScreen(ST77XX_BLACK);
  65. screenSetup();
  66.  
  67.  
  68.   weatherState = 4;
  69.  
  70.   hightemp->onMessage(handleTemp);
  71.   precipitation->onMessage(handleCondition);
  72.   hourlytime->onMessage(handleTime);
  73.  
  74.   // wait for a connection
  75.   while(io.status() < AIO_CONNECTED) {
  76.     Serial.print(".");
  77.     colorfade();
  78.     delay(500);
  79.   }
  80.  
  81.   colorWipe(strip.Color(255,255,255,255),100);
  82.   colorWipe(strip.Color(0,0,255,0),100);
  83.   Serial.println();
  84.   Serial.println(io.statusText());
  85.  
  86.   lcd.begin(16, 2);
  87.   lcd.setCursor(0,0);
  88.   lcd.print("Getting Temp...");
  89.   lcd.setCursor(0, 1);
  90.   lcd.print("Getting Cond...");
  91.   delay(3000);
  92.  
  93.   fadeint = millis() + 100; // initialize fading interval
  94.   oneMinuteCounter = millis() + 60 * 1000;
  95.   patternInterval = millis() + CYCLE_INT;
  96.  
  97. }
  98.  
  99. void loop() {
  100.  
  101. // Run IO Program
  102.   io.run();
  103.  
  104. if (millis() > oneMinuteCounter) {
  105.   ++minutes;
  106.   Serial.print(hourlyInt);
  107.   Serial.print(':');
  108.   Serial.print(minutes);
  109.   Serial.print('\n');
  110.   drawClock();
  111.   oneMinuteCounter = millis() + (60 * 1000);
  112. }
  113.  
  114. if ((millis() + 11) > patternInterval) {
  115.       whiteOverRainbow(1,75,5,3);
  116.       Serial.println("pattern");
  117.       strip.clear();
  118.       patternInterval += CYCLE_INT;
  119. }
  120.  
  121.   // put your main code here, to run repeatedly:
  122. if (millis() > fadeint) {
  123.   colorfade();
  124.   fadeint = millis() + 100; // Increment up by 100 millis
  125. }
  126.  
  127. }
  128.  
  129. void handleTemp(AdafruitIO_Data *data) {
  130.  
  131.   Serial.print("received <- ");
  132.   Serial.println(data->value());  // print the temperature data to the serial monitor
  133.  
  134.   int todaysHigh = data->toInt(); // store the incoming temperature data as an integer
  135.  
  136.   lcd.setCursor(0, 0);
  137.   lcd.print("                ");
  138.   lcd.setCursor(0, 0);
  139.   lcd.print("Temp: ");
  140.   lcd.print(todaysHigh);
  141.   lcd.print(" F");
  142.  
  143.   drawThermometer(todaysHigh);
  144.   delay(500);                     // wait half a second
  145. }
  146.  
  147. void handleCondition(AdafruitIO_Data *data) {
  148.   strip.clear();
  149.   String forecast = data->toString(); // store the incoming weather data in a string
  150.  
  151.   lcd.setCursor(0, 1);
  152.   lcd.print("                ");
  153.   lcd.setCursor(0, 1);
  154.   lcd.print(forecast);
  155.  
  156.  
  157.   //the following strings store the varous IFTTT weather report words I've discovered so far
  158.   String rain = String("Rain");
  159.   String lightrain = String("Light Rain");
  160.   String rainshower = String ("Rain Shower");
  161.   String snow = String("Snow");
  162.   String cloudy = String("Cloudy");
  163.   String mostlycloudy = String("Mostly Cloudy");
  164.   String partlycloudy = String("Partly Cloudy");
  165.   String clearsky = String("Clear");
  166.   String fair = String("Fair");
  167.   String sunny = String("Sunny");
  168.   String rainandsnow = String("Rain and Snow");
  169.   String snowshower = String("Snow Shower");
  170.  
  171.  
  172.   if (forecast.equalsIgnoreCase(snow) || forecast.equalsIgnoreCase(rainandsnow) || forecast.equalsIgnoreCase(snowshower)){
  173.     Serial.println("snow in the forecast today");
  174.     weatherState = 0;
  175.     pixStart = 0;
  176.     r = 200;
  177.     g = 220;
  178.     b = 255;
  179.     for (int i = pixStart; i < pixStart+LED_PER_SEG; ++i){
  180.       strip.setPixelColor(i, r, g, b);
  181.     }
  182.   }
  183.  
  184.   if (forecast.equalsIgnoreCase(cloudy) || forecast.equalsIgnoreCase(mostlycloudy) || forecast.equalsIgnoreCase(partlycloudy)){
  185.     Serial.println("cloudy sky in the forecast today");
  186.     weatherState = 1;
  187.     pixStart = LED_PER_SEG;
  188.     r = 210;
  189.     g = 200;
  190.     b = 255;
  191.     for (int i = pixStart; i < pixStart+LED_PER_SEG; ++i){
  192.       strip.setPixelColor(i, r, g, b);
  193.     }
  194. }
  195.  
  196.   if (forecast.equalsIgnoreCase(clearsky) || forecast.equalsIgnoreCase(fair) || forecast.equalsIgnoreCase(sunny)){
  197.     Serial.println("some kind of sun in the forecast today");
  198.     weatherState = 2;
  199.     pixStart = LED_PER_SEG*2;
  200.     r = 255;
  201.     g = 0;
  202.     b = 0;
  203.     for (int i = pixStart; i < pixStart+LED_PER_SEG; ++i){
  204.       strip.setPixelColor(i, r, g, b);
  205.   }
  206. }
  207.  
  208.   if (forecast.equalsIgnoreCase(rain) || forecast.equalsIgnoreCase(lightrain) || forecast.equalsIgnoreCase(rainshower)){
  209.     Serial.println("rain in the forecast today");
  210.     weatherState = 3;
  211.     pixStart = LED_PER_SEG*3;
  212.     r = 0;
  213.     g = 0;
  214.     b = 255;
  215.     for (int i = pixStart; i < pixStart+LED_PER_SEG; ++i){
  216.       strip.setPixelColor(i, r, g, b);
  217.     }
  218.    
  219.     strip.show();
  220.   }
  221. }
  222.  
  223. void colorfade() {
  224. switch(weatherState) {
  225.   case 0: // snow -- Starts at r: 200, g: 220, b: 255 -- ends at rgb: 255, 255, 255
  226.     if (~reversefade) {
  227.       if (r > 255) {
  228.         reversefade = 1;
  229.       }
  230.       r += 5;
  231.       if (g < 255) {  // Green gets incremented up second
  232.         g += 5;
  233.       }
  234.     } else {
  235.       if (r < 200) {
  236.         reversefade = 0;
  237.       }
  238.       r -= 5;
  239.       if (g > 220) {
  240.         g -= 5;
  241.       }
  242.  
  243.     }
  244.  
  245.     for (int i=pixStart; i < pixStart + LED_PER_SEG; i++){
  246.       strip.setPixelColor(i, r, g, b);
  247.     }
  248.    
  249.     strip.show();
  250.     break;
  251.    
  252.   case 1: // clouds -- starts at rgb: 210, 200, 255 -- ends at rgb: 200, 220, 255
  253.     if (~reversefade) {
  254.       if (g > 220) {
  255.         reversefade = 1;
  256.       }
  257.       /*increase RGB Value incrementally */
  258.       g += 2;
  259.       if (r > 200) {  // Green gets incremented up second
  260.         r -= 1;
  261.       }
  262.     } else {
  263.       if (g < 200) {
  264.       reversefade = 0;
  265.     }
  266.       g -= 2;
  267.       if (r < 210) {
  268.         r += 1;
  269.       }
  270.     }
  271.    
  272.     for (int i=pixStart; i < pixStart + LED_PER_SEG; i++){
  273.       strip.setPixelColor(i, r, g, b);
  274.     }
  275.    
  276.     strip.show();
  277.     break;
  278.    
  279.   case 2: // sun -- starts at rgb: 255, 0, 0 -- ends at rgb: 255, 255: 0
  280.     if (~reversefade) {
  281.       if ( g > 255) {
  282.         reversefade = 1;
  283.       }
  284.       g += 20;
  285.     } else {
  286.     if (g < 0) {
  287.       reversefade = 0;
  288.     }
  289.       g -= 20;
  290.     }
  291.    
  292.     for (int i=pixStart; i < pixStart + LED_PER_SEG; i++){
  293.       strip.setPixelColor(i, r, g, b);
  294.     }
  295.    
  296.     strip.show();
  297.     break;
  298.    
  299.   case 3: // rain -- starts at rgb: 0, 0, 255 -- ends at rgb: 0, 255, 255
  300.     if (~reversefade) {
  301.       if ( g > 255) {
  302.         reversefade = 1;
  303.       }
  304.       g += 20;
  305.     } else {
  306.     if (g < 0) {
  307.       reversefade = 0;
  308.     }
  309.       g -= 20;
  310.     }
  311.    
  312.     for (int i=pixStart; i < pixStart + LED_PER_SEG; i++){
  313.       strip.setPixelColor(i, r, g, b);
  314.     }
  315.    
  316.     strip.show();
  317.     break;
  318.    
  319.   default:
  320.   if (rbit < 256) {
  321.     ++rbit;
  322.     for(int i=0; i<strip.numPixels(); i++) {
  323.       strip.setPixelColor(i, Wheel((i+rbit) & 255));
  324.     }
  325.    
  326.     strip.show();
  327.   } else {
  328.     rbit = 0;
  329.   }
  330. }
  331. }
  332.  
  333. uint32_t Wheel(byte WheelPos) {
  334.   WheelPos = 255 - WheelPos;
  335.   if(WheelPos < 85) {
  336.     return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  337.   }
  338.   if(WheelPos < 170) {
  339.     WheelPos -= 85;
  340.     return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  341.   }
  342.   WheelPos -= 170;
  343.   return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  344. }
  345.  
  346. void colorWipe(uint32_t c, uint8_t wait) {
  347.   for(uint16_t i=0; i<NUM_LEDS; i++) {
  348.     strip.setPixelColor(i, c);
  349.     strip.show();
  350.     delay(wait);
  351.   }
  352. }
  353.  
  354. void whiteOverRainbow(uint8_t wait, uint8_t whiteSpeed, uint8_t whiteLength, uint8_t loops) {
  355.  
  356.   if(whiteLength >= strip.numPixels()) whiteLength = strip.numPixels() - 1;
  357.  
  358.   int head = whiteLength - 1;
  359.   int tail = 0;
  360.   int loopNum = 0;
  361.  
  362.   static unsigned long lastTime = 0;
  363.  
  364.  
  365.   while(true){
  366.     for(int j=0; j<256; j++) {
  367.       for(uint16_t i=0; i<strip.numPixels(); i++) {
  368.         if((i >= tail && i <= head) || (tail > head && i >= tail) || (tail > head && i <= head) ){
  369.           strip.setPixelColor(i, strip.Color(0,0,0, 255 ) );
  370.         }
  371.         else{
  372.           strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
  373.         }
  374.        
  375.       }
  376.  
  377.       if(millis() - lastTime > whiteSpeed) {
  378.         head++;
  379.         tail++;
  380.         if(head == strip.numPixels()){
  381.           loopNum++;
  382.         }
  383.         lastTime = millis();
  384.       }
  385.  
  386.       if(loopNum == loops) {
  387.         return;
  388.       }
  389.    
  390.       head%=strip.numPixels();
  391.       tail%=strip.numPixels();
  392.       io.run();
  393.       strip.show();
  394.     }
  395.   }
  396.  
  397. }
  398.  
  399. void handleTime(AdafruitIO_Data *data) {
  400.   minutes = 0;
  401.   String checkTime = data->toString();
  402.   String hourlyTime = checkTime.substring(checkTime.lastIndexOf(' ')+1,checkTime.lastIndexOf(' ')+3);
  403.   hourlyInt = hourlyTime.toInt();
  404.  
  405.   // Dim lights during night time
  406.   char ampm = checkTime.charAt(checkTime.length() - 2);
  407.   if ((ampm == 'P' && hourlyInt > 9) || (ampm == 'A' && hourlyInt < 5)) {
  408.     brightness = 50;
  409.     strip.setBrightness(brightness);
  410.   } else {
  411.     brightness = 200;
  412.     strip.setBrightness(brightness);
  413.   }
  414.   Serial.println(hourlyInt);
  415.   drawClock();
  416.   oneMinuteCounter = millis() + 60 * 1000;
  417. }
  418.  
  419. void screenSetup() {
  420.   // Text wrap option
  421.   tft.setTextWrap(false);
  422.  
  423.   // Initializes the screen to Black
  424.   tft.fillScreen(ST77XX_BLACK);
  425.  
  426.   // The Weather Men Title
  427.   tft.setCursor(5, 15);
  428.   tft.setTextColor(ST77XX_YELLOW);
  429.   tft.setTextSize(2);
  430.   tft.println("WeatherMen");
  431.  
  432.   // The clock box
  433.   tft.drawRoundRect(5, 45, 120, 43, 5, ST77XX_WHITE);
  434. }
  435.  
  436. void drawClock() {
  437.  
  438.   // Writes Refreshed Clock to screen
  439.   tft.fillRect(7, 47, 116, 39, ST77XX_BLACK);
  440.   tft.setCursor(5, 52);
  441.   tft.setTextColor(ST77XX_GREEN);
  442.   tft.setTextSize(4);
  443.   if(hourlyInt < 10)
  444.     tft.print('0');
  445.   tft.print(hourlyInt);
  446.   tft.print(':');
  447.   if(minutes < 10)
  448.     tft.print('0');
  449.   tft.print(minutes);
  450. }
  451.  
  452. void drawThermometer(int temp) {
  453.   // Thermometer Reading calcultater
  454.   int thermoReading = 100 * temp / 120;
  455.  
  456.   // Themometer Shapes
  457.   tft.drawRoundRect(15, 105, 110, 10, 5, ST77XX_WHITE);
  458.   tft.fillCircle(15, 110, 7, ST77XX_BLUE);
  459.   tft.drawCircle(15, 110, 8, ST77XX_WHITE);
  460.   tft.fillRect(15, 106, 100, 8, ST77XX_BLACK);
  461.   tft.fillRect(15, 106, thermoReading, 8, ST77XX_BLUE);
  462.  
  463.   // Thermometer Scale
  464.   tft.setCursor(22, 97);
  465.   tft.setTextColor(ST77XX_WHITE);
  466.   tft.setTextSize(0);
  467.   tft.println("0   40   80   120");
  468. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement