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

    Arduino & ESP32 - Basic Ethernet (TCP/IP)

    Scheduled Pinned Locked Moved
    Video Tutorials
    1
    1
    670
    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 kl3m3n


      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 Ethernet (TCP/IP) using Esp32 board. You can expand the example for your specific project.

      Download BasicEthernet.ino sketch from (or see code snippet below):
      https://drive.google.com/file/d/1bYiYV5-9yzMpWqDnqDRNzXLW2ZXCEayz/view?usp=sharing

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

      /*
       * GUI-O Basic Ethernet example (using ESP32-WROOM-32)
       *
       * Copyright (C) 2022, GUI-O Team
       *
       * SPDX-License-Identifier: BSD-3-Clause
       *
       * see https://forum.gui-o.com/category/10/video-tutorials
       */
      
      #include <WiFi.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
      
      // setup for fixed ip address
      IPAddress ip(192, 168, 1, 123);
      IPAddress gateway(192, 168, 1, 1);
      IPAddress subnet(255, 255, 0, 0);
      IPAddress primaryDNS(8, 8, 8, 8);
      IPAddress secondaryDNS(8, 8, 4, 4);
      
      // wifi server with custom port number
      WiFiServer server(34567);
      
      // forward declare parser for incoming messages
      void parseGuioMsg(const String &msg, WiFiClient &client);
      
      // icoming data buffer
      String in;
      
      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);
      
        // requesting fixed ip address
        if(!WiFi.config(ip, gateway, subnet, primaryDNS, secondaryDNS)) {
          Serial.println("WiFi configuration failed!");
        }  
        
        // 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!");
        Serial.print("IP address: ");
        Serial.println(WiFi.localIP());
      
        // start server
        server.begin();
      }
      
      void loop() {
        // listen for clients
        WiFiClient client = server.available();
      
        // new (GUI-O) client connected
        if(client) {
          Serial.println("Client connected!");
          
          // loop while connected
          while(client.connected()) {
            // bytes from client available
            if(client.available()) {
              const char c = client.read();
              in += c;
              
              if(c == '\n') {
                // parse message string
                parseGuioMsg(in, client);
                // clear buffer
                in = "";
              }        
            }
          }
        }
      }
      
      /***************************/
      /* IMPLEMENT YOUR GUI HERE */
      /***************************/
      void parseGuioMsg(const String &msg, WiFiClient &client) {
        if(msg.startsWith("@init")) {
          Serial.println("GUI-O app is requesting INITIALIZATION!");
      
          // clear screen and set background
          client.println("@cls");
          client.println("@guis BGC:#FFFFFF");
          delay(100);
      
          // initialize simple example GUI
          client.println("|LB UID:title X:50 Y:15 TXT:\"Simple light switch\" FFA:\"font8\" FSZ:3.5");
          client.println("|LB UID:tap_me X:50 Y:70 TXT:\"TAP ME!\" FFA:\"font8\" FSZ:3 FFA:\"font5\"");
          client.println("|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");
          client.println("|IM UID:light_off X:50 Y:50 IP:\"https://i.imgur.com/3VbsS0Z.png\" VIS:1");
          client.println("|IM UID:light_on X:50 Y:50 IP:\"https://i.imgur.com/gNdck9A.png\" VIS:0");
        }
        else if(msg.startsWith("@light_off")) {
          Serial.println("GUI-O app is requesting LIGHT ON!");
      
          // "drive GUI-O app"
          client.println("@light_off VIS:0");
          client.println("@light_on VIS:1");
          client.println("@brightness VIS:1 VAL:100");
      
          // 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"
          client.println("@light_off VIS:1");
          client.println("@light_on VIS:0");
          client.println("@brightness VIS:0 VAL:0");
      
          // 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));
            }            
          }    
        }
        client.flush();
      }
      
      
      1 Reply Last reply Reply Quote 0
      • First post
        Last post