Advertisement
Guest User

Lamp.ino

a guest
Jun 14th, 2015
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.57 KB | None | 0 0
  1. #include <Bridge.h>
  2. #include <HttpClient.h>
  3. #include <Process.h>
  4. #include <Wire.h>
  5. #include <Time.h>
  6. #include <infineonrgb.h>
  7. #include <stdlib.h>
  8.  
  9. String lon;
  10. String lat;
  11. float localSetTime;
  12. float localRiseTime;
  13. boolean daylight;
  14.  
  15. InfineonRGB LEDS;
  16.  
  17. void setup() {
  18.     Bridge.begin();
  19.     Serial.begin(9600);
  20.     while(!Serial);
  21.     LEDS.begin();      
  22.     LEDS.SetDimmingLevel(0x0000);
  23.     findLon();
  24.     findLat();
  25.     thisIsTheTime();
  26.     getSunrise();
  27.     getSunset();
  28. }
  29.  
  30. void loop() {
  31.   if(hour() == convertToTime(localSetTime).substring(0,2).toInt() && convertToTime(localSetTime).substring(3).toInt())
  32.   {
  33.     lightOn();
  34.   }
  35.   delay(500);
  36.   if(hour() == convertToTime(localRiseTime).substring(0,2).toInt() && convertToTime(localRiseTime).substring(3).toInt())
  37.   {
  38.     lightOff();
  39.   }
  40.   delay(500);
  41. }
  42.  
  43. String findLon() {
  44.     HttpClient clientLon;
  45.     clientLon.get("http://ip-api.com/csv/?fields=lon");
  46.    
  47.     while (clientLon.available()) {
  48.        lon = clientLon.readString();
  49.     }
  50.    
  51.     Serial.print("Lon: ");
  52.     Serial.println(lon);
  53.    
  54.     return lon;
  55. }
  56.  
  57. String findLat() {
  58.     HttpClient clientLat;
  59.     clientLat.get("http://ip-api.com/csv/?fields=lat");
  60.    
  61.     while (clientLat.available()) {
  62.        lat = clientLat.readString();
  63.     }
  64.    
  65.     Serial.print("Lat: ");
  66.     Serial.println(lat);
  67.    
  68.     return lat;
  69. }
  70.  
  71. void thisIsTheTime() {
  72.     String DateL = "";
  73.     String TimeL = "";
  74.    
  75.     Serial.print("Setting time zone... ");
  76.     ProcExec("echo","'CET-1CEST,M3.5.0,M10.5.0/3' > /etc/TZ");
  77.     Serial.println("done.");
  78.  
  79.     Serial.print("Syncing time with NTP... ");
  80.     ProcExec("ntpd","-nqp 0.openwrt.pool.ntp.org");
  81.     Serial.println("done.");
  82.     Serial.println("");
  83.  
  84.     DateL = ProcExec("date","+%Y-%m-%d");
  85.     Serial.print("Linino Date:");
  86.     Serial.println(DateL);
  87.  
  88.     TimeL = ProcExec("date","+%T");  
  89.     Serial.print("Linino Time:");
  90.     Serial.println(TimeL);
  91.  
  92.     int dt[6];
  93.     dt[0]=DateL.substring(0,4).toInt();
  94.     dt[1]=DateL.substring(5,7).toInt();
  95.     dt[2]=DateL.substring(8).toInt();
  96.     dt[3]=TimeL.substring(0,2).toInt();
  97.     dt[4]=TimeL.substring(3,5).toInt();
  98.     dt[5]=TimeL.substring(6).toInt();
  99.    
  100.     setTime(dt[3], dt[4], dt[5], dt[2], dt[1], dt[0]);
  101.    
  102.     Serial.print("Date: ");
  103.     Serial.print(month());
  104.     Serial.print("/");
  105.     Serial.print(day());
  106.     Serial.print("/");
  107.     Serial.println(year());
  108.     Serial.print("Time: ");
  109.     Serial.print(hour());
  110.     Serial.print(":");
  111.     Serial.print(minute());
  112.     Serial.print(":");
  113.     Serial.println(second());
  114. }
  115.  
  116. String ProcExec(String Comm, String Par) {
  117.    String Res = "";
  118.    Process p;
  119.    p.begin(Comm);
  120.    p.addParameter(Par);
  121.    p.run();
  122.  
  123.    while (p.available()>0) {
  124.      char c = p.read();
  125.      Res += c;
  126.    }
  127.    return Res;
  128. }
  129.  
  130. float getSunrise() {
  131.    isDaylightSavings();
  132.  
  133.    char longitude[20];
  134.    char latitude[20];
  135.    float longi, lati, A, B, C, E, F, JD, JC, longSun, anomSun, eOrbit, sunEqCtr, trueLongSun, trueAnomSun, sunRadVector, sunAppLong, obliqEcliptic, obliqCorr;
  136.    float sunAscen, sunDeclin, varY, eqTime, haSunrise, solarNoon;
  137.    float D2R = PI / 180;
  138.    float R2D = 180 / PI;
  139.    
  140.    lat.toCharArray(latitude, 20);
  141.    lati = atof(latitude);
  142.    
  143.    lon.toCharArray(longitude, 20);
  144.    longi = atof(longitude);
  145.    
  146.    A = year() / 100;
  147.    B = A / 4;
  148.    C = 2 - A + B;
  149.    E = 365.25 * (year() + 4716);
  150.    F = 30.6001 * (month() + 1);
  151.    JD = C + day() + E + F - 1524.5;
  152.    JC = (JD - 2451545) / 36525;
  153.    longSun = int(280.46646 + JC * (36000.76983 + JC * 0.0003032)) % 360; //TODO: Non integer modulo
  154.    anomSun = 357.52911 + JC * (35999.05029 - 0.0001537 * JC);
  155.    eOrbit = 0.016708634 - JC * (0.000042037 + 0.0000001267 * JC);
  156.    sunEqCtr = sin(D2R * (anomSun)) * (1.914602 - JC * (0.004817 + 0.000014 * JC)) + sin(D2R * (2 * anomSun)) * (0.019993 - 0.000101 * JC) + sin(D2R * (3 * anomSun)) * 0.000289;
  157.    trueLongSun = longSun + sunEqCtr;
  158.    trueAnomSun = anomSun + sunEqCtr;
  159.    sunRadVector = (1.000001018 * (1 - eOrbit * eOrbit)) / (1 + eOrbit * cos(D2R * (trueAnomSun)));
  160.    sunAppLong = trueLongSun - 0.00569 - 0.00478 * sin(D2R * (125.04 - 1934.136 * JC));
  161.    obliqEcliptic = 23 + (26 + ((21.448 - JC * (46.815 + JC * (0.00059 - JC * 0.001813)))) / 60) / 60;
  162.    obliqCorr = obliqEcliptic + 0.00256 * cos(D2R * (125.04 - 1934.136 * JC));
  163.    sunAscen = R2D * (atan2(cos(D2R * (sunAppLong)), cos(D2R * (obliqCorr)) * sin(D2R * (sunAppLong))));
  164.    sunDeclin = R2D * (asin(sin(D2R * (obliqCorr)) * sin(D2R * (sunAppLong))));
  165.    varY = tan(D2R * (obliqCorr / 2)) * tan(D2R * (obliqCorr / 2));
  166.    eqTime = 4 * R2D * (varY * sin(2 * D2R * (longSun)) - 2 * eOrbit * sin(D2R * (anomSun)) + 4 * eOrbit * varY * sin(D2R * (anomSun)) * cos(2 * D2R * (longSun)) - 0.5 * varY * varY *
  167.      sin(4 * D2R * (longSun)) - 1.25 * eOrbit * eOrbit * sin(2 * D2R * (anomSun)));
  168.    haSunrise = R2D * (acos(cos(D2R * (90.833)) / (cos(D2R * (lati)) * cos(D2R * (sunDeclin))) - tan(D2R * (lati)) * tan(D2R * (sunDeclin))));
  169.    if (daylight == true) {
  170.      solarNoon = (720 - 4 * longi - eqTime + -5 * 60) / 1440; //TODO: Change -5 to a method for finding timezone
  171.      solarNoon = solarNoon + .041667;
  172.    }
  173.    else {
  174.      solarNoon = (720 - 4 * longi - eqTime + -5 * 60) / 1440; //TODO: Change -5 to a method for finding timezone
  175.    }
  176.    localRiseTime = (solarNoon * 1440 - haSunrise * 4) / 1440;
  177.    
  178.    Serial.print("Sunrise is at: ");
  179.    Serial.println(convertToTime(localRiseTime));
  180.    
  181.    return localRiseTime;
  182. }
  183.  
  184. float getSunset() {
  185.    isDaylightSavings();
  186.  
  187.    char longitude[20];
  188.    char latitude[20];
  189.    float longi, lati, A, B, C, E, F, JD, JC, longSun, anomSun, eOrbit, sunEqCtr, trueLongSun, trueAnomSun, sunRadVector, sunAppLong, obliqEcliptic, obliqCorr;
  190.    float sunAscen, sunDeclin, varY, eqTime, haSunrise, solarNoon;
  191.    float D2R = PI / 180;
  192.    float R2D = 180 / PI;
  193.    
  194.    lat.toCharArray(latitude, 20);
  195.    lati = atof(latitude);
  196.    
  197.    lon.toCharArray(longitude, 20);
  198.    longi = atof(longitude);
  199.    
  200.    A = year() / 100;
  201.    B = A / 4;
  202.    C = 2 - A + B;
  203.    E = 365.25 * (year() + 4716);
  204.    F = 30.6001 * (month() + 1);
  205.    JD = C + day() + E + F - 1524.5;
  206.    JC = (JD - 2451545) / 36525;
  207.    longSun = int(280.46646 + JC * (36000.76983 + JC * 0.0003032)) % 360; //TODO: Non integer modulo
  208.    anomSun = 357.52911 + JC * (35999.05029 - 0.0001537 * JC);
  209.    eOrbit = 0.016708634 - JC * (0.000042037 + 0.0000001267 * JC);
  210.    sunEqCtr = sin(D2R * (anomSun)) * (1.914602 - JC * (0.004817 + 0.000014 * JC)) + sin(D2R * (2 * anomSun)) * (0.019993 - 0.000101 * JC) + sin(D2R * (3 * anomSun)) * 0.000289;
  211.    trueLongSun = longSun + sunEqCtr;
  212.    trueAnomSun = anomSun + sunEqCtr;
  213.    sunRadVector = (1.000001018 * (1 - eOrbit * eOrbit)) / (1 + eOrbit * cos(D2R * (trueAnomSun)));
  214.    sunAppLong = trueLongSun - 0.00569 - 0.00478 * sin(D2R * (125.04 - 1934.136 * JC));
  215.    obliqEcliptic = 23 + (26 + ((21.448 - JC * (46.815 + JC * (0.00059 - JC * 0.001813)))) / 60) / 60;
  216.    obliqCorr = obliqEcliptic + 0.00256 * cos(D2R * (125.04 - 1934.136 * JC));
  217.    sunAscen = R2D * (atan2(cos(D2R * (sunAppLong)), cos(D2R * (obliqCorr)) * sin(D2R * (sunAppLong))));
  218.    sunDeclin = R2D * (asin(sin(D2R * (obliqCorr)) * sin(D2R * (sunAppLong))));
  219.    varY = tan(D2R * (obliqCorr / 2)) * tan(D2R * (obliqCorr / 2));
  220.    eqTime = 4 * R2D * (varY * sin(2 * D2R * (longSun)) - 2 * eOrbit * sin(D2R * (anomSun)) + 4 * eOrbit * varY * sin(D2R * (anomSun)) * cos(2 * D2R * (longSun)) - 0.5 * varY * varY *
  221.      sin(4 * D2R * (longSun)) - 1.25 * eOrbit * eOrbit * sin(2 * D2R * (anomSun)));
  222.    haSunrise = R2D * (acos(cos(D2R * (90.833)) / (cos(D2R * (lati)) * cos(D2R * (sunDeclin))) - tan(D2R * (lati)) * tan(D2R * (sunDeclin))));
  223.    if (daylight == true) {
  224.      solarNoon = (720 - 4 * longi - eqTime + -5 * 60) / 1440; //TODO: Change -5 to a method for finding timezone
  225.      solarNoon = solarNoon + .041667;
  226.    }
  227.    else {
  228.      solarNoon = (720 - 4 * longi - eqTime + -5 * 60) / 1440; //TODO: Change -5 to a method for finding timezone
  229.    }
  230.    localSetTime = (solarNoon * 1440 + haSunrise * 4) / 1440;
  231.    
  232.    Serial.print("Sunset is at: ");
  233.    Serial.println(convertToTime(localSetTime));
  234.    
  235.    return localSetTime;
  236. }
  237.  
  238. String convertToTime(float n) {
  239.   float tmp;
  240.   int newHour, newMinute, newSecond;
  241.   String convertedTime;
  242.  
  243.   tmp = n * 24;
  244.   newHour = floor(tmp);
  245.   tmp = tmp - newHour;
  246.   tmp = tmp * 60;
  247.   newMinute = floor(tmp);
  248.   if (newMinute < 10) {
  249.     convertedTime = String(newHour) + ":0" + String(newMinute);
  250.   }
  251.   else {
  252.     convertedTime = String(newHour) + ":" + String(newMinute);
  253.   }
  254.  
  255.   return convertedTime;
  256. }
  257.  
  258. boolean isDaylightSavings() {
  259.   int marchSecondSunday, novFirstSunday, cent, i, n;
  260.  
  261.   cent = floor(year() / 100);
  262.  
  263.   for (i = 8; i <= 14; i++) {
  264.     if (marchSecondSunday != 0) {
  265.       marchSecondSunday = int((i + floor(2.6 * 3 - 0.2) - 2 * cent + year() + floor(year() / 4) + floor(cent / 4))) % 7;
  266.     }
  267.     break;
  268.   }
  269.   for (n = 1; n <= 7; n++) {
  270.     if (novFirstSunday != 0) {
  271.       novFirstSunday = int((i + floor(2.6 * 11 - 0.2) - 2 * cent + year() + floor(year() / 4) + floor(cent / 4))) % 7;
  272.     }
  273.     break;
  274.   }
  275.  
  276.   if (month() == 3) {
  277.    if (day() >= i) {
  278.      daylight = true;
  279.     }
  280.   }
  281.   else if (month() == 11) {
  282.    if (day() <= n) {
  283.     daylight = true;
  284.    }
  285.   }
  286.   else if (month() > 3) {
  287.    daylight = true;
  288.   }
  289.   else if (month() < 11) {
  290.    daylight = true;
  291.   }
  292.   else {
  293.    daylight = false;
  294.   }
  295.  
  296.   return daylight;
  297. }
  298. void lightOn() {
  299.   LEDS.SetIntensityRGB(0x0199, 0x0000, 0x0000);
  300.   LEDS.SetDimmingLevel(0x0199);
  301.   delay(200000);
  302.   LEDS.SetDimmingLevel(0x0333);
  303.   delay(200000);
  304.   LEDS.SetDimmingLevel(0x04CC);
  305.   delay(200000);
  306.   LEDS.SetDimmingLevel(0x0666);
  307.   delay(200000);
  308.   LEDS.SetDimmingLevel(0x07FF);
  309.   delay(200000);
  310.   LEDS.SetDimmingLevel(0x0999);
  311.   delay(200000);
  312.   LEDS.SetDimmingLevel(0x0B32);
  313.   delay(200000);
  314.   LEDS.SetDimmingLevel(0x0CCC);
  315.   delay(200000);
  316.   LEDS.SetDimmingLevel(0x0E65);
  317.   delay(200000);
  318.   LEDS.SetDimmingLevel(0x0FFF);
  319. }
  320. void lightOff() {
  321.   LEDS.SetIntensityRGB(0x0E65, 0x0000, 0x0000);
  322.   LEDS.SetDimmingLevel(0x0E65);
  323.   delay(200000);
  324.   LEDS.SetDimmingLevel(0x0CCC);
  325.   delay(200000);
  326.   LEDS.SetDimmingLevel(0x0B32);
  327.   delay(200000);
  328.   LEDS.SetDimmingLevel(0x0999);
  329.   delay(200000);
  330.   LEDS.SetDimmingLevel(0x07FF);
  331.   delay(200000);
  332.   LEDS.SetDimmingLevel(0x0666);
  333.   delay(200000);
  334.   LEDS.SetDimmingLevel(0x04CC);
  335.   delay(200000);
  336.   LEDS.SetDimmingLevel(0x0333);
  337.   delay(200000);
  338.   LEDS.SetDimmingLevel(0x0199);
  339.   delay(200000);
  340.   LEDS.SetDimmingLevel(0x0000);
  341. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement