Hardware:
- Arduino Mega 2560 Rev3
- Ethernet Shield Rev3
- A sensor
Goal:
- Take value from sensor, upload it to ThingSpeak.
- Host a web page on the Arduino.
Question:
I've done both things in the past. But now I want to upload the data to ThingSpeak and host a page on the board at the same time , and it doesn't seem to work. Should I try to make two clients? I'm really new two the whole networking stuff.
I'm basically trying to stitch up ThingSpeak's example code with Zoomkat's client/server code. Here is what I'm trying. The webpage does work, but it doesn't upload to ThingSpeak for some reason.
//zoomkat 12-08-11, combined client and server
//simple button GET with iframe code
//for use with IDE 1.0
//open serial monitor and send an g to test client and
//see what the arduino client/server receives
//web page buttons make pin 4 high/low
//use the \ slash to escape the " in the html
//address will look like http://192.168.1.102:84 when submited
//for use with W5100 based ethernet shields
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
IPAddress ip(192, 168, 0, 197); // ip in lan
IPAddress gateway(192, 168, 0, 1); // internet access via router
IPAddress subnet(255, 255, 255, 0); //subnet mask
// ThingSpeak Settings
char thingSpeakAddress[] = "api.thingspeak.com";
String writeAPIKey = "2M6SFMKICXR7MU1A";
const int updateThingSpeakInterval = 4 * 1000;
// Variable Setup
long lastConnectionTime = 0;
boolean lastConnected = false;
int failedCounter = 0;
EthernetServer server(84); //server port
EthernetClient client;
String readString;
void setup() {
pinMode(4, OUTPUT); //pin selected to control
Ethernet.begin(mac, ip, subnet, gateway);
server.begin();
Serial.begin(9600);
Serial.println("server/client 1.0 test 12/08/11");
// keep track of what is loaded
Serial.println("Send an g in serial monitor to test client");
// what to do to test client
}
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();
}
if(!client.connected() && (millis() - lastConnectionTime > updateThingSpeakInterval)) {
Serial.println("sendPost function started");
sendPost();
}
EthernetClient client = server.available();
if (client) {
while (client.connected()) {
if (client.available()) {
char c = client.read();
//read char by char HTTP request
if (readString.length() < 100) {
//store characters to string
readString += c;
//Serial.print(c);
}
//if HTTP request has ended
if (c == '\n') {
Serial.println(readString);
//print to serial monitor for debuging
//now output HTML data header
if (readString.indexOf('?') >= 0) { //don't send new page
client.println("HTTP/1.1 204 ");
client.println();
client.println();
} else {
client.println("HTTP/1.1 200 OK"); //send new page
client.println("Content-Type: text/html");
client.println();
client.println("<HTML>");
client.println("<HEAD>");
client.println("<TITLE>Arduino test page</TITLE>");
client.println("</HEAD>");
client.println("<BODY>");
client.println("<H1>THIS IS A TEST</H1>");
client.println("</BODY>");
client.println("</HTML>");
}
delay(1);
//stopping client
client.stop();
readString = "";
}
}
}
}
}
void sendPost() {
Serial.println("function initiated");
client.stop();
String tsData = String(analogRead(A0), DEC);
if (client.connect(thingSpeakAddress, 80)) {
client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: " + writeAPIKey + "\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("field1=");
//// is this the prob?
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();
}
}