IoT: Rapid prototypes using Particle
Most IoT projects start with an idea, I.E „we should be able to predict problems based on temperature / power consumption / virbration“. Microsoft Azure’s „Machine Learning“ provides interesting options to analyse and interpret data gathered from IoT devices. However in order for them to work, a lot of data has to be gathered up front and of course working with real data instead of mocked data ensures best results.
Implementing the full chain from hardware sensors to a front end application involves a lot of work on different technology levels, which therefore takes some time to develop. In order to gather data early and to try and adapt the analysis or data gathered, an iterative prototyping approach is useful here. When I was searching for a good IoT platform that already provides good infrastructure I found the „particle project“.
The Particle project and the Photon device
Initially founded as a Kickstarter project, the Particle project is a small but versatile IoT Platform that is in my opinion very suitable for rapid prototyping. The following key features are noteworthy:
- Similar to develop on as an Arduino
- Integrated WIFI Module
- Post stamp sized
- Already integrated into a Cloud system
- Can be accessed using a rest interface
- Very affordable at 19$
I have never worked on embedded systems and consider myself more a middleware/frontend developer. However setting up the device, programming the sensor access and accessing the particle cloud was very easy and I made progress very fast. It allowed me to access and manage the IoT devices directly from by web application.
Ok, I think that’s enough introduction for the moment, lets take a look at what I made to try out the concept:
Its a very simple „weather station“ that allows to capture temperature and humidity using a DHT22 sensor on the particle device. A web page accesses the device using the particle cloud and displays the values.
I will not go into detail about the Website’s implementation (its implemented in standard ASP.Net MVC), but concentrate more on the device itself and how to interact with it.
Programming the IoT device (Photon)
As already mentioned, the code for the device (called Photon) looks very similar to what you might already know from an Arduino. However what really stands out is the possibility to register named „Methods“ and „Variables“ and even „Events“ that can be accessed using a rest interface. Lets take a look at the code that runs on my device:
void setup(void) { Serial.begin(9600); Spark.variable("currentState", ¤tState, INT); Spark.variable("airtemp", ¤tTemperature, DOUBLE); Spark.function("water", startWatering); } void loop() { delay(1000); Serial.println("Loop"); ReadAirSensorToVariables(); //other code here } int startWatering(String wateringLocation) { if (wateringLocation == "First"){ Serial.println("WATERING: Zone 1"); return 1; //Todo: Execute watering process } if (wateringLocation == "Second"){ Serial.println("WATERING: Zone 2"); return 2; //Todo: Execute watering process } return -1; } void ReadAirSensorToVariables(){ //Retrieving information from Air sensor; DHT22_Air.acquire(); while (DHT22_Air.acquiring()); int result = DHT22_Air.getStatus(); switch (result) { case IDDHTLIB_OK: currentAirHumidity = DHT22_Air.getHumidity(); currentTemperature = DHT22_Air.getCelsius(); break; //Skipped error handling code default: Serial.println("Unknown error"); break; } }
The example code is shortened to fit this blog post so it does not contain basic initialization code and variable definitions, but it basically does the following:
- Line 1: Setup
This is where things are getting interesting. It allows us to define any method or variable so we can access any value we compute on the device, or execute actions just by registering the functions and variables. The device will use this to register itself in the cloud, and you will be able to call the registered functions.
- Line 9: Loop
This is the devices „Main“ method. As with the Arduino devices, code placed here is executed as fast as possible. In this very simple example we only call „ReadAirSensorToVariables“ to read out sensor values and store them in our variables.
- Line 17: „Public Method“
By registering this function in the „Setup“ function, it can be called from any client that is connected to the cloud. in my web application example this function can be executed by pressing a button in the UI.
- Line 30: „private functions“
This is „helper code“ to access the sensor. it is specific to the sensor and will not be explained in more detail.
Calling the device from any client
To access our device from web application we just have to send REST requests to the particle cloud, for instance the following to retrieve the air temperature:
„https://api.spark.io/v1/devices/0123456789abcdef01234567/airtemp ? access_token=1234123412341234123412341234123412341234“
This can be sent using .Net’s standard HttpClient. It will return a response in the following format that can be parsed.
// EXAMPLE RESPONSE { "id": "0123456789abcdef01234567", "name": "YourDevice", "connected": true, "return_value": 28.2 }
Conclusion
For me it was very convenient that the particle project takes care of the whole Cloud interfacing. I was able to concentrate on building the client application for the end user and the complexity of managing the devices, connecting them to the internet etc. was taken care of already. In this way, I was able to get a first end to end example running within a couple of hours, which was amazing.
I think for IoT prototyping this might be a very nice platform to use.
Drawbacks: The company selling the devices is still ramping up, so it is hard to tell how it will evolve. They’ve just released their first batch of devices and are currently in production phase for the next one, so fast shipment is currently an issue.
Also for a productive IoT project, I believe that a customer would want more control and independence over his project. This means building up your own IoT-Cloud infrastructure, perhaps also to develop your own embedded device to have full control and independence.
Further Information
All necessary information to get started as well as a store to order devices can be found here: http://www.particle.io/