Giter VIP home page Giter VIP logo

Comments (2)

bryantamayo1 avatar bryantamayo1 commented on September 14, 2024 1

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.

jesawyers avatar jesawyers commented on September 14, 2024

Going to use Arduino Forums

from arduinomqttclient.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.