Code conversion - button to radio button

Hi there.

I have a code showing 2 buttons (on/off) which I would like to have replaced with 2 radio buttons instead. I'm not sure how to do this with the "get action". I would be really grateful if someone could point me in the right direction.

         client.println("LED switch");
          
          client.println("<form METHOD=get action=\"");
          client.println(baseurl);
          client.println("\">");
          client.println("<input type=hidden name=LED value=0>");
          client.println("<input type=submit value=\"off\"></form>");
          
          client.println("<form METHOD=get action=\"");
          client.println(baseurl);
          client.println("\">");
          client.println("<input type=hidden name=LED value=1>");
          client.println("<input type=submit value=\"on\"></form>");

Radio buttons need to be on the same form to work. Why are you sending two forms, anyway?

Hi,
Wrong forum ?

Here you go anyway

Duane B

rcarduino.blogspot.com

Hello Paul.

Thank you for taking the time to reply.
That's a good question. I took this from a tutorial on Open Blackboard: Controlling LED from a web page with Arduino and Ethernet Shield
If there is a better way to do this than I'm more than happy to try that.

My apologies for posting in the wrong forum.
Thank you for the link. I'm on it :slight_smile:

Kind regards,
Sebastiaan

Hi,
Might not be the wrong forum, just not used to seeing HTML Around here, are you using it with an Arduino ?

Duane B

Get rid of the first and the second <form ...> (3 lines). The result will be one form with 2 buttons on it. From there, the link that DuaneB posted will show you how to modify the stuff on the form, to use radio buttons, instead.

The action will be the same as for buttons.

Hm.... I have my 2 radio buttons showing up and no compiling errors when uploading however I'm not able to get the digitalWrite section to work.
The section with the digitalWrite code I copied from an example is kind of confusing me.

Is there an easier way to get this working? I have tried some examples with an action "onclick" but I seem to be stuck on this point.
I'm very grateful If someone could help me out.

Right now I have the following code (I just copy the parts that I think are relevant)

static char baseurl[]="http://192.168.1.177/";
int waterPin = 10;    // Water valve connected to digital pin 10
int val;                    // Variable for reading the pin status

In the void setup section:

pinMode(waterPin, OUTPUT);    // sets the digital pin as output to switch water valve

In the void loop section:

val = digitalRead(waterPin);   // read pin status from waterPin and store it in val

client.println("<form METHOD=get action=\"");
client.println(baseurl);
client.println("\">");
          
          
if (val == 1) {               // check if the water valve is on
client.println("<input type=radio name=LED value=1 / "); // Create first part of radio button
client.println("checked>");   // Mark radio button as checked
client.println(" On
"); // Create last part of radio button
client.println("<input type=radio name=LED value=0 /> Off");
}
else {                          // check if the water valve is off
client.println("<input type=radio name=LED value=1 /> On
");
client.println("<input type=radio name=LED value=0 / "); // Create first part of radio button
client.println("checked>");   // Mark radio button as checked
client.println(" Off"); // Create last part of radio button
}

client.println("
");
client.println("
");
client.println(val);              // For html testing purpose, to see if the pin status has changed
client.println("</form> ");



// The digitalWrite section
 	char c = client.read();
        if(reading && c == ' ') reading = false;
        if(c == '?') reading = true; //found the ?, begin reading the info
        if(reading){
          Serial.print(c);
          if(c == '0') {
            digitalWrite(waterPin, LOW);
          }
          if(c == '1') {
            digitalWrite(waterPin, HIGH);
          }
        }
         if (c == '\n' && currentLineIsBlank)  break;
         if (c == '\n') {
          currentLineIsBlank = true;
        }else if (c != '\r') {
          currentLineIsBlank = false;
        }

          Serial.print(c);

What is this showing you? It should be moved up right after the read that valued c.

You have spaces in names of the form fields. I'm not sure that they are allowed. Even if they are, the name of the field is part of the return string:
GET /?LED value=0 HTTP 1.n
or
GET /?LED value=1 HTTP 1.n

Since you set reading to false at the space, it's really not surprising that the if statements are never true, since they are never evaluated.

Hi Paul.

Thank you for replying. Much appreciated!
To be honest with you. I have no idea what that code means, but after reading your post I couldn't resist reading http://arduino.cc/it/Serial/Print
Its my first Arduino and my first time programming C.
I have som experience with AS3 (flash) but C is new to me.

I will try again tonight.
Thanks again!

Well done good job

Hello again.

I've tried a lot of things. I also went to http://bildr.org/2011/06/arduino-ethernet-pin-control/
I don't seem to able to get the the pin status changed and am grateful if someone could help me out with this.

The complete code is:

#include <Ethernet.h>
#include <SPI.h>
boolean reading = false;

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192,168,1, 177 };
static char baseurl[]="http://192.168.1.177/";

Server server = Server(80); //port 80

// Analog sensor configuration
int smokePin = 0;     // Ionization chamber smoke detector connected to A0
int smokeReading;     // the analog reading from the smoke detector
int tempPin = 1;      // Temperature sensor connected to A1
int tempReading;      // the analog reading from the temperature sensor
int humidPin = 2;     // Humidity sensor connected to A2
int humidReading;     // the analog reading from the humidity sensor
int soundPin = 3;     // Sound sensor connected to A3
int soundReading;     // the analog reading from the sound sensor
int lightPin = 4;     // Light sensor connected to A4
int lightReading;     // the analog reading from the light sensor

// Digital input configuration
int waterPin = 10;    // Water valve connected to digital pin 10
int val;              // variable for reading the pin status

void setup(){         // run once, when the sketch starts
  smokeReading = analogRead(smokePin);
  tempReading = analogRead(tempPin);
  humidReading = analogRead(humidPin);
  soundReading = analogRead(soundPin);
  lightReading = analogRead(lightPin);
  pinMode(waterPin, OUTPUT);    // sets the digital pin as output to switch water valve
  pinMode(9, OUTPUT);

  Ethernet.begin(mac, ip);
  server.begin();
  
  
}

  

void loop(){        // run over and over again
  // listen for incoming clients, and process qequest.
  checkForClient();
  val = digitalRead(waterPin);   // read pin status from waterPin and store it in val
  
}

void checkForClient(){
  Client client = server.available();

  if (client) {

    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    boolean sentHeader = false;

    while (client.connected()) {
      if (client.available()) {
        if(!sentHeader){
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();
          
           //output the value of each analog input pin
            client.print("<b>Analog readings</b>");
            client.println("
");
            client.println("Smoke particles = ");
            client.println(smokeReading);
            client.println("
");
            client.println("Temperature = ");
            client.println(tempReading);
            client.println("
");
            client.println("Humidity = ");
            client.println(humidReading);
            client.println("
");
            client.println("Sound level = ");
            client.println(soundReading);
            client.println("
");
            client.println("Light level = ");
            client.println(lightReading);
            //}
            
          client.println("
");
          client.println("
");
          client.println("<b>Digital inputs</b>");
          client.println("
");
          client.println("LED switch");
          client.println("<form METHOD=get action=\"");
          client.println(baseurl);
          client.println("\">");
          
          
          if (val == 1) {               // check if the water valve is on
          client.println("<input type=radio name=LED value=1 / "); // Create first part of radio buton
          client.println("checked>");   // Mark radio button as checked
          client.println(" On
"); // Create last part of radio buton
          client.println("<input type=radio name=LED value=0 /> Off");
          }
          else {              // check if the water valve is off
          client.println("<input type=radio name=LED value=1 /> On
");
          client.println("<input type=radio name=LED value=0 / "); // Create first part of radio buton
          client.println("checked>");   // Mark radio button as checked
          client.println(" Off"); // Create last part of radio buton
          }
          client.println("
");
          client.println("
");
          client.println(val);
          client.println("</form> ");
                    
          sentHeader = true;
        }

        char c = client.read();
        if(reading && c == ' ') reading = false;
        if(c == '?') reading = true; //found the ?, begin reading the info
        if(reading){
          Serial.print(c);
          if(c == '0') {
            digitalWrite(waterPin, LOW);
          }
          if(c == '1') {
            digitalWrite(waterPin, HIGH);
          }
        }
         if (c == '\n' && currentLineIsBlank)  break;
         if (c == '\n') {
          currentLineIsBlank = true;
        }else if (c != '\r') {
          currentLineIsBlank = false;
        }
      }
    }
    delay(1); // give the web browser time to receive the data
    client.stop(); // close the connection:
  } 
}
  checkForClient();
  val = digitalRead(waterPin);   // read pin status from waterPin and store it in val

If there is a client connection, send the client the value in val, then, assign val a value.

horse ---> cart

Hello Paul.
Thanks again for your reply.
I've given up on this and will work with the buttons instead of radio buttons

I added some radio buttons to the below test code. If you want a black dot to appear in the radio button, then you would also probably need to add the checked="yes" in the returned html for the button clicked.

//zoomkat 3-17-12
//simple button GET server code to control servo and arduino pin 4
//for use with IDE 1.0
//open serial monitor to see what the arduino receives
//use the \ slash to escape the " in the html 
//address will look like http://192.168.1.102:84 when submited
//for use with W5100 based ethernet shields
//Powering a servo from the arduino usually DOES NOT WORK.
//note that the below bug fix may be required
// http://code.google.com/p/arduino/issues/detail?id=605 

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

#include <Servo.h> 
Servo myservo;  // create servo object to control a servo 

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
EthernetServer server(84); //server port

String readString; 

//////////////////////

void setup(){

  pinMode(4, OUTPUT); //pin selected to control
  //start Ethernet
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();

  myservo.write(90); //set initial servo position if desired
  myservo.attach(7);  //the pin for the servo co
  //enable serial data print 
  Serial.begin(9600); 
  Serial.println("server LED test 1.0"); // so I can keep track of what is loaded
}

void loop(){
  // Create a client connection
  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.println(readString); //print to serial monitor for debuging 

          client.println("HTTP/1.1 200 OK"); //send new page
          client.println("Content-Type: text/html");
          client.println();

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

          client.println("<H1>Zoomkat's simple Arduino button</H1>");
          
          // DIY buttons
          client.println("<a href=\"/?on\"\">ON</a>"); 
          client.println("<a href=\"/?off\"\">OFF</a>
"); 

          // mousedown buttons
          client.println("
<input type=\"button\" value=\"ON\" onmousedown=\"location.href ('/?on');\"/>"); 
          client.println("<input type=\"button\" value=\"OFF\" onmousedown=\"location.href ('/?off');\"/>");        
          
          // mousedown radio buttons
          client.println("

<input type=\"radio\" value=\"ON\" onmousedown=\"location.href ('/?on');\"\">ON</>"); 
          client.println("<input type=\"radio\" value=\"OFF\" onmousedown=\"location.href ('/?off');\"\">OFF</>");        
 
          client.println("</BODY>");
          client.println("</HTML>");
 
          delay(1);
          //stopping client
          client.stop();

          ///////////////////// control arduino pin
          if(readString.indexOf("on") >0)//checks for on
          {
            myservo.write(40);
            digitalWrite(4, HIGH);    // set pin 4 high
            Serial.println("Led On");
          }
          if(readString.indexOf("off") >0)//checks for off
          {
            myservo.write(140);
            digitalWrite(4, LOW);    // set pin 4 low
            Serial.println("Led Off");
          }
          //clearing string for next read
          readString="";

        }
      }
    }
  }
}

Hello Zoomkat.

Thank you for your code! I have it all working now with radio buttons :slight_smile:
Thanks again.

Kind regards,
Sebastiaan