Cloud Communication - Calling Functions

Cloud Communication

iot_cloud

Review: Particle Cloud Features

Does

  • Update device code and firmware
  • Receive status messages

  • Read the values of variables
  • Control device / execute function calls

Review: Particle Cloud Features

Does NOT

  • Store data from device
  • Run analytics
  • Manage device with publicly-accessible web/mobile

Key operations in Particle Cloud

  • Publishing (events part 1)
  • Subscribing (events part 2)
  • Accessing data (cloud variables)
  • Control device (cloud functions)

Cloud Functions

  • Register / expose a C++ function in your Argon code so it can be called online
  • Up to 15 functions may be registered

Cloud Function Process

  1. Create a regular C++ function to perform a task
    • Function must take exactly one parameter of type String
  2. Call Particle.function within setup() to register the function

Cloud Function Syntax

Particle.function(<<REGISTERED_NAME>>, <<ACTUAL_FUNCTION_NAME>>);

Example

int ledStatus(String command) {...}	//actual C++ function
    
void setup() {
  Particle.function("ledStatus", ledStatus); //register func

Function must return an int (typically -1 for fail)

Calling Cloud Functions - Particle Console

1569447605240

Calling Cloud Functions - App

1569447605240

Calling Cloud Functions - REST

  • Functions can be called by any device or service via REST

  • REST is very common protocol for sharing data across the internet

  • REST call syntax

    POST /v1/devices/{DEVICE_ID}/{FUNCTION}

Lab Wiring

rgb_led_tmp36

Lab

  • Review Cloud Variables by sending current temperature as a Cloud Variable
  • Connect at RGB LED
  • Create a cloud function called int changeLEDColor(String color)
    • Function should take a string and then change the color (red, green, blue, cyan, magenta, yellow, white, off, random)
  • Connect changeLEDColor to the cloud via Particle.function
  • Control light color from your phone

Quick Note: C++ and Strings

  • Similar to Python, we can use == to compare the contents of two Strings
String str1 = "cat";
String str2 = "gato";
if (str1 == str2) {
 
  • We can also compare strings by their capitalization using equalsIgnoreCase
String str1 = "cat";
String str2 = "CaT";
if (str1.equalsIgnoreCase(str2)) {
// statement will be true

Documentation

Updated: