Hi,
This is probably a simple one when you know what your doing or can dig into the library code in the ethernet files but I am having difficulty in figuring out how to switch between my ethernet board being a server then switching to a client and back again.
I have an embedded application that allows me to switch lights on and off & display the temperature of my solar hot water system. The web side of it runs on an ethermega and as it serves my pages, embedded tokens get filled in by the real-world data (via RS485 & RFM12 links). This all works OK.
Now what I would like to do is from time-to-time, send some of the temperature data to Pachube but I cant figure out how to stop the board being a server and switch to a client to allow the Pachube stuff, then back to a server again.
Any help with this would be appreciated.
I am not a 'C' programmer but bit by painful bit I'm getting less worse. Usually I write embedded stuff in assembly but as Arduino seemed the way to go for the web-interface side, I'm slowly trying to get there....
combined client/server code that worked with the 0022 IDE. You would need to up date it for use with the 1.0 ide.
//zoomkat 11-25-11
//simple button GET with iframe code
//for use with IDE 0021
//open serial monitor and send an e to test client and
//open serial monitor to see what the arduino receives
//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
byte ip[] = { 192, 168, 1, 102 }; // ip in lan
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
byte myserver[] = { 208, 104, 2, 86 }; // zoomkat web page
Server server(84); //server port
Client client(myserver, 80);
String readString;
//////////////////////
void setup(){
pinMode(5, OUTPUT); //pin selected to control
Ethernet.begin(mac, ip, gateway, subnet);
server.begin();
Serial.begin(9600);
Serial.println("server/client test 11/25/11"); // so I can keep track of what is loaded
}
void loop(){
// check for serial input
if (Serial.available() > 0)
{
byte inChar;
inChar = Serial.read();
if(inChar == 'e')
{
sendGET(); // call sendGET function
}
}
Client 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 Zoomkat");
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 GET test page</TITLE>");
client.println("</HEAD>");
client.println("<BODY>");
client.println("<H1>Zoomkat's simple Arduino button</H1>");
client.println("<a href=\"/?on\" target=\"inlineframe\">ON</a>");
client.println("<a href=\"/?off\" target=\"inlineframe\">OFF</a>");
//client.println("<IFRAME name=inlineframe src=\"res://D:/WINDOWS/dnserror.htm\" width=1 height=1\">");
client.println("<IFRAME name=inlineframe style=\"display:none\" >");
client.println("</IFRAME>");
client.println("</BODY>");
client.println("</HTML>");
}
delay(1);
//stopping client
client.stop();
///////////////////// control arduino pin
if(readString.indexOf("on") >0)//checks for on
{
digitalWrite(5, HIGH); // set pin 4 high
Serial.println("Led On");
}
if(readString.indexOf("off") >0)//checks for off
{
digitalWrite(5, LOW); // set pin 4 low
Serial.println("Led Off");
}
//clearing string for next read
readString="";
}
}
}
}
}
//////////////////////////
void sendGET() //client function to send/receie GET request data.
{
if (client.connect()) {
Serial.println("connected");
client.println("GET /~shb/arduino.txt HTTP/1.0");
client.println();
}
else {
Serial.println("connection failed");
Serial.println();
}
while(client.connected() && !client.available()) delay(1); //waits for data
while (client.connected() || client.available()) { //connected or data available
char c = client.read();
Serial.print(c);
}
Serial.println();
Serial.println("disconnecting.");
Serial.println("==================");
Serial.println();
client.stop();
}