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

    Arduino & ESP32 - Basic Bluetooth

    Scheduled Pinned Locked Moved
    Video Tutorials
    1
    1
    291
    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.
    • K
      kl3m3n
      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 (classic) Bluetooth using Esp32 board. You can expand the example for your specific project.

      Download BasicBluetooth.ino sketch from:
      https://drive.google.com/file/d/1enW3ghvZDcW72ZqO4pWwTulHovmsBc8k/view?usp=sharing

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

      /*
       * GUI-O Basic Bluetooth example 
       *
       * Copyright (C) 2021, GUI-O Team
       *
       * SPDX-License-Identifier: BSD-3-Clause
       */
      
      #include "BluetoothSerial.h"
      
      // if not enabled, the SDK must be recompiled
      #if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
      #error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
      #endif
      
      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
      
      // forward declare parser for incoming messages
      void parseGuioMsg(const String &msg);
      
      // icoming data buffer
      String in;
      
      // global
      BluetoothSerial btSerial;
      
      void setup() {
        // debug output
        Serial.begin(115200);
        
        // setup bluetooth serial
        btSerial.begin("BasicBluetooth");
        
        Serial.println("Bluetooth ready to pair!");
      
        // setup builtin led
        ledcSetup(led::LED_CHANNEL, led::LED_FREQ, led::LED_BITS); // channel, freq, resolution_bits
        ledcAttachPin(led::LED_BUILTIN, led::LED_CHANNEL);
      }
      
      void loop() {
        while(btSerial.available()) {
          const char c = btSerial.read();
          in += c;
        
          if(c == '\n') {
            // parse message string
            parseGuioMsg(in);
            // clear buffer
            in = "";
          }  
        }
      }
      
      /***************************/
      /* IMPLEMENT YOUR GUI HERE */
      /***************************/
      void sendMsg(const String &msg) {
        btSerial.write((const uint8_t*) msg.c_str(), msg.length());
      }
      
      void parseGuioMsg(const String &msg) {
        if(msg.startsWith("@init")) {
          Serial.println("GUI-O app is requesting INITIALIZATION!");
          
          // clear screen and set background
          sendMsg("@cls\r\n");
          sendMsg("@guis BGC:#FFFFFF\r\n");
          delay(100);
      
          // initialize simple example GUI
          sendMsg("|LB UID:title X:50 Y:15 TXT:\"Simple light switch\" FFA:\"font8\" FSZ:3.5\r\n");
          sendMsg("|LB UID:tap_me X:50 Y:70 TXT:\"TAP ME!\" FFA:\"font8\" FSZ:3 FFA:\"font5\"\r\n");
          sendMsg("|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");
          sendMsg("|IM UID:light_off X:50 Y:50 IP:\"https://i.imgur.com/3VbsS0Z.png\" VIS:1\r\n");
          sendMsg("|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"
          sendMsg("@light_off VIS:0\r\n");
          sendMsg("@light_on VIS:1\r\n");
          sendMsg("@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"
          sendMsg("@light_off VIS:1\r\n");
          sendMsg("@light_on VIS:0\r\n");
          sendMsg("@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