read dynamic data using http API
Posted: Mon Aug 08, 2016 3:02 pm
Hi, I'm using the the following code to read random bits (0s and 1s) from a webpage and use them to blink a LED randomly. I've been trying to read such bits from random.org using their old html API - e.g. https://www.random.org/integers/?num=10 ... in&rnd=new - this will generate 100 binary 1-bit numbers. My problem is that the ESP8266 refuses to connect to this host - I get a 'connection failed' message.
am I missing something?
best
m
am I missing something?
best
Code: Select all
#include <ESP8266WiFi.h>
const char* ssid = "XXXXXXXXX";
const char* password = "XXXXXXXXXXX";
const char* host = "www.agxivatein.com";
WiFiClient client;
void setup() {
Serial.begin(115200);
delay(100);
pinMode(0, OUTPUT);
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());
delay(5000);
Serial.print("connecting to ");
Serial.println(host);
// Use WiFiClient class to create TCP connections
const int httpPort = 80;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
// We now create a URI for the request
String url = "/test/bits.txt";
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");
delay(500);
}
void loop() {
// Read all the lines (each line only has a bit) of the reply from server and use them to blink a led
if (client.available()) {
String line = client.readStringUntil('\n');
Serial.println(line);
digitalWrite(0, line.toInt());
}
delay(200);
}
m