Temperature Logger powered by Arduino + Orchestrate.io

This blog post gives an overview of how I created a simple IoT project using Arduino, a Temperature Sensor and the excellent Orchestrate.io Database as a Service. I have also included source code at each step. 

First up, let me explain what the eventual goal is and how this is a first step in that process. The goal is to setup a series of low cost climate/environment modules that capture various types of data like temperature, humidity and more. Then take all this data and put it in the cloud where we can eventually build out dashboards, alerts and more. This blog post explains one single setup where we create a system comprising an Arduino Uno, a temperature sensor, a Python application that can read the data from the Arduino Uno (yes, I did not use an Arduino Internet Shield) and post that data to the cloud.

Towards this goal, chosing Arduino as a microcontroller is a no-brainer though I do plan to look into other controllers in the near future. Once our system is collecting data, ,the important thing is where do I put this data. The Cloud comes up as a rational choice to eventually have all Temperature Sensor stations push their data and we can monitor and build dashboards from a single place. The choice was to build a backend using tools like Google Cloud Platform’s App Engine but that would mean quite a bit of extra work for something that is not immediately central to this project. I went with Orchestrate.io, a Database as a Service provider that makes it dead simple to funnel your data from sources into your database in the cloud, as the post will eventually show.

The Hardware Setup

I used the following:

  • Arduino Uno microcontroller
  • LM35 Temperature Sensor
  • Eventually we will have the Raspberry Pi that interfaces with the Arduino to read and transmit off the values but to validate things for now, the Uno was powered via a laptop/desktop with Python installed on it. The communication between the Uno and the PC is via serial port communication.

Arduino Uno + Temperature Sensor Setup

Here is how the LM35 sensor is connected to the Arduino Uno board.

Temperature_Sensor_bb

Ofcourse, we used a breadboard to connect all this together but I am simplifying the diagram here so that you know what is connected to which pin. The LM35 has 3 pins. The first one goes to the 5V power pin on Arduino, the 3rd one is the GND and the middle pin is the VOUT where it emits out the values that we need to capture. We connect this to the Analog Pin (A0) on the Arduino. We can then write our Arduino code to read that value, as is shown next.

Arduino Code

The Arduino Code is straight forward as given below:


float temp;
int tempPin = 0;

void setup()
{
 Serial.begin(9600);
}

void loop()
{
 temp = analogRead(tempPin);
 temp = temp * 0.48828125;
 Serial.print(temp);
 Serial.println();
 delay(10000);
}

You will notice in the loop that every 10 seconds, we are printing out the temperature value that was read from the Analog Pin (#0)

If you run the Serial Port Monitor that comes with the Arduino IDE and if the Arduino is powered up and connected as per the diagram shown, then you will find the Temperature value being printed on the Serial Monitor as given below:

orcpy2

Once this happens, we know that the Arduino setup is looking good and all we need to do now is to write a client program on the PC that interfaces with this Arduino, read the values via the Serial port and then push them to the Cloud (Database Service).

But first, let us take a look at the Orchestrate.io Database Service setup that we did.

Orchestrate.io Setup

Orchestrate.io is a Database as a Service provider. One of my reasons of going with Orchestrate.io is to eventually make use of their strengths in providing a solid REST API around your data. What this means is that I am just not interested in making sure that it is dead simple to store my data in the cloud. Eventually I will need an API that allows me to do sophisticated searches, time ordered events and more and Orchestrate.io fits that model.

One can get started for free with Orchestrate.io to check out their service and which is what I did. It gives you one application that you can create for free along with a 50K API calls limit / month, which is good enough for my prototype for now.

Once logged into Orchestrate.io, I created the Application by clicking on New Application. It popped up a dialog where we provided the Application name (Temperature_Log) and selected a data center as shown below:

orcpy3

That’s it. The Application is created and the next step was to just define the Collection. A collection is similar to a Table into which all the data records would go. Orchestrate will figure out from the data that you upload and create the schema accordingly for you. So all you need to do for now is to define your collection and which is what I did. 

Simply click on New Collection and then I gave my collection name as given below:

 

orcpy4

 

That’s all I needed to do to setup my Application and one collection in Orchestrate.io.

If you go to the Orchestrate.io Dashboard, you will see the collection for your application along with the API KEY that we shall use soon. The API KEY will be used when posting data into Orchestrate from your client application and is used to identify yourself to the Orchestrate service.

Python Code

Now, let us move on to the Python code that interfaces over Serial port to read the temperature values from the Arduino setup and posts that data into Orchestrate.io.

The steps are simple:

1) We initialize 2 things: the Orchestrate.io API Client using our Key. And then we initialize the serial port communication via which we will be reading the Temperature values that the Arduino unit will be emitting every 10 seconds. You need to figure out which Serial Port on your machine is interfaced to the Arduino.

2) Every 10 seconds, the code will read the value from the Serial Port. We can obviously build in more validations in the code, but this is good for now to demonstrate how all the pieces come together.

3) Once we get the data, it will use the Orchestrate.io REST service to post data into your Application data collection. To make things easier, Orchestrate.io has a solid list of client language APIs available that you can use to ease your task. I used the Python client library for Orchestrate.io. To setup that library all one had to do was

pip install porc

The important thing to note in the POST to Orchestrate.io Service is the client.post code. Here, all we are doing is specifying the collection (Temperature_Data) that we had created for our Application in Orchestrate.io and the Data. The Data is specified in JSON format and the fields that I am specifying are:

  • Temperature in Centigrade
  • Date of the recording
  • Time of the recording
  • Location Name (Weather Station Name).

Take a look at the Python client program below:


import serial
import time
from porc import Client

API_KEY = "Your API Key"
# create an Orchestrate.io client using the default AWS US East host: https://api.orchestrate.io
client = Client(API_KEY)

# make sure our API key works
client.ping().raise_for_status()

#Connect to Serial Port for communication
ser = serial.Serial('COM15', 9600, timeout=0)

#Setup a loop to send Temperature values at fixed intervals
#in seconds
fixed_interval = 10
while 1:
 try:
  #temperature value obtained from Arduino + LM35 Temp Sensor
  temperature_c = ser.readline()
  #current time and date
  time_hhmmss = time.strftime("%H:%M:%S")
  date_mmddyyyy = time.strftime("%d/%m/%Y")

  #current location name
  temperature_location = "Mumbai-Kandivali"
  print temperature_c + ',' + time_hhmmss + ',' + date_mmddyyyy + ',' + temperature_location

  #insert record
  response = client.post('Temperature_Data',
                         {"location":temperature_location,
                          "date" : date_mmddyyyy,
                          "time" : time_hhmmss,
                          "value" : temperature_c})
  response.raise_for_status()
  print "Record inserted. Key = " + response.key
  time.sleep(fixed_interval)
except ser.SerialTimeoutException:
  print('Error! Could not read the Temperature Value from unit')
  time.sleep(fixed_interval)

Checking our data

The final step was to validate if our data was being transmitted successfully and into Orchestrate. All you need to do is to ensure that the Python code executes , you do get the Record inserted message.

A sample run of the Python code is shown below:

orcpy1

Once that is done, come back to Orchestrate Dashboard and visit your collection. A sample snapshot of the Search Query for my collection is shown below:

orcpy7

This validates the end to end working of the project.

In Summary

Arduino makes electronics prototyping fun. With languages like Python and services like Orchestrate.io, the process of collecting / transmitting  / saving the data in the cloud is made simple too. I hope to explore Orchestrate.io in more projects for their other powerful features like Time Events and Graphs.

You can use the steps outlined below to build your own version of a Data Collection IOT Project that uses the Cloud to store and analyze its data.

Till next time.

IMG_0816

 

Advertisements

6 thoughts on “Temperature Logger powered by Arduino + Orchestrate.io

  1. I have been looking for a cloud service to back my IoT for a few days,
    I have found many services, and I’m kind of confused of which one should I go with.
    I can categorize the results I found into three parts:

    1- Pass Services: such as Google cloud, AWS, and Azure, these are very powerful solutions and salable, but they need more work to get things done.
    2- Baas Services: such as Parse, Firebase, and maybe Orchestrate as you mentioned, these are easy to use, solid, but not salable.
    3- IoT services: such as TheThings.io, buddy.com, and thingspeak.com. I don’t know very much about these, but they claim that they provide the best solutions for IoT projects.

    I’m really confused which way should I go with, I’ll appreciate your opinion about which you think is the best for a project, let’s say with 10,000 devices.
    Thank you

    1. Each of the services that you mention i.e. 1,2,3 all have their use cases and can apply to a wide range of projects. I cannot speak much for the services that you have listed in (3) since I have not used them but there must be something extra that they are providing from an IoT perspective for them to make sense to be used in your project. You will need to figure that out.

      What you will get from services in (3) would be a higher level abstraction of service that will help you to focus on your code more and let them take care of other nuances.

      With (1) — you will be pretty much on your own to write every single thing because they are giving you infrastructure – so you go ahead write your code using a Stack of your preference, deploy it and make it run. Ofcourse they are scalable to a large extent and can address any kind of growth. But you will have more work to do here instead of just focusing on your app.

      With (2) – these are managed services running for you. You focus on your app and let them take care of the backend for you. They are scalable too – so do not assume that they cannot scale as you grow. Ofcourse do a price wise comparison.

      If I were you and if I had to go one way or the other, I would rather focus on the IoT use case you are addressing rather than the infrastructure and worrying about it.

      I would probably do a POC with one of the BaaS services and one of the IoT Services and see the pros and cons.

      Agreed that it is not an easy decision but still hopefully I have given some general guidelines.

      Cheers and All the best for your project.

      1. Thank you very much for your efforts, actually that’s what I was thinking of, but I was not sure about it.
        Thank you again, and best wishes for you 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s