Arduino Client/Server Help

I want to send data from an arduino to a web page for remote monitoring as well as send data from the web page to arduino for remote control. Is it possible to create both a client and a server in the same Arduino? Also is there any method by which it is possible to control and monitor an Arduino at the same time?

Thanks

The short answer is a conditional yes. The ethernet shield will run a server and client concurrently. It will service only one at a time. Your best bet is to use a client on the Arduino.

I use UDP for high speed duplex communication.

Suppose I use it as a client, can you give an example in which I can use it to send the data to the server as well as receive data from the server using a GET/POST request?
Thanks.

You can't "send data to a web page". You can have the Arduino act as a server and server up something that a web browser renders as a web page.

Suppose I use it as a client, can you give an example in which I can use it to send the data to the server as well as receive data from the server using a GET/POST request?

There is an example in the ethernet folder. Why didn't you look at them first?

You can send data to the server with a GET or POST request, then parse the response for the return data from the server.

Here is an example I did a few days back for another member..

It sends data to a (local) server...

A .php script handles the parsing of the data.. and inserts it into a database.

Then returns some text to the Arduino...

(the .php script does some other stuff.. like checking the timestamp entry to another in the database..etc.. but you can ignore or rip that part out)

http://forum.arduino.cc/index.php?topic=396991.msg2737595#msg2737595

Side note:

  • I am really interested in learning more about what SurferTim mentions about the ethernet shield running both server & client concurrently..

as I think that is what the OP (in the link I provided) was really after... but I wasnt clear about how to flip/flop from one to another..

So taking SurferTim's comment..

is this possible then.

  • Arduino HOSTs an HTML 'web page'..
  • Some other components attached to the Arduino (ie: a switch)
  • Attached switch is pressed, Arduino 'reaches out' (as a client a this point) to some .php script to dump some data to..
  • PHP scripts parses data.. (dumps to database..or whatever it needs to do).. and returns some value/response back to the Arduino..
  • The Arduino (which is serving a webpage to a browser).. receives this response from the .php script.. and then.. updates its locally hosted webpage? to either re-direct to a new page?..or just update to display the result of the returned response?

In use:

I take my phone/pc (whatever) connect to the Arduino.. I see the locally hosted page.. with some HTML/CSS buttons..etc.. I click one of the buttons..

The Arduino sends some data out to a .php script.. .php script does what it needs to do.. and sends a response back to the Arduino...

The page that I am viewing/displaying in my browser... then receives this response and updates the current page or re-directs to a new page??

Last question:

How much of the full stack does the Ethernet shield support? HTML? CSS? Javascript? PHP? <-- (assumming not PHP!?)

The rest should just be 'browser' dependent.. yes? (and not have any bearing on the Arduino...correct?)

(where can I read up more about this?)

thanks.

Side note:

  • I am really interested in learning more about what SurferTim mentions about the ethernet shield running both server & client concurrently..

zoomkat occasionally posts code where the Arduino is both a client and a server. You might look for it.

zoomkat got most of his code from me. :wink: Here is a client server code example. Change the network and server settings. Note I use 115200 baud.

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip( 192,168,2, 2 );
IPAddress gateway( 192,168,2,1 );
IPAddress subnet( 255,255,255,0 );
IPAddress dnServer( 192,168,2,1 );
IPAddress myserver(192,168,1,253);

EthernetServer server(80);

void setup()
{
  Serial.begin(115200);
  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);
  Ethernet.begin(mac, ip, dnServer, gateway, subnet);
  delay(2000);
  server.begin();
  Serial.println("Ready");
}

void loop()
{
  while(Serial.available())
  {
    char c = Serial.read();
  
    if(c == 'e')
    {
      sendGET();
    }
  }
 
  EthernetClient client = server.available();
  if(client) {
    boolean currentLineIsBlank = true;
    Serial.println("Client");
    
    while (client.connected()) {
      while(client.available()) {
        char c = client.read();
        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response
          while(client.available()) client.read();
          
          Serial.println("Sending response");
          client.write("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n<html><body><H1>TEST</H1></body></html>\r\n\r\n");
          client.stop();
        }
        else if (c == '\n') {
          currentLineIsBlank = true;
        } 
        else if (c != '\r') {
          currentLineIsBlank = false;
        }
      }
    }
    Serial.println("done");
  }
}

void sendGET() //client function to send/receie GET request data.
{
  EthernetClient client;
  
  if (client.connect(myserver, 80)) {
    Serial.println("connected");
    client.write("GET / HTTP/1.0\r\n\r\n");
  } 
  else {
    Serial.println("connection failed");
    return;
  }

  while(client.connected()) {
    while(client.available()) { //connected or data available
    char c = client.read();
    Serial.print(c);
    }
  }
  client.stop();
  
  Serial.println("disconnected");
}

It is "Perfect World" code. It doesn't have the timeout or most other fault tolerance and error checking features. You can add those later.

edit: If you want to take a look at my "Real World" server code, you can find it here.
http://playground.arduino.cc/Code/WebServerST
It serves HTML, CSS, JavaScript, and most graphics format files, including favicon.ico. Very limited server side processing. No PHP.

I have a similar problem. I ahve a program on my laptop that outputs a string of data to a specified ip address. I can use the following code on a Wifi Rev 2 to read this data and print it on the serial monitor

/*
  WiFi Web Server

 
 created 13 July 2010
 by dlf (Metodo2 srl)
 modified 31 May 2012
 by Tom Igoe

 */

#include <SPI.h>
#include <WiFiNINA.h>
#include "arduino_secrets.h" 

IPAddress ip(192,168,0,200);
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID;        // your network SSID (name)
char pass[] = SECRET_PASS;    // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0;                 // your network key Index number (needed only for WEP)

int status = WL_IDLE_STATUS;

WiFiServer server(80);

void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // check for the WiFi module:
  if (WiFi.status() == WL_NO_MODULE) {
    Serial.println("Communication with WiFi module failed!");
    // don't continue
    while (true);
  }
WiFi.config(ip);
  String fv = WiFi.firmwareVersion();
  if (fv < "1.0.0") {
    Serial.println("Please upgrade the firmware");
  }

  // attempt to connect to Wifi network:
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);

    // wait 10 seconds for connection:
    delay(10000);
  }
  server.begin();
  // you're connected now, so print out the status:
  printWifiStatus();
}


void loop() {
  // listen for incoming clients
  WiFiClient client = server.available();
  if (client) {
    Serial.println("new client");
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.write(c);
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connection: close");  // the connection will be closed after completion of the response
          client.println("Refresh: 5");  // refresh the page automatically every 5 sec
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
       
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        } else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);

    // close the connection:
    client.stop();
    Serial.println("client disonnected");
  }
}


void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your board's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

What I need to do is to make this string of data available to be read by other Arduinos on different ip addresses via WiFi.

Can anyone help?

Thanks