Sending Sensor data to Mosquitto on Windows from NodeMCU

Hannah Lormenyo
4 min readNov 18, 2020

Mosquitto is a lightweight MQTT broker from Eclipse. Since it is lightweight, it is very suitable for Internet of Things applications that use low power sensors or embedded systems.

Message Queuing Telemetry Transport(MQTT) is a protocol that uses a publish/subscribe model for Machine-to-Machine communication. It has a client and a broker. The clients are devices that send or receive the data whilst the broker is a server.

Installations

Install mosquitto here.

In the Arduino IDE, click on Sketch at the top. Then go to Include Library -> Manage Libraries.

In the manage Libraries window, type PubSubClient in the search bar and install PubSubClient from the results given.

NodeMCU and sensor connections

Circuit setup

In this example, the sensors used were a Light Dependent Resistor(LDR) and DHT 21(Humidity and temperature) sensor. I connected the LDR to pin A0 of the NodeMCU and the DHT signal pin to pin 2(D4).

Starting the Mosquitto server

To start the mosquitto server, open the windows command prompt and cd into the directory where mosquitto is installed. For me, it is C:\Program Files\mosquitto So I will type the following in the command prompt:

cd "C:\Program Files\mosquitto"

Afterward, type the command below in the command prompt. This starts the mosquitto server on port 1883 in verbose mode.

mosquitto -v

If your server fails to start, try enabling the port. You can read more on how to do that here.

The mosquitto server

Getting the IP address of your machine

Open the command prompt and type ipconfig . This will give you a list of all the available connections on your machine. Look for an active connection and copy the ipv4 address which looks something like this “192.xxx.xxx.xxx”

The code

The NodeMCU and your machine must be on on the same network(connected to the same wi-fi).

Specify your wifi credentials and insert your IP address
Wifi client

Create an instance of wificlient and pass it to the PubSubClient.

Function for connecting to the MQTT server

The reconnect function above, connects the client to the MQTT server using the credentials provided.

void loop()
{
if (!client.connected()) {
reconnect();
}
client.loop();
// — — — — — — — — — — — — — — — — — — — — —
//post after every 3 secondS — — — — — — — — — — -
//figure out how to post after every few seconds (based on earlier work)
// — — — — — — — — — — — — — — — — — — — — —
current_time = millis();
if (current_time — last_run >= THREE_SECONDS) {
// save the last time you blinked the LED
last_run = current_time;
int sensorValue = analogRead(A0); // read the input on analog pin 0
String ldrValue = String(sensorValue);
float t = dht.readTemperature();
String temp = String(t);
client.publish(“LDRSensorValues”, ldrValue.c_str());
client.publish(“TemperatureValues”, temp.c_str());
Serial.print(“LDR: “);
Serial.print(ldrValue);
Serial.print(“ Temperature: “);
Serial.print(temp);
Serial.println(“Message published”);
}

client.subscribe(“LDRSensorValues”);
client.subscribe(“TemperatureValues”);
}

In the void loop, the sensor values from the LDR and the DHT are taken. On temperature values are taken from the DHT. Then using the millis function, the sensor values are published to the MQTT server every 3 seconds(3000 milliseconds).

The sensor values are published to a topic. A topic is string which the server uses to filter messages. In this example, the topics used were “LDRSensorValues” and “TemperatureValues” for the LDR values and temperature values respectively.

Serial monitor showing the values published to the Mosquitto broker

To view the sensor values being sent to the Mosquitto broker, you can use mosquitto_sub to subscribe to the topics as shown in the image below:

A mosquitto subscriber subscribing to the topics for the sensor values
  • -t denotes a topic whilst -v is for verbose mode.

You can access the entire code here.

Thank you for reading this article. Don’t forget to leave a clap if you found this interesting or helpful. You can follow me on Github or on Twitter.

--

--

Hannah Lormenyo

Software Engineer I write about the new things I learn in building software and sometimes hardware (embedded systems)