3 min read

 In this article by Marco Schwartz, author of the book Intel Galileo Networking Cookbook, we will cover the recipe, Reading pins via a web server.

(For more resources related to this topic, see here.)

Reading pins via a web server

We are now going to see how to use a web server for useful things. For example, we will see here how to use a web server to read the pins of the Galileo board, and then how to display these readings on a web page.

Getting ready

For this chapter, you won’t need to do much with your Galileo board, as we just want to see if we can read the state of a pin from a web server. I simply connected pin number 7 of the Galileo board to the VCC pin, as shown in this picture:

How to do it…

We are now going to see how to read the state from pin number 7, and display this state on a web page. This is the complete code:

// Required modules
var m = require("mraa");
var util = require('util');

var express = require('express');
var app = express();

// Set input on pin 7
var myDigitalPin = new m.Gpio(7);
myDigitalPin.dir(m.DIR_IN);

// Routes
app.get('/read', function (req, res) {
var myDigitalValue = myDigitalPin.read();
res.send("Digital pin 7 value is: " + myDigitalValue);
});

// Start server
var server = app.listen(3000, function () {

console.log("Express app started!");

});

You can now simply copy this code and paste it inside a blank Node.js project. Also make sure that the package.json file includes the Express module.

Then, as usual, upload, build, and run the application using Intel XDK. You should see the confirmation message inside the XDK console.

Then, use a browser to access your board on port 3000, at the /read route. You should see the following message, which is the reading from pin number 7:

If you can see this, congratulations, you can now read the state of the pins on your board, and display this on your web server!

How it works…

In this recipe, we combined two things that we saw in previous recipes. We again used the mraa module to read from pins, here from pin number 7 of the board. You can find out more about the mraa module at:

https://github.com/intel-iot-devkit/mraa

Then, we combined this with a web server using the Express framework, and we defined a new route called /read that reads the state of the pin, and sends it back so that it can be displayed inside a web browser, with this code:

app.get('/read', function (req, res) {
var myDigitalValue = myDigitalPin.read();
res.send("Digital pin 7 value is: " + myDigitalValue);
});

See also

You can now check the next recipe to see how to control a pin from the Node.js server running on the Galileo board.

Summary

In this recipe, we saw how to read the state from pin number 7, and display this state on a web page. If you liked this article please buy the book Intel Galileo Networking Cookbook, Packt Publishing, to learn over 45 Galileo recipes.

Resources for Article:


Further resources on this subject:


Packt

Share
Published by
Packt

Recent Posts

Top life hacks for prepping for your IT certification exam

I remember deciding to pursue my first IT certification, the CompTIA A+. I had signed…

3 years ago

Learn Transformers for Natural Language Processing with Denis Rothman

Key takeaways The transformer architecture has proved to be revolutionary in outperforming the classical RNN…

3 years ago

Learning Essential Linux Commands for Navigating the Shell Effectively

Once we learn how to deploy an Ubuntu server, how to manage users, and how…

3 years ago

Clean Coding in Python with Mariano Anaya

Key-takeaways:   Clean code isn’t just a nice thing to have or a luxury in software projects; it's a necessity. If we…

3 years ago

Exploring Forms in Angular – types, benefits and differences   

While developing a web application, or setting dynamic pages and meta tags we need to deal with…

3 years ago

Gain Practical Expertise with the Latest Edition of Software Architecture with C# 9 and .NET 5

Software architecture is one of the most discussed topics in the software industry today, and…

3 years ago