I was wondering if anyone had any knowledge on how to send a number from the Arduino Ethernet to a text box that is on a webpage.
The webpage on a local networked computer, the Raspberry Pi, with a LAMP server hosting Wordpress.
The idea behind it is being able to press a submit button on my webpage, that initiates a IF statement on the arduino, which then posts a numeric value into the text box, on the webpage. While I do this, I do not want the page to refresh.
I have been looking around for the past week and cannot find a proper solution. Upon searching, I mostly saw how one would use the arduino as a webserver, but I already have a server. I just need to send a value to my webpages text input. Any help would be very appreciated.
Below is some combined client/server test code. To not refresh the main web page, an Iframe might be used. The below site might have useful info.
//zoomkat 10-02-14, combined client and server
//simple button GET with iframe code
//for use with IDE 1.0
//open serial monitor and send a g to test client GET and
//see what the arduino client/server receives
//web page buttons make pins high/low
//use the ' in html instead of " to prevent having to escape the "
//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 }; //assign arduino mac address
byte ip[] = {192, 168, 1, 102 }; // ip in lan assigned to arduino
byte gateway[] = {192, 168, 1, 1 }; // internet access via router
byte subnet[] = {255, 255, 255, 0 }; //subnet mask
EthernetServer server(84); //server port arduino server will use
EthernetClient client;
char serverName[] = "checkip.dyndns.com"; // (DNS) dyndns web page server
//byte serverName[] = { 208, 104, 2, 86 }; // (IP) zoomkat web page server IP address
String readString; //used by server to capture GET request
//////////////////////
void setup(){
pinMode(5, OUTPUT); //pin selected to control
pinMode(6, OUTPUT); //pin selected to control
pinMode(7, OUTPUT); //pin selected to control
pinMode(8, OUTPUT); //pin selected to control
//pinMode(5, OUTPUT); //pin 5 selected to control
Ethernet.begin(mac,ip,gateway,gateway,subnet);
server.begin();
Serial.begin(9600);
Serial.println(F("server/client 1.0 test 9/02/14")); // keep track of what is loaded
Serial.println(F("Send a g in serial monitor to test client")); // what to do to test client
}
void loop(){
// check for serial input
if (Serial.available() > 0)
{
byte inChar;
inChar = Serial.read();
if(inChar == 'g')
{
sendGET(); // call client sendGET function
}
}
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.print(readString); //print to serial monitor for debuging
//now output HTML data header
if(readString.indexOf('?') >=0) { //don't send new page
client.println(F("HTTP/1.1 204 Zoomkat"));
client.println();
client.println();
}
else {
client.println(F("HTTP/1.1 200 OK")); //send new page on browser request
client.println(F("Content-Type: text/html"));
client.println();
client.println(F("<HTML>"));
client.println(F("<HEAD>"));
client.println(F("<TITLE>Arduino GET test page</TITLE>"));
client.println(F("</HEAD>"));
client.println(F("<BODY>"));
client.println(F("<H1>Zoomkat's simple Arduino 1.0 button</H1>"));
// DIY buttons
client.println(F("Pin5"));
client.println(F("<a href=/?on2 target=inlineframe>ON</a>"));
client.println(F("<a href=/?off3 target=inlineframe>OFF</a>
"));
client.println(F("Pin6"));
client.println(F("<a href=/?on4 target=inlineframe>ON</a>"));
client.println(F("<a href=/?off5 target=inlineframe>OFF</a>
"));
client.println(F("Pin7"));
client.println(F("<a href=/?on6 target=inlineframe>ON</a>"));
client.println(F("<a href=/?off7 target=inlineframe>OFF</a>
"));
client.println(F("Pin8"));
client.println(F("<a href=/?on8 target=inlineframe>ON</a>"));
client.println(F("<a href=/?off9 target=inlineframe>OFF</a>
"));
client.println(F("Pins"));
client.println(F(" <a href=/?off2468 target=inlineframe>ALL ON</a>"));
client.println(F(" <a href=/?off3579 target=inlineframe>ALL OFF</a>"));
client.println(F("<IFRAME name=inlineframe style='display:none'>"));
client.println(F("</IFRAME>"));
client.println(F("</BODY>"));
client.println(F("</HTML>"));
}
delay(1);
//stopping client
client.stop();
///////////////////// control arduino pin
if(readString.indexOf('2') >0)//checks for 2
{
digitalWrite(5, HIGH); // set pin 5 high
Serial.println(F("Led 5 On"));
Serial.println();
}
if(readString.indexOf('3') >0)//checks for 3
{
digitalWrite(5, LOW); // set pin 5 low
Serial.println(F("Led 5 Off"));
Serial.println();
}
if(readString.indexOf('4') >0)//checks for 4
{
digitalWrite(6, HIGH); // set pin 6 high
Serial.println(F("Led 6 On"));
Serial.println();
}
if(readString.indexOf('5') >0)//checks for 5
{
digitalWrite(6, LOW); // set pin 6 low
Serial.println(F("Led 6 Off"));
Serial.println();
}
if(readString.indexOf('6') >0)//checks for 6
{
digitalWrite(7, HIGH); // set pin 7 high
Serial.println(F("Led 7 On"));
Serial.println();
}
if(readString.indexOf('7') >0)//checks for 7
{
digitalWrite(7, LOW); // set pin 7 low
Serial.println(F("Led 7 Off"));
Serial.println();
}
if(readString.indexOf('8') >0)//checks for 8
{
digitalWrite(8, HIGH); // set pin 8 high
Serial.println(F("Led 8 On"));
Serial.println();
}
if(readString.indexOf('9') >0)//checks for 9
{
digitalWrite(8, LOW); // set pin 8 low
Serial.println(F("Led 8 Off"));
Serial.println();
}
//clearing string for next read
readString="";
}
}
}
}
}
//////////////////////////
void sendGET() //client function to send and receive GET data from external server.
{
if (client.connect(serverName, 80)) {
Serial.println(F("connected"));
client.println(F("GET / HTTP/1.1"));
client.println(F("Host: checkip.dyndns.com"));
client.println(F("Connection: close"));
client.println();
}
else {
Serial.println(F("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(); //gets byte from ethernet buffer
readString += c; //places captured byte in readString
}
//Serial.println();
client.stop(); //stop client
Serial.println(F("client disconnected."));
Serial.println(F("Data from server captured in readString:"));
Serial.println();
Serial.print(readString); //prints readString to serial monitor
Serial.println();
Serial.println(F("End of readString"));
Serial.println(F("=================="));
Serial.println();
readString=""; //clear readString variable
}
Im sorry, but I honestly do not understand how to convert this code to let me send a value to a server that already exists on my network.
Please keep in mind that I HAVE been researching this for the past couple hours, and still cannot determine what I might need to do. It may seem trivial to you, but It is very complicated and frustrating to me lol.
To make this simpler and maybe easier for me to understand...How would I update a single textbox on a single webpage, we will forget about the button for now:
To say the the value of '5' with the arduino. And we can pretend the ip address of the server is "192.168.0.25" and the Arduino has an ip of "192.168.0.35"
I am thinking that I will have to use the POST command to do this, but I do not know what I am supposed to post this too. The text box's ID is id="textbox" so I know I will have to refer to this id.
I hope I am not frustrating anyone with since less questions.
Bottom is some older client test code that uses an IP address like you would use on your LAN.
How would I update a single textbox on a single webpage
You "update" a text box on a web page by typing stuff in text box, then send. You are probably talking about updating text on a web page, which is a different operation that is performed by your server.
//zoomkat 12-01-11
//simple client test
//for use with IDE 0021
//open serial monitor and send an e to test
//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 assigned to arduino
//byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
//byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
byte myserver[] = { 208, 104, 244, 25 }; // zoomkat web page server IP address
EthernetClient client; // connect to web server using port 80
//////////////////////
void setup(){
Ethernet.begin(mac);
//Ethernet.begin(mac, ip);
//Ethernet.begin(mac, ip, gateway, subnet);
Serial.begin(9600);
Serial.println("Better client test 12/01/11"); // so I can keep track of what is loaded
Serial.println("Send an e in serial monitor to test"); // what to do to test
}
void loop(){
// check for serial input
if (Serial.available() > 0) //if something in serial buffer
{
byte inChar; // sets inChar as a byte
inChar = Serial.read(); //gets byte from buffer
if(inChar == 'e') // checks to see byte is an e
{
sendGET(); // call sendGET function below when byte is an e
}
}
}
//////////////////////////
void sendGET() //client function to send/receive GET request data.
{
if (client.connect(myserver, 80)) { //starts client connection, checks for connection
Serial.println("connected");
client.println("GET /~shb/arduino.txt HTTP/1.1"); //download text
client.println("Host: web.comporium.net");
client.println("Connection: close"); //close 1.1 persistent connection
client.println(); //end of get request
}
else {
Serial.println("connection failed"); //error message if no client connect
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(); //gets byte from ethernet buffer
Serial.print(c); //prints byte to serial monitor
}
Serial.println();
Serial.println("disconnecting.");
Serial.println("==================");
Serial.println();
client.stop(); //stop client
}