Putting the I in IoT
Up to Now Electronics Software logic Debugging Now… getting on the network
Remember this? Spark.publish(token, data); -token is the String identifier for the event -data is the String message value By the way, there are some overloads. Can add TTL, for example.
Remember this? Spark.subscribe(token, handler, deviceID) token is a String identifier for the event handler is the name of the method you want to run when the event is received deviceID is the ID# of the photon you are listening to The handler should take 2 inputs: void handler(const char *event, const char *data) event is the String identifier data is the interesting part - the String that was sent as value
2 IDs Device ID: Click “Devices” on Build, then open the arrow for the device you are using to see the ID. Access Token: Click “Settings” on Build to see your Access Token
Talk to the Photon “Expose” a function: Spark.function(token, handler); token is a String identifier for the event handler is the name of the method you want to run when the event is received Handler functions take one input, a String, and give one output, an int telling success or failure. Warning: tokens can only be up to 12 letters long. The input string can be up to 63.
Example in setup: Spark.function(“brew”, brewCoffee); then later: int brewCoffee(String command){ if (command.equals(“coffee”)) // turn on pot… return 1;
How to issue the command Something online is going to issue the command to your Photon to perform its function. POST /v1/devices/<DEVICEID>/<HANDLER> where DEVICEID is the Device ID and HANDLER is the name of the function to call. You can add a parameter that is passed to the function as well
curl Useful for debugging - libraries for sending & receiving internet messages through a variety of protocols. Already on Macs, Windows needs a download. Example from coffee machine: curl https://api.particle.io/v1/devices/0123456789abcdef/brew \ -d access_token=123412341234 \ -d "args=coffee"
Hear from the Photon You may expose up to 10 variables: Spark.variable(token, address, type); token is a String identifier of up to 12 characters address is the address of the variable you want to map it to type is the type of the variable which must be either INT, DOUBLE, or STRING Remember how to denote the address of a variable?
Example int temp; // the temperature registered by my temp sensor in setup: Spark.variable(“temperature”, &temp, INT);
How to register the value GET /v1/devices/<DEVICEID>/<TOKEN> curl "https://api.particle.io/v1/devices/0123456789abcdef/temp?access_token=12341 2341234" Open a browser to: https://api.particle.io/v1/devices/<DEVICEID>/<TOKEN>? access_token = <ACCESSTOKEN> You will see the message - “result” is the value of the variable you want.
Let’s try it out Make a circuit: 3.3 -> photoresist -> A0 ->1K ohm -> Ground And D7 -> 220 ohm -> LED -> Ground Use my HTML/Javascript and change the token ids You write the Photon code.