Reading multiple pins in html and jquery

Reading multiple pins in html and jquery.
Good morning everybody.
I’m developing a tool to control photon pins by html and javascript. I already have html code that works perfectly fine for a single pin. The code allows you to click on a particular pin and at the same time reports via html the current status of the pin. The problem is they are multiple pins and I don’t really know how to return the multipin state via get. The code below reports the status of a single pin but how to do it for the logic level of more than one pin. My difficulty is in “function(data, status)” returning data from multiple variables and pins.

<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript" charset="utf-8"></script>
<script>

var accessToken = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
var deviceID = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
var url = "https://api.spark.io/v1/devices/" + deviceID + "/IoT";

$(document).ready(function(){
  $("button").click(function(){
    $.get(url, {access_token: accessToken}, function(data, status){
      statusPin = parseInt(data.result);
      alert("Data: " + statusPin + "\nStatus: " + status);
      //$("p").html("Data: " + statusPin + "\nStatus: " + status);
      if(statusPin == 1){
        $("p").html("On");
      }
      if(statusPin == 0){
        $("p").html("Off");
      }
    });
  });
});

function switchD7()
{
	$.post(url, {params: "D7", access_token: accessToken });
}
function switchD6()
{
	$.post(url, {params: "D6", access_token: accessToken });
}
</script>
</head>

<body>
  <button>click here to check the status of the photon</button>
  <p>Status do photon.</p>
  <input type="button" onClick="switchD7()" value="click here to check the status of the led"/>
<input type="button" onClick="switchD6()" value="click here to check the status of the led"/>

</body>
</html>
int ledPin7 = D7;
int statusPin7;

void setup() {
    pinMode(ledPin7, OUTPUT);
    Spark.function("IoT", ledSwitch);
    Spark.variable("IoT", &statusPin7, INT);
}

void loop() {
}
int ledSwitch(String command) {
    if (command.equalsIgnoreCase("D7")){
        statusPin7 = !statusPin7;                   // toggle the value
        digitalWrite(ledPin7, statusPin7);
    return 1;
    }
    return -1;
}

Below to interact with two pins

int ledPin7 = D7;
int statusPin7;

int ledPin6 = D6;
int statusPin6;

void setup() {
    pinMode(ledPin7, OUTPUT);
    pinMode(ledPin6, OUTPUT);
    Spark.function("IoT", ledSwitch);
    Spark.variable("IoT", &statusPin7, INT);
    Spark.variable("IoT", &statusPin6, INT);
}
}

void loop() {
}
int ledSwitch(String command) {
    if (command.equalsIgnoreCase("D7")){
        statusPin7 = !statusPin7;                   // toggle the value
        digitalWrite(ledPin7, statusPin7);
    return 1;
    }
if (command.equalsIgnoreCase("D6")){
        statusPin6 = !statusPin6;                   // toggle the value
        digitalWrite(ledPin6, statusPin6);
    return 1;
    }
    return -1;
    -1;
}

To start with, Spark.xxx() is deprecated for years now. Nowadays we use Particle.xxx()
Also Particle.variable() now only uses two parameters as in

  Particle.variable("IoT", status);

You cannot have multiple variables referenced by the same name. How would the device know which "IoT" you mean?
You need one variable per pin you want to register.
Or you have a single variable that holds the state of all pins.
A calculated variable would be an option for that - e.g. one bit per pin in an int or one character in a string to be parsed on the receiving side.

The binary encoding would also work in the return value of your Particle.function().

1 Like