Skip to content
This repository has been archived by the owner on Oct 24, 2019. It is now read-only.

Commit

Permalink
Fixed Requests and added a small test server to troubleshoot the client
Browse files Browse the repository at this point in the history
  • Loading branch information
reefab committed Jun 16, 2013
1 parent 0b5a208 commit 960c416
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 79 deletions.
7 changes: 2 additions & 5 deletions src/ntp.h
Expand Up @@ -34,14 +34,11 @@ unsigned long sendNTPpacket(IPAddress& address)

unsigned long getTimeStamp() {
if(Dns.getHostByName("pool.ntp.org", timeServer) == 1 ){
Serial.println(F("DNS resolve..."));
Serial.print(F("pool.ntp.org"));
Serial.print(" = ");
Serial.println(F("DNS resolve... = "));
Serial.println(timeServer);
sendNTPpacket(timeServer);
}else{
Serial.print(F("DNS fail..."));
Serial.print(F("time.nist.gov = "));
Serial.print(F("DNS fallback..."));
Serial.println(defaultTimeServer); // fallback
sendNTPpacket(defaultTimeServer); // send an NTP packet to a time server
}
Expand Down
135 changes: 64 additions & 71 deletions src/runkeeper.h
Expand Up @@ -7,38 +7,36 @@
* Author: reefab
*/

#define server "api.runkeeper.com"
#define status_inprogress "Uploading result"
#define status_failure "failed to connect"
#define status_data_uploaded "Data uploaded"
#define status_session_created "Session Created"
#define status_error_code "ERROR: Server returned "


IPAddress api_server;

boolean uploadResult(String startTimeStr, unsigned int totalDistance, unsigned long effectiveTime)
{
if (client.connect(server, 80)) {
Lcd.infoMessage(status_inprogress);
Serial.println(F("connected"));
client.print(F("POST /fitnessActivities HTTP/1.1\nHost: api.runkeeper.com\nContent-Type: application/vnd.com.runkeeper.NewFitnessActivity+json\nUser-Agent: Arduino/1.0\nAuthorization: "));
client.println(accessToken);
client.println();
client.print(F("{\"type\": \"Cycling\", \"equipment\": \"Stationary Bike\", \"start_time\": \""));
client.println(startTimeStr);
client.print(F("\", \"total_distance\": "));
client.println(totalDistance);
client.print(F("duration\": "));
client.println((int) (effectiveTime / 1000UL));
client.print("}");
delay(2000);
} else {
Serial.println(F("Conn. Failed"));
Lcd.errorMessage(status_failure);
client.stop();
Dns.getHostByName("api.runkeeper.com", api_server);
Serial.println(api_server);

while(!client.connected()) {
Lcd.infoMessage("Connecting");
client.connect(api_server, 80);
delay(1000);
}
Lcd.clear();

String duration = String(effectiveTime / 1000UL);
String distance = String(totalDistance);
Lcd.infoMessage("Uploading result");
delay(500);
client.print(F("POST /fitnessActivities HTTP/1.1\nHost: api.runkeeper.com\nContent-Type: application/vnd.com.runkeeper.NewFitnessActivity+json\nUser-Agent: Arduino/1.0\nAuthorization: "));
client.println(accessToken);
client.print(F("Content-Length: "));
client.println(103 + startTimeStr.length() + distance.length() + duration.length());
client.println();
client.print(F("{\"type\": \"Cycling\", \"equipment\": \"Stationary Bike\", \"start_time\": \""));
client.print(startTimeStr);
client.print(F("\", \"total_distance\": "));
client.print(distance);
client.print(F(", \"duration\": "));
client.print(duration);
client.print("}");
delay(2000);

// Extract status code to see if POST was succesful
// Borrowed from https://github.com/interactive-matter/HTTPClient
Expand All @@ -47,58 +45,53 @@ boolean uploadResult(String startTimeStr, unsigned int totalDistance, unsigned l
char c = '\0';
int statusCode = 0;
int iState = 0;
Serial.println();
while (client.available()) {
c = client.read();
Serial.print(c);
if (c != -1) {
switch(iState) {
case 0:
// We haven't reached the status code yet
if ( (*statusPtr == '*') || (*statusPtr == c) ) {
// This character matches, just move along
statusPtr++;
if (*statusPtr == '\0'){
// We've reached the end of the prefix
iState = 1;
while (client.connected()) {
if (client.available()) {
c = client.read();
Serial.print(c);
if (c != -1) {
switch(iState) {
case 0:
// We haven't reached the status code yet
if ( (*statusPtr == '*') || (*statusPtr == c) ) {
// This character matches, just move along
statusPtr++;
if (*statusPtr == '\0'){
// We've reached the end of the prefix
iState = 1;
}
} else {
Lcd.errorMessage("error");
}
break;
case 1:
if (isdigit(c)) {
// This assumes we won't get more than the 3 digits we
// want
statusCode = statusCode*10 + (c - '0');
} else {
// We've reached the end of the status code
// We could sanity check it here or double-check for ' '
// rather than anything else, but let's be lenient
iState = 2;
}
} else {
Lcd.errorMessage(status_failure);
}
break;
case 1:
if (isdigit(c)) {
// This assumes we won't get more than the 3 digits we
// want
statusCode = statusCode*10 + (c - '0');
} else {
// We've reached the end of the status code
// We could sanity check it here or double-check for ' '
// rather than anything else, but let's be lenient
iState = 2;
}
break;
case 2:
// We're just waiting for the end of the line now
break;
};
break;
/*case 2:*/
/* // We're just waiting for the end of the line now*/
/* break;*/
};
}
}
}
Serial.println();

if (!client.connected()) {
client.stop();
}
/*Lcd.infoMessage("Disconnecting");*/
client.stop();

if (statusCode == 201) {
Lcd.infoMessage(status_data_uploaded);
Lcd.setSecondLine(status_session_created);
Serial.println(status_session_created);
Lcd.infoMessage("Data uploaded");
return true;
} else {
Lcd.errorMessage(status_error_code);
Lcd.errorMessage("Error: ");
Lcd.setSecondLine((String) statusCode);
Serial.print(F("Error code: "));
Serial.println((String) statusCode);
return false;
}
Expand Down
5 changes: 2 additions & 3 deletions src/sketch.ino
Expand Up @@ -121,7 +121,6 @@ void setup() {
// Upload saved session if present
if (savePresent()) {
Lcd.infoMessage("Prev. act. found");
Lcd.setSecondLine("Uploading");
delay(1000);
uploaded = uploadResult(getSavedStartTimeStr(), getSavedDistance(), getSavedTime());
if(uploaded) {
Expand All @@ -134,7 +133,7 @@ void setup() {
Lcd.clear();

// get time
Lcd.setSecondLine("Getting Time...");
/*Lcd.setSecondLine("Getting Time...");*/
setStartTime();
delay(1000);
// Retry if unable to get time from NTP
Expand All @@ -143,7 +142,7 @@ void setup() {
Lcd.errorMessage("Retrying ...");
setStartTime();
}
Serial.print("Time: ");
Serial.print(F("Time: "));
Serial.println(getTimeString());
Lcd.clear();

Expand Down
12 changes: 12 additions & 0 deletions testserver.py
@@ -0,0 +1,12 @@
from flask import Flask, request
app = Flask(__name__)

@app.route("/fitnessActivities", methods=["POST"])
def test():
data = request.stream.read()
app.logger.debug(data)

return "ok"

if __name__ == "__main__":
app.run(host='0.0.0.0', debug=True)

0 comments on commit 960c416

Please sign in to comment.