Peristaltic Doser - Doser set amount over 24hr period


#82

BTW, I never got asked funny questions so far, when I played with my Particles (stop grinning :grin: there!!!) on a plane - but I did resist the urge to have beeping countdowns tho’
Local server on my dev PC is nice too.

I guess European airlines are (were) a bit more relaxed :sunglasses:


#83

Ha! That’s pretty good…definitely gave me a laugh.


#84

Ok, feeling better…got your little test working. Now I’m going to play with my code a bit and see what I can do. Thank you.

The question now is, which javascript library should I be using now. To be honest, I’m not even sure where I find the particle javascript libraries.


#85

Glad it’s working.

As for the libraries… Your best best is to go with the documented one, since that one will be updated, whereas the other one is now depreciated. The new/current one doesn’t seem to have callbacks though, but rather uses promises, which I’m not (yet) familiar with. You can find it in the reference section of the docs. Have a look :smile:


#86

ha…just one more thing to add to it all.

Been reading through it and I’m already confused. I updated the scrip in the head of the html, and I’m already getting errors and now my refresh buttons aren’t working…ugh!


#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.