I must have become kind of blind as I'm making circles for a couple of hours now trying to find out what I did wrong. I've tried on the command line using cURL and I know that the service and my account do work. It's just my cpp code that must be buggeous.
connecting to server...
making HTTP request...
Connection: close
And now here's my full code, it is based on the standard Arduino sample "TwitterClient":
#include <SPI.h>
#include <WiFi.h>
#define USERAGENT "Arduino" // user agent is the project name
char ssid[] = "frgre_valtechpublic"; // your network SSID (name)
char pass[] = "_WIFIPASSWORDHERE_"; // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0; // your network key Index number (needed only for WEP)
int status = WL_IDLE_STATUS; // status of the wifi connection
// initialize the library instance:
WiFiClient client;
const unsigned long requestInterval = 60*1000; // delay between requests; 30 seconds
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
boolean requested; // whether you've made a request since connecting
unsigned long lastAttemptTime = 0; // last time you connected to the server, in milliseconds
String currentLine = ""; // string to hold the text from server
String tweet = ""; // string to hold the tweet
boolean readingTweet = false; // if you're currently reading the tweet
void setup() {
// reserve space for the strings:
currentLine.reserve(2048);
tweet.reserve(150);
//Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
// don't continue:
while(true);
}
// attempt to connect to Wifi network:
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(6000);
}
// you're connected now, so print out the status:
printWifiStatus();
connectToServer();
}
void loop()
{
if (client.connected()) {
if (client.available()) {
// read incoming bytes:
char inChar = client.read();
// add incoming byte to end of line:
currentLine += inChar;
// if you get a newline, clear the line:
if (inChar == '\n') {
Serial.println(currentLine);
currentLine = "";
}
}
}
else if (millis() - lastAttemptTime > requestInterval) {
// if you're not connected, and two minutes have passed since
// your last connection, then attempt to connect again:
connectToServer();
}
}
void connectToServer() {
// I guess the problem must come from here, I'm unsure which input format is right for a status update
// String tweetData = "{\"status\":%20\"Hello%20twitter%20from%20Arduino%20UNO!\"}";
String tweetData = "status=Hello%20twitter%20from%20Arduino%20UNO";
// attempt to connect, and wait a millisecond:
Serial.println("connecting to server...");
if (client.connect(server, 80)) {
Serial.println("making HTTP request...");
// make HTTP GET request to twitter:
client.println("POST /1.1/statuses/update.json HTTP/1.1");
client.println("Authorization: Basic _MYBASE64CREDENTIALSHERE_");
client.print("Content-Length: ");
client.println(tweetData.length());
client.print("User-Agent: ");
client.println(USERAGENT);
client.println("Connection:close");
client.println("");
client.println(tweetData);
}
// note the time of this connect attempt:
lastAttemptTime = millis();
}
void printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}