Go Down

Topic: Switching between ethernet client & server (Read 1 time) previous topic - next topic


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.

Code: [Select]

//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 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);
  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;

        //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");
          else {
            client.println("HTTP/1.1 200 OK"); //send new page
            client.println("Content-Type: text/html");

            client.println("<TITLE>Arduino GET test page</TITLE>");

            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\" >");         


          //stopping client

          ///////////////////// 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


void sendGET() //client function to send/receie GET request data.
  if (client.connect()) {
    client.println("GET /~shb/arduino.txt HTTP/1.0");
  else {
    Serial.println("connection failed");

  while(client.connected() && !client.available()) delay(1); //waits for data
  while (client.connected() || client.available()) { //connected or data available
    char c = client.read();



Google forum search: Use Google Search box in upper right side of this page.
Why I like my 2005 Rio Yellow Honda S2000  https://www.youtube.com/watch?v=pWjMvrkUqX0

Go Up