SHARE
TWEET

Untitled

a guest Apr 21st, 2017 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <ArduinoJson.h>
  2. #include <SPI.h>
  3. #include <Ethernet.h>
  4.  
  5. #define RBUFFSIZE 600
  6. #define Light  A0
  7.  
  8. byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
  9. const char server[] = "api.openweathermap.org";
  10. IPAddress ip(192,168,1,202);
  11. EthernetClient client;
  12. char responseBuffer[RBUFFSIZE];
  13. int  rbindex = 0;
  14. double Sunset = 0;
  15. double Sunrise = 0;
  16. double CTime = 0;
  17. boolean startCapture;
  18.  
  19. void setup() {
  20.   Serial.begin(9600);
  21.   pinMode(Light, OUTPUT);
  22.   if (Ethernet.begin(mac) == 0){
  23.     Serial.println("Failed to configure Ethernet using DHCP");
  24.     Ethernet.begin(mac, ip);
  25.   }
  26.   delay(500);
  27.   Serial.print("My IP Address is: ");
  28.   Serial.println(Ethernet.localIP());
  29.   Serial.println("Connecting...");
  30. }
  31. void loop()
  32. {
  33.   getSunTime();
  34.   delay(1000);
  35.   getTime();
  36.   delay(1000);
  37.   if(CTime > Sunrise && CTime < Sunset){
  38.      digitalWrite(Light,LOW);
  39.      Serial.println("Day");
  40.   }
  41.   else{
  42.     Serial.println("Night");
  43.     digitalWrite(Light, HIGH);
  44.   }
  45.   delay(5000);
  46. }
  47. void getSunTime(){
  48.   if (client.connect(server, 80)){
  49.     Serial.println("Connected!");
  50.     const String html_cmd1 = "GET /data/2.5/weather?q=ChiangMai,th&appid=9e27124ecd105c1b80968f04d5d088f3 HTTP/1.1";
  51.     const String html_cmd2 = "Host: api.openweathermap.org";
  52.     const String html_cmd3 = "Connection: close";
  53.    
  54.     //Serial.println(" " + html_cmd1);
  55.     //Serial.println(" " + html_cmd2);
  56.     //Serial.println(" " + html_cmd3);
  57.    
  58.     client.println(html_cmd1);
  59.     client.println(html_cmd2);
  60.     client.println(html_cmd3);
  61.     client.println();
  62.    
  63.     responseBuffer[0] = '\0';
  64.     rbindex = 0;
  65.  
  66.     startCapture = false;  
  67.   }
  68.   else{
  69.     Serial.println("Connection failed!");
  70.   }
  71.   int r = 0;
  72.   while(r == 0){
  73.     if (client.available()){
  74.       char c = client.read();
  75.       if(c == '{') {
  76.         startCapture=true;
  77.       }
  78.       if(startCapture && rbindex < RBUFFSIZE){
  79.         responseBuffer[rbindex] = c;
  80.         rbindex++;
  81.       }
  82.     }
  83.      if(!client.connected()){  
  84.       Serial.print("Get Sun Time....");
  85.       //Serial.print(strlen(responseBuffer));
  86.       Serial.println("OK");
  87.       client.stop();
  88.       client.flush();
  89.      
  90.       //Serial.println(responseBuffer);
  91.      
  92.       Serial.println();
  93.          
  94.       StaticJsonBuffer<500> jsonBuffer;
  95.      
  96.       JsonObject& root = jsonBuffer.parseObject(responseBuffer);
  97.  
  98.       if (!root.success()) {
  99.         Serial.println("parseObject() failed");
  100.         getSunTime();
  101.       }  else {
  102.         Serial.print("Sunset : ");
  103.         Serial.println((double)root["sys"]["sunset"]);
  104.         Serial.print("Sunrise : ");
  105.         Serial.println((double)root["sys"]["sunrise"]);
  106.         Sunset = (double)root["sys"]["sunset"];
  107.         Sunrise = (double)root["sys"]["sunrise"];
  108.       }  
  109.       r = 1;  
  110.      }
  111.     }
  112.   }
  113. void getTime(){
  114.   if (client.connect("www.convert-unix-time.com", 80))
  115.   {
  116.     Serial.println("Connected!");
  117.    
  118.     const String html_cmd1 = "GET /api?timestamp=now HTTP/1.1 HTTP/1.1";
  119.     const String html_cmd2 = "Host: www.convert-unix-time.com";
  120.     const String html_cmd3 = "Connection: close";
  121.    
  122.    
  123.  
  124.     //Serial.println(" " + html_cmd1);
  125.     //Serial.println(" " + html_cmd2);
  126.     //Serial.println(" " + html_cmd3);
  127.    
  128.     // Make a HTTP request:
  129.     client.println(html_cmd1);
  130.     client.println(html_cmd2);
  131.     client.println(html_cmd3);
  132.     client.println();
  133.    
  134.     responseBuffer[0] = '\0';
  135.     rbindex = 0;
  136.  
  137.     startCapture = false;  
  138.   }
  139.   else
  140.   {
  141.     Serial.println("Connection failed!");
  142.   }
  143.   int r = 0;
  144.   while(r == 0){
  145.     if (client.available())
  146.   {
  147.     char c = client.read();
  148.     if(c == '{') {
  149.       startCapture=true;
  150.     }
  151.    
  152.     if(startCapture && rbindex < RBUFFSIZE) {
  153.       responseBuffer[rbindex] = c;
  154.       rbindex++;
  155.     }
  156.   }
  157.   if (!client.connected()) {  
  158.      
  159.    
  160.     Serial.print("Get Time.....");
  161.     //Serial.print(strlen(responseBuffer));
  162.     Serial.println("OK");
  163.     client.stop();
  164.     client.flush();
  165.    
  166.     //Serial.println(responseBuffer);
  167.    
  168.     Serial.println();
  169.        
  170.     StaticJsonBuffer<500> jsonBuffer;
  171.    
  172.     JsonObject& root = jsonBuffer.parseObject(responseBuffer);
  173.  
  174.     if (!root.success()) {
  175.       Serial.println("parseObject() failed");
  176.       getTime();
  177.     }  else {
  178.       Serial.print("timenow : ");
  179.       Serial.println((char*)root["timestamp"]);
  180.       CTime = (double)root["timestamp"] ;
  181.     }
  182.     // do nothing forevermore:
  183.     r = 1;  
  184.    }
  185.   }
  186. }
RAW Paste Data
Top