DevToPrd
  • Home
  • Knowledge Base
  • About Me
  1. Knowledge Base
  2. Articles
  3. Arduino / Raspberry Pi Remote Sensor
  • Knowledge Base
    • Articles
      • 3D Printing and CNC
      • 6502 Programming in C
      • Add Settings File to .NET Console Application
      • AMD Ryzen 3 3200G with Radeon Vega Graphics Keeps Freezing
      • Angular Cheat Sheet
      • Angular CLI
      • Arduino / Raspberry Pi Remote Sensor
      • Bad owner or permissions on ssh config file
      • Basic Arduino Uno Dev Setup in Linux (Debian-based)
      • Boilerplate Date Definitions for SQL
      • Boot Linux Mint from flash drive on a netbook
      • Boot Linux to text mode
      • Bundling a Multiple Module Python Project Into a Zip File
      • Cache Resources Exhausted in Imagemagick
      • Call Async Method from Non-Async Method
      • Check an XML file to see if it is well-formed
      • Clear Linux Cache
      • Command-Line Arduino
      • Complex Object Collections in C++
      • Component Not Found error in VirtualBox
      • Compress and Extract Entire Directory Tree With Tar
      • Conditions on aggregates in SQL
      • Configuration of hddtemp
      • Connect To MySQL From .NET
      • Create Code Behind File for Razor Page In Blazor Application
      • Create An ISO File From a CD or DVD-ROM Using the Terminal
      • Cross-Compile Rust, Targeting Windows from Linux
      • C# Language Versioning
      • Dependency Injection in .NET/C#
      • Determine path to web.config at runtime
      • Display GRUB Menu
      • Docker in Linux
      • .NET IoT
      • Entity Framework
      • Enumeration Scoping in C++
      • File Operations in Java
      • Find Duplicate Rows By Specific Columns
      • Find Unique Instances of Text in Bash
      • Flash Disk Image to SD Card
      • Flask
      • Force SSL On Domain
      • GDB and GPROF
      • Get Started With Laravel
      • Git Tips and Tricks
      • Graphical sudo
      • Hide website from search engines
      • Hostname On Linux
      • How to install a .bundle file in Ubuntu Linux
      • HP Laptop Keyboard Not Working At Boot Start
      • Install .NET From Microsoft Feed in Ubuntu
      • Install Node.js
      • Install php7-xml to avoid utf encode/decode issue
      • Install truetype fonts in Ubuntu Linux
      • Java Links
      • JCL and Programming On the MVS Turnkey System
      • List Hardware Information in Ubuntu and other Debian-based Linux
      • Mainframe Emulation on Raspberry Pi Zero
      • Making Colors in a GIF Transparent
      • Manually Install Firefox
      • Markup and Code-Behind Examples for reveal.js
      • Merge Images
      • MicroPython on ESP32
      • Microservice Notes
      • Migrating from Windows to Linux
      • MiniDLNA - A lightweight, simple media server
      • Modify Default Folders in Ubuntu Linux
      • Modify default Java runtime in Linux
      • MongoDB Quick Start in Docker
      • Mount local folder as drive C in DOSBox
      • MVS Turnkey in Docker
      • MySQL command line – quick tips
      • No Frameworks Were Found error
      • NPM Cheat Sheet
      • NT_STATUS_UNSUCCESSFUL error when browsing machines/shares in Ubuntu Linux
      • Online IoT/Embedded Simulators
      • Package and Publish To NuGet
      • Packaging Python Projects
      • Passing a value in the URL for use by PHP
      • PDF Conversion Policy Error
      • PHP Login Script Tutorial
      • Pip Behind a Corporate Proxy
      • Prevent Code Injection In PHP
      • Programming Arduino (AVR) and Raspberry Pi Pico (ARM) in C
      • Publish to Crates.io
      • Python Optimization and Language Binding
      • Python Tkinter Examples
      • Query Windows shares from the command line in Ubuntu Linux
      • RabbitMQ in Docker
      • React Cheat Sheet
      • Recursive File Search
      • Redirect in PHP
      • Remote Access for Raspberry Pi
      • Remove Duplicates From List Collection
      • Requirements Files for Python
      • Rhythmbox tray icon plugin
      • Rsync as a Backup Solution
      • Rust Books (online)
      • Rust/Cargo Behind a Corporate Proxy
      • Selective chmod for directories and files
      • Sense HAT
      • Side-by-Side .NET Core Installations
      • Simple Raspberry Pi Control With .NET IoT and Python
      • Simple Web Server, Using Python or PHP
      • Single-File / Multi-OS / Multi-Architecture Publishing in .NET
      • Slim Framework
      • SQL Server in Docker
      • SQL Server in Linux
      • SQL Transaction Template
      • Start an X Windows session in Cygwin
      • stdio.h: No such file or directory
      • Stream Media from Ubuntu/Mint Linux to XBox360
      • Supporting Material for ‘.NET and Linux’ tech talk
      • TUI Frameworks for .NET
      • Use a value from a posted form in PHP
      • Using Your Router As A Wireless Repeater
      • Verbose PHP Logging To Web Browser
      • Version Mismatch in Python Package
      • Vim Tips and Tricks
      • Virtual Environment for Python
      • Vue.js Cheat Sheet
      • WCF Export
      • Web API with ASP.NET
      • Web Frameworks, Libraries, and Plugins
    • Glossary
      • Initial Program Load (IPL)
      • Job Control Language (JCL)
      • Just-in-time compilation (JIT)
      • Model–View–Controller (MVC)
      • Multiple Virtual Storage (MVS)
      • Resource Access Control Facility (RACF)
      • Real-time locating system (RTLS)
      • Time Sharing Option (TSO)
    • Learning Paths
      • Embedded and IoT
      • Low-Level
      • Mainframe
    • Other
      • Job and Employment Tools
  • Pages
    • About Me
  • Posts
    • 2007
      • Fake Science Makes Me Angry
      • Genetic Material from T-Rex Decoded
      • Vacuum and the Nature of Mass
    • 2011
      • Unhitched
    • 2012
      • Chick-fil-A
      • Choosing a First Telescope
      • Interesting Fossil
      • Rigorous Logic and Amelia
      • Pale Blue Dot
      • Science and Youth
      • Steve Jobs: The Lost Interview
      • Theism and the Thinking Mind
    • 2013
      • Duck and Cover
      • Duct Tape
      • Firefox OS
      • Idiocracy is Upon Us
      • It’s Just a Theory
      • Ken Feder and the Myth of the Ancient Astronauts
      • Martian Exopaleontology
      • …from the Moon and Mars
      • SunWatch Video Podcast
      • Tech Generation… not?
    • 2014
      • ‘Andean Gold’ lecture at SunWatch
      • Chelyabinsk Meteor
      • Lunar Eclipse
      • A New Meteor Shower Is Coming
      • Sleep Well, Philae
      • Why do I not believe?
    • 2015
      • Early Humans in the Americas
      • SETI@Home
      • What is a fossil?
    • 2017
      • Cleaning Fossils
      • The Dating Game
      • How Firefox Got Its Groove Back
      • March For Science
      • Printing the Past
      • Who Ya Gonna Call?
    • 2018
      • 2018 AIA/SunWatch Lecture Series: Science vs. Pseudoscience
      • Coordinate Systems: Finding Your Way Around the Sky
      • Natural Selection At Work
      • Like Water For Gravity
    • 2019
      • ‘Astronomy for Beginners’ Program
      • Homeopathy
      • Scientific and Critical Thinking Omnibus
      • Show Me What You’re Made Of
      • Visitors From Space
    • 2020
      • A Dim View of Betelgeuse
      • Cleveland Archaeological Society Lecture Series
      • Going Viral
      • The Trinity Mineral
      • Venusians, Perchance
    • 2021
      • Create Extension for Visual Studio Code
      • Early .NET 6 Observations
      • The Upcoming Lunar Eclipse: Real-World Application of the Practical Astronomy .NET Library (+ Python!)
      • Enumeration Scoping in C++
      • Fossil Hunting in Central Kentucky
      • Fossil Sites Map in Leaflet.js
      • Geology of Jezero
      • Python and XML
      • Running a Mainframe Emulator on a Raspberry Pi Zero (Lunch-n-Learn)
      • Rust in the Android Platform
      • Rust Foundation
      • Speak with Elegance (or not)
      • Supporting Material for ‘.NET and Linux’ tech talk
      • Traditional Programming on Hobbyist Microcontroller Boards
      • ‘Visual Studio Code: The Swiss Army Knife of Editors’ (tech talk)
    • 2022
      • James Webb Space Telescope
      • Program Like It’s 1982
      • Rust In the Linux Kernel
    • 2023
      • .NET IoT and Terminal GUI
      • Modern .NET Tooling in VS Code
      • Perl: Is It Really That Bad?
      • Sci-Fi Recommendations
    • 2024
      • 6502 Programming in C
      • DuckDuckGo AI Chat
      • Modern Perl
      • My AI Chatbot Experience
      • Practical Astronomy Algorithms in Various Languages
      • Site Updates
      • Think Async

On this page

  • Architecture
  • Required Hardware
  • Arduino Configuration
  • Arduino IDE
  • Processing source
  • Running with serial monitor
  • Plotly account
  • Node.js
  • Node.js project setup
  • Node Server
  • Plotly View
  • Summary
  1. Knowledge Base
  2. Articles
  3. Arduino / Raspberry Pi Remote Sensor

Arduino / Raspberry Pi Remote Sensor

Embedded and IoT

This project will provide an introduction to the concept of the “Internet_of_Things”.

Technopedia defines Internet of Things as:

…a computing concept that describes a future where everyday physical objects will be connected to the Internet and be able to identify themselves to other devices. The term is closely identified with RFID as the method of communication, although it also may include other sensor technologies, wireless technologies or QR codes.

The IoT is significant because an object that can represent itself digitally becomes something greater than the object by itself. No longer does the object relate just to you, but is now connected to surrounding objects and database data. When many objects act in unison, they are known as having “ambient intelligence.”

Specifically, we will program a device to provide temperature data, and then make that data publicly available on the web.

(If you’d like to save some time typing in scripts, you can download them here.)

Architecture

Our component architecture will be as follows:

  • The physical layer will be used to capture the temperature data. We will implement this using an Arduino Uno board and a temperature sensor.
  • The coordination layer will be used to capture the temperature measurements from the physical layer and for sending the measurements to our application. This will be implemented using Node.js running on a Raspberry Pi. We will also use the Raspberry Pi as a development platform for the Arduino.
  • The application layer will be used to visualize the measurements in real-time. This will be implemented using a data visualization cloud service called Plotly.

This guide assumes that you already have your Raspberry Pi up and running.

Required Hardware

  • Raspberry Pi If you tweak the instructions a bit, it’s not difficult to use a desktop PC or laptop instead of a Raspberry Pi. (Probably easier, in fact.) I’m using a Raspberry Pi – B+, not a Raspberry 2. You can probably use a different model, I just haven’t tried it.
  • Arduino with USB cable. I’m using an Arduino Uno. As with the Raspberry Pi, you can probably use a different model.
  • breadboard
  • TMP36 temperature sensor Similar sensors don’t necessarily report the same temperature data, so keep that in mind if you make a substitution here. For example, the TMP36 reports data in Celsius, whereas the TMP35 reports in Kelvin.
  • jumper wires (5)

Arduino Configuration

Wire up the Arduino as follows:

(I created this breadboard layout image in Fritzing.)

It’s a very simple setup. We provide power to the temperature sensor, and the sensor returns temperature data via digital pin 0.

Arduino IDE

To write code and upload it to the Arduino board, you’ll need the free Arduino IDE.

Versions are available for Windows, Mac, and Linux. Since we’re running it on the Raspberry Pi, we’ll be using the Linux version.

  1. If you want the latest version, download and install it from here.

  2. If using the latest version isn’t important to you (it isn’t required), you can install it from a terminal prompt using apt-get:

sudo apt-get install arduino

The Arduino IDE does have a few dependencies, and required about 80MB on my Raspberry Pi.

After you install the IDE, plug in the Arduino using the supplied USB cable, then run the IDE.

  1. Open the “Tools” menu, go to the “Board” section and make sure your Arduino model is selected.
  2. In the “Serial Port” section, make sure the serial port value is selected. Also, note the value of the serial port string. You’ll need it later. (It will look something like this: “/dev/ttyACM0”)

Processing source

Code for the Arduino is written in Processing. Processing is syntactically very similar to the C, C++, and Java languages. A code module for the Arduino is called a “sketch”.

This is the code we’ll use to get data from the temperature sensor. Type this code into the sketch editor in the Arduino IDE:

/* This is the pin getting the stream of temperature data. */
#define sensorPin 0
    
float Celsius, Fahrenheit;
int sensorValue;
    
void setup() {
    Serial.begin(9600);  /* Initialize the Serial communications */
}
    
void loop() {
    
    GetTemp();
    
    Serial.println(Fahrenheit);  /* You can easily change this to print Celsius if you want. */
    
    delay(2000);  /* Wait 2 seconds before getting the temperature again. */
}
    
void GetTemp() {
    sensorValue = analogRead(sensorPin);  /* Get the current temperature from the sensor. */
    
    /*
    * The data from the sensor is in mV (millivolts), where 10mV = 1 degree Celsius.
    * So if, for example, you receive a value of 220 from the sensor, this indicates
    * a temperature of 22 degrees Celsius.
    */
    
    Celsius = sensorValue / 10;         /* Convert the sensor value to Celsius */
    Fahrenheit = (Celsius * 1.8) + 32;  /* Convert the Celsius value to Fahrenheit */
}

After you’ve typed in this source code, click the “Verify” button in the toolbar to check the syntax. If you’ve made any mistakes, correct them before continuing.

Once the code is verified, click the “Upload” button in the toolbar to write it to the Arduino’s flash memory.

Running with serial monitor

Once the sketch has been written to the Arduino, it will start running automatically. You can check the values being received from the temperature sensor by opening the serial monitor in the Arduino IDE. To do that, click the “Serial Monitor” button on the right side of the toolbar. A console window will open up, and you should see a stream of data similar to this:

86.0
86.0
86.2
86.2
86.0
85.8
85.8
85.8

You may see values lower or higher than this. (The sensor on my Arduino seems to run a little hot.)

Now that we have the Arduino supplying temperature data, the next step is to make it available on the web.

Plotly account

Plotly is an online analytics and data visualization tool. It provides online graphing, analytics, and stats tools for individuals and collaboration, as well as scientific graphing libraries for Python, R, MATLAB, Perl, Julia, Arduino, and REST.

It also has a streaming API, which we’ll use to get our data to the web.

To set up a free Plotly account, go to the Plotly home page here. After you create your account, there are three pieces of information you’ll need to remember. We’ll be using them later:

  • Username
  • API key
  • Streaming API token

Node.js

To get our data from the Arduino to Plotly, we’ll use Node.js.

Node.js is an open source, cross-platform runtime environment for server-side and networking applications. Node.js applications are written in JavaScript and can be run within the Node.js runtime on OS X, Microsoft Windows, Linux, and a handful of other operating systems.

First, make sure your system is up-to-date. Open a terminal and issue the following command:

sudo apt-get update

sudo apt-get upgrade -y

(Probably a good idea to reboot after this.)

Then, download and install node.js:

For the wget step, the latest as of this writing seems to be http://nodejs.org/dist/v0.11.9/, but I was not able to get this version to work. I used http://nodejs.org/dist/v0.10.16/.

wget http://nodejs.org/dist/v0.10.16/node-v0.10.16-linux-arm-pi.tar.gz

tar xvfz node-v0.10.16-linux-arm-pi.tar.gz

sudo mv node-v0.10.16-linux-arm-pi /opt/node/

You need to retrieve the version from nodejs.org. The version in the repository does not work, so you can’t use apt-get to install it.

Configure your path:

echo 'export PATH="$PATH:/opt/node/bin"' >> ~/.bashrc
 
source ~/.bashrc

Node.js project setup

Open a terminal, and create a directory for your Node.js project. Change your working directory to the new directory. Example:

mkdir temp_nodejs
 
cd temp_nodejs

We’ll need a couple of additional libraries for our Node.js project, serialport and plotly. Install them using the following commands in your project folder:

npm install serialport
 
npm install plotly

If you get a “failed to fetch from registry” error when you try to use npm install, you may need to make the following change on your Raspberry Pi:

npm config set registry http://registry.npmjs.org/

(By default, npm install uses https://registry.npmjs.org)

Now we’re ready to create the Javascript file for Node.js to execute. Use the following as a template:

var serialport = require('serialport'),
        plotly = require('plotly')('Plotly_UserName', 'Plotly_API'),
        token = 'Plotly_Token';
var portName = '/dev/tty.usbmodem1411';
var sp = new serialport.SerialPort(portName, {
    baudRate: 9600,
    dataBits: 8,
    parity: 'none',
    stopBits: 1,
    flowControl: false,
    parser: serialport.parsers.readline("\r\n")
});
    
// helper function to get a nicely formatted date string
function getDateString() {
    var time = new Date().getTime();
    // 32400000 is (GMT+9 Japan)
    // for your timezone just multiply +/-GMT by 36000000
    var datestr = new Date(time + 32400000).toISOString().replace(/T/, ' ').replace(/Z/, '');
    return datestr;
}
    
var initdata = [{x: [], y: [], stream: {token: token, maxpoints: 500}}];
var initlayout = {fileopt: "extend", filename: "ambient-fahrenheit-temperature-sensor"};
    
plotly.plot(initdata, initlayout, function (err, msg) {
    if (err)
        return console.log(err)
    
    console.log(msg);
    var stream = plotly.stream(token, function (err, res) {
        console.log(err, res);
    });
    
    sp.on('data', function (input) {
        if (isNaN(input) || input > 1023)
            return;
    
        var streamObject = JSON.stringify({x: getDateString(), y: input});
        console.log(streamObject);
        stream.write(streamObject + '\n');
    });
});

Make sure you change the portName value to match the serial port value from our “Arduino IDE” step. Also, change the Plotly_UserName, Plotly_API, and Plotly_Token text to match the values from the Plotly account you opened.

Node Server

Make sure your Arduino board is plugged in, then start your Node.js server by issuing the following command:

node server.js

You should see information similar to the following:

{ streamstatus: 'All Streams Go!',
  url: 'https://plot.ly/~username/44',
  message: '',
  warning: '',
  filename: 'ambient-fahrenheit-temperature-sensor',
  error: '' }
{"x":"2015-08-16 08:55:57.418","y":"87.80"}
{"x":"2015-08-16 08:55:59.417","y":"86.00"}
{"x":"2015-08-16 08:56:01.415","y":"86.00"}
{"x":"2015-08-16 08:56:03.414","y":"86.00"}
{"x":"2015-08-16 08:56:05.413","y":"86.00"}

This indicates that the Node.js server is receiving data from the Arduino board and sending it to Plotly. The server will continue to log data until you press [Ctrl-C] to stop it.

Plotly View

While the Node.js server is running, open a web browser and log in to your Plotly account. Click the “Organize” link at the top of the screen, then click the “Open Plot” button on the “ambient-fahrenheit-temperature-sensor” project.

After the project opens, you should see a graph similar to this, updating in real-time:

Plotly projects are public by default, so you are now sharing your real-time temperature data with the world!

If you’re wondering about that high first value, I’ve noticed that the first one or two values tend to be anomalous. This may be related to the sketch being initialized, the initial board reset, or something else.

Summary

Using some inexpensive, low-powered hardware, we were able to provide some (somewhat) useful information on the web for public consumption.

Hopefully this will inspire you to create some IoT projects of your own. Good luck!

 

Copyright 2024, Jim Carr

Cookie Preferences