GUI-O Forum
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Register
    • Login

    Arduino & ESP32 - Basic MQTT

    Scheduled Pinned Locked Moved
    Video Tutorials
    1
    1
    370
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • A
      admin
      last edited by admin


      GUI-O + Arduino + ESP32

      Developer manual: https://www.gui-o.com/assets/gui-o_developer_manual.pdf


      A DEMO project to help you quickly get started with GUI-O and MQTT using Esp32 board. You can expand the example for your specific project.

      Download BasicMQTT.ino sketch from (or see code snippet below):
      https://drive.google.com/file/d/17vnYJrMzHJLfSP1q8YAOLSVYZG0xI9rW/view?usp=sharing

      Additional Boards Manager URLs:
      https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json

      /*
       * GUI-O Basic MQTT example 
       *
       * Copyright (C) 2021, GUI-O Team
       *
       * SPDX-License-Identifier: BSD-3-Clause
       */
      
      #include <WiFi.h>
      #include <PubSubClient.h>
      
      namespace led {
        static const uint8_t LED_BUILTIN = 2;
        static const uint8_t LED_CHANNEL = 0;
        static const double LED_FREQ = 5000.0;
        static const uint8_t LED_BITS = 8;
      } // namespace led
      
      static const char *ssid = "<ssid>"; // router name
      static const char *pass = "<pass>"; // router password
      
      static const char *mqttServer = "mqtt.gui-o.com";   // host name
      static const char *mqttUser = "gui-o-mqtt-generic"; // user name
      static const char *mqttPass = "lqXeZpv5VJyv0XBT";   // password
      
      static const char *Out = "<Out>"; // GUI-O app publish topic
      static const char *In = "<In>";  // GUI-O app subscribe topic
      
      // mqtt client
      WiFiClient wiFiClient;
      PubSubClient mqttClient(wiFiClient);
      
      // forward declare functions for mqtt messages handling
      void mqttCallback(char* topic, byte* message, unsigned int length);
      void parseGuioMsg(const String &msg);
      
      void setup() {
        // debug output
        Serial.begin(115200);
      
        // setup builtin led
        ledcSetup(led::LED_CHANNEL, led::LED_FREQ, led::LED_BITS); // channel, freq, resolution_bits
        ledcAttachPin(led::LED_BUILTIN, led::LED_CHANNEL);
        
        // connect WiFi (keep trying...)
        Serial.print("Connecting to ");
        Serial.println(ssid);
        
        WiFi.begin(ssid, pass);
        while(WiFi.status() != WL_CONNECTED) { 
          Serial.print(".");
          delay(500); 
        }
        
        Serial.println("WiFi connected!"); 
        
        // setup mqtt
        mqttClient.setServer(mqttServer, 1883);
        mqttClient.setCallback(mqttCallback);
      }
      
      void loop() {
        while(!mqttClient.connected()) {
          Serial.println("MQTT connecting...");
      
          if(mqttClient.connect("", mqttUser, mqttPass)) {
            Serial.println("MQTT connected!");
            mqttClient.subscribe(&Out[0]);
          }
          else {
            Serial.print("MQTT connection failed (");
            Serial.print(mqttClient.state());
            Serial.println(")! Retrying...");      
            delay(2500);
          }
        }
        mqttClient.loop();
      }
      
      // mqtt callback function
      void mqttCallback(char* topic, byte* message, unsigned int length) {
        // build message string
        String msg;
        for(int i = 0; i < length; i++) {
          msg += (char) message[i];
        }
        // parse message string
        parseGuioMsg(msg);
      }
      
      /***************************/
      /* IMPLEMENT YOUR GUI HERE */
      /***************************/
      void parseGuioMsg(const String &msg) {
        if(msg.startsWith("@init")) {
          Serial.println("GUI-O app is requesting INITIALIZATION!");
      
          // clear screen and set background
          mqttClient.publish(&In[0], "@cls\r\n");
          mqttClient.publish(&In[0], "@guis BGC:#FFFFFF\r\n");
          delay(100);
      
          // initialize simple example GUI
          mqttClient.publish(&In[0], "|LB UID:title X:50 Y:15 TXT:\"Simple light switch\" FFA:\"font8\" FSZ:3.5\r\n");
          mqttClient.publish(&In[0], "|LB UID:tap_me X:50 Y:70 TXT:\"TAP ME!\" FFA:\"font8\" FSZ:3 FFA:\"font5\"\r\n");
          mqttClient.publish(&In[0], "|CB UID:brightness X:50 Y:50 W:90 BTH:5 HAH:8 HAW:8 VIS:0 STA:135 ENA:45 FGC:#000000 SFGC:#FFFF00 BGC:#CBCBCB\r\n");
          mqttClient.publish(&In[0], "|IM UID:light_off X:50 Y:50 IP:\"https://i.imgur.com/3VbsS0Z.png\" VIS:1\r\n");
          mqttClient.publish(&In[0], "|IM UID:light_on X:50 Y:50 IP:\"https://i.imgur.com/gNdck9A.png\" VIS:0\r\n");
        }
        else if(msg.startsWith("@light_off")) {
          Serial.println("GUI-O app is requesting LIGHT ON!");
      
          // "drive GUI-O app"
          mqttClient.publish(&In[0], "@light_off VIS:0\r\n");
          mqttClient.publish(&In[0], "@light_on VIS:1\r\n");
          mqttClient.publish(&In[0], "@brightness VIS:1 VAL:100\r\n");
      
          // led enable
          ledcWrite(led::LED_CHANNEL, 255);
        }
        else if(msg.startsWith("@light_on")) {
          Serial.println("GUI-O app is requesting LIGHT OFF!");
      
          // "drive GUI-O app"
          mqttClient.publish(&In[0], "@light_off VIS:1\r\n");
          mqttClient.publish(&In[0], "@light_on VIS:0\r\n");
          mqttClient.publish(&In[0], "@brightness VIS:0 VAL:0\r\n");
      
          // led disable
          ledcWrite(led::LED_CHANNEL, 0);
        }
        else if(msg.startsWith("@brightness")) {
          const int idx = msg.indexOf(' ');
          
          if(idx > 0) {
            const int val = msg.substring(idx + 1).toInt();
            
            if(val >= 0 && val <= 100) {
              Serial.print("GUI-O app is requesting LIGHT BRIGHTNESS: ");
              Serial.println(val);
      
              // led drive
              ledcWrite(led::LED_CHANNEL, static_cast<uint8_t>(val * 2.55));
            }            
          }    
        }  
      }
      
      1 Reply Last reply Reply Quote 0
      • First post
        Last post