Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * This sketch sends data via HTTP GET requests to data.sparkfun.com service.
- *
- * You need to get streamId and privateKey at data.sparkfun.com and paste them
- * below. Or just customize this script to talk to other HTTP servers.
- *
- */
- #include <ESP8266WiFi.h>
- const char* ssid = "ssid";
- const char* password = "password";
- const char* host = "www.frack.nl";
- const char* streamId = "spacestate";
- const char* privateKey = "?image";
- String readString, readString1;
- String data;
- //char c = "";
- //int index=0; //for counting line feeds
- char lf=10; //line feed character
- void setup() {
- Serial.begin(115200);
- delay(10);
- pinMode(12, OUTPUT); //pin green led
- pinMode(15, OUTPUT); //pin red led
- // We start by connecting to a WiFi network
- Serial.println();
- Serial.println();
- Serial.print("Connecting to ");
- Serial.println(ssid);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("");
- Serial.println("WiFi connected");
- Serial.println("IP address: ");
- Serial.println(WiFi.localIP());
- }
- int value = 0;
- void loop() {
- delay(5000);
- ++value;
- Serial.print("connecting to ");
- Serial.println(host);
- // Use WiFiClient class to create TCP connections
- WiFiClient client;
- const int httpPort = 80;
- if (!client.connect(host, 80)) {
- Serial.println("connection failed");
- return;
- }
- // We now create a URI for the request
- String url = "/";
- url += streamId;
- url += "/";
- url += privateKey;
- Serial.print("Requesting URL: ");
- Serial.println(url);
- // This will send the request to the server
- client.print(String("GET ") + url + " HTTP/1.1\r\n" +
- "Host: " + host + "\r\n" +
- "Connection: close\r\n\r\n");
- unsigned long timeout = millis();
- Serial.println("Request URL");
- while (client.available() == 0) {
- if (millis() - timeout > 5000) {
- Serial.println(">>> Client Timeout !");
- client.stop();
- return;
- }
- }
- if (client.available()) {
- char c = client.read();
- // If it isn't a new line, add the character to the buffer
- if (c != '\n' && c != '\r') {
- readString1[index] = c;
- index++;
- // Are we too big for the buffer? Start tossing out data
- if (index >= BUFSIZ)
- index = BUFSIZ -1;
- // Continue to read more data!
- continue;
- }
- // Got a \n or \r new line, which means the string is done.
- readString1[index] = 0;
- // Print it out for debugging.
- Serial.println(readString1);
- /*
- // Read all the lines of the reply from server and print them to Serial
- if (client.available())
- {
- while(client.read()!=lf)
- {
- char c = client.read();
- x=(x+1);
- if (x==9) readString += c;
- data = readString;
- data.trim();
- Serial.print(data);
- }
- }
- */
- /* while(client.available()){
- delay(100);
- char c = client.read(); //gets byte from ethernet buffer
- // Serial.print(c); //prints raw feed for testing
- if (c==lf) x=(x+1); //counting line feeds
- if (x==9) readString += c; //building readString
- data = readString;
- data.trim();
- Serial.print(data);
- */
- if (data == "Location: /spacestate/banner_closed.png")
- {
- digitalWrite(15, HIGH);
- digitalWrite(12, LOW);
- }
- if (data == "Location: /spacestate/banner_open.png")
- {
- digitalWrite(12, HIGH);
- digitalWrite(15, LOW);
- }
- // else Serial.print("Open");
- //String line = client.readStringUntil('\r');
- //int Status = line.lastIndexOf("banner_closed");
- //Serial.print(find_text("banner_closed", line));
- //if (line.substring(28) == "closed") {
- // Serial.print("Closed"); }
- // Serial.print(line);
- //Serial.print(Status);
- Serial.println();
- Serial.println("closing connection");
- client.stop();
- x=0;
- delay(5000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement