Advertisement
Guest User

Untitled

a guest
May 25th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.78 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <WiFiClient.h>
  3. #include <Adafruit_NeoPixel.h>
  4. #define PIN 6
  5. Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, PIN, NEO_GRB + NEO_KHZ800);
  6. const char WiFiSSID[] = "NSA";
  7. const int LED_PIN = 5; // Thing's onboard, green LED
  8. const int ANALOG_PIN = A0; // The only analog pin on the Thing
  9. const int DIGITAL_PIN = 12; // Digital pin to be read
  10. WiFiClient client;
  11. String responseString;
  12. String currentCondition;
  13. boolean startCapture;
  14.  
  15. void connect()
  16. {
  17. byte ledStatus = LOW;
  18. Serial.println();
  19. Serial.println("Connecting to: " + String(WiFiSSID));
  20. // Set WiFi mode to station (as opposed to AP or AP_STA)
  21. WiFi.mode(WIFI_STA);
  22.  
  23. // WiFI.begin([ssid], [passkey]) initiates a WiFI connection
  24. // to the stated [ssid], using the [passkey] as a WPA, WPA2,
  25. // or WEP passphrase.
  26. WiFi.begin(WiFiSSID);
  27.  
  28. // Use the WiFi.status() function to check if the ESP8266
  29. // is connected to a WiFi network.
  30. while (WiFi.status() != WL_CONNECTED){
  31. // Blink the LED
  32. digitalWrite(LED_PIN, ledStatus); // Write LED high/low
  33. ledStatus = (ledStatus == HIGH) ? LOW : HIGH;
  34.  
  35. // Delays allow the ESP8266 to perform critical tasks
  36. // defined outside of the sketch. These tasks include
  37. // setting up, and maintaining, a WiFi connection.
  38. delay(100);
  39. // Potentially infinite loops are generally dangerous.
  40. // Add delays -- allowing the processor to perform other
  41. // tasks -- wherever possible.
  42. }
  43. Serial.println("WiFi connected");
  44. Serial.println("IP address: ");
  45. Serial.println(WiFi.localIP());
  46. }
  47.  
  48. void initHardware()
  49. {
  50. Serial.begin(9600);
  51. pinMode(DIGITAL_PIN, INPUT_PULLUP); // Setup an input to read
  52. pinMode(LED_PIN, OUTPUT); // Set LED as output
  53. digitalWrite(LED_PIN, HIGH); // LED off
  54. // Don't need to set ANALOG_PIN as input,
  55. // that's all it can be.
  56. }
  57.  
  58. // Perform an HTTP GET request to a remote page
  59. bool getPage() {
  60. const char http_site[] = "api.openweathermap.org";
  61. const int http_port = 80;
  62. Serial.println("About to connect");
  63. // Attempt to make a connection to the remote server
  64. if ( !client.connect(http_site, http_port) ) {
  65. Serial.println("Connect failed");
  66. return false;
  67. }
  68. Serial.println("Connect succeeded");
  69. // Make an HTTP GET request
  70. client.println("GET /data/2.5/weather?q=SanFrancisco,usa&appid=e798a6b30f92812133a91c06a33dbe41 HTTP/1.1");
  71. client.print("Host: ");
  72. client.println(http_site);
  73. client.println("Connection: close");
  74. client.println();
  75.  
  76. Serial.println("Data sent");
  77. return true;
  78. }
  79.  
  80. String getValuesFromKey(const String response, const String sKey){
  81. String quotedKey;
  82. quotedKey = "\"" + sKey + "\":";
  83. char key[quotedKey.length()];
  84. quotedKey.toCharArray(key, sizeof(key));
  85. int keySize = sizeof(key)-1;
  86. String result = "";
  87. int responseLength = response.length();
  88. for(int i=0; i < (responseLength-keySize-1); i++){
  89. char c[keySize];
  90. for(int k=0; k<keySize; k++){
  91. c[k] = response.charAt(i+k);
  92. }
  93. boolean isEqual = true;
  94. for(int k=0; k<keySize; k++){
  95. if(!(c[k] == key[k])){
  96. isEqual = false;
  97. break;
  98. }
  99. }
  100. if(isEqual){
  101. int j= i + keySize + 1;
  102. while(!(response.charAt(j) == ',')){
  103. result += response.charAt(j);
  104. j++;
  105. }
  106. //Remove char '"'
  107. result.replace("\"","");
  108. break;
  109. }
  110. }
  111. return result;
  112. }
  113.  
  114. void colorWipe(uint32_t c, uint8_t wait) {
  115. for(uint16_t i=0; i<strip.numPixels(); i++) {
  116. strip.setPixelColor(i, c);
  117. strip.show();
  118. delay(10);
  119. }
  120. }
  121.  
  122. void setColor(int R, int G, int B){
  123. colorWipe(strip.Color(R, G, B), 50);
  124. }
  125.  
  126. void updateColor(String condition){
  127. if (condition == "clear sky"){
  128. setColor(100, 100, 100);
  129. }
  130.  
  131. if (condition == "few clouds"){
  132. setColor(100, 100, 100);
  133. }
  134.  
  135. if (condition == "scattered clouds"){
  136. setColor(100, 100, 100);
  137. }
  138.  
  139. if (condition == "broken clouds"){
  140. setColor(100, 100, 100);
  141. }
  142.  
  143. if (condition == "shower rain"){
  144. setColor(100, 100, 100);
  145. }
  146.  
  147. if (condition == "broken clouds"){
  148. setColor(100, 100, 100);
  149. }
  150.  
  151. if (condition == "rain"){
  152. setColor(100, 100, 100);
  153. }
  154.  
  155. if (condition == "thunderstorm"){
  156. setColor(100, 100, 100);
  157. }
  158.  
  159. if (condition == "snow"){
  160. setColor(100, 100, 100);
  161. }
  162.  
  163. if (condition == "mist"){
  164. setColor(100, 100, 100);
  165. }
  166. }
  167.  
  168.  
  169. void setup()
  170. {
  171. initHardware(); // Setup input/output I/O pins
  172. connect(); // Connect to WiFi
  173. digitalWrite(LED_PIN, LOW); // LED on to indicate connect success
  174. delay(500);
  175. if ( !getPage() ) {
  176. Serial.println("GET request failed");
  177. }
  178. }
  179.  
  180. void loop()
  181. {
  182. if (client.available())
  183. {
  184. char c = client.read();
  185. if(c == '{'){
  186. startCapture=true;
  187. }
  188. if(startCapture){
  189. responseString += c;
  190. }
  191. }
  192.  
  193. // If the server has disconnected, stop the client and WiFi
  194. if ( !client.connected() ) {
  195. Serial.println();
  196.  
  197. // Close socket and wait for disconnect from WiFi
  198. client.stop();
  199. if ( WiFi.status() != WL_DISCONNECTED ) {
  200. WiFi.disconnect();
  201. }
  202.  
  203. // Turn off LED
  204. digitalWrite(LED_PIN, LOW);
  205.  
  206. // Do nothing
  207. Serial.println("Received " + (String)responseString.length() + " bytes");
  208. Serial.println("Disconnecting.");
  209. client.stop();
  210. client.flush();
  211. Serial.println();
  212. currentCondition = getValuesFromKey(responseString,"description");
  213. Serial.print("description:");
  214. Serial.println(currentCondition);
  215. Serial.println();
  216. updateColor(currentCondition);
  217. delay(1800000);
  218. connect();
  219. // Serial.println("reconnected");
  220. if( !getPage() ) {
  221. Serial.println("GET request failed");
  222. }
  223. }
  224. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement