Guest User

frack spacestate

a guest
Jul 23rd, 2018
156
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. * This sketch sends data via HTTP GET requests to data.sparkfun.com service.
  3. *
  4. * You need to get streamId and privateKey at data.sparkfun.com and paste them
  5. * below. Or just customize this script to talk to other HTTP servers.
  6. *
  7. */
  8.  
  9. #include <ESP8266WiFi.h>
  10.  
  11. const char* ssid = "ssid";
  12. const char* password = "password";
  13.  
  14. const char* host = "www.frack.nl";
  15. const char* streamId = "spacestate";
  16. const char* privateKey = "?image";
  17.  
  18. String readString, readString1;
  19. String data;
  20. //char c = "";
  21.  
  22. //int index=0; //for counting line feeds
  23. char lf=10; //line feed character
  24.  
  25. void setup() {
  26. Serial.begin(115200);
  27. delay(10);
  28. pinMode(12, OUTPUT); //pin green led
  29. pinMode(15, OUTPUT); //pin red led
  30.  
  31. // We start by connecting to a WiFi network
  32.  
  33. Serial.println();
  34. Serial.println();
  35. Serial.print("Connecting to ");
  36. Serial.println(ssid);
  37.  
  38. WiFi.begin(ssid, password);
  39.  
  40. while (WiFi.status() != WL_CONNECTED) {
  41. delay(500);
  42. Serial.print(".");
  43. }
  44.  
  45. Serial.println("");
  46. Serial.println("WiFi connected");
  47. Serial.println("IP address: ");
  48. Serial.println(WiFi.localIP());
  49. }
  50.  
  51. int value = 0;
  52.  
  53. void loop() {
  54. delay(5000);
  55. ++value;
  56.  
  57. Serial.print("connecting to ");
  58. Serial.println(host);
  59.  
  60. // Use WiFiClient class to create TCP connections
  61. WiFiClient client;
  62. const int httpPort = 80;
  63. if (!client.connect(host, 80)) {
  64. Serial.println("connection failed");
  65. return;
  66. }
  67.  
  68.  
  69. // We now create a URI for the request
  70. String url = "/";
  71. url += streamId;
  72. url += "/";
  73. url += privateKey;
  74.  
  75. Serial.print("Requesting URL: ");
  76. Serial.println(url);
  77.  
  78. // This will send the request to the server
  79. client.print(String("GET ") + url + " HTTP/1.1\r\n" +
  80. "Host: " + host + "\r\n" +
  81. "Connection: close\r\n\r\n");
  82. unsigned long timeout = millis();
  83. Serial.println("Request URL");
  84. while (client.available() == 0) {
  85. if (millis() - timeout > 5000) {
  86. Serial.println(">>> Client Timeout !");
  87. client.stop();
  88. return;
  89. }
  90. }
  91.  
  92. if (client.available()) {
  93. char c = client.read();
  94.  
  95. // If it isn't a new line, add the character to the buffer
  96. if (c != '\n' && c != '\r') {
  97. readString1[index] = c;
  98. index++;
  99. // Are we too big for the buffer? Start tossing out data
  100. if (index >= BUFSIZ)
  101. index = BUFSIZ -1;
  102.  
  103. // Continue to read more data!
  104. continue;
  105. }
  106.  
  107. // Got a \n or \r new line, which means the string is done.
  108. readString1[index] = 0;
  109.  
  110. // Print it out for debugging.
  111. Serial.println(readString1);
  112.  
  113.  
  114. /*
  115. // Read all the lines of the reply from server and print them to Serial
  116.  
  117. if (client.available())
  118.  
  119. {
  120. while(client.read()!=lf)
  121. {
  122. char c = client.read();
  123. x=(x+1);
  124. if (x==9) readString += c;
  125. data = readString;
  126. data.trim();
  127. Serial.print(data);
  128. }
  129.  
  130. }
  131.  
  132. */
  133. /* while(client.available()){
  134. delay(100);
  135. char c = client.read(); //gets byte from ethernet buffer
  136. // Serial.print(c); //prints raw feed for testing
  137. if (c==lf) x=(x+1); //counting line feeds
  138. if (x==9) readString += c; //building readString
  139. data = readString;
  140. data.trim();
  141. Serial.print(data);
  142. */
  143.  
  144. if (data == "Location: /spacestate/banner_closed.png")
  145. {
  146. digitalWrite(15, HIGH);
  147. digitalWrite(12, LOW);
  148. }
  149. if (data == "Location: /spacestate/banner_open.png")
  150. {
  151. digitalWrite(12, HIGH);
  152. digitalWrite(15, LOW);
  153. }
  154.  
  155. // else Serial.print("Open");
  156.  
  157.  
  158. //String line = client.readStringUntil('\r');
  159. //int Status = line.lastIndexOf("banner_closed");
  160. //Serial.print(find_text("banner_closed", line));
  161.  
  162. //if (line.substring(28) == "closed") {
  163. // Serial.print("Closed"); }
  164. // Serial.print(line);
  165. //Serial.print(Status);
  166.  
  167. Serial.println();
  168. Serial.println("closing connection");
  169. client.stop();
  170. x=0;
  171. delay(5000);
  172. }
RAW Paste Data