Hey guys, how much fun are microcontrollers? They’re your own miniature slave to do your bidding in whatever way you choose, however as fun and all as using them to do math and crunching numbers, the real joy is seeing them interact with the outside world. Receiving data from sensory input and making decisions based of that data is what it’s all about.
But imagine if you could capture data remotely from one device, and use it on another device, or log it in a way that humans can easily read and process. Sending information from one place to another requires a network of some kind, and while Wi-Fi is great, it’s not always practical. And this is where the Electron board from Particle comes in.
If you haven’t already, I highly recommend that you go and check out our tutorial on Getting Started with Particle Electron as it covers many of the features and elements that we’ll be talking about and using today.
Today we’ll be looking at a few different applications of remote sensing, with a very unique goal. It should be noted that you can use any sensor you want, the only thing that changes is the manipulation of the input data. However, one of the more controversial points around the office is the health of our aptly named ‘Happy Plant’. Requiring constant attention, with dire consequences should anything happen to it, a personalised soil monitor for it should do the trick.
The Goal
We’ll go through a few different ways to use our sensor data over a cellular connection, logging it periodically, and accessing it using another Particle board.
The Gear
To get all the parts required for this tutorial check out the tutorial wishlist: http://coreelec.io/18
Logging Data to Google Drive
One of the simplest ways to log data is to save it in a Google Sheet using the IFTTT integration with Particle. This is incredibly simple to do, requires only 8 lines of code, and an IFTTT recipe.
The Circuit
As you can see, this circuit is as simple as it gets, we’re simply connecting up our soil sensor to an analogue pin, with the power and ground wires connected up respectively.
**Note that whilst this tutorial is covering the Particle Electron board, it will also work just as well with the Photon board, provided that you’re connected to the internet via Wi-Fi**
The Code
The program we’re using here is about as simple as it gets. All we’re doing is creating a Particle variable that can be accessed through the Particle.io cloud, and the IFTTT automation is doing all the work for us.
Because of the data usage required for the Electron, we’re going to flash our code over USB using the Particle CLI (Command Line Interface). If you’re not sure how to do this, refer to our Getting Started with Particle Electron tutorial.
//global variables int sensor = A5; int sensorValue; void setup() { //declare our cloud variable that can be accessed using the Particle.io API Particle.variable("sensorData", &sensorValue, INT); pinMode(sensor, INPUT); } void loop() { //wait 10 seconds, the read out sensor input delay(10000); sensorValue = analogRead(sensor); }
Once you’ve flashed your code, don’t expect anything to happen yet, because we haven’t created our IFTTT recipe yet.
Configuring IFTTT
Now we’re going to go through and create the IFTTT recipe which will listen to our Particle variable and action it if anything changes.
Before going on, take a look at our article on Particle and IFTTT which goes through the setup of creating an IFTTT recipe. You’ll also need a Google account to use Google Docs.
Choose Particle as your trigger channel, Google Drive as your action channel, and configure the options as shown below:
Everything should now be running, so check the Google sheet that IFTTT created for you and see your data coming in. Bear in mind that the Google Drive API that is being called has a limit on the frequency you can ping it to avoid clogged servers, so it should average a new log every 2-3 minutes.
Requesting and Viewing Data on a Photon
This is all well and good, however sometimes it can be frustrating to access a spread sheet every time you wanted to view the data your sensor is picking up. Now, we’re going to use a second Particle board to request and display the sensor data that our remote Particle Electron is processing. Bear in mind that to follow this example, you’ll need a second Particle device (the Particle Photon is recommended to save on data usage).
The Circuit
The circuit for our remote sensor will stay the same, however we need a way to view the data on our second Particle device. For this tutorial we’ll be using an OLED character display, however you can use any type of display you like.
For more info on using character displays, check out our tutorial here.
The Code
The code here again is remarkably simple. All we’re doing is using a simple button to publish and event to the Particle cloud that our remote sensor is subscribed to (listening for), and when it receives this event, it reads the sensor value, then publish it on a separate event which our monitoring device can subscribe to, receive the data, and print it onto a character display. As all of this is happening through the Particle cloud, there’s no need to use IFTTT.
The first piece of code is for the Electron connected to the sensor:
//global variables int sensor = A5; String sensorReading; int lastReading; void setup() { //declare our cloud variable that can be accessed using the Particle.io API Particle.subscribe("checkSensor", sensorHandler); pinMode(sensor, INPUT); } void loop() { } void sensorHandler(const char *event, const char *data) { sensorReading = analogRead(sensor); Particle.publish("sensorResults", sensorReading); }
And this second piece of code is for the Photon that will be used to monitor the Electron's data:
// This #include statement was automatically added by the Particle IDE. #include "Adafruit_CharacterOLED_Particle/Adafruit_CharacterOLED_Particle.h" Adafruit_CharacterOLED lcd(OLED_V2, 6, 5, 4, 3, 2, 1, 0); int button = A0; int lastPress; int buttonState; void setup() { lcd.begin(16,2); lcd.clear(); pinMode(button, INPUT_PULLUP); Particle.subscribe("sensorResults", resultsHandler); } void loop() { buttonState = digitalRead(button); if(buttonState == LOW && (millis() - lastPress) > 1000) { lastPress = millis(); Particle.publish("checkSensor"); lcd.clear(); lcd.setCursor(0,0); lcd.print("Request sent"); } } void resultsHandler(String event, String data) lcd.clear(); lcd.setCursor(0,0); lcd.print("Data Received:"); lcd.setCursor(0,1); lcd.print(data); }
And there you have it! When you push the button you should receive your sensor reading, and you could even go an incorporate an IFTTT recipe to include Google Drive logging as well!
Hopefully you’ve learnt a few new ways to use your Particle gear, and learnt about remote sensing with Particle Electron. Get making!