Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "digole.h""
- bool makeRequest = true;
- bool waitForResponse = false;
- bool getResponse = false;
- int lastStepTime = 0;
- char response[600];
- int responseIndex = 0;
- DigoleSerialDisp mydisp(SS);
- void setup() {
- Serial.begin(9600);
- Serial.println("setup");
- pinMode(D0, OUTPUT);
- mydisp.begin();
- mydisp.clearScreen();
- mydisp.setFont(10);
- delay(1000);
- mydisp.println("Starting...");
- String time = http_get("www.timeapi.org", "/utc/now");
- mydisp.println(time);
- mydisp.println("Second Attempt");
- String time2 = http_get("www.timeapi.org", "/utc/now?\\s");
- mydisp.println(time2);
- mydisp.println("Finishing");
- }
- TCPClient client;
- char buffer[512];
- String http_get(char const* hostname, String path) {
- if (client.connect(hostname, 80)) {
- client.print("GET ");
- client.print(path);
- client.print(" HTTP/1.0\n");
- client.print("HOST: ");
- client.println(hostname);
- client.print("\n");
- // client.print("Connection: close\n\n");
- client.flush();
- } else {
- Serial.println("connection failed");
- mydisp.println("Connection Failed...");
- client.stop();
- return NULL;
- }
- uint32_t startTime = millis();
- int i = 0;
- while(!client.available() && (millis() - startTime) < 5000);
- while (client.available()) {
- char c = client.read();
- if (c == -1) break;
- Serial.print(c);
- buffer[i] = c;
- i++;
- }
- client.stop();
- mydisp.println(buffer);
- String response(buffer);
- int bodyPos = response.indexOf("\r\n\r\n");
- if (bodyPos == -1) {
- Serial.println("can not find http reponse body");
- mydisp.println("Can not find http reponse body...");
- return NULL;
- }
- return response.substring(bodyPos + 4);
- }
- void loop() {
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement