I am trying to post a tweet using thingspeak but it is not getting posted. Am I missing something in my POST request or something wrong in my code?
#include <SPI.h>
#include <Ethernet.h>
const byte mac [] = {0x00, 0xC2, 0xA2, 0xE6, 0x3D, 0x57};
const int ledPin = 3;
const char webServer [] = "api.thingspeak.com";
const char webFile [] = "/apps/thingtweet/1/statuses/update";
const char apiKey [] = "xxxxxxxxxxxxxxxx";
EthernetClient myClient;
String msg = "This is my tweet";
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
Serial.println("Connecting to Network");
if (Ethernet.begin(mac) != 1) {
Serial.println("Connection Failed");
while (true) {
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
delay(500);
}
} else {
Serial.println("Connected to Network");
}
// connect to thingstweet server
Serial.print("Connecting to ");
Serial.println(webServer);
if (myClient.connect(webServer, 80) != 1) {
Serial.println("Failed to connect to Thingspeak");
} else {
Serial.println("Sucessfully connected to ThingsSpeak");
myClient.stop();
}
myClient.print("POST ");
myClient.print(webFile);
myClient.println(" HTTP/1.0");
myClient.print("Host: ");
myClient.println(webServer);
myClient.println("Connection: close");
myClient.println("Content-Type: application/x-www-form-urlencoded");
myClient.print("Content-Length: ");
myClient.println(msg.length() + 32);
myClient.println();
//POST some data
myClient.print("api_key=");
myClient.print(apiKey);
myClient.print("&status=");
myClient.println(msg);
}
void loop() {
// put your main code here, to run repeatedly:
}