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?
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.
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?
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?)
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.