Skip to content

Instantly share code, notes, and snippets.

@hak8or
Created June 28, 2015 19:48
Show Gist options
  • Save hak8or/9c92efa3c9a01ad8bb40 to your computer and use it in GitHub Desktop.
Save hak8or/9c92efa3c9a01ad8bb40 to your computer and use it in GitHub Desktop.
Del me
/**
* @brief Sends a peice of data over TCP to our backend.
* @details Does a DNS lookup for our backend, sets up a TCP connection, and sends our data.
*
* @param An enum for if we want to send our data over Wireless or Wired.
*/
void Network::send_packet(Network::Interface interface){
if (interface == Wired) {
// https://api.thingspeak.com/update?key=8ZISX29L64E3ZDHW&field1=0
Stash stash;
byte sd = stash.create();
stash.print("field1=3");
stash.save();
// generate the header with payload - note that the stash size is used,
// and that a "stash descriptor" is passed in as argument using "$H"
Stash::prepare(
PSTR("POST /update HTTP/1.1\n"
"Host: api.thingspeak.com\n"
"Connection: close\n"
"X-THINGSPEAKAPIKEY: 8ZISX29L64E3ZDHW\n"
"Content-Type: application/x-www-form-urlencoded\n"
"Content-Length: $D\n\n"
"$H"),
stash.size(), sd);
// send the packet - this also releases all stash buffers once done
byte session = ether.tcpSend();
Serial.println("Send TCP packet to thingspeak!");
while(1){
ether.packetLoop(ether.packetReceive());
const char* reply = ether.tcpReply(session);
if (reply != 0) {
Serial.println("Got a response!");
Serial.println(reply);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment