Posted in raspberrypi, aws, cloudwatch

header image

Did you ever wonder how to bring your mini computer to the cloud?

I always was under the impression that the cloud services are very expensive and it would be impossible for people like me to use them. But I was wrong, cloud giants offer several services for free. Even though there are more limited, they can provide value on a personal/home and even startup project. There are many cloud services providers out there: Azure, Amazon Web Services (AWS), Google Cloud, etc. Because of the competition, most of them offer something for free or for a limited period (12 months).

What’s the best service to learn? It should be fun, interesting and easy to use. From so many out there to chose, the only that matched the description was AWS CloudWatch. This product allows us to publish a value at a point in time. If we publish enough points we will end up with a time-series. We can have up to 10 time-series.

For any thought/question/problem, use the comment section and tell me your thoughts on this. 

What's the hardware prerequisite?

raspberry pi dht11 sensor with aws inside

I always start with the hardware, because the only thing that remains is to focus on the code.

For this how-to you are free to choose/imagine any sensor you would like. I have chosen the DHT11 sensor (it’s the only sensor that I have, not much of a choice) that will collect the humidity and temperature. If you are going this way, you will also need 3 wires and a resistor of 6KΩ - 10KΩ (ohm). I know that are components out there which already contain the resistor, if you own one of them, you don't need a resistor.

I the image below shows how the components are connected between them. The good part of this sensor is that it can be connected at 3.3V or 5V (red wire), your choice! The blue wire is the data wire, used to read the values of interest. The resistor needs to be connected between this two pins (red and blue). The black wire is connected to GND. 

Use this table for writing representation of the cable connection that I made. 

DHT11SignalRaspberry Pi Zero
13.3V ~ 5.5V 4 (VCC)
2Data 7 (BCM 4)
3not used 
4Ground 6 (GND)

How do I setup my AWS account?

Although this part looks a little bit scary if you never had any contact with AWS. You don’t have to worry about this, you are in good hands.

The first thing that you have to do is creating your account for free at .

After you log in to the console, you might feel intimidated by the number of services, luckily, we need to use just one, CloudWatch. Maybe I lied, we need to use 2 of them (IAM – Identity and Access Management). Before going on, you can go on an adventure on your own by exploring the console.

For what is IAM used for? Simply put, we need a way to authenticate and publish the metrics. In this service, we will create a user that will have the rights to publish metrics in CloudWatch.

Go to IAM service in the AWS console and click on the Add user button, give it a name and make sure that you select the Programmatic access

aws iam add user


In the next step, we will grant permissions to the user in order to be able to publish metrics in CloudWatch. To keep things simple, we will choose an existing policy, CloudWatchFullAccess. Make sure that you check the checkbox in order to give the rights. 

aws iam attach policy for cloudwatch full access

One last step and we are done. Hit next until you stumble on to the Access key ID and Secret access keyDon’t close the window, the Secret access key will be shown only in this place. Save them in a safe place, we need them in the next phase.

aws iam save credentials step

I am all set; how do I publish metrics in CloudWatch?

Remember what I said at the beginning? We need to publish one point at a time. 

The first thing that should be done is creating the CloudWatch client. Replace the INSERT_YOUR_ACCESS_KEY_ID_HERE and INSERT_YOUR_SECRET_ACCESS_KEY_HERE with the values saved above. Make sure that you are in the right region. In the region that you configure, you will be able to see the metrics.

In order to publish metrics to cloudwatch, we need to use the putMetricData method from the API. This method requires some parameters to be set and are explained below:

  • MetricName - the name of the metric, in this case, Temperature and Humidity
  • Dimensions - it's used to identify the metric, as a best practice I recommend to include the source of the metric (SensorType: DHT11)
  • TimeStamp - the moment in time when I read the metric. Using new Date() will assure that I will log the metrics at the current time.
  • Unit - unfortunately CloudWatch doesn't have the "Custom" option, here should have been the °C for temperature. Humidity uses percent and it already exists.
  • Value - the value that is read from the sensor. 
  • Namespace - this can be anything you like

The most important fields from the above are TimeStamp and Value. This is what is really building or time-series. For full details, you can check the official documentation for CloudWatch.

We use setInterval to read the values from DHT11 every five seconds and insert them in the cloud.

Don't forget that you have to make some preparation before using the node-dht-sensor library. If you are stumbling in any problem, I described in a previous project how to make the setup.

const sensor = require('node-dht-sensor');
const AWS = require('aws-sdk');

var cloudWatch = new AWS.CloudWatch({
    region: "us-east-1" 
}); const DHT = 11; const SENSOR_PIN = 4; publishMetric = (metricName, value) => { var params = { MetricData: [ /* required */ { MetricName: metricName, /* required */ Dimensions: [ { Name: 'SensorType', /* required */ Value: 'DHT11' /* required */ } /* more dimensions */ ], Timestamp: new Date(), Unit: "None", Value: value } /* more items */ ], Namespace: 'TheHumbleCode' /* required */ }; cloudWatch.putMetricData(params, function(err, data) { if (err) console.log(err, err.stack); // an error occurred else console.log(data); // successful response }); } setInterval(() => { const sensorResult =, SENSOR_PIN); console.log(`The current temperature is ${sensorResult.temperature.toFixed(2)} °C and humidity ${sensorResult.humidity.toFixed(2)} %`); // publish sensor values in Cloud Watch publishMetric("Temperature", sensorResult.temperature) publishMetric("Humidity", sensorResult.humidity) }, 5000)

In case you need it, I'm using the following package.json file.

  "name": "cloudwatch-sensor",
  "version": "0.0.1",
  "description": "A tool that publishes metrics to cloudwatch (e.g. temperature and humidity).",
  "main": "publisher.js",
  "author": "TheHumbleCode",
  "dependencies": {
    "aws-sdk": "^2.228.1",
    "node-dht-sensor": "^0.0.34"

First thing, install all the dependencies for this project and run it by executing the following commands:

npm install
node publisher

The metrics should start publishing and in ~15 minutes can be seen in AWS CloudWatch console.

If you connect to your Raspberry Pi by SSH and plan to leave the terminal open for several days, you need to use the screen command, otherwise, it will close after a while. If you don't have the command, you can install it using the following command:

sudo apt-get install screen
screen #open a new screen and start the publisher

After you start the program on the screen, you can detach from it by pressing Ctrl+a d. If you wish to reattach to the screen to make sure that everything works fine, run this command:

screen -r #reattach to the screen

if you stumble on any problem/curiosity/question use the comment section to address them, I'm more then glad to help/answer you. 

Final thoughts

Showing temperature and humidity metrics in CloudWatch

In the above results, I monitored my room for more than 1 day. From the time-series, ti can be seen a drop in temperature and humidity. That happened because I opened the window very early in the morning all the values dropped but recovered after during the day.

CloudWatch allows setting alarms on the metrics that you publish. If you left your AC ON while you are out, you can receive an email that the temperature dropped/increased to a certain level. And other handy situations that can be built with different sensors. 

But most importantly, stay curious!