-
-
Save domgiles/5e4340c61e2a9ef08ab5 to your computer and use it in GitHub Desktop.
GetTime.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} | |
delay(2000); // TODO: work arround the delay | |
unsigned int bytes = client.available(); | |
for ( unsigned int i = 0; i < sizeof(buffer) && i < bytes; i++) { | |
char c = client.read(); | |
if (c == -1) { | |
break; | |
} | |
buffer[i] = c; | |
} | |
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() { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment