Go Down

Topic: Controlling an LED from Web Server on Yun (Read 2619 times) previous topic - next topic

arturdanil

May 01, 2020, 10:17 am Last Edit: May 01, 2020, 10:52 am by arturdanil
Hi,

As the title says, I'm simply trying to control an LED/pin 13 by choosing between two buttons. I used several tutorials and this is the code that I ended up with:


Arduino:
Code: [Select]

//Inlude the appropriate libraries
#include <Bridge.h>
#include <BridgeServer.h>
#include <BridgeClient.h>

//WiFi setup:
// Listen on default port 5555, the webserver on the Yún
// will forward there all the HTTP requests for us.
BridgeServer server;
String startString;
long hits = 0;

//Setup LED pin
int led=13; //hardware connection: Yún digital pin 7 to LED to 220 Ohm resistor to Ground.
int val = 1;
void setup() {
  Bridge.begin();
 // Console.begin();
  // Listen for incoming connection only from localhost
  // (no one from the external network could connect)
  server.listenOnLocalhost();
  server.begin();
   
  // get the time that this sketch started:
   
  pinMode(led,OUTPUT); //initialize the digital pin (which controls the LED) as an output.
 // digitalWrite(led,1);
 //while(!Console);
 
}

void loop() {
  int LED_choice;
  BridgeClient client = server.accept(); 
  delay(50);
  if(client){
    Console.println(client);
  }
  if (client) {
    String command = client.readStringUntil('/'); //Read in the string up to the frist
    command.trim();        //kill whitespace
    if (command == "temperature") {  //Check to see if the first part of the URL command
      client.print(" degrees C");     
      LED_choice=client.parseInt();
      if (LED_choice == 1) { // the URL "/arduino/temperature/1" was sent
          client.print("<br>Arduino says LED on."); //Turn the LED on by making the
          digitalWrite(led, HIGH);
      }
      if (LED_choice == 0) { // the URL "/arduino/temperature/0" was sent
        client.print("<br>Arduino says LED off.");
        digitalWrite(led, LOW); //Turn the LED off by making the voltage LOW
      }
    }
     
     // Close connection and free resources.
    client.stop();
    client.flush();//discard any bytes that have been written to client but not
    //yet read.
    hits++; //increment the "hits" counter by 1.
  }   
 
}


HTML file in www folder:
Code: [Select]

<!DOCTYPE html>
<html>
<head>
</head>
<!--<body onload="setInterval(refresh, 500);">-->
<body>
 <img src="cat.jpg" alt="Cute Cat" height="142" width="142">
<br />
<p>Click the button to turn on and off the LED.</p>

<form id="LED_Selection" action="">
<input type="radio" name="LEDCheck" value="off" id="off" />
<label for="off">LED Off</label><br>
<input type="radio" name="LEDCheck" value="on" id="on" />
<label for="on">LED On</label><br>
</form>
<br />
<div id="sensor_content">Loading data from Arduino...</div>
<div id="LED_content"></div>
<div id="LED_content_test"></div>
<script type="text/javascript" src="js/zepto.min.js"></script>
<script type="text/javascript" src="js/jsHere.js"></script>

</body>

</html>




And the script in jsHere:
Code: [Select]

$(function() {
getSensorvalue();
});

function getSensorvalue() {
    //This function gets sensor values from Arduino AND sends out a request to turn LED on
//$('#sensor_content').load('/arduino/temperature/'); //send a request for temperature/humidity data
//$('#LED_content').load('/arduino/temperature/1');
var checked_option_radio = $('input[name=LEDCheck]:checked','#LED_Selection').val();

if (checked_option_radio =='off') //if the user selected for LED to be off, send request to Arduino
{
//$("#LED_content_test").html("LED is turned off");
$('#LED_content').load('/arduino/temperature/0');
}

  if (checked_option_radio =='on') //if the user selected for LED to be on, send request to Arduino
{
//$("#LED_content").html("LED is turned on");
$('#LED_content').load('/arduino/temperature/1');
}

setTimeout("getSensorvalue()",1000); //Pole every one seconds.
}




So the buttons don't actually do anything right now and this is something I have been trying to debug for a few days. From printing to console I found out the client is never 1, hence block under if(client) is never triggered. What I have not seen in any of the tutorials is an example of how to call a command and have the sketch see and read it. So I'm not even sure if I did something wrong on the sketch part, or I simply don't understand how to make calls to the sketch from web. Any hint/suggestion or just a pointer towards a tutorial that explains command calls will be appreciated!

Edit:
I tried basic Bridge example provided by Arduino IDE and tried entering the following URL in the browser: "http://yunname.local/arduino/digital/13/1"; however it says access denied. And that's probably why sketch doesn't see any commands(?). If anyone knows a solution to this, I would appreciate the help :)

Apologies if this is a dumb question~

arturdanil

Jk guys, I figured it out. In the window where you configure wifi setting, I had to choose REST API with no password which isn't default. Otherwise, it prevents you from using the commands.

Go Up