Variables: undefined and functions : undefined

Hello , i m trying to see my spark variables and functions . I have used the login example. Login is no problem and also connecting to the core is no problem .But in the log i see

Device name: can_core
 - connected?: true
 - variables: undefined
 - functions: undefined

No problem to connect with the core but the variables and functions?
I don’t know why they are undefined.
Wenn I check these cloud variables and functions with SPARK DEV, I have no problem checking them, I can read the variable and send arguments into the function with a result.

code html:

<html>
  <head>
    <title>Spark Login Example</title>
  </head>

  <body>
    <div id="spark-login" />

    <script src="http://cdn.jsdelivr.net/sparkjs/0.2.4/spark.min.js"></script>
    <script>
        console.log("Hieronder de uitprint van de inlog data");
	    sparkLogin(function(data) {
	        console.log(data);
		var devicesPr = spark.listDevices();

		devicesPr.then(
			function(devices){
				console.log('Devices: ', devices);
			},
			function(err) {
				console.log('List devices call failed: ', err);
			}
		);

		spark.listDevices(function(err, devices) {
			var device = devices[0];

			console.log('Device name: ' + device.name);
			console.log('- connected?: ' + device.connected);
			console.log('- variables: ' + device.variables);
			console.log('- functions: ' + device.functions);
			console.log('- version: ' + device.version);
			console.log('- requires upgrade?: ' + device.requiresUpgrade);
		});
	});
    </script>
  </body>
</html>

code core

int temperature = 0;
void setup()
{
  Spark.variable("temperature", &temperature, INT);
  Spark.function("SendToSerial", send);
  pinMode(A7, INPUT);
}
void loop()
{
  temperature = analogRead(A7);
}
int send(String data){
    return 1;
}

There are some problems with the SparkJS library, as far as browser usage goes, some of which are noted in this thread. I’ve tried to rewrite your code in a way that it produces similar results to what is required. It’s probably not perfect, but it’ll get the job done;

<html>
    <head>
        <title>Spark Login Example</title>    
    </head>

    <body>
	<div id="spark-login"></div>
    </body>	
	  
    <script src="http://cdn.jsdelivr.net/sparkjs/0.2.4/spark.min.js"></script>
    <script>
	spark.on('login', function(err, body) {
	    //console.log('API call completed on Login event:', body);	  
	    var devicesAt = spark.getAttributesForAll();

	    devicesAt.then(
	        function(data){
		//console.log('Core attrs retrieved successfully:', data);
		    for (var i = 0; i < data.length; i++) {
		        console.log('Device name: ' + data[i].name);
		        console.log('- connected: ' + data[i].connected);

			//display functions	
 			if (data[i].functions == null){
			    console.log('- functions: no functions available');						
			}
			else {
			    console.log('- functions: ' + data[i].functions);
			}
					
			//display variables
			console.log('- variables: ');			
			if (data[i].variables != null) {	
		            for (variable in data[i].variables) {
			        var type = data[i].variables[variable];
				console.log("-- variable: " + variable + ", type: " + type);			
			    }
			}
			console.log("\n");
		    }
		},
		function(err) {
		    console.log('API call failed: ', err);
		}
	    );
	});
	
        sparkLogin(function(data) {
	    console.log(data);
        });		
    </script>
</html>

Feel free to ask for help, or clarification, if you need any.

2 Likes

Great !
Now i can see my function and variable .
THX also for a fast answer.

1 Like

Depending on what you want to make, you could take a look at this: http://jordymoors.nl/interface It’ll show you your exposed functions/variables and will allow you to query them. Let me know if you need any further help, and I’ll give it a try.

@Moors7 I tried your code to get the functions defined on my particle. It is not undefined anymore but in the console the name is blank. Do you have any idea why it is just blank?

Thank you

Sorry for the late reply, it’s been a busy couple of days. If you could explain a bit further on what exactly isn’t working, and maybe provide some screenshots, I might be able to help.

@Moors7 thank you for replying. Sorry I was also busy the past few days.

I got it working.

My problem was that I didn’t publish my function. (Particle.publish()).

Thanks for the help

1 Like

I don’t really understand that, since you shouldn’t need particle.publish() to use a particle.function(). Also, the code I posted above does nothing with publishes, so that appears to be unrelated. Would you mind clarifying this some more so other can benefit from this in the future? Thanks in advance!