Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

IoT Arduino Programming: Monitor Enviroment

DZone's Guide to

IoT Arduino Programming: Monitor Enviroment

In this IoT Arduino programming tutorial, the Arduino board is connected to a set of sensors and an ethernet shield. Sensors used in this IoT Arduino programming project are:

· IoT Zone
Free Resource

Learn how Bluetooth mesh helps you create industrial-grade networks. Download the mesh overview.

Data collected using sensors is sent to the cloud using TheThings.io. This IoT Arduino programming example shows how easy it is to build an IoT project. The final result of this IoT project is shown below:

IoT Arduino ProgrammingAs a result, this IoT Arduino programming tutorial will teach you how to build a dashboard using three different environment parameters:

  • Temperature
  • Humidity
  • Pressure

If you are not familiar with IoT projects, it is useful to read this tutorial. This is an interesting project because you can use it to monitor the air in your room, to predict weather, or to know if your room is comfortable.

In this IoT Arduino programming tutorial, the Arduino board is connected to a set of sensors and an ethernet shield. The sensors used in this IoT Arduino programming project are:

  • DHT11 (Temperature and Humidity sensor)
  • BMP180 (Pressure sensor)

Both sensors can be connected to +5V and they are perfect for an Arduino Uno. The details of the sketch and the way the sensors are wired to Arduino are not important, so it will not be shown here (but it is very very simple!).

The code to read data from DHT11 and BMP180 is shown below:

#include <Adafruit_BMP085.h>
#include "DHT.h"
#define DHTPIN 2 //Arduino PIN
#define DHTTYPE DHT11 // DHT11 Type

DHT dht(DHTPIN, DHTTYPE);
Adafruit_BMP085 bmp;

void setup() {
Serial.begin(9600);
Serial.println("Connected");
bmp.begin();
dht.begin();
}

void loop() {
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
float presPa = bmp.readPressure();
float presMb = presPa * 1013.25 / 101325;
}

One important thing to remember is to import the BMP180 library. You can do so using the Library Manager feature in Arduino IDE and look for BMP180 sensor:
 Arduino BMP180 sensor library

Let us focus our attention to the most important part in IoT Arduino programming: how to send data to the cloud. Of course, you have to use an ethernet shield or WiFi shield to connect Arduino to the Internet.

As cloud IoT platform, this IoT project uses TheThings.io. This IoT cloud platform is very easy to use and has interesting features like creating dashboards with the data you send.

You can create a free account and try it. This platform supports REST services so that you can call services using an Arduino HTTP client. Anyway, TheThings.io provides an Arduino library to simplify the process. You can download it from its GitHub repository and install it in your IDE. Once the library is ready, it is important to setup the Ethernet shield properly:

...
// Mac address
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };  // Arduino mac address
EthernetClient client;
IPAddress ip(192, 168, 1, 178); // Arduino IP Add
IPAddress dns1(8, 8, 8, 8);
IPAddress gw(192, 168, 1, 1);
...
void setup() {
...
Ethernet.begin(mac, ip, dns1, gw); // You could use DHCP instead
...
}

The next step is configuring the TheThings.io platform to accept data coming from Arduino. In this case you have to create a “thing” that is like a physical object that sends data. Below is a screenshot of a thing created for this tutorial:
Not arduino programmingThe important thing is the TOKEN that is the unique ID to identify your thing (like an Arduino board). This TOKEN has to be sent when connecting to TheThings.io to identify your Arduino board. Once the platform is configured correctly, the final step is setting up the code for sending data:

// TheThings tkoken
#define TOKEN "_xxxxxxxxxxxxxxxxxxx"
// The thing server address
thethingsiOEthernet thing(TOKEN);
....
void sendValues(float temp, float hum, float pres) {
delay(1000);
Serial.print("Sending data to TheThigs...");
thing.addValue("temp", String(temp));
thing.send();
delay(1000);
thing.addValue("hum", String(hum));
thing.send();
delay(1000);
thing.addValue("pres", String(pres));
thing.send();
}

You can monitor the value read from the sensors sent to the cloud:
IoT Arduino data cloudUsing the dashboard feature you can create a custom dashboard to show charts created from the data:
IoT Arduino pressure sensororIoT Arduino dashboard cloud data

It is possible to get real time value or historical values.

At the end of this IoT Arduino Programming tutorial, you gained the knowledge how Arduino sends data to the cloud and create dashboard using this information.

For a deeper look into Bluetooth mesh, check out this technical insight for developers.

Topics:
arduino ,board

Published at DZone with permission of Francesco Azzola, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}