Introduction: Weather Station Using SAMIIO, Arduino, and Raspberry Pi

Picture of Weather Station Using SAMIIO, Arduino, and Raspberry Pi

This article demonstrates using SAMI with simple, off-the-shelf sensors and hardware. Specifically, we will be gathering climate data from a low-cost temperature sensor and relaying that data to the cloud via SAMI APIs. From there, we can analyze our collected data in real-time or historically. This autonomous combination of hardware, software and networking is now being referred to as IoT (Internet of Things).

This article assumes you have experience uploading code to an Arduino Uno with the IDE and running a Raspberry Pi.

Hardware components we will use in our demo

  • Raspberry Pi with a network connection
  • Arduino Uno with a breadboard
  • Temperature sensor (DHT11)
  • USB and power cables, plus wiring for the breadboard

Software we will write

  • A Node.js script running on the Raspberry Pi
  • A Sketch program running on the Arduino

You can download the code samples here.

Step 1: Build the Weather Station

Connect to SAMI (it’s free, really!)

  1. First, sign into the SAMI User Portal. If you don’t have a Samsung account, you can create one at this step.
  2. Click to connect a device. Choose the already defined device type “Temp Sensor”.
  3. Name your device using your name (e.g., “Dan Temp Sensor”).
  4. Click “Connect Device…”. You’re taken back to the dashboard.
  5. Click the name of the device you just added. In the pop-up, click “Generate Device Token…”.
  6. Copy the device ID and device token on this screen. You will use these in the code.

Step 2: Set Up the Arduino and the Temperature Sensor

Picture of Set Up the Arduino and the Temperature Sensor

Now let’s wire the sensor. We are using a DHT11 here. They are not the most accurate, but they are cheap and easy to use for our simple use case.

Using the Arduino IDE, upload the Arduino code (dht11.ino) to the Uno. This code reads the temperature data from the sensor and sends the value (in Fahrenheit) to the serial port every 2 seconds (you can change this parameter in the code later, since SAMI limits the number of messages per day).

Here is the code:

#include "DHT.h"

#define DHTPIN 2 // data pin

#define DHTTYPE DHT11

DHT dht(DHTPIN, DHTTYPE);

void setup() {

Serial.begin(9600);

dht.begin();

}

void loop() {

delay(60000); // 1 min

float f = dht.readTemperature(true);

if (isnan(f)) {

Serial.println("200"); // error

return;

}

Serial.println(f); 21

}

Step 3: Set Up the Raspberry Pi

Picture of Set Up the Raspberry Pi

Connect your Raspberry Pi to a monitor, mouse and keyboard. Ensure that ethernet or WiFi is working, and make sure the OS is up-to-date:

$ sudo apt-get update
$ sudo apt-get upgrade

If not already installed, install Node.js for ARM, then add the packages ‘serialport’ and ‘node-rest-client’ via npm:

$ npm install serialport
$ npm install node-rest-client

Now connect the serial port from the Arduino to the USB on the Raspberry Pi.

Finally, copy the Node.js code (weather.js) to the Raspberry Pi (use scp to transfer it, or simply create a file and paste the code). Insert the device token and device ID you collected from the User Portal into the placeholders in the code.


Below is the code:

var sami = "https://api.samsungsami.io/v1.1/messages";
var bearer = "Bearer INSERT_TOKEN_HERE";

var sdid = "INSERT_SOURCE_ID";

var serialport = require("serialport")

var SerialPort = serialport.SerialPort;

var sp = new SerialPort("/dev/ttyACM0", {

baudrate: 9600,

parser: serialport.parsers.readline("\n") 10 });

var Client = require("node-rest-client").Client;

var c = new Client();

function build_args (temp, ts) {

var args = {

headers: {

"Content-Type": "application/json",

"Authorization": bearer

},

data: {

"sdid": sdid,

"ts": ts,

"type": "message",

"data": {

"temperature": temp

}

}

};

return args;

}

sp.on("open", function () {

sp.on('data', function(data) {

var args = build_args(parseInt(data).toString(), new Date().valueOf());

c.post(sami, args, function(data, response) {

console.log(data);

});

});

});

Step 4: Fire It Up and View the Results!

Picture of Fire It Up and View the Results!

Let’s start the Node.js program on the Raspberry Pi from the terminal:

$ node weather.js

You should see a JSON output, which is the response from SAMI. Log into the SAMI User Portal once again.

View your device data as it’s generated by clicking the magnifying glass in the device box.

Where to go from here?

The sky’s the limit. The DHT11 also records humidity, and you can define your own device type with multiple fields using the SAMI Developer Portal. So, a logical next step could be adding humidity to the mix.


Also, wouldn’t it be interesting to know where your weather station is located? What about gathering GPS coordinates from an additional sensor—or even hardcoded, if the weather station will not be moved? That could certainly be added with more data fields.

There are other sensors that detect barometric pressure and wind speed. Why not capture a full array of weather data for your particular microclimate wherever you may be?

Dan Gross

Director

Samsung SSIC

Comments

JasonT81 (author)2017-08-13

Im stuck Just trying to upload the intitial sketch. at first it says there is no DHT.h file so I changed it to dht.h now its giving errors about my '.' token inside the void loop() function. what am I doing wrong here?

nospoe (author)2016-01-21

Got stuck at this:

root@raspberrypi:/home/pi/Arduino# node weather.js
{ error:
{ code: 401,
message: 'Please provide a valid authorization header',
id: null } }

1212142KHTN (author)2015-09-24

MostafaE4 (author)2015-05-22

PI does not has analog to digital converter , to do it using PI must get analog to digital ic and interface it with PI ,
using arduino ethernet shield could be done only without PI

yujingwu (author)MostafaE42015-08-21

You cannot use "Arduino Uno + Ethernet Shield" to replace "Arduino Uno + Raspberry Pi".

It is because HTTPs is not supported in Arduino UNO.

imbika (author)2015-05-12

Cool... simple and nice

agrey4 (author)2015-05-02

why arduino and rpi? you can use rpi only for this project.

SamsungIoT (author)agrey42015-05-04

You are right. You can also just use a connected Arduino.

amberrayh (author)2015-05-01

Sounds like a great project. Thanks for sharing! I hope we see more from you on Instructables in the future!

SamsungIoT (author)amberrayh2015-05-01

You can count on it! We are over in Menlo Park. Since you are close by you are welcome to stop by our office and see what we are up to.

About This Instructable

13,191views

213favorites

License:

Bio: We're building an open platform for a better #IoT. Our goals are to minimize fragmentation and enable faster adoption. Let's make our lives ... More »
More by SamsungIoT:Make a fire detector in minutes with Samsung SAMIIO, Arduino UNO and Raspberry PiWeather station using SAMIIO, Arduino, and Raspberry Pi
Add instructable to: