Project 3 – Physical Computing Studio https://courses.ideate.cmu.edu/48-390/s2016 CMU | Spring 2016 | 48-390 Sun, 02 Oct 2016 15:29:26 +0000 en-US hourly 1 https://wordpress.org/?v=4.5.31 Calling Photon functions from Javascript https://courses.ideate.cmu.edu/48-390/s2016/2016/03/21/calling-photon-functions-from-javascript/ https://courses.ideate.cmu.edu/48-390/s2016/2016/03/21/calling-photon-functions-from-javascript/#respond Mon, 21 Mar 2016 01:13:34 +0000 http://courses.ideate.cmu.edu/physcomp/s16/48-390/?p=415 Continue Reading →]]> Sky showed me Particle’s javascript library, and I thought I would post what I had to do to get it working.

Particle’s documentation can be found at: https://docs.particle.io/reference/javascript/

My updated files are at: https://github.com/arathorn593/IDeATePhysCompProject03-Cardboard

  1. From the terminal, run:   npm install particle-api-js
  2. To allow you to use the library in the main script.js file, you must add this line to your index.html file: <script type="text/javascript" src="//cdn.jsdelivr.net/particle-api-js/5/particle.min.js"></script>. This line needs to go before you import the script.js file, so you should have your new line followed by the existing line that includes the script:
    /* this is the added line */
    <script type="text/javascript" src="//cdn.jsdelivr.net/particle-api-js/5/particle.min.js"></script>
    
    /* this is the existing line */
    <script src="js/script.js"></script>
  3. Next, add the code to login at the top of your init function. Make sure to save your token to a global variable and don’t include the var Particle = require('particle-api-js');  line that the particle documentation says to include. Thus, the top of your init file should look something like:
    var particle;
    var token;
    function init() {
    	particle = new Particle();
    
    	particle.login({username: user, password: pass}).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);
    	   }
    	);
    
    	//....
    
    }
  4. Now, you are set up and you can use the functions in the library. The only one I am using right now is the callFunction function. I call it whenever the default stool object is selected (check out the picker function in script.js). To call a function on your photon, you also need to register the function to the cloud within you photon code (see https://docs.particle.io/reference/firmware/photon/).
    • My photon code:
      bool state = false;
      
      void setup() {
          pinMode(D7, OUTPUT);
          
          //register the function with the cloud
          Particle.function("light", light);
      }
      
      void loop() {
          //don't need to do anything here
      }
      
      //this function is called from the script.js file when
      //the stool is selected
      int light (String str) {
          //toggle the state of the light 
          state = !state;
          
          //write to the light
          if (state) {
              digitalWrite(D7, HIGH);
          } else {
              digitalWrite(D7, LOW);
          }
          return 3;   //return any number
      }
    • The code to call the light function:
      var fnPr = particle.callFunction({ deviceId: deviceID, name: 'light', argument: 'hi', auth: token });
      
      fnPr.then(
      	function(data) {
      		console.log('Function called succesfully:', data);
      	}, function(err) {
      		console.log('An error occurred:', err);
      });

       

Let me know if I need to add anything to this post.

]]>
https://courses.ideate.cmu.edu/48-390/s2016/2016/03/21/calling-photon-functions-from-javascript/feed/ 0
Project 03 — Cardboard https://courses.ideate.cmu.edu/48-390/s2016/2016/03/01/project-03-cardboard/ https://courses.ideate.cmu.edu/48-390/s2016/2016/03/01/project-03-cardboard/#respond Tue, 01 Mar 2016 15:35:02 +0000 http://courses.ideate.cmu.edu/physcomp/s16/48-390/?p=365 Code, demos, and project descriptions are available for this project at the Github Repo: https://github.com/marsman12019/IDeATePhysCompProject03-Cardboard

]]>
https://courses.ideate.cmu.edu/48-390/s2016/2016/03/01/project-03-cardboard/feed/ 0