Peristaltic Doser - Doser set amount over 24hr period


#93

Alright, making some progress here, got the manual buttons working, as well as the calibrate buttons.

The next things will be the setting buttons and the refresh buttons. I presume for the refresh buttons I’ll need to use JSON for retrieving the settings, is that correct? Are there any examples with the newest SDK for particle.getVariable? I found this one, but its a bit old, does it still apply?


#94

So I was able to get the refresh button working with the following code. What I don’t know, or understand where to start is, how do I tell the script which relay I’m referring to without repeating that function code four times? The four variables are getRelay1, getRelay2, getRelay3, getRelay4. When I dig into the previous code I had I feel like I’m on to something, but I’m 100% sure.

html

<p>Pump 1: Current setting <span id="relay1">unknown</span>
    <button id="refreshbutton" onclick="getRelay1()">Refresh</button>

Javascript

function start(objButton) {

        document.getElementById("setup").innerHTML = "Waiting for data...";
        var deviceID = "35002400094734343231xxxx";
        var accessToken = "3aaacdf9121d404c1a60d5f5f853585c156axxxx";
        var varName = "getRelay1";

        requestURL = "https://api.particle.io/v1/devices/" + deviceID + "/" + varName + "/?access_token=" + accessToken;
        $.getJSON(requestURL, function(json) {
                 document.getElementById("setup").innerHTML = json.result;
                 });

Looking at the previous build by @bko I see on the core code he had the following;

   Spark.variable("getRelay1", relayStr[0], STRING);
   Spark.variable("getRelay2", relayStr[1], STRING);
   Spark.variable("getRelay3", relayStr[2], STRING);
   Spark.variable("getRelay4", relayStr[3], STRING);

And my current code is

    Particle.variable("getRelay1", getRelay1);
    Particle.variable("getRelay2", getRelay2);
    Particle.variable("getRelay3", getRelay3);
    Particle.variable("getRelay4", getRelay4);

Then in the html there was the following:

"relay"+relay.toString()

#95

It’s not clear to me what you’re trying to accomplish. Do you want to have 4 buttons, one for each relay? Or, do you want to get all the values at once with one button?


#96

I know you’re not a web programmer, but I’m guessing something has to be done on the photon programming side to accomplish this. If I’m wrong, correct me. I’m looking for a way to get all the values, whether that’s one button that gets all the values at once, or four buttons for each individual relay…either way is fine.


#97

You probably don’t need to do anything on the Photon end. Your one button can get all four variables by using a loop to loop through the varName values. I don’t know if you would need some delay between the requests or not, since I’ve never tried to get multiple variables one after the other. I don’t know how exactly to code that in Javascript.


#98

So… this is the point where I’m going to say cross-posting is a bad idea. One, you’re confusing people. Two, you’re having people double posting their answers. Three, it’s not efficient, at all. So, back to the task at hand.


So, that’s using the code you mentioned on this post? Any reason you’re sticking to the JQuery when you’ve specifically asked for a Particle SDK example to which you got a working response?

By making a function that takes an argument as an input in which you specify what devices/functions/variables you’d like to call. If I’m not mistaken, I’ve already given you an example of that over here. explained in-depth over here, and used in this example for you over here, again.

Well, let’s say you’re partially wrong. You can have different buttons that call different variables. You can have one button that calls multiple variables, and you can have a button that requests a variable that’s combined string of multiple variables. Only the latter requires changes on the device side. The former two can be done in javascript just fine.

The most important thing, however, is the following:

At this point, I’m staring to lose track of that too, and taking all your topics questions into consideration, I’ve got a feeling you’re uncertain yourself. You’re asking questions for the same thing in at least three topics, some of which are double, or ignored when answered.
Try getting a clear picture of what you want to do, sketch it out on paper if you must, and then let us know. When doing so, try to stick to one topic, since it’s way too confusing to keep track of what I’ve already answered.


#99

@moors7: Moved some posts from another post to here, since that makes more sense and is ultimately less confusing


I’m actually having the same issue…getting 404 not found error. My variables are:
getRelay1 (string)
getRelay2 (string)
getRelay3 (string)
getRelay4 (string)

I’m trying to get my relay variables in a larger html page, but in order to get a better understanding of reading the variables I’m starting with an individual page for now. So my bigger html file uses the latest Particle API javascript. I notice this example uses jquery, is it possible to load both in one html file, or is jquery included in the recent javascript?

In the mean time, staying relative to this example, here is the html/javascript I’m using and I’m getting error 404 Not Found. I don’t have a publish event, but from what I gather, the purpose of this tutorial is you don’t need a publish event, is that correct?

<!DOCTYPE HTML>
<html>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript" charset="utf-8"></script>
<body>
    <span id="setup"></span><br>

    <button id="connectbutton" onclick="start()">Get Setup</button>

    <script type="text/javascript">

    function start(objButton) {

        document.getElementById("setup").innerHTML = "Waiting for data...";
        var deviceID = "35002400094734343231xxxx";
        var accessToken = "3aaacdf9121d404c1a60d5f5f853585c156axxxx";
        var varName = "getRelay1";

        requestURL = "https://api.particle.io/v1/devices" + deviceID + "/" + varName + "/?access_token=" + accessToken;
        $.getJSON(requestURL, function(json) {
                 document.getElementById("setup").innerHTML = json.result;
                 });
    }
    </script>
</body>
</html>

[request] Latest Particle API Javascript SDK Examples
#100

If you’re using the Javascript library anyhow, you might as well use it for everything…

This example should work for both functions as well as variables, give ti a shot:


<html>
  <head>
    <title>Particle Javascript Example</title>
	
    <script type="text/javascript" src="http://cdn.jsdelivr.net/particle-api-js/5/particle.min.js"></script>
  </head>

  <body>
  Check the console to see the output of the code.
  <br>
    <button type="button" onclick="functionPost('Mandatory_function_name', 'Optional_function_argument_here')">function POST</button>
    
    <button type="button" onclick="variableGet('Mandatory_variable_name')">Variable GET</button>
  
  
    <script>
      var particle = new Particle();
      
      var token;
      var ActualDeviceID = "ENTER_DEVICE_ID_HERE";      

      function functionPost(functionName, functionArgument){
        var fnPr = particle.callFunction({ deviceId: ActualDeviceID, name: functionName, argument: functionArgument, auth: token });
      
        fnPr.then(
          function(data) {
            console.log('Function called succesfully:', data);
            console.log(data.body.return_value);
          }, function(err) {
            console.log('An error occurred:', err);
          }          
        );
      }

      function variableGet(variableName){
        var vrGr = particle.getVariable({ deviceId: ActualDeviceID, name: variableName, auth: token })
        
        vrGr.then(
          function(data) {
            console.log('Device variable retrieved successfully:', data);
            console.log(data.body.result);
          }, function(err) {
            console.log('An error occurred while getting attrs:', err);
          }
        );
      }

      particle.login({username: 'EMAIL_ADDRESS_HERE', password: 'PASSWORD_HERE'}).then(
        function(data){
          console.log('API call completed on promise resolve: ', data.body.access_token);
          token = data.body.access_token;
        },
        function(err) {
          console.log('API call completed on promise fail: ', err);
        }
      );
	
    </script>
  </body>
</html>

#101

Even if you want to do it without JS library

requestURL = "https://api.particle.io/v1/devices" + deviceID + "/" + varName + "/?access_token=" + accessToken;

You seem to be missing a forward slash (/) here.


#102

That works…thank you. Now I just need to work out how to get the output into an html field.


#103

So the original post seems to be working now. I had to load a second a javascript library in order for it to work though. Passing my next question on to my project post.


#104

So for the confusion, it all makes sense on my end. I have a check list here of what I’m trying to accomplish and rather than tackling a bunch of things at once, I’m going down the line. I really don’t mean to be a pain in the butt, so I apologize if that’s how I’m coming across.

Regarding the refresh button, I’m find doing which ever is easiest given the current photon code. If that means having one button that refreshes all four relays, or four single buttons, one for each relay. In my head, it seems easier to do one button to refresh all four, so I’m going to try that on my own first.

Once again, my apologies guys. I appreciate your help and can’t thank you enough.


#105

@Moors7
So this works as I’d hoped it would, however it seems there might be a more efficient way to do it. Also, you had mentioned that I should choose either JQuery or SDK, so would it be wrong to use both in the final html page?

<!DOCTYPE HTML>
<html>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript" charset="utf-8"></script>
<body>
        <span id="r1"></span><br>
    <button id="r1button" onclick="getRelay1()">Refresh Relay 1</button>
    <br>
        <span id="r2"></span><br>
    <button id="r2button" onclick="getRelay2()">Refresh Relay 2</button>
    <br>
        <span id="r3"></span><br>
    <button id="r3button" onclick="getRelay3()">Refresh Relay 3</button>
    <br>
        <span id="r4"></span><br>
    <button id="r4button" onclick="getRelay4()">Refresh Relay 4</button>
    <br>
    <script type="text/javascript">

    function getRelay1(objButton) {

        document.getElementById("r1").innerHTML = "Waiting for data...";
        var deviceID = "35002400094734343231xxxx";
        var accessToken = "3aaacdf9121d404c1a60d5f5f853585c156axxxx";
        var varName = "getRelay1";

        requestURL = "https://api.particle.io/v1/devices/" + deviceID + "/" + varName + "/?access_token=" + accessToken;
        $.getJSON(requestURL, function(json) {
                 document.getElementById("r1").innerHTML = json.result;
                 });
    }
        function getRelay2(objButton) {

        document.getElementById("r2").innerHTML = "Waiting for data...";
        var deviceID = "35002400094734343231xxxx";
        var accessToken = "3aaacdf9121d404c1a60d5f5f853585c156axxxx";
        var varName = "getRelay2";

        requestURL = "https://api.particle.io/v1/devices/" + deviceID + "/" + varName + "/?access_token=" + accessToken;
        $.getJSON(requestURL, function(json) {
                 document.getElementById("r2").innerHTML = json.result;
                 });
    }
        function getRelay3(objButton) {

        document.getElementById("r3").innerHTML = "Waiting for data...";
        var deviceID = "35002400094734343231xxxx";
        var accessToken = "3aaacdf9121d404c1a60d5f5f853585c156axxxx";
        var varName = "getRelay3";

        requestURL = "https://api.particle.io/v1/devices/" + deviceID + "/" + varName + "/?access_token=" + accessToken;
        $.getJSON(requestURL, function(json) {
                 document.getElementById("r3").innerHTML = json.result;
                 });
    }
        function getRelay4(objButton) {

        document.getElementById("r4").innerHTML = "Waiting for data...";
        var deviceID = "35002400094734343231xxxx";
        var accessToken = "3aaacdf9121d404c1a60d5f5f853585c156axxxx";
        var varName = "getRelay4";

        requestURL = "https://api.particle.io/v1/devices/" + deviceID + "/" + varName + "/?access_token=" + accessToken;
        $.getJSON(requestURL, function(json) {
                 document.getElementById("r4").innerHTML = json.result;
                 });
    }
    </script>
</body>
</html>

#106

Sounds like the same question to me, so you’re going to get the same answer:

Again, you’re still not using any function arguments, so why bother creating them?
Having the deviceID and accesstoken inside every function is redundant, and could be handles as a global variable.

It doesn’t matter. Heck, for all I care, you code it in binary. The computer won’t care either. You can mix/match twenty libraries if that’s what you’re into. It’s just not necessary, and frankly, it doesn’t look nice, is harder to maintain, takes longer to load, and is yet another thing that can go wrong, or you’d have to learn. hence my suggestion to choose one and stick with it. It’s also a bit frustrating to make several examples per your request for one thing, to have you then ignore that and use something else. Like I said, I’m cool with either, just try to stick to it, and not switch it around every post.

What exactly is wrong with the example I provided you here, that made you go “ah, nah, I’ll use JQuery instead”?
Also, if you’re asking for ways to make it more efficient, to which you get suggestions, why not try to use them, rather than trying something else, and then repeating the question? Doesn’t seem to make a whole lot of sense if you ask me.

If you could share the ‘list’ you’re following, we could have a look as well, and see your end-goal. Perhaps we can then offer some better advise, since this isn’t really going all that smooth so far, and is becoming more confusing with every post added…

Then again, if things work, and they’re good enough for you, then hey, all the power to you.


#107

Ok, I’m going to try and address a few things here:

Again, you’re still not using any function arguments, so why bother creating them?

I assume you’re referring to this statement - $.getJSON(requestURL, function(json)
I tried to remove the function, but I continued to get errors, so reverted to what worked.

Having the deviceID and accesstoken inside every function is redundant, and could be handles as a global variable.

I changed this, they are now a global variables.

What exactly is wrong with the example I provided you here, that made you go “ah, nah, I’ll use JQuery instead”?

The example you provided worked as expected. I was having problems understanding how to get that example to return the data back to the html page. I’m pretty sure it needs to use JSON, and I was having a hard time find JSON examples. I read through this but I couldn’t relate their examples to what I need.

As for the to do list, here it is:
Doser To Do List

1. Manual ON/OFF
2. Calibrate 60 Seconds ON
3. Refresh Current Settings
4. setupChannels with Set Button
Listed in order of string command
- Channel#
- time (convert seconds to 24hr TOD)
- dose rate (determined through calibration = total amount dosed divided by 60)
- total dose per 24hr period
5. Optional - In Order of Priority
-Current programming is set for 12 doses over 24hr period. Would like select field to change that for options       between 1 - 24 doses per 24hr period.
-CSS for cleaner visuals
-Support for stepper motors
-HTML formatting for mobile devices
6. Pumps - Give customer multiple vendors for different grades of pumps.
Options:
-High Quality / Low Quality
-AC & DC
-Stepper
-Variable speed
7. Enclosures
8. Power Supply
9. Thorough writeup for “How To” with vendor list and cost breakdown.


#108

Nope, that one actually does use the argument, which is the one in the line above:

requestURL = "https://api.particle.io/v1/devices/" + deviceID + "/" + varName + "/?access_token=" + accessToken;

How does the code you made display it to the HTML, and why would that be any different from my example? I made it output the data to the console since I didn’t feel like creating a bunch of extra HTML which you’d have to change anyhow.

JSON is just a way of formatting a string of data, nothing more, nothing less. It’s no black magic.
In fact, you’re also using JSON in your version by doing

function(json) {
  document.getElementById("r2").innerHTML = json.result;
}

That json object is the response from the variable request, and looks like this:

{
  "cmd": "VarReturn",
  "name": "temp",
  "result": 20.799999237060547,
  "coreInfo": {
    "last_app": "",
    "last_heard": "2016-04-06T15:08:35.026Z",
    "connected": true,
    "last_handshake_at": "2016-04-06T13:23:42.004Z",
    "deviceID": "50ff6c065yadayada6580487",
    "product_id": 0
  }
}

So that whole thing can be addressed as json. If you want any of the key:value pairs, you use the selector. Say you want the name, then you’d use json.name, for the result you’d use json.result, which is exactly what you did.

Now, compare that to my version, and spot the differences/similarities…

function(data) {
  console.log('Function called succesfully:', data);
  console.log(data.body.return_value);
}

#109

Alright, I’m down to something stupid now…not that the rest of my questions haven’t been stupid…sorry guys. :disappointed_relieved:

I have the code below returning the proper results for the corresponding button, but clearly something in the “innerHTML=data.body.result” is wrong…or at least that’s my suspicion. If I click Relay1, I get the results for relay1 next to each button, if I click Relay2 I get all the relay2 results next to each button…and so on. Here is a screen shot:

It should look more like this:

Here is the current code I’m using:

<html>
  <head>
    <title>Particle Javascript Example</title>
    
    <script type="text/javascript" src="http://cdn.jsdelivr.net/particle-api-js/5/particle.min.js"></script>
  </head>

  <body>
  <br>
    <span id="r1"></span>
    <button type="button" onclick="variableGet('getRelay1')">Relay1</button><br>
    <span id="r2"></span>
    <button type="button" onclick="variableGet('getRelay2')">Relay2</button><br>
    <span id="r3"></span>
    <button type="button" onclick="variableGet('getRelay3')">Relay3</button><br>
    <span id="r4"></span>
    <button type="button" onclick="variableGet('getRelay4')">Relay3</button><br>
 
  
    <script>
      var particle = new Particle();
      
      var token;
      var ActualDeviceID = "35002400094734343231xxxx";      

      function variableGet(variableName){
        var vrGr = particle.getVariable({ deviceId: ActualDeviceID, name: variableName, auth: token })
        
        vrGr.then(
          function(data) {
            document.getElementById("r1").innerHTML = data.body.result
          }
        );
        vrGr.then(
          function(data) {
            document.getElementById("r2").innerHTML = data.body.result
          }
        );
        vrGr.then(
          function(data) {
            document.getElementById("r3").innerHTML = data.body.result
      }
      );
          vrGr.then(
          function(data) {
            document.getElementById("r4").innerHTML = data.body.result
      }
      );
      }

      particle.login({username: 'xxx@xxx.net', password: 'xxxx'}).then(
        function(data){
          console.log('API call completed on promise resolve: ', data.body.access_token);
          token = data.body.access_token;
        },
        function(err) {
          console.log('API call completed on promise fail: ', err);
        }
      );
    
    </script>
  </body>
</html>

#110

Yes it is …

Why should it?
I guess if you just looked at your own code and did your own homework you might come to the same conclusion.

I have seen the reason in the code you posted further up, but we are not there to stop you from learning to find this yourself.
You would not even have to browse through several posts and guess what your current code is, since you have the current code right there at your desk - we only have the info scattered all over several threads.


#111

The code is fine, but the logic is wrong…

int functionNumber = 1;

if (functionNumber == 1){
  function1();
  function2();
  function3();
  function4();
}

If I were to run that code, which of the four functions would get called?


#112

I’d say function1();

But I feel like this is a trick question…