NodeMCU first message
Updated: Dec 4, 2021

We will send the first message to our system with Nodemcu.
Our software uses incoming messages in the format of device name / data name and data content.
devicename/topic", "content"
For this reason, we should send the mqtt messages that we will publish in accordance with this format.
In this example, we will send the message 2021 to our smartespiot software with nodemcu.
client.publish("devicename/topic", "2021");
The libraries we will be using are ESP8266Wifi and PubSubClient.
#include <ESP8266WiFi.h> //The library we will use for the wifi connection
#include <PubSubClient.h> // The library we will use for the mqtt connection
//mqtt connection library settings
WiFiClient espClient;
PubSubClient client(espClient);
//
After the purchase, your server information will be sent to you.
Using this information, you need to complete the codes below.
A special server is prepared for each user.
You should not share this information.
//settings for internet and mqtt connection
const char* ssid = ""; // wifi SSID
const char* password = ""; // wifi password
const char* mqttServer = ""; // mqtt server ip to be sent by smartespiot by mail
const int mqttPort = ; // mqtt port to be sent by smartespiot by mail
const char* mqttUser = ""; // mqtt username to be sent by smartespiot by mail
const char* mqttPassword = ""; // mqtt pass to be sent by smartespiot by mail
//
We run MQTT and wifi connection processes in the setup section.
For now, we will not do reconnection and reset operations.You can find it in the next examples.
void setup() {
Serial.begin(115200); // start serial communication at 115200 speed
WiFi.begin(ssid, password); // start the wifi connection process
while (WiFi.status() != WL_CONNECTED) { //
delay(500); //wait 500ms
Serial.println("Connecting to wifi..."); // wait until the wifi process connects
}
Serial.println("Wifi connection ready :)"); // print the connection made information to the serial port
client.setServer(mqttServer, mqttPort);//start connecting to the mqtt server from the specified port
while (!client.connected()) { //wait until we connect to mqtt
Serial.println("Connecting to MQTT server...");
if (client.connect("SmartEspIOT", mqttUser, mqttPassword )) {//login to mqtt server with username and password with smartespIOT name
Serial.println("MQTT connection established :) ");
} else {
Serial.print("Unable to connect... ");// If the connection cannot be established, let's give a warning
Serial.print(client.state()); // print the problem description
// We can understand the problem according to the information returned from the clients state.
//-4 : MQTT_CONNECTION_TIMEOUT - the server did not respond in the ongoing time
//-3 : MQTT_CONNECTION_LOST - network connection lost
//-2 : MQTT_CONNECT_FAILED - network connection failed
//-1 : MQTT_DISCONNECTED - disconnected
//0 : MQTT_CONNECTED - connection established
//1 : MQTT_CONNECT_BAD_PROTOCOL - The server does not support the requested MQTT version
//2 : MQTT_CONNECT_BAD_CLIENT_ID - The server did not accept your information
//3 : MQTT_CONNECT_UNAVAILABLE - The server did not accept the connectionetmed
//4 : MQTT_CONNECT_BAD_CREDENTIALS - username password not accepted
//5 : MQTT_CONNECT_UNAUTHORIZED - you are not authorized to connect
delay(2000); // wait two seconds and try connecting again
}
}
}
We publish our message in our loop software.
For now, we publish a message every two seconds by using delay command.
We will then use the milis command.
void loop() {
client.loop(); // mrun the mqtt data processing function
client.publish("devicename/topic", "2021");// send our message in the topic with the device name
delay(2000); // wait two seconds and send the message again
Serial.println("message send");
}