Adding Cloud Connectivity

Table of contents

  1. Monitoring with Variables
  2. Controlling functionality
  3. Publishing Events

The amazing part of the Particle Cloud is that it allows you to quickly connect aspects of your device’s operation to the internet

  • You can put variables from your device online using Particle.variable for any internet-connected service to monitor
  • You can expose functionality from your device using Particle.function allowing any internet-connected service to control actions on your microcontroller remotely; and
  • You can publish events using Particle.publish to let devices know about things happening in realtime.

We’re going to add some of these features next.

Monitoring with Variables

It would be useful to see what values are being read by the photo cell.

To see them online just add the following line to setup:

Particle.variable( "light", photoReading );

This will create an online value called light, mapped to the variable photoReading in our code.

Setup should now look like this

// run once
void setup()
{

  pinMode(ledPin, OUTPUT);

  Particle.variable( "light", photoReading );

  for( int i = 0; i < 3; i++ ){
    digitalWrite(ledPin, HIGH);
    delay(100);
    digitalWrite(ledPin, LOW);
    delay(100);
  }

}

Flash your code and open http://console.particle.io, visit My Devices and choose your device. On the right hand side you’ll see the variable appear. Click GET to see the value in real time.

Experiment with the readings and update the if( photoReading >= 4070 ){ to a number that will be a good threshold for detecting if the box is open or closed.

Controlling functionality

We can access functions and features of our code too:

Let’s allow someone remotely to trigger our Memory box by adding the following line to setup:

Particle.function("activate", handleActivation );

This says, I’m going to create a function known online as activate that will map to a function called handleActivation in my code.

This also means you need to add this method to your code too. Drop in this method at the end of your code:

int handleActivation( String command ){

  for( int i = 0; i < 3; i++ ){
    digitalWrite(ledPin, HIGH);
    delay(100);
    digitalWrite(ledPin, LOW);
    delay(100);
  }

  return 1;
}

This function will simply blink the LED three times when the web-function is called.

Save and flash your code and open http://console.particle.io, visit My Devices and choose your device. On the right hand side you’ll see the function appear. Click GET to try it out. Press it and a few seconds later your LED should blink!

Publishing Events

It would be great to get alerted when the box is opened and closed.

This is exactly what we’ll do. When the box is opened or closed we’ll announce it using Particle.publish( "eventName" );

Modify the loop to look like this:

// run over and over
void loop()
{

  photoReading = analogRead(photoPin);

  // our box is open...
  if( photoReading >= 4070 ){

	 // if the box just opened
	 // then signal it's changed
	 if( isBoxOpen == false ){
		 isBoxOpen = true;
		 Particle.publish( "box-opened" );
	 }


  }else{

	 if( isBoxOpen == true ){
		 Particle.publish( "box-closed" );
	 }
	 isBoxOpen = false;
  }

  delay( 100 );

}

You’ll notice that we’ve added two conditions:

  • If the box is above the light threshold (it’s bright and open), the box is open. And if it’s changed from closed to open in this iteration of the loop, we announce it with box-opened

  • Similarly, if it’s changed from open to closed, we announce it with box-closed

Save and flash your code and open http://console.particle.io, visit Events.

Try putting your finger over the photo resistor. You should see an event appear. Remove it and you’ll be alerted the box is closed.

Great!


Table of contents