Particle Photon IoT

This Photon Particle tutorial covers how to use Particle to develop an IoT project. This photon tutorial describes several aspects that can be useful when building an IoT project using Photon Particle. As you may already know, Photon Particle is an interesting IoT device that can be used in several scenarios. This Photon Particle tutorial describes several use cases where we can use this IoT device. During this Photon tutorial you will learn:

  • How to get started using Photon
  • How to build your first Photon project integrating it with BMP280
  • How to connect Photon to the cloud and get the temperature and pressure
  • How to use Particle Photon with Neopixel LEDs and control it

What is Photon Particle?

Before starting this Photon tutorial, it is useful to describe briefly what is Photon Particle and its main features.

Particle Photon is a small IoT device with Wi-Fi built-in that can be used to build IoT projects. Particle Photon is part of the Particle ecosystem that offers an integrated environment to build and deploy IoT project. Moreover, it supports cloud connectivity so that we can control Particle Photon from the cloud. From the hardware point of view, Photon has a
Cypress Wi-Fi chip and STM32 ARM Cortex M3 microcontroller. As we can see later, Particle offers a Web IDE to develop IoT projects and a desktop IDE.

Getting started using Particle Photon

When we get for the first time, before using it, it is necessary to configure it and connect it to the Wi-fi to unleash the power of the Photon.

Follow this steps:

  1. Go to setup.particle.io
  2. Download the file HTML
  3. Open the file

Once you opened the file, connect to the Photon Wifi:

photon particle setup wifi

Configure the Wifi credential to connect to your Wifi:

particle photon confing wifi

Finally you can configure the name of your Photon:

photon select name

That’s all. Your device is ready:

particle photon setup completed

Connecting Photon IoT to BMP280

Once the Particle Photon is configured, we can develop the first project of this Particle Photon tutorial. There are two different options to start developing an IoT project:

  • Using Web IDE
  • Using Desktop IDE

It is up to you to choose the one you prefer. In this Particle Photon tutorial, we use the Desktop IDE, anyway you can do the same things using the Web.

This first project uses a temperature and humidity sensor (BMP280). Let us connect the BMP280 to the Photon, the picture below describes how to do it:

particle photon bmp280

This sensor is an I2C sensor so we need four different connections;

  • Vcc (+3.3V)
  • GND
  • CLK
  • SDA

Open your IDE and start coding. Before using the sensor it is necessary to import the library that handles this sensor. You can do it using the Library Manager and looking for the sensor as shown in the picture below:

particle photon_import library

Then add the library to your project. That’s all we are ready to use the sensor.

#include <Adafruit_BMP280.h&gt;

Adafruit_BMP280 bmp280;

double temp;
double press;

void setup() {
    Serial.begin(9600);
    if (!bmp280.begin()) {
        Serial.println("Can't find the sensor BMP280");
    }
    Serial.println("BMP280 connected!");
}

void loop() {
  temp = bmp280.readTemperature();
  press = bmp280.readPressure();
  Serial.println("Temperature ["+String(temp)+"] - Pressure ["+String(press)+"]");
  delay(1000);
}

This simple Photon code reads the temperature and the pressure detected by the BMP280. Click on the flash icon and wait until the firmware is flashed

Open the serial console and check the current temperature and pressure.

How to connect Particle Photon to the cloud and get the temperature and pressure

It is time to connect the Photon to the cloud and get the current temperature and pressure. Particle Photon has an interesting feature that simplifies the cloud connection. As you remember, we have covered how to connect Arduino to the cloud using API library, well we can do the same in a really simple way.

In this Photon tutorial, we want to access to the temperature and the pressure from the cloud. To do it, let us modify the code shown above in this way:

void setup() {
    Serial.begin(9600);
    Particle.variable("temp", &amp;temp, DOUBLE);
    Particle.variable("press", &amp;press, DOUBLE);
    if (!bmp280.begin()) {
        Serial.println("Can't find the sensor BMP280");
    }
    Serial.println("BMP280 connected!");
}

To publish the a variable to the cloud, it is necessary to use

Particle(variable name, reference to the variable, variable type)

In the example above, the temperature is published as temp and the pressure is published as press .

Now it is possible to read, from the cloud, the variable values using a browser. Before doing it, it is necessary to have the device ID and the authorization token. To this purpose, go to the console and you should see your connected device:

photon particle console

To retrieve the authorization token go to the web console and the to the settings:

photon particle api key

Now we can call retrieve the temperature using:

 

How to use Particle Photon with Neopixel LEDs

The next IoT project covered in this Particle Photon tutorial is how to control Neopixel LEDs to Particle and control it. You can use several Neopixel LEDs, in this Particle tutorial we are using the Neopixel Ring. Anyway, if you use a different type of Neopixel the connections remain the same. The picture below shows how to connect Neopixel to Photon:

neopixel photon

The code to handle these LEDs is simpe:

#include "neopixel/neopixel.h"

#define PIXEL_COUNT 24
#define PIXEL_PIN D6
#define PIXEL_TYPE WS2812B

// Init the LED strip
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);

void setup() {
  strip.begin();
}

void loop() {
  // The core of your code will likely live here.
  for (int i=0; i < PIXEL_COUNT; i++)
    strip.setColor(i, 255,0,0);

  strip.show();
}

In this example, the Photon turns on all the Neopixel LEDs using red color. Before using this code, it is necessary to import the Neopixel library into your project.

How to publish data to the cloud

Once we have connected the LEDs, we can control them remotely from the cloud. The way is almost the same we have covered in the previous paragraph and we have to use Particle.function . Let us modify the previous code:

#include "neopixel/neopixel.h"

#define PIXEL_COUNT 24
#define PIXEL_PIN D6
#define PIXEL_TYPE WS2812B

// Init the LED strip
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);

int green;
int red;
int blue;

void setup() {
  Particle.function("red", setRed);
  Particle.function("green", setGreen);
  Particle.function("blue", setBlue);

  Serial.begin(9600);
  strip.begin();
}

void loop() {
}

void setStripColor() {
  Serial.println("Set color ["+String(red)+"," +String(green)+ "," +String(blue)+ "]");
  for (int i=0; i < PIXEL_COUNT; i++)
    strip.setColor(i, red, green, blue);
  strip.show();
}
int setRed(String r) {
  red = r.toInt();
  setStripColor();
  return red;
}
int setGreen(String g) {
  green = g.toInt();
  setStripColor();
  return green;
}
int setBlue(String b) {
  blue = b.toInt();
  setStripColor();
  return blue;
}

Notice that we added three different methods to handle the three different colors and exposed these methods using Particle.funcion. Now you can control remotely the LEDs from the cloud.

How to use MQTT to publish data from Particle Photon

In this last project, we want to publish the data acquired from the sensor using MQTT using Particle.publish. To do it, we will reuse the source code that reads data from BMP280 and we want to publish these vaules to the cloud. The code is shwon below:

#include <Adafruit_BMP280.h&gt;

Adafruit_BMP280 bmp280;

double temp;
double press;

void setup() {
    Serial.begin(9600);
    Particle.variable("temp", &amp;temp, DOUBLE);
    Particle.variable("press", &amp;press, DOUBLE);

    if (!bmp280.begin()) {
        Serial.println("Can't find the sensor BMP280");
    }
    Serial.println("BMP280 connected!");
}

void loop() {
  temp = bmp280.readTemperature();
  press = bmp280.readPressure();
  Particle.publish("temperature", String(temp), 60, PRIVATE);
  Particle.publish("pressure", String(press), 60, PRIVATE);
  delay(1000);
}

In the loop() method, the Photon code publishes two events called:

  • temperature
  • pressure

The first one is the value related to the temp variable, while the pressure is related to the press value. You can check the value published using the event console:

photon events

Final considerations

At the end of this post, you hopefully gained the knowledge about how to use Particle Photon in different scenarios. You had the chance to verify its power and how simple is building an IoT system.

LEAVE A REPLY

Please enter your comment!
Please enter your name here