Calling Agent function from Device code

It works!?!? Is this a feature or a bug?

As in, calling agent.send()? That’s a feature. If not, I’d like to see the code that manages that feat!

if(v<3.0) { //imp.enableblinkup(1); server.log("LowV"); if(time()-t>5*60){ sendTwilioSMS("Low Voltage"); t=time(); }

sendTwilioSMS is a function in Agent window on my screen only.
Not only does it compile, it runs as you would expect!

Erm ok that’s rather confusing, unless someone has been implementing some magic…

I’m not losing my mind. Try it yourself with a simple example? Let me clarify so you understand. The code Snippet above is in the Device, as you can tell by
imp.enableblinkup(1) and hardware.voltage() not shown. Both work. I found this by mistake as a beginner before I realized it is “wrong”. 3.0 is a bit low to be helpful I’ve since changed it to 3.10v.

sendTwilioSMS is my function in Agent. It sends an SMS and works when you run this code. It would not work in Device window. Can you think of another explanation of why this works when you run it? According to you shouldn’t it fail to compile or cause a bright red text error in the log? There is no such error. It sends an SMS! Surprise!

I’ve also done it the proper way with agent.send().
The results are the same. All the rest of my code is done properly.
This was a mistake!

It compiles, because it’s not a syntax error. (In the general case, the compiler can’t prove that a function called sendTwilioSMS won’t exist by the time the call is made.) However, in this case, if there is no function called sendTwilioSMS in the device code, then at the time it attempts to make the call, it will throw an exception: “the index ‘sendTwilioSMS’ does not exist”.


The confusing thing is that sbright33 is saying not only does it not error out, it actually runs (ie, sends an SMS). That’s quite a feat…

I can’t reproduce this… here is the code I’m using:

Agent Code:
server.log(“agent startup”);

function agentfunction()
server.log(“in agentfunction”);

Device Code:
server.log(“device startup”);


The code checks out OK (which is expected, since syntactically it’s correct) - but when I try to run the code the device code fails… here’s the log:

Thu Apr 18 2013 14:46:38 GMT-0700 (PDT): device startup Thu Apr 18 2013 14:46:38 GMT-0700 (PDT): ERROR: the index 'agentfunction' does not exist Thu Apr 18 2013 14:46:38 GMT-0700 (PDT): ERROR: at main:3

Did I interpret what you were trying to do correctly?

Yes, that seems the same as mine. Please hold on while I experiment more. Now that I’ve heard from you both I fear I may have lost my mind! I will start with my code that actually executes sending an SMS and simplify until the results change. I will get back to you with my results. Thanks for listening to a new Imp user! I must look like an idiot to you…

Everyone has to start somewhere - we’re here to help :slight_smile:

Let me know how your experimentation goes!

I guess it didn’t run… Squirrel is different from C++
Sure thought I heard my phone beep without a red error message…