GithubHelp home page GithubHelp logo

huster-songtao / espmqttwrapper Goto Github PK

View Code? Open in Web Editor NEW

This project forked from anwarminarso/espwifimqttwrapper

0.0 0.0 0.0 44 KB

ESP32/ESP8266 MQTT wrapper for ease of use

License: MIT License

C++ 100.00%

espmqttwrapper's Introduction

ESP WiFi Mqtt Wrapper

Overview

This library is for ESP32/ESP8266 for easy use of WiFi and MQTT

Example Code

Publishing MQTT Topic every 10 seconds

wrapper.setPublisher("/MyTopic", 10000, [&] {
  String message = "";
  DynamicJsonDocument doc(512);
  
  float temp = random(2500.0f, 3500.0f) / 100.0f;
  float hum = random(7500.0f, 9000.0f) / 100.0f;

  // create JSON document
  doc["temperature"] = temp;
  doc["humidity"] = hum;

  // Serialize to JSON
  // Excample Output messge => { "temperature": 25,"humidity":75 }
  serializeJson(doc, message);
  return message;
});

Simple Subscription

wrapper.setSubscription("/MyTopic", [&](const char* message) {
  Serial.print("Message Received : ");
  Serial.print(message);
  Serial.println();

  //If our message is json format
  //for example mqtt message => { "temp": 28.3, "hum": 78.4 }
  DynamicJsonDocument doc(512);
  deserializeJson(doc, message); //deserialize JSON message to DynamicJsonDocument
  
  float temperature = doc["temp"];
  float humidity = doc["hum"];
  
  Serial.print("Temperature: ");
  Serial.print(temperature);
  Serial.print(", Humidity: ");
  Serial.println(humidity);
});

Related Link

License

MIT License

espmqttwrapper's People

Contributors

anwarminarso avatar

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.