Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. #include <ArduinoJson.h>
  2.  
  3. #include <EthernetServer.h>
  4. #include <Ethernet.h>
  5. #include <Dhcp.h>
  6. #include <EthernetClient.h>
  7. #include <EthernetUdp.h>
  8. #include <Dns.h>
  9.  
  10. #include <SPI.h>
  11.  
  12.  
  13. #define RBUFFSIZE 600
  14.  
  15. #include <TimeLib.h>
  16. #include <Time.h>
  17. #include <SevSeg.h>
  18.  
  19. SevSeg sevseg;
  20. char* Buffer = malloc(4 * sizeof(char)); // array to hold the result
  21. int MIN;
  22. int HOR;
  23.  
  24. byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
  25.  
  26. const char server[] = "api.openweathermap.org";
  27.  
  28. // Set the static IP address to use if the DHCP fails to assign
  29. IPAddress ip(192,168,1,202);
  30.  
  31. EthernetClient client;
  32.  
  33. char responseBuffer[RBUFFSIZE];
  34. int rbindex = 0;
  35.  
  36. boolean startCapture;
  37. void DrawClock() {
  38. time_t t = now();
  39. MIN= minute(t);
  40. char buf[2];
  41.  
  42. sprintf (buf, "%04i", MIN);
  43. //as of now only prints minute
  44. //Serial.println(Buffer);
  45. sevseg.setChars( buf);
  46. sevseg.refreshDisplay();
  47. }
  48. int HitClient(){
  49. // start the Ethernet connection:
  50. if (Ethernet.begin(mac) == 0)
  51. {
  52. Serial.println("Failed to start Ethernet");
  53. Ethernet.begin(mac, ip);
  54. }
  55.  
  56. // give the Ethernet shield a half-second to initialize:
  57. delay(500);
  58.  
  59.  
  60. Serial.println("Connecting...");
  61.  
  62. // if you get a connection, report back via serial:
  63. if (client.connect(server, 80))
  64. {
  65. Serial.println("Connected!");
  66.  
  67. const String html_cmd1 = "https://script.googleusercontent.com/macros/echo?user_content_key=PW95UwY2XPlsWCTOh6jzuRMgqIuaq9cNkMqicouqMXm8ephtrjBYGGUJP2M32Nw3jdorQm-cfz--S2d7P4gnNfygbqsEG9b-m5_BxDlH2jW0nuo2oDemN9CCS2h10ox_1xSncGQajx_ryfhECjZEnJ9GRkcRevgjTvo8Dc32iw_BLJPcPfRdVKhJT5HNzQuXEeN3QFwl2n0M6ZmO-h7C6bwVq0tbM60-_IQDS8gp7-x-dZ28mtWEA5GNj-qTZ_41&lib=MwxUjRcLr2qLlnVOLh12wSNkqcO1Ikdrk";
  68.  
  69.  
  70.  
  71. // Make a HTTP request:
  72. client.println(html_cmd1);
  73.  
  74.  
  75. client.println();
  76.  
  77. responseBuffer[0] = '\0';
  78. rbindex = 0;
  79.  
  80. startCapture = false;
  81. }
  82. else
  83. {
  84. // if you didn't get a connection to the server:
  85. Serial.println("failed!");
  86. }
  87. if (client.available())
  88. {
  89. char c = client.read();
  90. if (c == '{') {
  91. startCapture = true;
  92. }
  93.  
  94. if (startCapture && rbindex < RBUFFSIZE) {
  95. responseBuffer[rbindex] = c;
  96. rbindex++;
  97. }
  98. }
  99.  
  100. // if the server's disconnected, stop the client:
  101. if (!client.connected()) {
  102.  
  103.  
  104.  
  105. Serial.println(strlen(responseBuffer));
  106.  
  107.  
  108. client.stop();
  109. client.flush();
  110.  
  111.  
  112.  
  113. Serial.println();
  114.  
  115. StaticJsonBuffer<465> jsonBuffer;
  116.  
  117. JsonObject& root = jsonBuffer.parseObject(responseBuffer);
  118.  
  119.  
  120. if (!root.success()) {
  121. Serial.println("failed");
  122. } else {
  123.  
  124.  
  125.  
  126. Serial.println(root["hours"].as<String>()+":"+root["minutes"].as<String>());
  127. return(root["hours"].as<int>()*60*60)+(root["minutes"].as<int>()*60);//return hour and minute in seconds snce mdnght jan1 1970
  128. }
  129. }
  130. }
  131. void setup() {
  132. Serial.begin(9600);
  133. HitClient();
  134. setSyncProvider(HitClient());
  135. setSyncInterval(10000000);
  136. byte numDigits = 4;
  137.  
  138. byte digitPins[] = {4, 1, 10, 8};
  139. byte segmentPins[] = {0, 2, 7, 6, 5, 3, 9, 12};
  140. bool resistorsOnSegments = true; // Use 'true' if on digit pins
  141. byte hardwareConfig = COMMON_ANODE; // See README.md for options
  142. sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments);
  143. }
  144.  
  145. void loop() {
  146. DrawClock();
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement