esp8266 mqtt use arduino development tutorial

esp8266 arduino development may be used to develop an IDE, or developed using the SDK
used arduino IDE development is the most simple.

Use of arduino IDE development, development can be divided into soft or connected via arduino developed directly on a module

Esp826601s use to transmit data, for example mqtt
describes how to use the following arduino development esp8266 mqtt

arduino placed IDE

  1. Adding additional boards Manager URL:
    http://arduino.esp8266.com/stable/package_esp8266com_index.json

  2. Installation pubsubclient (mqtt supported protocol library 2.6.0)
    enter description here
    example there mqtt_esp8266 test code can be tested

  3. Open the tool development board development board manager search esp8266 download esp8266 by ESP8266 Community (2.4.2)
    enter description here

  4. Tool selection boards generic esp8266 module baud 115200 flash size 1M no SPIFS
    other unchanged
    (in the code below)
    code upload GPIO0 lower potential
    can be used to upload or specialized tools ttl esp Downloader module (easy)
    enter description here

  5. After uploading the code line connecting
    vcc and ground 3.3v power en
    ** GPIO0 high
    GND Ground

This is an example esp826601s
corresponding configuration of the module according esp

Use arduino IDE connection wifi service and mqtt

Code analysis

MQTT applicable to the Arduino Client
API documentation

  1. Download mqtt support library Pubsubclient, and esp8266wifi libraries, as well as the development board esp8266

/ #include <ESP8266WiFi.h>
#include <PubSubClient.h>

  1. Add information mqtt wifi server information
    when declaring global variables for later use for connection

char * SSID = const "YourNetworkName";
const char * password = "YourNetworkPassword";
const char * mqttServer = "m11.cloudmqtt.com";
const int mqttPort = 12948;
const char * mqttUser = "YourMqttUser";
const char * mqttPassword = "YourMqttUserPassword";
(the latter two if no user name and password can not be added)

  1. WIFIClien the class declaration object that allows to establish a connection characteristics ip and port
    objects in the class declaration PubSubClient, receiving as input WiFiClient previously defined constructor

WiFiClient espClient;
PubSubClient client(espClient);

  1. Connection wifi, the serial connection is opened, it is possible to view the information in the output port (which can be deleted)

Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(“Connecting to WiFi…”);
}
Serial.println(“Connected to the WiFi network”);

  1. MQTT IP and port of the server, when the function received using setcallback subscription message, executes the corresponding function

client.setServer(mqttServer,mqttport);
client.setCallback(callback);

  1. MQTT connection server, using the while loop if not connected, continuous connection, client.connected () returns false ture or
    use connect method, the actual connection. ESP8266Client client ID when connecting to the server you want to use, if a username password added to the end of the argument, if not you can not add.
    state function returns the reason for the connection failure.

while (!client.connected()) {
Serial.println(“Connecting to MQTT…”);
if (client.connect(“ESP8266Client”, mqttUser, mqttPassword )) {
Serial.println(“connected”);
} else {
Serial.print("failed with state ");
Serial.print(client.state());
delay(2000);
}
}

  1. Subscribe and post a topic

client.publish(“test”,“Hello Word”);
client.subscribe(test/status);

  1. callback function used to process the received message is subscribed topic. Parameter is the name of the theme, content publishing, and publishing content length. You can add other functions on the basis of modifications added.

void callback(char* topic, byte* payload, unsigned int length) {
Serial.print(“Message arrived in topic: “);
Serial.println(topic);
Serial.print(“Message:”);
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println(”-----------------------”);
}

  1. Call the main loop of the loop method PubSubClient, periodic function call, the server implementation and acceptance message sent mqtt.

Such processes are generally, with appropriate modifications to use different codes different functional modules.

The complete code

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
 
const char* ssid = "nihen_pc";
const char* password = "****";
const char* mqtt_server = "192.168.137.41";
const int mqttPort = 1883;

 
WiFiClient espClient;
PubSubClient client(espClient);
 
void setup() {
 
  Serial.begin(115200);
 
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.println("Connecting to WiFi..");
  }
  Serial.println("Connected to the WiFi network");
 
  client.setServer(mqtt_server, mqttPort);
  client.setCallback(callback);
 
  while (!client.connected()) {
    Serial.println("Connecting to MQTT...");
 
    if (client.connect("ESP8266Client")) {
      Serial.println("connected");  
 
    } else {
 
      Serial.print("failed with state ");
      Serial.print(client.state());
      delay(2000);
 
    }
  }
 
  client.publish("esp/test", "Hello from ESP8266");
  client.subscribe("esp/test");
 
}
 
void callback(char* topic, byte* payload, unsigned int length) {
 
  Serial.print("Message arrived in topic: ");
  Serial.println(topic);
 
  Serial.print("Message:");
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
  }
 
}
 
void loop() {
  client.loop();
}

Reference article
referenced article

arduino soft serial connection

The above method is to use esp8266 board directly uploaded to the implementation of the above esp
Here we introduce the use arduino control esp (esp sometimes require joint use)
baud rate esp module needs to be modified to 9600 is initialized to 115200
using the "SoftwareSerial.h" soft setting communication between the serial port and arduino

Use WiFiESP underlying this AT command is encapsulated wifi connection library

The reaction is slow obvious shortcomings often disconnected

Reference http://xcx1024.com/ArtInfo/117805.html

Sdk development using reference links

Reference Code links

GitHub address

More tutorials

ESP developers learn the basics

Including an understanding of the basics esp modules awareness and understanding mqtt agreement, arduino IDE application code is written, and so on.

  1. arduino based learning
  2. Introduction esp Series Modules
  3. Introduction and use mqtt agreement
  4. Using mqtt esp module arduino IDE Development Methodology
  5. AT command esp brush firmware module
  6. esp module using sleep mode
  7. esp8266-01s introduction and use
  8. esp8266-12f introduction and use
  9. NodeMcu introduction and use
esp development of IOT applications

Based modules and other modules esp8266 application scenario according to an actual production and demand of Things

  1. FRID access control systems FRID arduino relay electromagnetic lock-based development
  2. esp32-cam video stream acquired image processing
  3. Stepping motor esp8266 mqtt developed automatic curtain control
  4. Gets indoor temperature and humidity based on DHT11 Esp8266 mqtt
  5. Gets indoor air quality based on CCS811 esp8266 mqtt
  6. Based on infrared remote control intelligent module esp8266 mqtt development
  7. Based ws2812 esp8266 mqtt development of intelligent multi-level lighting
  8. Based ws2812 esp8266 mqtt development of intelligent multi-mode light atmosphere
  9. Broadcast System Intelligent Voice mp3player esp8266 mqtt based development
  10. The wisdom of the comprehensive application of IOT classroom project development
Published 46 original articles · won praise 59 · views 70000 +

Guess you like

Origin blog.csdn.net/Nirvana_6174/article/details/104402855