---
# System prepended metadata

title: MQTT and Node-Red for Intel Galileo

---

# MQTT and Node-Red for Intel Galileo

[source gihub](https://github.com/Codophile1/IoT-Galileo)


>[name= Quentin RICHARD]
>[name= Yanis OUAKRIM]
>
# Introduction


This notebook is meant to describe the use of:
* MQTT
* Node-red
* Intel Galileo Board
* Temperature sensor

We are going to use the Galileo board as an Arduino board to read the temperature and to send it to Node-Red using MQTT through Ethernet.

## I. Reading the temperature and sending it using MQTT


We're using the temperature sensor from the Grove Starter Kit Plus with the Base Shield. To read the information from the sensor, we use the Galileo board with Arduino. 

The following code snippet enables to read the temperature every five seconds and publishes it to the MQTT topic "temperature". The MQTT (and Node-red) server is the computer linked to the board.
```C=
#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>
#include <math.h>


// MAC Address
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

// Server IP
IPAddress mqtt_server(192, 168, 15, 2);

// Topic we're working with
const char* topicName = "temperature";

const int B = 4275;               // B value of the thermistor
const int R0 = 100000;            // R0 = 100k
const int pinTempSensor = A0; 
EthernetClient ethClient;
PubSubClient client(ethClient);

void setup()
{
  Serial.begin(9600);
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
  }
  client.setServer(mqtt_server, 1883);
}

void loop()
{
  if (!client.connected()) {
    Serial.print("Connecting ...\n");
    client.connect("Arduino Client");
  }
  else {
    // Reading tmperature from pin
    int a = analogRead(pinTempSensor);
    float R = 1023.0/a-1.0;
    R = R0*R;

    float temp = 1.0/(log(R/R0)/B+1/298.15)-273.15;
    char buffer[10];
    dtostrf(temp,0, 0, buffer);
    client.publish(topicName, buffer);
  }
  // Time interval
  delay(5000);
}
```

## II. Receiving the temperature with Node-Red
### Getting the temperature with Node-Red
We're using Mosca as our MQTT server, it works directly with Node-Red, and we can install it directly using the Node-Red palette.

We first need to add a "Mosca MQTT" broker node in the project, we can then read MQTT topics using nodes MQTT out.

![](https://i.imgur.com/jarXU3k.png)

### Displaying information with Node-red
We're now going to display the temperature on a web page using the __dashboard__ Node-red module.

We used a _gauge_ node to display the temperature with different colors according to the value. 

![](https://i.imgur.com/qq70h42.png)


![](https://i.imgur.com/EquqjdZ.png)


![](https://i.imgur.com/TqszfKV.png)


## Conclusion
In this project, we used MQTT to communicate sensor information to Node-Red through Ethernet. We used the web dashboard Node-red extension to display temperature information on a web page.

## References
* https://funprojects.blog/2018/03/04/arduino-talking-mqtt-to-node-red/
* https://flows.nodered.org/node/node-red-dashboard
* http://wiki.seeedstudio.com/Grove-Temperature_Sensor_V1.2
* https://ricveal.com/blog/arduino-mqtt/
