Hans on IoT

ThingSpeak, MATLAB, and the Internet of Things

Create and Train a Feedforward Neural Network

We have published an example in the ThingSpeak documentation that shows you how to train a feedforward neural network to predict temperature. The feedforward neural network is one of the simplest types of artificial networks but has broad applications in IoT. Feedforward networks consist of a series of layers. The first layer has a connection from the network input. Each other layer has a connection from the previous layer. The final layer produces the network’s output. In our IoT application, the output will be the predicted temperature.

IoT Application

We are collecting data in a ThingSpeak channel and will use the integrated MATLAB analytics. To predict the temperature, this example makes use of the Neural Network Toolbox in MATLAB along with the data collected in a ThingSpeak channel. We will be using data collected by a weather station located at MathWorks offices in Natick, Massachusetts.

The process for creating, training, and using a feedforward network to predict the temperature is as follows:

  1. Gather data from the weather station
  2. Create a two-layer feedforward network
  3. Train the feedforward network
  4. Use the trained model to predict data

Read Data from the Weather Station ThingSpeak Channel

ThingSpeak channel 12397 contains data from the MathWorks weather station, located in Natick, Massachusetts. The data is collected once every minute. Fields 2, 3, 4, and 6 contain wind speed (mph), relative humidity, temperature (F), and atmospheric pressure (hg) data respectively. To read the data from the weather station within MATLAB, use the thingSpeakRead function.

data = thingSpeakRead(12397,'Fields',[2 3 4 6],'DateRange',[datetime('Jul 30, 2018'),datetime('Jul 31, 2018')],...
    'outputFormat','table');

Create Two-Layer Feedforward Network

Use the feedforwardnet function to create a two-layer feedforward network. The network has one hidden layer with 10 neurons and an output layer.

net = feedforwardnet(10);

Train the Feedforward Network

Use the train function to train the feed-forward network.

[net,tr] = train(net,inputs,targets);

Use the Trained Model to Predict Data

After the network is trained and validated, you can use the network object to calculate the network response to any input.

output = net(inputs(:,5))
output =

   74.9756

This example can be adapted to other IoT applications. Check out the ThingSpeak documentation for the code and explanation.

|
  • print

Comments

To leave a comment, please click here to sign in to your MathWorks Account or create a new one.