Comments (2)
Hi!
Nowadays , I work with ESP32 and ArduinoMqttClient 0.1.8 by Arduino library and WiFi.h library that comes with ESP32 core for Arduino.
If you just want to create a publisher from ESP32 this code works for me:
// File main.ino
#include <WiFi.h>
#include <ArduinoMqttClient.h>
// WIFI
WiFiClient wifiClient;
const char *ssid = "WIFI_SSID"; // WiFi ssid
const char *password = "WIFI_PASSWORD"; // WiFi password
// MQTT
MqttClient mqttClient(wifiClient);
const char mqtt_username[] = "MQTT_USERNAME";
const char mqtt_password[] = "MQTT_PASSWORD";
char mqtt_client_id[] = "MQTT_CLIENT_ID";
const char mqtt_broker_url[] = "MQTT_BROKER_URL";
int mqtt_broker_port = "MQTT_BROKER_PORT";
void setupWifi()
{
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(1000);
Serial.println("Connecting to WiFi ...");
}
Serial.println("WiFi connected");
Serial.println("IP address set: ");
Serial.println(WiFi.localIP());
Serial.println("[wifi] is ok");
}
void setupMqtt()
{
mqttClient.setId(mqtt_client_id);
mqttClient.setUsernamePassword(mqtt_username, mqtt_password);
while (!mqttClient.connect(mqtt_broker_url, mqtt_broker_port))
{
delay(1000);
Serial.print("[mqtt] connection failed! Error code = ");
Serial.println(mqttClient.connectError());
}
Serial.println("[mqtt] is ok");
}
void setup()
{
setupWifi();
setupMqtt();
}
void loop()
{
// To avoid being disconnected by the broker and keep the
// connection alive
mqttClient.poll();
mqttClient.beginMessage("topic");
mqttClient.print("your_msg");
mqttClient.endMessage();
delay(2000);
}
from arduinomqttclient.
Going to use Arduino Forums
from arduinomqttclient.
Related Issues (20)
- Return values of `MqttClient::subscribe` not documented HOT 4
- Unit of mqttClient.setKeepAliveInterval is undocumented HOT 1
- docs?
- Does not compile on ESP8266 when calling connect() with port HOT 3
- wifiecho example error -2 on uno r4 HOT 2
- `-Wvla` compiler warnings HOT 1
- Cannot connect to broker HOT 2
- SSL/TLS with Aduino UNO Wifi REV2 HOT 1
- This MQTT library needs IPv6 support
- can not set setKeepAliveInterval HOT 1
- MQTT not connecting to AWS IOT with MKR NB 15000 HOT 3
- Doesn't connect to ThingsBoard HOT 1
- Giga R1 WiFi works with a small change HOT 3
- Failure to connect to 127.0.0.1 (localhost) using Mosquitto Broker HOT 1
- Slow and then non responsive mqtt.read
- json string is "damaged" on transmitting HOT 2
- How to send MQTT server's username and password HOT 2
- mqttClient.connected() always 0 despite being connected HOT 3
- Add Uno R4 WiFi support to examples HOT 1
- Linker error undefined reference to .. if I use the MQTT_CLIENT_STD_FUNCTION_CALLBACK HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from arduinomqttclient.