Yes I manage to do the webserver and tweet but separately.
Here is the code I use to send tweet. I am using thingSpeak API key.
#include <SPI.h>
#include <Ethernet.h>
// Local Network Settings
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// ThingSpeak Settings
char thingSpeakAddress[] = "api.thingspeak.com";
String thingtweetAPIKey = "MYAPIKEY "; // here I put my API key from thingSpeak
// Variable Setup
long lastConnectionTime = 0;
boolean lastConnected = false;
int failedCounter = 0;
int tweetID = 0;
String tweetThis = "";
// Initialize Arduino Ethernet Client
EthernetClient client;
void setup()
{
// Start Serial for debugging on the Serial Monitor
Serial.begin(9600);
startEthernet();
delay(1000);
// Update Twitter via ThingTweet
tweetID = random(100);
tweetThis += "Hello. #";
tweetThis += tweetID;
Serial.println(tweetID);
updateTwitterStatus(tweetThis);
}
void loop()
{
// Print Update Response to Serial Monitor
if (client.available()){
char c = client.read();
Serial.print(c);
}
// Disconnect from ThingSpeak
if (!client.connected() && lastConnected){
Serial.println("...disconnected");
Serial.println();
client.stop();
}
// Check if Arduino Ethernet needs to be restarted
if (failedCounter > 3 ) {startEthernet();}
lastConnected = client.connected();
}
void updateTwitterStatus(String tsData)
{
if (client.connect(thingSpeakAddress, 80)){
// Create HTTP POST Data
tsData = "api_key="+thingtweetAPIKey+"&status="+tsData;
client.print("POST /apps/thingtweet/1/statuses/update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(tsData.length());
client.print("\n\n");
client.print(tsData);
lastConnectionTime = millis();
if (client.connected()){
Serial.println("Connecting to ThingSpeak...");
Serial.println();
failedCounter = 0;
}
else{
failedCounter++;
Serial.println("Connection to ThingSpeak failed ("+String(failedCounter, DEC)+")");
Serial.println();
}
}
else
{
failedCounter++;
Serial.println("Connection to ThingSpeak Failed ("+String(failedCounter, DEC)+")");
Serial.println();
lastConnectionTime = millis();
}
}
void startEthernet()
{
client.stop();
Serial.println("Connecting Arduino to network...");
Serial.println();
delay(1000);
// Connect to network amd obtain an IP address using DHCP
if (Ethernet.begin(mac) == 0)
{
Serial.println("DHCP Failed, reset Arduino to try again");
Serial.println();
}
else
{
Serial.println("Arduino connected to network using DHCP");
Serial.println();
}
delay(1000);
}
This is the code of my failed attempt to combine them. I comment out few parts, so just the web server are working.
#include <SPI.h>
#include <Ethernet.h>
EthernetServer server (80);
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = {10, 100, 156, 61};
byte gateway[] = {10, 100, 156, 62};
byte subnet[] = { 255, 255, 255, 192 };
// ThingSpeak Settings
char thingSpeakAddress[] = "api.thingspeak.com";
String thingtweetAPIKey = "MYAPIKEY "; // here I put my API key from thingSpeak
int piezoPin = 9;
String inString = String (100);
// Variable Setup
String tweetThis = "";
boolean lastConnected = false;
long lastConnectionTime = 0;
int prevID=0, tweetID = 0, failedCounter=0;
//FUNCTION DECLARATIONS
void header(EthernetClient);
void body(EthernetClient);
void footer(EthernetClient);
//tweet function
void startTweeting();
void startEthernet();
EthernetClient clientTweet;
void setup(){
Serial.begin(9600);
//start the Ethernet connection and the server:
Ethernet.begin(mac, ip, gateway, subnet);
server.begin();
pinMode(piezoPin,OUTPUT);
}
void loop(){
EthernetClient client = server.available(); //listen for incoming clients
if(client){
Serial.println("Got a client");
Serial.print(inString);
boolean current_line_is_blank = true;
while(client.connected()){
if(client.available()){
char c = client.read();
if (inString.length() < 100) {
inString.concat(c);
}
//then the request is over:
if (c == '\n' && current_line_is_blank){
//send a standard http response header
header(client);
body(client);
footer(client);
break;
}
if (c == '\n') {
// we're starting a new line
current_line_is_blank = true;
}
else if (c != '\r') {
current_line_is_blank = false; // we've gotten a character on the current line
}
}//if
}//while
delay(1);
client.stop(); //close the connection
inString = "";
}
//startTweeting();
} // loop - end
void startTweeting(){
do{
tweetID = random(1000);
}while(prevID==tweetID);
tweetThis = "Hello ";
tweetThis += "#"; tweetThis += tweetID;
startEthernet();
delay(1000);
updateTwitterStatus(tweetThis);
}
void startEthernet(){
}
void updateTwitterStatus(String tsData){
if (clientTweet.connect(thingSpeakAddress, 80))
{
// Create HTTP POST Data
tsData = "api_key="+thingtweetAPIKey+"&status="+tsData;
clientTweet.print("POST /apps/thingtweet/1/statuses/update HTTP/1.1\n");
clientTweet.print("Host: api.thingspeak.com\n");
clientTweet.print("Connection: close\n");
clientTweet.print("Content-Type: application/x-www-form-urlencoded\n");
clientTweet.print("Content-Length: ");
clientTweet.print(tsData.length());
clientTweet.print("\n\n");
clientTweet.print(tsData);
lastConnectionTime = millis();
if (clientTweet.connected()){
Serial.println("Connecting to ThingSpeak...");
Serial.println();
failedCounter = 0;
}
else{
failedCounter++;
Serial.println("Connection to ThingSpeak failed ("+String(failedCounter, DEC)+")");
Serial.println();
}
}
else{
failedCounter++;
Serial.println("Connection to ThingSpeak Failed ("+String(failedCounter, DEC)+")");
Serial.println();
lastConnectionTime = millis();
}
} // updateTwitterStatus - end
void header(EthernetClient thisClient){
thisClient.print("HTTP/1.1 200 OK\n");
thisClient.print("Content-Type: text/html\n\n");
thisClient.print("<head><title>project 238</title></head><body>\n");
}
void footer(EthernetClient thisClient){
thisClient.print("</body></html>\n");
}
void body(EthernetClient thisClient){
thisClient.println("<p>NSMF</p>");
thisClient.println(F("<form method=\"get\">"));
thisClient.println(F(" <fieldset>"));
thisClient.println(F(" <legend>Send tweet</legend>"));
thisClient.println(F(" <input type=\"hidden\" name=\"tweet\" value=\"on\">
"));
thisClient.println(F(" <button type=\"submit\">Tweet</button>"));
thisClient.println(F(" </fieldset>"));
thisClient.println(F("</form>"));
thisClient.println(F(""));
thisClient.println(F(""));
}