void tcpKeepAlive() { unsigned long now = millis(); if (tcp.connected()) { if (now > kaTimer) { tcp.flush(); tcpLog("ENQ"); // For logger view tcp.print(ENQ); // Heartbeat signal kaWait = now + 500; kaWaiting = true; tcpKeepAliveReset(); } if (kaWaiting && (kaWait > now)) { if (tcp.available()) { char read = tcp.read(); if (read == ACK) { // tcpLog("ACK"); kaWaiting = false; } } } else if (kaWaiting && kaWait < now) { tcpLog("Timed out"); kaWaiting = false; tcp.flush(); tcp.stop(); } } }
int tcpLog(String message) { if (tcp.connected()) { tcp.print(STX + Spark.deviceID() + ETX + message + EOT); delay(250); // try not to flood socket with too many writes return 1; } else { return -1; } }
char * http_get(char const * hostname, String path) { int i = 0; int j = 0; int k = 0; bool printOnce = false; if (client.connect(hostname, 80)) { client.print("GET "); client.print(path); client.println(" HTTP/1.1"); client.print("HOST: "); client.println(hostname); client.println("Connection: close"); client.println(); client.flush(); } else { if(DEBUG_SERIAL) Serial.println("\r\n\r\nConnection Failed!"); client.flush(); client.stop(); return NULL; } // wait 5 seconds or less for the host to respond uint32_t startTime = millis(); while(!client.available() && (millis() - startTime) < 5000); if(DEBUG_SERIAL) Serial.println("\r\n\r\nREADING HOST DATA......"); uint32_t lastRead = millis(); // If the host doesn't close it's connection, we'll timeout in 10 seconds. while (client.connected() && (millis() - lastRead) < 10000) { while (client.available()) { char c = client.read(); /* if(DEBUG_SERIAL) { Serial.print(c); if(i++ > 100) { delay(5); i = 0; } } */ if(c == -1) { Serial.print("\r\n\r\nERROR......\r\n\r\n"); client.flush(); client.stop(); } if(j++ >= SKIP_CHARS) { // don't buffer the first X bytes to save memory if(DEBUG_SERIAL && !printOnce) { Serial.print("\r\n\r\nSAVING......\r\n\r\n"); printOnce = true; } buffer[k++] = c; // save character to buffer Serial.print(c); delayMicroseconds(100); if(k >= BUFFER_SIZE_MAX) { // if we reach the end of our buffer, just bail. Serial.print("\r\n\r\nOUT OF BUFFER SPACE......\r\n\r\n"); client.flush(); client.stop(); } } // as long as we're reading data, reset the lastRead time. lastRead = millis(); } // END while(client.available()) } // END while(client.connected()) client.flush(); client.stop(); if(DEBUG_SERIAL) { Serial.print("\r\nCHARACTERS RECEIVED: "); Serial.println(SKIP_CHARS + k); } return buffer; }
// Called in a loop forever void loop() { // When you push the button if (digitalRead(D0) == HIGH) { // It might take a couple of loop() iterations to // successfully connect to iamresponding.com connecting = true; // This is how to control the Spark's RGB LED RGB.control(true); // Red to indicate we're attempting to connect RGB.color(255, 0, 0); // Push the button once to respond to station. // Push it again to cancel. if (responding) { // Do just what the iamresponding.com app does data = "{\"keyEntryDesc\":\"CANCELLED\",\"memberIDS\":\"\",\"subscriberID\":\"\",\"userfullname\":\"Jack Bates\"}"; } else { data = "{\"keyEntryDesc\":\"Station\",\"memberIDS\":\"\",\"subscriberID\":\"\",\"userfullname\":\"Jack Bates\"}"; } } // We haven't successfully connected since the button was last // pushed, keep trying! if (connecting && client.connect(server, 80)) // HTTP is port { // number 80, FYI // We successfully connected, stop trying connecting = false; // Yellow to indicate we successfully connected and // sent the magic words RGB.color(255, 255, 0); // Again, just ape the iamresponding.com app client.println("POST /v3/keyEntries.asmx/AddCallEntry HTTP/1.0"); client.print("Content-Length: "); client.println(strlen(data)); client.println("Content-Type: application/json"); client.println("Host: iamresponding.com"); client.println(); // Send the magic words client.print(data); } // iamresponding.com sent something back to *us*. // (This is our only indication that TCPClient is done // transmitting the magic words. You're welcome to get clever // and check if iamresponding.com sent back confirmation or is // reporting an error, this just assumes it's confirmation.) if (client.available()) { // If we were already responding, then now we aren't // (cancelled) and vice versa (if we weren't, then now // we are). responding = !responding; if (responding) { // Blue to indicate that iamresponding.com // knows we're responding to station! RGB.color(0, 0, 255); } else { // Restore the default RGB LED behavior when // we're not responding (cancelled). // (By default it shows the Wi-Fi status.) RGB.control(false); } // Tricky: Clean up after transmitting the magic // words. Close the connection to iamresponding.com // and discard (flush) anything sent back to us, or // else in the next loop() iteration we'll think // iamresponding.com sent back something *again*. client.stop(); client.flush(); } }