How to Connect an Arduino with Carriots and build a system for measuring weight or force.

0. Introduction

This easy tutorial helps you build a system for measuring weight or strength.
For this, a Arduino able to measure the pressure produced by the force in a force sensitive resistor sensor is used.
In this project you will learn how to:

  • Connect an Arduino to Carriots to send data streams using Carriots HTTP REST API.
  • Build an Alert App on Carriots by programming a Listener to send an email using the SDK.

Difficulty:

Ingredients

  • 1 x Arduino Uno. An alternative is to use a Rasbberry-Pi
  • 1 x Ethernet Shield. You can also use a WiFi or GRPS Shield
  • 1 x Tiny Breadboard
  • 1 x Force sensitive resistor (FSR)
  • 1 x Resistance. Value of 10 KΩ
  • 1 x Resistance. Value of 220Ω
  • 1 x Led
TOP

1. Project Schema

This project is a quick way to create an alarm app and understand the basics of combining Arduino and Carriots.

Here is an overall diagram of the project:

  • Schema

Arduino is programmed to send a data stream to Carriots stating whether or not detecting weight.

All the data streams sent by Arduino are collected and stored in Carriots. Carriots is a huge database that collects all the information that your sensors send.

In addition to storing data, the true power of Carriots is to let you build Apps very quickly with few lines of Groovy code. In this scenario we are going to build a very simple Alert App that sends an email to you in case that Arduino detects weight or not.

With Carriots you can build very complex Apps to support all the logic of your product on the cloud and integrate it with other devices and IT systems. But for now let’s keep it simple.

Although this Alert App is very simplistic, you can used for example to measure the weight by creating a digital scale.

TOP

2. Arduino. Connections description

The connections in Arduino are extremely simple. You can see in the diagram below.

  • Diagram
  • Connections

TOP

3. Carriots Project and Device Registration

Device

If you are registered in Carriots, you have a default device already created for you.
Check your control panel and see what it looks like.
Basically you need the device id_developer that might be something like defaultDevice@myusername.
But if you want, you can create a new device and use it in this example.

Apikey

Now, go to your control panel “My account menu” and check your Apikey.
Its a big alphanumeric token like:
98346673a6377ef1fde2357ebdcb0da582b150b00cabcd5a0d83045425407ab4.
You need this number to complete the example.

TOP

4. Data Transferred from Arduino to Carriots

From Arduino you have to build a HTTP request and send the data.

  • HTTP request
    POST /streams HTTP/1.1
    Host: api.carriots.com
    Accept: application/json
    User-Agent: Arduino-Carriots
    Content-Type: application/json
    carriots.apikey: YOUR APIKEY HERE
    Content-Length: YOUR CONTENT LENGTH HERE
    Connection: close
  • Data
    {
    "protocol":"v2",
    "device":"YOUR DEVICEs ID_DEVELOPER HERE",
    "at":1356390000,
    "data":{"light":"YOUR DATA HERE"}
    }

TOP

5. Code implemented on Arduino

This is the code to arduino. Note that some values should be replaced by your own values.

TOP

6. Checking Data collected in Carriots

If you get here, your device must be sending streams when you turn on and turn off the lights.

It's time to test our control panel and check if we have new streams.
Go to “Data management” → “Data streams” and you will see your data.

  • menu option
  • new data

TOP

7. Creating an Alert with Carriots Listeners

A listener in Carriots can be associated to any hierarchy entity, from Project to Device. If you associate a listener to a project, all devices below the services of that project will be affected. If you associate a listener to service, all devices below the groups of that services will be affected. And so on.

In this example, we create a listener associated with our device. This listener waits for an event to occur in the context of the device and then evaluate the content.

To create a listener go to the control panel, then to “Rules” → “Listeners” and click on the new button. Select "Device" as "entity type" and select your device on the "Id" field.

Fill the fields with the following values and save the new listener

  • Name: The name that you prefer
  • Description: The description that you prefer
  • Entity type: Device
  • Entity id: Your id device
  • Event to listen: data_persisted
  • If expression:
    true
  • Then expression:
    import com.carriots.sdk.utils.Email;
    def email = new Email ();
    email.to="YOUR EMAIL HERE";
    if (context.data.weight == "OFF") {
    email.message = "Sensor detects weight, alarm " + context.data.weight;
    } else {
    email.message = "Sensor no detects weight, alarm " + context.data.weight;
    }
    email.send();
  • Then rule: leave empty
  • Else expression: leave empty
  • Else rule: leave empty
  • Be sure to have your listener enabled if you want it to be executed

Its time to test our listener. Send a data stream to this device, check your control panel and your email!!!

TOP

8. From here on: Enhancing the project

Now you know how to connect an Arduino device to send data to Carriots and how to create a simple App with a single Listener and some SDK programing in Groovy. But you can enhance this project in many ways:

  • Try to add a motion sensor to the Arduino and send at least two values: weight and status motion
  • Create your own control panel to check the status of the Arduino, by building an HTML page with a request to our API to ask for the status value of your device. You can see more in Others APIs & Carriots

TOP