I'm struggling to get the CC3000 to connect to a heroku hosted app, and other examples posted online haven't turned on the light bulb quite yet for me.
The gist is that I collect some temperature and humidity readings, and then send those via a GET request using the CC3000. The heroku app then reads the parameters and displays them on a page.
The CC3000 hardware is A-OK, as I've tested it using the test sketches. The sketch I've put together (on github: https://github.com/jsimms/compost_monitor) works except for the last bit where it needs to connect to the server and send the HTTP request. Which makes me wonder if this is a heroku quirk, or if I'm just screwing something up right at the finish line.
First, I define everything
#define WEBSITE "myapp.herokuapp.com"
String route = "/sensor";
uint32_t ip;
int port = 80;
Then during setup, I use getHostByName to assign the ip variable. I print it out, just to confirm it was able to snag an ip. I then close it to make sure I don't screw up anything during the loop.
ip = 0;
Serial.print(WEBSITE); Serial.print(F(" -> "));
while (ip == 0) {
if (! cc3000.getHostByName(WEBSITE, &ip)) {
Serial.println(F("Couldn't resolve!"));
}
delay(500);
}
cc3000.printIPdotsRev(ip);
Serial.println("");
// Close the connection
Serial.println(F("Closing connection..."));
cc3000.disconnect();
Serial.println(F("Connection closed."));
During the loop, I create a String with the request, and then call a function that actually does the request.
String request = "GET " + route + "?temp=" + temperature + "&hum=" + humidity + " HTTP/1.0\r\n";
send_request(request);
Finally, the send_request function is supposed to take care of connecting via TCP and sending it.
void send_request (String request)
{
// Connect to server
Serial.println("Connecting to server...");
Adafruit_CC3000_Client www = cc3000.connectTCP(ip, port);
// Send the request
if (www.connected()) {
www.println(request);
www.println(F("User-agent: CompostMonitor/1.0\r\n"));
www.println(F("\r\n"));
Serial.println("Connected & data sent successfully...");
}
else {
Serial.println(F("Connection failed."));
}
while (www.connected()) {
while (www.available()) {
// Read answer
char c = www.read();
Serial.println(c);
}
}
// Disconnect
Serial.println("Closing connection...");
Serial.println("");
www.close();
}
However, I never get anything but connection failed.
Any insight and help would be wonderfully appreciated. :)
UPDATE: Ok. So, through the comments below, I was able to identify by telnet'ing the web app, that there was something wrong with the HTTP requests. I made a small tweak to the app, but the real issue was that I wasn't including Host: weathervane.herokuapp.com in the header. FML! I'm going to test out sending the request with the Arduino later tonight. Hopefully that puts an end to this HTTP 101 lesson I've been doing.