Have found in the forum an example of a webpage on an Arduino displaying four
pushbuttons , that I have modified a little , and it works fine . But then I wanted
to include a potentiometer reading in the scetch and had it´s value displayed (ReadValue)
on that web-page , but not succeeded ...have tried to find a solution among
other examples ... but no luck ...
here is the code :
/* Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
Potentiometer to A0
*/
#include <EtherCard.h>
// Ethernet Part declaration
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
#define BUFFER_SIZE 800
BufferFiller bfill;
byte Ethernet::buffer[BUFFER_SIZE];
static byte myip[] = {192,168,0,2};
// Ethernet Part ends here
// Diodepins definition
const int ledPin1 = 3;
const int ledPin2 = 4;
const int ledPin3 = 5;
//Volt meter start
//Analog volt read pin
const int voltPin = 0;
float Readvalue;
void setup () {
/* sök efter ip-adress till Arduinon */
Serial.begin(9600);
Serial.println("\n[Starting the Arduino Server]");
Serial.print("MAC: ");
for (byte i = 0; i < 6; ++i) {
Serial.print(mymac[i], HEX);
if (i < 5)
Serial.print(':');
}
Serial.println();
if (ether.begin(sizeof Ethernet::buffer, mymac) == 0)
Serial.println( "Failed to access Ethernet controller");
Serial.println("Setting up DHCP (this can take a while...)");
if (!ether.dhcpSetup())
Serial.println( "DHCP failed");
ether.printIp("IP: ", ether.myip);
ether.printIp("Netmask: ", ether.mymask);
ether.printIp("GW IP: ", ether.gwip);
ether.printIp("DNS IP: ", ether.dnsip);
ether.printIp("Surf to this IP to view the Arduino Server: ", ether.myip);
myip ==ether.myip;
/* initierad ...*/
//Ethernet Debugging
ether.begin(sizeof Ethernet::buffer, mymac);
ether.staticSetup(myip);
//Ethernet Debugging Ends Here
// Turn off all diodes
pinMode(ledPin1, OUTPUT);
digitalWrite(ledPin1, LOW);
pinMode(ledPin2, OUTPUT);
digitalWrite(ledPin3, LOW);
pinMode(ledPin3, OUTPUT);
digitalWrite(ledPin3, LOW);
}
void homepage(){
bfill.emit_p(PSTR(
"<!DOCTYPE html><html><title>Ethernet Controller</title>"
"<body><div id='content' style='background-color:lightgreen;width:1100px;height:900px;'><hr><h1><p><center><font size='28'>
Kalles Arduino-website
test
"
"</p><hr>
<table bgcolor='#58D3F7' border='1'><tr><th colspan='4' >Choose diode :</th></tr>"
"<tr><td><INPUT Type='BUTTON' VALUE='Red ' ONCLICK='window.location.href=\"/?cmd=Red\"'></td><td>"
"<INPUT Type='BUTTON' VALUE='Yellow' ONCLICK='window.location.href=\"/?cmd=Yellow\"'></td>"
"<td><INPUT Type='BUTTON' VALUE='Green' ONCLICK='window.location.href=\"/?cmd=Green\"'></td>"
"<td><INPUT Type='BUTTON' VALUE='Off' ONCLICK='window.location.href=\"/?cmd=Off\"'></td></tr>"
"</table>
<hr>xxx</font>
<hr></div></body></html>"
));
}
void http_Unauthorized(){
bfill.emit_p(PSTR("HTTP/1.0 401 Unauthorized\r\n"
"Content-Type: text/html\r\n\r\n"
"<h1>401 Unauthorized</h1>"
));
}
void loop() {
//checking for requesting client
word len = ether.packetReceive();
word pos = ether.packetLoop(len);
if(pos) {
bfill = ether.tcpOffset();
//storing buffer on data variable
char *data = (char *) Ethernet::buffer + pos;
if (strncmp("GET /", data, 5) != 0) {
// Unsupported HTTP request
// 304 or 501 response would be more appropriate
http_Unauthorized();
}
else {
data += 5;
if (data[0] == ' ') {
// Return home page
homepage();
}
else if (strncmp("?cmd=Red ", data, 8) == 0) {
homepage();
digitalWrite(ledPin1, HIGH); // Red diode on
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
}
else if (strncmp("?cmd=Yellow ", data, 11) == 0) {
homepage();
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, HIGH); // Yellow diode on
digitalWrite(ledPin3, LOW);
}
else if (strncmp("?cmd=Green ", data, 10) == 0) {
homepage();
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, HIGH); // Green diode on
}
else if (strncmp("?cmd=Off ", data, 8) == 0) {
homepage();
digitalWrite(ledPin1, LOW); // All off
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
}
else {
// Page not found
http_Unauthorized();
}
delay(1000);
}
Readvalue=VoltageMeasure();
ether.httpServerReply(bfill.position());
len ==0;
}
}
float VoltageMeasure()
{
float voltage;
//volt meter start
//Obtain RAW voltage data
voltage = analogRead(voltPin);
//Convert to actual voltage (0 - 5 Vdc)
voltage = (voltage / 1024) * 5.0;
return voltage;
}
Webpage_med_inmatning_av_data.ino (4.76 KB)