Welcome, Guest
You have to register before you can post on our site.

Username
  

Password
  





Search Forums

(Advanced Search)

Forum Statistics
» Members: 8,298
» Latest member: supriyapathak
» Forum threads: 3,627
» Forum posts: 18,713

Full Statistics

Online Users
There are currently 28 online users.
» 0 Member(s) | 16 Guest(s)
AhrefsBot, Amazonbot, Bing, Crawl, PetalBot, bot

Latest Threads
N30 Energy entry not work...
Forum: N30
Last Post: admin
15 minutes ago
» Replies: 4
» Views: 16
KC868-Server ESP32 Ethern...
Forum: KC868-Server Raspberry Pi4 local server
Last Post: admin
17 minutes ago
» Replies: 5
» Views: 29
adaptor V2 and KC868 h32b...
Forum: KC868-ATC / Tuya adapter V2
Last Post: admin
9 hours ago
» Replies: 1
» Views: 2
KC868-A6 - how to connect...
Forum: KC868-A6
Last Post: admin
9 hours ago
» Replies: 1
» Views: 1
easy way to export/import...
Forum: KC868-A series and Uair Smart Controller
Last Post: admin
9 hours ago
» Replies: 7
» Views: 5,583
KC868-A16 home assistant
Forum: DIY Project
Last Post: admin
11 hours ago
» Replies: 1
» Views: 9
Some bugs in Home Assista...
Forum: N60
Last Post: Vega
Yesterday, 09:21 PM
» Replies: 3
» Views: 64
a16v3 - reed switch and m...
Forum: KC868-A16v3
Last Post: admin
Yesterday, 12:54 PM
» Replies: 17
» Views: 593
N30 LoRa and RS485 code f...
Forum: N30
Last Post: Vega
Yesterday, 11:40 AM
» Replies: 2
» Views: 18
N60/N30/N20/N10/M30 CT se...
Forum: N20
Last Post: SharkBait
Yesterday, 03:59 AM
» Replies: 3
» Views: 413

  [arduino code examples for T64M]-05 Ethernet W5500 chip work with TCP Server mode
Posted by: admin - 04-20-2025, 07:40 AM - Forum: T64M - No Replies

Code:
/*
* Made by KinCony IoT: https://www.kincony.com
*
* This Arduino program sets up an ESP32-S3 with a W5500 Ethernet module
* as a TCP server. It listens on port 4196 and echoes back any string
* received from a client.
*
* Hardware connections:
* - CLK: GPIO42
* - MOSI: GPIO43
* - MISO: GPIO44
* - CS: GPIO41
* - RST: GPIO1
* - INT: GPIO2
*
* Static IP address: 192.168.3.55
* Subnet Mask: 255.255.255.0
* Gateway: 192.168.3.1
* DNS: 192.168.3.1
*/

#include <SPI.h>
#include <Ethernet.h>

// Define the W5500 Ethernet module pins
#define W5500_CS_PIN  41
#define W5500_RST_PIN 1
#define W5500_INT_PIN 2
#define W5500_CLK_PIN 42
#define W5500_MOSI_PIN 43
#define W5500_MISO_PIN 44

// MAC address for your Ethernet shield (must be unique on your network)
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

// Static IP address configuration
IPAddress ip(192, 168, 3, 55);       // Static IP address
IPAddress subnet(255, 255, 255, 0);   // Subnet mask
IPAddress gateway(192, 168, 3, 1);    // Default gateway
IPAddress dns(192, 168, 3, 1);        // DNS server address

// Create an EthernetServer object to handle TCP connections
EthernetServer server(4196);

void setup() {
  // Initialize serial communication
  Serial.begin(115200);
  while (!Serial) {
    ; // Wait for serial port to connect
  }

  // Initialize the W5500 module
  pinMode(W5500_RST_PIN, OUTPUT);
  pinMode(W5500_INT_PIN, INPUT);
  digitalWrite(W5500_RST_PIN, LOW);  // Reset the W5500 module
  delay(100);                       // Wait for reset to complete
  digitalWrite(W5500_RST_PIN, HIGH); // Release reset

  // Initialize SPI with the correct pin definitions
  SPI.begin(W5500_CLK_PIN, W5500_MISO_PIN, W5500_MOSI_PIN);

  // Set up the Ethernet library with W5500-specific pins
  Ethernet.init(W5500_CS_PIN);

  // Start the Ethernet connection with static IP configuration
  Ethernet.begin(mac, ip, dns, gateway, subnet);

  // Print the IP address to the serial monitor
  Serial.print("IP Address: ");
  Serial.println(Ethernet.localIP());

  // Start listening for incoming TCP connections
  server.begin();
}

void loop() {
  // Check for incoming client connections
  EthernetClient client = server.available();
  if (client) {
    Serial.println("New client connected");

    // Read data from the client and echo it back
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        server.write(c);
      }
    }

    // Close the connection when done
    client.stop();
    Serial.println("Client disconnected");
  }
}
arduino ino file download:

.zip   5-Ethernet-W5500.zip (Size: 1.23 KB / Downloads: 312)
BIN file (you can use esp32 download tool download to ESP32-S3 with address 0x0 then directly to use) download:

.zip   5-Ethernet-W5500.ino.merged.zip (Size: 191.08 KB / Downloads: 316)

Print this item

  [arduino code examples for T64M]-04 Read free GPIO state
Posted by: admin - 04-20-2025, 07:38 AM - Forum: T64M - No Replies

Code:
/*
* Made by KinCony IoT: https://www.kincony.com
*
* GPIO Status Monitoring
*
* This program monitors the status (high or low) of multiple GPIO pins on the ESP32-S3.
* It prints the status of the pins to the serial monitor whenever a change is detected.
*
* GPIO Pins Monitored:
* - GPIO 8
* - GPIO 9
* - GPIO 10
* - GPIO 15
* - GPIO 16
* - GPIO 17
* - GPIO 18
* - GPIO 0
*
* Hardware Requirements:
* - Connect the pins to appropriate devices or pull them to HIGH/LOW for testing
*/

#define GPIO_PIN_8 8
#define GPIO_PIN_9 9
#define GPIO_PIN_10 10
#define GPIO_PIN_15 15
#define GPIO_PIN_16 16
#define GPIO_PIN_17 17
#define GPIO_PIN_18 18
#define GPIO_PIN_0 0

// Store the previous state of the GPIO pins
bool prevState[8] = {false, false, false, false, false, false, false, false};

void setup() {
  // Initialize serial communication for debugging purposes
  Serial.begin(115200); // Initialize the serial monitor at 115200 baud
  while (!Serial);      // Wait for the serial monitor to open

  // Initialize GPIO pins as inputs
  pinMode(GPIO_PIN_8, INPUT);
  pinMode(GPIO_PIN_9, INPUT);
  pinMode(GPIO_PIN_10, INPUT);
  pinMode(GPIO_PIN_15, INPUT);
  pinMode(GPIO_PIN_16, INPUT);
  pinMode(GPIO_PIN_17, INPUT);
  pinMode(GPIO_PIN_18, INPUT);
  pinMode(GPIO_PIN_0, INPUT);

  Serial.println("GPIO Status Monitoring Started");
}

void loop() {
  // Read the current state of each GPIO pin
  bool currentState[8];
  currentState[0] = digitalRead(GPIO_PIN_8);
  currentState[1] = digitalRead(GPIO_PIN_9);
  currentState[2] = digitalRead(GPIO_PIN_10);
  currentState[3] = digitalRead(GPIO_PIN_15);
  currentState[4] = digitalRead(GPIO_PIN_16);
  currentState[5] = digitalRead(GPIO_PIN_17);
  currentState[6] = digitalRead(GPIO_PIN_18);
  currentState[7] = digitalRead(GPIO_PIN_0);

  // Check for changes in GPIO pin states
  for (int i = 0; i < 8; i++) {
    if (currentState[i] != prevState[i]) {
      // Print the pin number and its new state if it has changed
      Serial.print("GPIO ");
      Serial.print(i == 0 ? GPIO_PIN_8 :
                   i == 1 ? GPIO_PIN_9 :
                   i == 2 ? GPIO_PIN_10 :
                   i == 3 ? GPIO_PIN_15 :
                   i == 4 ? GPIO_PIN_16 :
                   i == 5 ? GPIO_PIN_17 :
                   i == 6 ? GPIO_PIN_18 : GPIO_PIN_0);
      Serial.print(" changed to ");
      Serial.println(currentState[i] ? "HIGH" : "LOW");
      // Update the previous state
      prevState[i] = currentState[i];
    }
  }

  // Delay to avoid flooding the serial monitor
  delay(100); // Adjust the delay as needed
}
arduino ino file download:

.zip   4-free-gpio-state.zip (Size: 1.04 KB / Downloads: 279)
BIN file (you can use esp32 download tool download to ESP32-S3 with address 0x0 then directly to use) download:

.zip   4-free-gpio-state.ino.merged.zip (Size: 181.77 KB / Downloads: 292)

Print this item

  [arduino code examples for T64M]-03 RS485 communication test
Posted by: admin - 04-20-2025, 07:37 AM - Forum: T64M - No Replies

Code:
/*
* Made by KinCony IoT: https://www.kincony.com
*
* RS485 Communication Test with Direction Control (SP3485EEN)
*
* This program is a simple test for RS485 communication using ESP32-S3.
* It will send a message over RS485 and then read incoming messages.
* The TXD pin is defined as GPIO 13, RXD is GPIO 21, and EN is GPIO 14.
*/

#include <HardwareSerial.h>

// Define RS485 pins
#define RS485_RXD 21     // RS485 Receive
#define RS485_TXD 13     // RS485 Transmit
#define RS485_EN  14     // RS485 Direction control (High: Send, Low: Receive)

// Create a hardware serial object on UART1
HardwareSerial rs485Serial(1);

void setup() {
  // Start serial communication for debugging
  Serial.begin(115200);
  while (!Serial);

  // Set RS485 direction control pin
  pinMode(RS485_EN, OUTPUT);
  digitalWrite(RS485_EN, LOW); // Start in receive mode

  // Initialize RS485 serial communication
  rs485Serial.begin(9600, SERIAL_8N1, RS485_RXD, RS485_TXD);

  Serial.println("RS485 Test Start (with direction control)");
}

void loop() {
  // Prepare the message to send
  String message = "Hello from KinCony T64M!";
 
  // Switch to transmit mode
  digitalWrite(RS485_EN, HIGH);
  delay(2); // Short delay to allow driver to switch
 
  // Send the message
  rs485Serial.println(message);
  rs485Serial.flush(); // Wait for transmission to complete
 
  // Switch back to receive mode
  digitalWrite(RS485_EN, LOW);
 
  Serial.println("Message sent. Waiting for response...");

  // Wait for a response (up to 1 second)
  unsigned long startTime = millis();
  while (millis() - startTime < 1000) {
    if (rs485Serial.available()) {
      String receivedMessage = "";
      while (rs485Serial.available()) {
        char c = rs485Serial.read();
        receivedMessage += c;
      }
      // Print the received message
      Serial.print("Received: ");
      Serial.println(receivedMessage);
      break;
    }
  }

  // Wait before sending the next message
  delay(2000);
}
arduino ino file download:

.zip   3-RS485-Test.zip (Size: 1.02 KB / Downloads: 325)
BIN file (you can use esp32 download tool download to ESP32-S3 with address 0x0 then directly to use) download:

.zip   3-RS485-Test.ino.merged.zip (Size: 186.66 KB / Downloads: 334)

Print this item

  [arduino code examples for T64M]-02 Read digital input ports state
Posted by: admin - 04-20-2025, 07:35 AM - Forum: T64M - No Replies

Code:
/*
* Made by KinCony IoT: https://www.kincony.com
*
* Description:
* This Arduino program reads the state of 64 input channels using 4 PCF8575 I/O expanders
* and prints the state of all input pins to the Serial Monitor. Each input state is printed
* in binary (0 = ON, 1 = OFF), grouped by 16 bits per chip.
*
* I2C Pin Definitions:
* - SDA: GPIO 12
* - SCL: GPIO 11
*
* PCF8575 Addresses:
* - Input  1~16 : 0x25
* - Input 17~32: 0x24
* - Input 33~48: 0x21
* - Input 49~64: 0x20
*/

#include <Wire.h>
#include <PCF8575.h>

// Define I2C pins
#define SDA_PIN 12
#define SCL_PIN 11

// Create PCF8575 objects
PCF8575 pcf_1(0x25); // Input  1-16
PCF8575 pcf_2(0x24); // Input 17-32
PCF8575 pcf_3(0x21); // Input 33-48
PCF8575 pcf_4(0x20); // Input 49-64

// Array of PCF8575 pointers
PCF8575* pcfArray[4] = { &pcf_1, &pcf_2, &pcf_3, &pcf_4 };

void setup() {
  Serial.begin(115200);
  Wire.begin(SDA_PIN, SCL_PIN); // Initialize I2C

  // Initialize each PCF8575
  for (int i = 0; i < 4; i++) {
    pcfArray[i]->begin();
  }

  Serial.println("KinCony F64 64 channel input state  0:ON  1:OFF");
}

void loop() {
  Serial.println("Reading Inputs...");

  for (int chip = 0; chip < 4; chip++) {
    uint16_t state = 0;

    // Read 16 input pins per PCF8575
    for (int pin = 0; pin < 16; pin++) {
      if (pcfArray[chip]->read(pin)) {
        state |= (1 << pin); // Bit = 1 means OFF (no signal), 0 means ON
      }
    }

    // Display result
    Serial.print("Input ");
    Serial.print(chip * 16 + 1);
    Serial.print("~");
    Serial.print((chip + 1) * 16);
    Serial.print(": ");
    for (int b = 15; b >= 0; b--) {
      Serial.print(bitRead(state, b));
    }
    Serial.println();
  }

  Serial.println(); // Blank line between readings
  delay(500);       // Wait 500ms before next read
}
arduino ino file download:

.zip   2-digital-input.zip (Size: 1.01 KB / Downloads: 309)
BIN file (you can use esp32 download tool download to ESP32-S3 with address 0x0 then directly to use) download:

.zip   2-digital-input.ino.merged.zip (Size: 192.41 KB / Downloads: 304)

Print this item

  [arduino code examples for T64M]-01 Turn ON/OFF OUTPUT
Posted by: admin - 04-20-2025, 07:34 AM - Forum: T64M - No Replies

Code:
/*
* Made by KinCony IoT: https://www.kincony.com
*
* This program controls 64 outputs using 4 PCF8575 I/O expanders.
* It sequentially turns on outputs 1 to 64, then turns them off from 64 to 1, in a loop.
*
* I2C Pin Definitions:
* - SDA: GPIO 48
* - SCL: GPIO 47
*
* PCF8575 Addresses:
* - Output  1~16 : 0x25
* - Output 17~32: 0x24
* - Output 33~48: 0x21
* - Output 49~64: 0x20
*
* Delay Time:
* - 200 milliseconds between switching outputs
*/

#include <Wire.h>        // Include the Wire library for I2C communication
#include <PCF8575.h>     // Include the PCF8575 library

#define SDA_PIN 48       // Define SDA pin
#define SCL_PIN 47       // Define SCL pin
#define DELAY_TIME 200   // Delay between operations in milliseconds

// Create PCF8575 objects for each chip
PCF8575 pcf_1(0x25); // output  1-16
PCF8575 pcf_2(0x24); // output 17-32
PCF8575 pcf_3(0x21); // output 33-48
PCF8575 pcf_4(0x20); // output 49-64

// Array of pointers to PCF8575 objects
PCF8575* pcfArray[4] = { &pcf_1, &pcf_2, &pcf_3, &pcf_4 };

void setup() {
  Wire.begin(SDA_PIN, SCL_PIN);  // Initialize I2C with specified pins
  Serial.begin(115200);
  Serial.println("PCF8575 64-Channel Output Control Starting...");

  // Initialize all PCF8575 modules and set all pins to HIGH (OFF)
  for (int i = 0; i < 4; i++) {
    pcfArray[i]->begin();
    for (int j = 0; j < 16; j++) {
      pcfArray[i]->write(j, HIGH);  // Turn off all outputs initially
    }
  }
}

void loop() {
  // Turn ON outputs from 1 to 64 (LOW)
  for (int channel = 0; channel < 64; channel++) {
    int chipIndex = channel / 16;         // Which PCF8575 (0 to 3)
    int pinIndex = channel % 16;          // Which pin on that PCF8575 (0 to 15)
    pcfArray[chipIndex]->write(pinIndex, LOW); // Turn ON the output
    delay(DELAY_TIME);
  }

  // Turn OFF outputs from 64 to 1 (HIGH)
  for (int channel = 63; channel >= 0; channel--) {
    int chipIndex = channel / 16;
    int pinIndex = channel % 16;
    pcfArray[chipIndex]->write(pinIndex, HIGH); // Turn OFF the output
    delay(DELAY_TIME);
  }
}
arduino ino file download:

.zip   1-output.zip (Size: 1.03 KB / Downloads: 307)
BIN file (you can use esp32 download tool download to ESP32-S3 with address 0x0 then directly to use) download:

.zip   1-output.ino.merged.zip (Size: 192.12 KB / Downloads: 290)

Print this item

  T64M ESPHome yaml for home assistant
Posted by: admin - 04-20-2025, 07:32 AM - Forum: T64M - No Replies

Code:
/*
* Made by KinCony IoT: https://www.kincony.com
*
* This program reads 8 input states from a PCF8575 I/O expander and
* controls a corresponding 8-channel relay module. When an input pin
* is LOW, the corresponding relay is turned ON (LOW means ON for the relay).
*
* Pin Definitions:
* - SDA: GPIO 8
* - SCL: GPIO 18
* - PCF8575 I2C Address: 0x24
*/

#include <Wire.h>
#include <PCF8575.h>

// I2C pins
#define SDA 8
#define SCL 18

// PCF8575 I2C address
#define INPUT_I2C_ADDRESS 0x24   

// Create PCF8575 object
PCF8575 pcf8575_IN(INPUT_I2C_ADDRESS);

void setup() {
  // Initialize I2C communication
  Wire.begin(SDA, SCL);

  // Initialize serial communication
  Serial.begin(115200);
 
  // Initialize PCF8575
  pcf8575_IN.begin();

  // Turn off all relays first (assuming LOW activates the relay)
  for (int i = 8; i < 16; i++) {
    pcf8575_IN.write(i, HIGH);  // Set to HIGH to turn off the relay
  }

  Serial.println("System started: Input state controlling 8 relays");
}

void loop() {
  // Read 16-bit state from PCF8575
  uint16_t inputState = pcf8575_IN.read16();

  for (int i = 0; i < 8; i++) {
    bool state = bitRead(inputState, i); // Read digital input state

    Serial.print("DI");
    Serial.print(i + 1);
    Serial.print(" State: ");
    Serial.println(state ? "HIGH" : "LOW");

    // Control relay (LOW activates the relay)
    pcf8575_IN.write(8 + i, state ? HIGH : LOW);
  }

  Serial.println("---------------------");
  delay(500);
}
yaml download: 

.txt   T64M-HA.txt (Size: 23.77 KB / Downloads: 205)

Print this item

  T64M ESP32-S3 IO pins define
Posted by: admin - 04-20-2025, 07:30 AM - Forum: T64M - No Replies

Code:
analog A1 (0-5v): GPIO7
analog A2 (0-5v): GPIO6
analog A3 (4-20mA): GPIO5
analog A4 (4-20mA): GPIO4

-----------------
IIC Bus-1:

SDA:GPIO48
SCL:GPIO47

PCF8575:(output1-16): i2c address:0x25
PCF8575:(output17-32): i2c address:0x24
PCF8575:(output33-48): i2c address:0x21
PCF8575:(output49-64): i2c address:0x20

24C02 EPROM i2c address: 0x50

------------------

IIC Bus-2:

SDA:GPIO12
SCL:GPIO11

PCF8575:(input1-16): i2c address:0x25
PCF8575:(input17-32): i2c address:0x24
PCF8575:(input33-48): i2c address:0x21
PCF8575:(input49-64): i2c address:0x20



-----------------

1-wire (pull-up resistance on PCB):
GPIO15
GPIO16
GPIO17
GPIO18

free GPIO:
GPIO8
GPIO9
GPIO10

-----------------

Ethernet (W5500) I/O define:

clk_pin: GPIO42
mosi_pin: GPIO43
miso_pin: GPIO44
cs_pin: GPIO41

interrupt_pin: GPIO2
reset_pin: GPIO1

--------------------
RS485:
RXD:GPIO21
TXD:GPIO13
EN:GPIO14

Print this item

  [arduino code examples for F8]-11 Digital input trigger relay output
Posted by: admin - 04-20-2025, 07:28 AM - Forum: F8 - No Replies

Code:
/*
* Made by KinCony IoT: https://www.kincony.com
*
* This program reads 8 input states from a PCF8575 I/O expander and
* controls a corresponding 8-channel relay module. When an input pin
* is LOW, the corresponding relay is turned ON (LOW means ON for the relay).
*
* Pin Definitions:
* - SDA: GPIO 8
* - SCL: GPIO 18
* - PCF8575 I2C Address: 0x24
*/

#include <Wire.h>
#include <PCF8575.h>

// I2C pins
#define SDA 8
#define SCL 18

// PCF8575 I2C address
#define INPUT_I2C_ADDRESS 0x24   

// Create PCF8575 object
PCF8575 pcf8575_IN(INPUT_I2C_ADDRESS);

void setup() {
  // Initialize I2C communication
  Wire.begin(SDA, SCL);

  // Initialize serial communication
  Serial.begin(115200);
 
  // Initialize PCF8575
  pcf8575_IN.begin();

  // Turn off all relays first (assuming LOW activates the relay)
  for (int i = 8; i < 16; i++) {
    pcf8575_IN.write(i, HIGH);  // Set to HIGH to turn off the relay
  }

  Serial.println("System started: Input state controlling 8 relays");
}

void loop() {
  // Read 16-bit state from PCF8575
  uint16_t inputState = pcf8575_IN.read16();

  for (int i = 0; i < 8; i++) {
    bool state = bitRead(inputState, i); // Read digital input state

    Serial.print("DI");
    Serial.print(i + 1);
    Serial.print(" State: ");
    Serial.println(state ? "HIGH" : "LOW");

    // Control relay (LOW activates the relay)
    pcf8575_IN.write(8 + i, state ? HIGH : LOW);
  }

  Serial.println("---------------------");
  delay(500);
}
arduino ino file download:

.zip   11-input-trigger-output.zip (Size: 898 bytes / Downloads: 310)
BIN file (you can use esp32 download tool download to ESP32-S3 with address 0x0 then directly to use) download:

.zip   11-input-trigger-output.ino.merged.zip (Size: 189.63 KB / Downloads: 317)

Print this item

  [arduino code examples for F8]-10 Print TEXT on SSD1306 OLED displayer
Posted by: admin - 04-20-2025, 07:21 AM - Forum: F8 - No Replies

Code:
/*
* Made by KinCony IoT: https://www.kincony.com
*
* This Arduino program demonstrates how to display text on an SSD1306 128x64 OLED display using the U8g2 library.
* The program draws two lines of text on the display:
* - The first line is "KINCONY" in a larger font.
* - The second line is "www.kincony.com" in a smaller font.
*
* The display is connected via I2C (software implementation) with:
* - SCL (clock) on pin IO18
* - SDA (data) on pin IO8
*
* The display's I2C address is set to 0x3C.
*/

#include <U8g2lib.h>  // Include the U8g2 library for controlling the OLED display
#include <Wire.h>     // Include the Wire library for I2C communication

// Initialize the display using the software I2C method (SCL = IO18, SDA = IO8)
U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0,  18, 8, U8X8_PIN_NONE);  // Screen rotation: U8G2_R0

// Function to display page 1 content
void page1() {
  // Set font size 18 for the larger "KINCONY" text
  u8g2.setFont(u8g2_font_timR18_tf);  // Use the Times Roman font, size 18
  u8g2.setFontPosTop();               // Set the text position at the top of the display
  u8g2.setCursor(5, 0);               // Position the cursor at coordinates (5, 0)
  u8g2.print("KINCONY");              // Display the text "KINCONY" on the screen

  // Set font size 12 for the smaller "www.kincony.com" text
  u8g2.setFont(u8g2_font_timR12_tf);  // Use the Times Roman font, size 12
  u8g2.setCursor(0, 40);              // Position the cursor at coordinates (0, 40)
  u8g2.print("www.kincony.com");      // Display the text "www.kincony.com"
}

// Setup function, runs once when the program starts
void setup() {
  // Set the I2C address for the display to 0x3C
  u8g2.setI2CAddress(0x3C*2);  // I2C address shift for 8-bit format
 
  // Initialize the display
  u8g2.begin();
 
  // Enable UTF-8 character printing for the display
  u8g2.enableUTF8Print();  // Allow UTF-8 encoded text to be printed
}

// Main loop function, continuously runs after setup()
void loop() {
  // Begin the display drawing process
  u8g2.firstPage();  // Prepare the first page for drawing
  do {
    // Call the page1() function to draw content on the display
    page1();
  } while (u8g2.nextPage());  // Continue to the next page until all pages are drawn
}
arduino ino file download:

.zip   10-oled-ssd1306.zip (Size: 1.11 KB / Downloads: 301)
BIN file (you can use esp32 download tool download to ESP32-S3 with address 0x0 then directly to use) download:

.zip   10-oled-ssd1306.ino.merged.zip (Size: 201.24 KB / Downloads: 288)

Print this item

  [arduino code examples for F8]-09 how to communication with Tuya WiFi module
Posted by: admin - 04-20-2025, 07:19 AM - Forum: F8 - No Replies

Code:
/*
* Made by KinCony IoT: https://www.kincony.com
*
* This Arduino program implements communication between ESP32 and the Tuya module
* via UART (serial communication). It listens for specific packets from the Tuya module
* and responds according to the predefined commands.
*
* Functionality:
* 1. When the ESP32 receives a heartbeat packet (55 AA 00 00 00 00 FF),
*    it sends a heartbeat response (55 AA 03 00 00 01 00 03).
* 2. When the ESP32 receives a product information request (55 AA 00 01 00 00 00),
*    it sends a product information response (55 AA 03 01 ...).
* 3. When the ESP32 receives a work mode request (55 AA 00 02 00 00 01),
*    it sends a work mode response (55 AA 03 02 00 03 10 1C 14 47).
* 4. When the ESP32 receives a network status request (55 AA 00 03 00 01 00 03),
*    it sends a network status response (55 AA 03 03 00 00 05).
* 5. Subsequent heartbeat packets (55 AA 00 00 00 00 FF) are responded to with
*    (55 AA 03 00 00 01 01 04).
*/

#include <HardwareSerial.h>

// Create a HardwareSerial object for UART communication on ESP32
HardwareSerial tuyaSerial(1);

// Define the GPIO pins for TXD and RXD used for serial communication
#define TXD_PIN 39
#define RXD_PIN 38

// Set the baud rate for Tuya module communication to 9600
#define BAUD_RATE 9600

// Define the response packets for different commands from the Tuya module

// Heartbeat response: 55 AA 03 00 00 01 00 03
uint8_t heartBeatResponse[] = {0x55, 0xAA, 0x03, 0x00, 0x00, 0x01, 0x00, 0x03};

// Product info response with a detailed payload (e.g., firmware version, product name, etc.)
uint8_t productInfoResponse[] = {
  0x55, 0xAA, 0x03, 0x01, 0x00, 0x2A, 0x7B, 0x22, 0x70, 0x22, 0x3A, 0x22,
  0x63, 0x68, 0x6D, 0x7A, 0x6C, 0x67, 0x6A, 0x70, 0x61, 0x64, 0x70, 0x71,
  0x78, 0x64, 0x6B, 0x6F, 0x22, 0x2C, 0x22, 0x76, 0x22, 0x3A, 0x22, 0x31,
  0x2E, 0x30, 0x2E, 0x30, 0x22, 0x2C, 0x22, 0x6D, 0x22, 0x3A, 0x30, 0x7D, 0xAA
};

// Work mode response: 55 AA 03 02 00 03 10 1C 14 47
uint8_t workModeResponse[] = {0x55, 0xAA, 0x03, 0x02, 0x00, 0x03, 0x10, 0x1C, 0x14, 0x47};

// Network status response: 55 AA 03 03 00 00 05
uint8_t netStatusResponse[] = {0x55, 0xAA, 0x03, 0x03, 0x00, 0x00, 0x05};

// Subsequent heartbeat response: 55 AA 03 00 00 01 01 04
uint8_t secondHeartBeatResponse[] = {0x55, 0xAA, 0x03, 0x00, 0x00, 0x01, 0x01, 0x04};

void setup() {
  // Initialize the serial communication for debugging at 115200 baud rate
  Serial.begin(115200);

  // Initialize the serial communication with Tuya module at 9600 baud rate
  tuyaSerial.begin(BAUD_RATE, SERIAL_8N1, RXD_PIN, TXD_PIN);

  // Debug message to indicate that the serial communication has been initialized
  Serial.println("ESP32-Tuya serial communication initialized.");
}

void loop() {
  // Check if data is available from the Tuya module
  if (tuyaSerial.available()) {
    uint8_t incomingPacket[7];  // Array to store the received packet
    size_t bytesRead = tuyaSerial.readBytes(incomingPacket, 7); // Read 7 bytes from Tuya

    // Check if the packet has a valid header (0x55, 0xAA)
    if (bytesRead >= 2 && incomingPacket[0] == 0x55 && incomingPacket[1] == 0xAA) {
      // If less than 7 bytes were received, wait for more data
      if (bytesRead < 7) {
        Serial.println("Incomplete packet received. Waiting for remaining bytes...");
        delay(50); // Delay to allow more data to be received
        while (tuyaSerial.available()) {
          incomingPacket[bytesRead++] = tuyaSerial.read(); // Continue reading remaining bytes
          if (bytesRead >= 7) break;
        }
      }

      // If still less than 7 bytes, discard the incomplete packet
      if (bytesRead < 7) {
        Serial.println("Error: Incomplete packet discarded.");
        return;
      }

      // Debug: Print the received packet for logging
      Serial.print("Received packet: ");
      for (size_t i = 0; i < 7; i++) {
        Serial.print(incomingPacket[i], HEX);
        Serial.print(" ");
      }
      Serial.println();

      // Call the function to process the received packet
      processTuyaPacket(incomingPacket, 7);

    } else {
      // If the header is invalid, discard the packet and flush the buffer
      Serial.print("Error: Invalid packet header. Data received: ");
      for (size_t i = 0; i < bytesRead; i++) {
        Serial.print(incomingPacket[i], HEX);
        Serial.print(" ");
      }
      Serial.println();
      tuyaSerial.flush(); // Clear the serial buffer
    }
  }

  // Delay to avoid CPU overuse
  delay(100);
}

// Function to process the received packet and send the appropriate response
void processTuyaPacket(uint8_t* packet, size_t size) {
  // Ensure the packet size is 7 and the header is valid
  if (size == 7 && packet[0] == 0x55 && packet[1] == 0xAA) {
    // Determine the command in the packet (packet[2])
    switch(packet[2]) {
      case 0x00:
        if (packet[3] == 0x00 && packet[4] == 0x00 && packet[5] == 0x00 && packet[6] == 0xFF) {
          Serial.println("Heartbeat received.");
          sendPacket(heartBeatResponse, sizeof(heartBeatResponse));
        } else if (packet[3] == 0x01 && packet[4] == 0x00 && packet[5] == 0x00 && packet[6] == 0x00) {
          Serial.println("Product info request received.");
          sendPacket(productInfoResponse, sizeof(productInfoResponse));
        } else if (packet[3] == 0x02 && packet[4] == 0x00 && packet[5] == 0x00 && packet[6] == 0x01) {
          Serial.println("Work mode request received.");
          sendPacket(workModeResponse, sizeof(workModeResponse));
        } else if (packet[3] == 0x03 && packet[4] == 0x00 && packet[5] == 0x01 && packet[6] == 0x00) {
          Serial.println("Network status request received.");
          sendPacket(netStatusResponse, sizeof(netStatusResponse));
        }
        break;

      default:
        Serial.println("Error: Unhandled command received.");
        break;
    }
  }
}

// Function to send the response packet to the Tuya module
void sendPacket(uint8_t* packet, size_t size) {
  // Send the packet via UART to Tuya module
  tuyaSerial.write(packet, size);

  // Debug: Print the sent packet for logging
  Serial.print("Sent packet: ");
  for (size_t i = 0; i < size; i++) {
    Serial.print(packet[i], HEX);
    Serial.print(" ");
  }
  Serial.println();
}
arduino ino file download:

.zip   9-tuya-wifi-config.zip (Size: 2 KB / Downloads: 304)
BIN file (you can use esp32 download tool download to ESP32-S3 with address 0x0 then directly to use) download:

.zip   9-tuya-wifi-config.ino.merged.zip (Size: 184.92 KB / Downloads: 321)

Print this item