Avatar
Brian knew he wanted to work with computers since he first played Space Cadet Pinball on Windows 95. To that end, Brian graduated from Rensselaer Polytechnic Institute with a degree in Computer & Systems Engineering before acquiring a Master's degree in Computer Science from Syracuse University. His professional career started in embedded systems before moving to enterprise web applications. Among other things, Brian enjoys a spirited debate, a stiff glass of whiskey, and a useful analogy.

The Internet of Things (IoT) revolution is all about connecting devices to the internet, especially ones that have typically been outside the scope of prevailing software systems. With this development, intelligence is no longer limited to the desktop or the browser. The proliferation of IoT solutions increasingly enables action to take place where it matters most: at the edge. This can be in the soil on a farm, in a weather balloon, or on a building rooftop.

Consider the value of adding IoT capabilities to an HVAC system. An HVAC unit is typically scheduled for periodic maintenance by a technician. The technician must be on site to check system vitals for abnormalities. If the unit is equipped with IoT technology, however, these visits could be less frequent or scheduled only when necessary. The system may even be smart enough to identify and correct common issues. The potential cost savings is enormous.

This scenario is not unique. Many industries are ripe for innovations that take advantage of IoT solutions that were not previously feasible. Software developers often see the physical world as outside the purview of the systems they create, but we must now think differently. With the advent of modern IoT platforms, it’s never been easier to unite the physical and digital realms.

As an example, I'll demonstrate how you can use the Particle device platform to read the temperature from a DS18B20 temperature sensor. Then you’ll send it up to the Particle cloud where it can be used anywhere.

Particle Platform

The Particle platform includes everything you’ll need to get your IoT project off the ground. This includes:

  • Prototype-ready hardware.
  • An embedded operating system on which to base your application firmware.
  • A device management console to help manage your device fleet.

One obvious choice to be made in any IoT project is the hardware platform. This is driven by several factors including, but not limited to:

  • Connectivity (Wi-Fi, cellular, Bluetooth, and so on).
  • Space constraints.
  • IO requirements (such as analog inputs and digital IO pins).

The example here uses the Particle Photon, a Wi-Fi device based on the STM32 ARM Cortex M3 chipset. This device is a good start if the operating environment has access to a Wi-Fi connection.

Device OS

Device OS is an open source operating system maintained by the Particle team. Here are its responsibilities according to Particle’s documentation:

  • Secure communication: Ensuring that all communications between the device and the Particle cloud are authorized and encrypted
  • Hardware abstraction: Providing a single, unified interface to the device, regardless of the underlying hardware architecture
  • Application enablement: Exposing a feature-rich API that is used by developers to write applications for the device
  • Over-the-air updates: Allowing rapid remote changes to code running on the device while providing resilience in poor connectivity environments

These features allow you to focus on the novel parts of your software instead of reinventing the wheel.

The hardware abstraction layer (HAL) means you don’t have to worry about the quirks of the Photon device when you want to do something like configure a pin for analog input. It also enables the software you write to be portable across Particle devices. If you decide later that you need a cellular connection, you can port your firmware to run on the different Particle devices with little or no changes.

The feature APIs are critical to ensure that you don’t end up writing code for typical requirements like serial communication or Wi-Fi connection. They are built into the operating system so you can be up and running in minutes.

Another important aspect of Device OS is the ecosystem. Device OS is mostly compatible with Arduino APIs, so the Particle community already has many libraries available. Additionally, Particle verifies high-quality libraries to indicate that they are safe to use in serious projects. You will benefit from this in your application because you will be using a verified library to communicate with the DS18B20 temperature sensor.

Device Management Console

The device management console is a unique feature that illustrates the maturity of the Particle platform. Combined with Device OS, the management console simplifies the most difficult parts of operating a fleet of IoT devices such as performing over-the-air (OTA) updates. OTA updates are so simple, in fact, that you’ll use them in development to achieve a tight development feedback loop.

Aside from OTA updates, the management console allows you to do other interesting things such as:

  • Monitor events published from each device in your fleet.
  • Call registered functions on each device in your fleet.
  • Monitor device vitals such as signal strength, network latency, and memory usage.
  • Conduct on-demand health checks.
  • Connect integrations that push device events to other platforms.

DS18B20 Temperature Sensor

The DS18B20 is a digital thermometer that provides temperature in degrees Celsius with 12 bits of accuracy corresponding to increments of 0.0625°C. While some temperature sensors can be read simply by reading an analog voltage corresponding to the temperature, the DS18B20 performs its own analog-to-digital conversion. This allows for some advanced features, but makes reading the temperature more complex.

The DS18B20 communicates using the 1-Wire bus protocol, which requires only one data line (and a ground). When operating in this “parasitic” mode, the device derives power directly from the data line. When the data line is high, it powers the device and charges an internal capacitor. This capacitor then powers the device when the data line is low. This feature is useful when IO is extremely limited or when many DS18B20s are used together because it eliminates the need to run power to each temperature sensor.

Features like this can make a difference when choosing parts for large enterprise deployments. Each cent on a bill of materials (BoM) counts when devices scale to the millions. You won’t be using this feature, but it’s worth explaining why this feature might be desirable. Instead, you’ll use the optional third line on the DS18B20, which may be connected to 3.3-5V to power the sensor. The device operates more reliably when powered directly.

Getting Started

Before you start the next steps, you’ll need to create a Particle account and set up your Photon. Follow the instructions at https://docs.particle.io/quickstart/photon/. Particle has excellent documentation, so setting up your Photon should be a breeze.

You’ll need a few components to get started. Here’s a list to make sure you have everything:

  • Breadboard
  • Jumper wires
  • Particle Photon (with USB cable for power)
  • DS18B20 temperature sensor
  • 4.7k resistor (or close)

The 1-Wire bus requires an external pullup resistor to “pull up” the data line when nothing is driving the line. The DS18B20 datasheet recommends a 4.7k pullup resistor, but something close should work if you’re not using parasitic power.

Let’s arbitrarily pick D0 as the digital pin to connect the DS18B20 data line. (You can use any digital pin. You just have to remember which pin you’re using when you write your firmware.)

This is roughly how your circuit should look on the breadboard.

Here is the corresponding circuit schematic:

Software Setup

The next step is to write the firmware to read the temperature from the DS18B20. As mentioned, you’ll lean heavily on a community library that exists to use the 1-Wire bus. Most of the work is already done for you, so reading the sensor is easy.

Particle allows you to write firmware locally using a Visual Studio Code extension called Particle Workbench, or you can use their web integrated developer environment (IDE). This tutorial demonstrates the web IDE since that’s the easiest way to get started.

At this point you may be wondering, “How will the code I write on the web get flashed to the device?” Developers with experience using Arduino or Raspberry Pi are used to flashing new firmware using a USB device, and of course you can do that with the Photon as well. However, the easiest way to flash firmware is via OTA updates using the Particle cloud. Once you do the initial setup on the Photon (just follow the instructions), the device is connected to the cloud and ready to receive firmware updates.

First, you’ll add the OneWire library to your project. This is a software library for communicating with the 1-Wire protocol.

Open the Particle Web IDE at https://build.particle.io/build and click the Libraries button on the left panel to add a library. Search for OneWire and it comes right up. Note the badge next to the library indicating that it is verified by Particle. Anyone can publish a library, so the badge gives us some confidence that it’s of high quality.

Then click the library to see its details. Clicking the blue “Include in Project” button will include the library in your project.

Now you can start writing code. This OneWire library includes code specifically to interface with the DS18B20, but you must manually include the DS18.h header in addition to the OneWire.h header.

The GitHub page for the library includes a sample that does exactly what you’re looking for. Here’s the final firmware for your device:

#include <OneWire.h>
#include <DS18.h>
 
DS18 sensor(D0);
 
void setup() {}
 
void loop() {
  if (sensor.read()) {
    Particle.publish("temperature", String(sensor.celsius()), PRIVATE);
  }
}

Let’s unpack what this code does. First, you include the necessary library header files. Then you instantiate a global DS18 object called sensor and assign it to pin D0.

Next, you create the setup() and loop() functions that will be called by Device OS. The setup() function is called once at device startup, and the loop() function is called continuously. You don’t require any setup, so that function is left empty.

In the loop() function, you want to read the temperature. The temperature is not always able to be read, as indicated by the sensor.read() function returning false. When it returns true, the temperature is available to read using sensor.celsius().

Device OS exposes an API to publish events to the Particle cloud. You do this using Particle.publish(). You give the event a name and a value. You also set the PRIVATE flag to indicate that only the owner of the Photon can subscribe to this event. Specifying a PUBLIC event will allow anyone on the Particle cloud to subscribe to the event.

Click the Flash button at the top left to send the firmware to your Photon. Once the update is complete, go to the device console and you should see the temperature events published there.

Potential Next Steps

Here are some potential next steps left as an exercise to the motivated reader:

  • Add more sensors to your setup and publish those events to the Particle cloud as well. Humidity and temperature are often measured together, for instance.
  • Add an RGB LED to display different colors based on the temperature in the room.
  • Add a web application to display the live temperature data.

How to work with us

  • Contact us to set up a call.
  • We will analyze your needs and recommend a content contract solution.
  • Sign on with ContentLab.
  • We deliver topic-curated, deeply technical content to you.

To get started, complete the form to the right to schedule a call with us.

Send this to a friend