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

Username
  

Password
  





Search Forums

(Advanced Search)

Forum Statistics
» Members: 8,720
» Latest member: sanavnish
» Forum threads: 3,823
» Forum posts: 19,572

Full Statistics

Online Users
There are currently 44 online users.
» 0 Member(s) | 39 Guest(s)
Amazonbot, bot

Latest Threads
"KCS" v2.2.19 firmware BI...
Forum: "KCS" v2 firmware system
Last Post: admin
6 hours ago
» Replies: 0
» Views: 8
kWh resolution
Forum: N30
Last Post: admin
7 hours ago
» Replies: 22
» Views: 451
Custom ESPHome firmware a...
Forum: KC868-A6
Last Post: admin
Yesterday, 01:33 PM
» Replies: 37
» Views: 8,447
sample code to receive ht...
Forum: F16
Last Post: admin
Yesterday, 06:33 AM
» Replies: 19
» Views: 696
A32pro issue
Forum: News
Last Post: admin
Yesterday, 05:53 AM
» Replies: 3
» Views: 37
N10 port modbus
Forum: N10
Last Post: admin
03-07-2026, 11:52 PM
» Replies: 13
» Views: 152
KC868-HAv2 work with F24 ...
Forum: KC868-HA /HA v2
Last Post: admin
03-06-2026, 11:29 PM
» Replies: 13
» Views: 1,262
N60/N30/N20/N10 PC softwa...
Forum: N60
Last Post: admin
03-06-2026, 08:22 AM
» Replies: 3
» Views: 639
N60 N30 N20 N10 ARM CPU f...
Forum: N60
Last Post: admin
03-06-2026, 08:17 AM
» Replies: 0
» Views: 32
KinCony ALR ESP32 I/O pin...
Forum: KinCony ALR
Last Post: admin
03-06-2026, 01:29 AM
» Replies: 6
» Views: 1,575

  [arduino code examples for B24M]-11 digital INPUT trigger OUTPUT directly
Posted by: admin - 07-31-2025, 07:08 AM - Forum: B24M - No Replies

Code:
/*
* Made by KinCony IoT: https://www.kincony.com
*
* This program reads 24 input states from two PCF8575 I/O expanders and
* controls a corresponding 24-channel relay module.
* When an input pin is LOW, the corresponding relay is turned ON (LOW means ON for the relay).
* When the input pin is HIGH, the corresponding relay is turned OFF.
*
* Pin Definitions:
* - SDA: GPIO 8
* - SCL: GPIO 18
* - Input I2C Address 1 (inputs 1-16): 0x24
* - Input/Relay I2C Address 2 (inputs 17-24 and relays 1-8): 0x26
* - Relay I2C Address 3 (relays 9-24): 0x25
*/

#include <Wire.h>        // I2C communication
#include <PCF8575.h>     // Library to control the PCF8575 I/O expander

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

// I2C addresses for the PCF8575 modules
#define INPUT_I2C_ADDRESS_1 0x22   // I2C address for the first input PCF8575 module (inputs 1-16)
#define INPUT_RELAY_I2C_ADDRESS_2 0x25   // I2C address for inputs 17-24 and relays 1-8
#define RELAY_I2C_ADDRESS_3 0x24   // I2C address for relays 9-24

PCF8575 pcf8575_IN1(INPUT_I2C_ADDRESS_1);    // Create an object for the first input PCF8575 (inputs 1-16)
PCF8575 pcf8575_IN2(INPUT_RELAY_I2C_ADDRESS_2);    // Create an object for the second input PCF8575 (inputs 17-24 and relays 1-8)
PCF8575 pcf8575_RL(RELAY_I2C_ADDRESS_3);    // Create an object for the relay PCF8575 (relays 9-24)

void setup() {
  // Initialize I2C communication
  Wire.begin(SDA, SCL);
 
  // Initialize serial communication
  Serial.begin(115200);
 
  // Initialize input and relay modules
  pcf8575_IN1.begin();  // For inputs 1-16
  pcf8575_IN2.begin();  // For inputs 17-24 and relays 1-8
  pcf8575_RL.begin();   // For relays 9-24
 
  // Turn off all relays at the start (LOW means OFF)
  for (int i = 8; i < 16; i++) {
    pcf8575_IN2.write(i, HIGH);  // Turn off relays 1-8
  }
  for (int i = 0; i < 16; i++) {
    pcf8575_RL.write(i, HIGH);  // Turn off relays 9-24
  }

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

void loop() {

if (pcf8575_IN1.read(8)==0) pcf8575_IN2.write(8, LOW); else pcf8575_IN2.write(8, HIGH);
if (pcf8575_IN1.read(9)==0) pcf8575_IN2.write(9, LOW); else pcf8575_IN2.write(9, HIGH);
if (pcf8575_IN1.read(10)==0) pcf8575_IN2.write(10, LOW); else pcf8575_IN2.write(10, HIGH);
if (pcf8575_IN1.read(11)==0) pcf8575_IN2.write(11, LOW); else pcf8575_IN2.write(11, HIGH);
if (pcf8575_IN1.read(12)==0) pcf8575_IN2.write(12, LOW); else pcf8575_IN2.write(12, HIGH);
if (pcf8575_IN1.read(13)==0) pcf8575_IN2.write(13, LOW); else pcf8575_IN2.write(13, HIGH);
if (pcf8575_IN1.read(14)==0) pcf8575_IN2.write(14, LOW); else pcf8575_IN2.write(14, HIGH);
if (pcf8575_IN1.read(15)==0) pcf8575_IN2.write(15, LOW); else pcf8575_IN2.write(15, HIGH);

if (pcf8575_IN1.read(0)==0) pcf8575_RL.write(0, LOW); else pcf8575_RL.write(0, HIGH);
if (pcf8575_IN1.read(1)==0) pcf8575_RL.write(1, LOW); else pcf8575_RL.write(1, HIGH);
if (pcf8575_IN1.read(2)==0) pcf8575_RL.write(2, LOW); else pcf8575_RL.write(2, HIGH);
if (pcf8575_IN1.read(3)==0) pcf8575_RL.write(3, LOW); else pcf8575_RL.write(3, HIGH);
if (pcf8575_IN1.read(4)==0) pcf8575_RL.write(4, LOW); else pcf8575_RL.write(4, HIGH);
if (pcf8575_IN1.read(5)==0) pcf8575_RL.write(5, LOW); else pcf8575_RL.write(5, HIGH);
if (pcf8575_IN1.read(6)==0) pcf8575_RL.write(6, LOW); else pcf8575_RL.write(6, HIGH);
if (pcf8575_IN1.read(7)==0) pcf8575_RL.write(7, LOW); else pcf8575_RL.write(7, HIGH);

if (pcf8575_IN2.read(0)==0) pcf8575_RL.write(8, LOW); else pcf8575_RL.write(8, HIGH);
if (pcf8575_IN2.read(1)==0) pcf8575_RL.write(9, LOW); else pcf8575_RL.write(9, HIGH);
if (pcf8575_IN2.read(2)==0) pcf8575_RL.write(10, LOW); else pcf8575_RL.write(10, HIGH);
if (pcf8575_IN2.read(3)==0) pcf8575_RL.write(11, LOW); else pcf8575_RL.write(11, HIGH);
if (pcf8575_IN2.read(4)==0) pcf8575_RL.write(12, LOW); else pcf8575_RL.write(12, HIGH);
if (pcf8575_IN2.read(5)==0) pcf8575_RL.write(13, LOW); else pcf8575_RL.write(13, HIGH);
if (pcf8575_IN2.read(6)==0) pcf8575_RL.write(14, LOW); else pcf8575_RL.write(14, HIGH);
if (pcf8575_IN2.read(7)==0) pcf8575_RL.write(15, LOW); else pcf8575_RL.write(15, HIGH);
  // Delay for 500 milliseconds
  delay(200);
}
arduino ino file download: 

.zip   11-input-trigger-output.zip (Size: 1.18 KB / Downloads: 305)
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: 192.44 KB / Downloads: 323)

Print this item

  [arduino code examples for B24]-11 digital INPUT trigger OUTPUT directly
Posted by: admin - 07-31-2025, 07:08 AM - Forum: B24 - No Replies

Code:
/*
* Made by KinCony IoT: https://www.kincony.com
*
* This program reads 24 input states from two PCF8575 I/O expanders and
* controls a corresponding 24-channel relay module.
* When an input pin is LOW, the corresponding relay is turned ON (LOW means ON for the relay).
* When the input pin is HIGH, the corresponding relay is turned OFF.
*
* Pin Definitions:
* - SDA: GPIO 8
* - SCL: GPIO 18
* - Input I2C Address 1 (inputs 1-16): 0x24
* - Input/Relay I2C Address 2 (inputs 17-24 and relays 1-8): 0x26
* - Relay I2C Address 3 (relays 9-24): 0x25
*/

#include <Wire.h>        // I2C communication
#include <PCF8575.h>     // Library to control the PCF8575 I/O expander

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

// I2C addresses for the PCF8575 modules
#define INPUT_I2C_ADDRESS_1 0x22   // I2C address for the first input PCF8575 module (inputs 1-16)
#define INPUT_RELAY_I2C_ADDRESS_2 0x25   // I2C address for inputs 17-24 and relays 1-8
#define RELAY_I2C_ADDRESS_3 0x24   // I2C address for relays 9-24

PCF8575 pcf8575_IN1(INPUT_I2C_ADDRESS_1);    // Create an object for the first input PCF8575 (inputs 1-16)
PCF8575 pcf8575_IN2(INPUT_RELAY_I2C_ADDRESS_2);    // Create an object for the second input PCF8575 (inputs 17-24 and relays 1-8)
PCF8575 pcf8575_RL(RELAY_I2C_ADDRESS_3);    // Create an object for the relay PCF8575 (relays 9-24)

void setup() {
  // Initialize I2C communication
  Wire.begin(SDA, SCL);
 
  // Initialize serial communication
  Serial.begin(115200);
 
  // Initialize input and relay modules
  pcf8575_IN1.begin();  // For inputs 1-16
  pcf8575_IN2.begin();  // For inputs 17-24 and relays 1-8
  pcf8575_RL.begin();   // For relays 9-24
 
  // Turn off all relays at the start (LOW means OFF)
  for (int i = 8; i < 16; i++) {
    pcf8575_IN2.write(i, HIGH);  // Turn off relays 1-8
  }
  for (int i = 0; i < 16; i++) {
    pcf8575_RL.write(i, HIGH);  // Turn off relays 9-24
  }

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

void loop() {

if (pcf8575_IN1.read(8)==0) pcf8575_IN2.write(8, LOW); else pcf8575_IN2.write(8, HIGH);
if (pcf8575_IN1.read(9)==0) pcf8575_IN2.write(9, LOW); else pcf8575_IN2.write(9, HIGH);
if (pcf8575_IN1.read(10)==0) pcf8575_IN2.write(10, LOW); else pcf8575_IN2.write(10, HIGH);
if (pcf8575_IN1.read(11)==0) pcf8575_IN2.write(11, LOW); else pcf8575_IN2.write(11, HIGH);
if (pcf8575_IN1.read(12)==0) pcf8575_IN2.write(12, LOW); else pcf8575_IN2.write(12, HIGH);
if (pcf8575_IN1.read(13)==0) pcf8575_IN2.write(13, LOW); else pcf8575_IN2.write(13, HIGH);
if (pcf8575_IN1.read(14)==0) pcf8575_IN2.write(14, LOW); else pcf8575_IN2.write(14, HIGH);
if (pcf8575_IN1.read(15)==0) pcf8575_IN2.write(15, LOW); else pcf8575_IN2.write(15, HIGH);

if (pcf8575_IN1.read(0)==0) pcf8575_RL.write(0, LOW); else pcf8575_RL.write(0, HIGH);
if (pcf8575_IN1.read(1)==0) pcf8575_RL.write(1, LOW); else pcf8575_RL.write(1, HIGH);
if (pcf8575_IN1.read(2)==0) pcf8575_RL.write(2, LOW); else pcf8575_RL.write(2, HIGH);
if (pcf8575_IN1.read(3)==0) pcf8575_RL.write(3, LOW); else pcf8575_RL.write(3, HIGH);
if (pcf8575_IN1.read(4)==0) pcf8575_RL.write(4, LOW); else pcf8575_RL.write(4, HIGH);
if (pcf8575_IN1.read(5)==0) pcf8575_RL.write(5, LOW); else pcf8575_RL.write(5, HIGH);
if (pcf8575_IN1.read(6)==0) pcf8575_RL.write(6, LOW); else pcf8575_RL.write(6, HIGH);
if (pcf8575_IN1.read(7)==0) pcf8575_RL.write(7, LOW); else pcf8575_RL.write(7, HIGH);

if (pcf8575_IN2.read(0)==0) pcf8575_RL.write(8, LOW); else pcf8575_RL.write(8, HIGH);
if (pcf8575_IN2.read(1)==0) pcf8575_RL.write(9, LOW); else pcf8575_RL.write(9, HIGH);
if (pcf8575_IN2.read(2)==0) pcf8575_RL.write(10, LOW); else pcf8575_RL.write(10, HIGH);
if (pcf8575_IN2.read(3)==0) pcf8575_RL.write(11, LOW); else pcf8575_RL.write(11, HIGH);
if (pcf8575_IN2.read(4)==0) pcf8575_RL.write(12, LOW); else pcf8575_RL.write(12, HIGH);
if (pcf8575_IN2.read(5)==0) pcf8575_RL.write(13, LOW); else pcf8575_RL.write(13, HIGH);
if (pcf8575_IN2.read(6)==0) pcf8575_RL.write(14, LOW); else pcf8575_RL.write(14, HIGH);
if (pcf8575_IN2.read(7)==0) pcf8575_RL.write(15, LOW); else pcf8575_RL.write(15, HIGH);
  // Delay for 500 milliseconds
  delay(200);
}
arduino ino file download: 

.zip   11-input-trigger-output.zip (Size: 1.18 KB / Downloads: 322)
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: 192.44 KB / Downloads: 326)

Print this item

  [arduino code examples for B32M]-11 digital INPUT trigger OUTPUT directly
Posted by: admin - 07-31-2025, 07:08 AM - Forum: B32M - No Replies

Code:
/*
* Made by KinCony IoT: https://www.kincony.com
*
* This program reads 32 input states from two PCF8575 I/O expanders and
* controls a corresponding 32-channel relay module.
* When an input pin is LOW, the corresponding relay is turned ON (LOW means ON for the relay).
* When the input pin is HIGH, the corresponding relay is turned OFF.
*
* Pin Definitions:
* - SDA: GPIO 8
* - SCL: GPIO 18
* - Input I2C Address 1 (inputs 1-16): 0x24
* - Input/Relay I2C Address 2 (inputs 17-24 and relays 1-8): 0x26
* - Relay I2C Address 3 (relays 9-24): 0x25
*/

#include <Wire.h>        // I2C communication
#include <PCF8575.h>     // Library to control the PCF8575 I/O expander

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

// I2C addresses for the PCF8575 modules
#define INPUT_I2C_ADDRESS_1 0x22   // I2C address for the first input PCF8575 module (inputs 1-16)
#define INPUT_RELAY_I2C_ADDRESS_2 0x25   // I2C address for inputs 17-24 and relays 9-16
#define RELAY_I2C_ADDRESS_3 0x24   // I2C address for relays 17-32
#define RELAY_I2C_ADDRESS_4 0x26   // I2C address for relays 1-8

PCF8575 pcf8575_IN1(INPUT_I2C_ADDRESS_1);    // Create an object for the first input PCF8575 (inputs 1-16)
PCF8575 pcf8575_IN2(INPUT_RELAY_I2C_ADDRESS_2);    // Create an object for the second input PCF8575 (inputs 17-24 and relays 9-16)
PCF8575 pcf8575_RL(RELAY_I2C_ADDRESS_3);    // Create an object for the relay PCF8575 (relays 17-32)
PCF8575 pcf8575_RL2(RELAY_I2C_ADDRESS_4);    // Create an object for the relay PCF8575 (relays 1-8)

void setup() {
  // Initialize I2C communication
  Wire.begin(SDA, SCL);
 
  // Initialize serial communication
  Serial.begin(115200);
 
  // Initialize input and relay modules
  pcf8575_IN1.begin();  // For inputs 1-16
  pcf8575_IN2.begin();  // For inputs 17-24 and relays 1-8
  pcf8575_RL.begin();   // For relays 9-24
  pcf8575_RL2.begin();   // For relays 25-32
 
  // Turn off all relays at the start (LOW means OFF)
  for (int i = 8; i < 16; i++) {
    pcf8575_RL2.write(i, HIGH);  // Turn off relays 1-8
  }

  for (int i = 8; i < 16; i++) {
    pcf8575_IN2.write(i, HIGH);  // Turn off relays 9-16
  }
  for (int i = 0; i < 16; i++) {
    pcf8575_RL.write(i, HIGH);  // Turn off relays 17-32
  }


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

void loop() {

if (pcf8575_IN1.read(8)==0) pcf8575_RL2.write(8, LOW); else pcf8575_RL2.write(8, HIGH);
if (pcf8575_IN1.read(9)==0) pcf8575_RL2.write(9, LOW); else pcf8575_RL2.write(9, HIGH);
if (pcf8575_IN1.read(10)==0) pcf8575_RL2.write(10, LOW); else pcf8575_RL2.write(10, HIGH);
if (pcf8575_IN1.read(11)==0) pcf8575_RL2.write(11, LOW); else pcf8575_RL2.write(11, HIGH);
if (pcf8575_IN1.read(12)==0) pcf8575_RL2.write(12, LOW); else pcf8575_RL2.write(12, HIGH);
if (pcf8575_IN1.read(13)==0) pcf8575_RL2.write(13, LOW); else pcf8575_RL2.write(13, HIGH);
if (pcf8575_IN1.read(14)==0) pcf8575_RL2.write(14, LOW); else pcf8575_RL2.write(14, HIGH);
if (pcf8575_IN1.read(15)==0) pcf8575_RL2.write(15, LOW); else pcf8575_RL2.write(15, HIGH);

if (pcf8575_IN1.read(0)==0) pcf8575_IN2.write(8, LOW); else pcf8575_IN2.write(8, HIGH);
if (pcf8575_IN1.read(1)==0) pcf8575_IN2.write(9, LOW); else pcf8575_IN2.write(9, HIGH);
if (pcf8575_IN1.read(2)==0) pcf8575_IN2.write(10, LOW); else pcf8575_IN2.write(10, HIGH);
if (pcf8575_IN1.read(3)==0) pcf8575_IN2.write(11, LOW); else pcf8575_IN2.write(11, HIGH);
if (pcf8575_IN1.read(4)==0) pcf8575_IN2.write(12, LOW); else pcf8575_IN2.write(12, HIGH);
if (pcf8575_IN1.read(5)==0) pcf8575_IN2.write(13, LOW); else pcf8575_IN2.write(13, HIGH);
if (pcf8575_IN1.read(6)==0) pcf8575_IN2.write(14, LOW); else pcf8575_IN2.write(14, HIGH);
if (pcf8575_IN1.read(7)==0) pcf8575_IN2.write(15, LOW); else pcf8575_IN2.write(15, HIGH);

if (pcf8575_IN2.read(0)==0) pcf8575_RL.write(0, LOW); else pcf8575_RL.write(0, HIGH);
if (pcf8575_IN2.read(1)==0) pcf8575_RL.write(1, LOW); else pcf8575_RL.write(1, HIGH);
if (pcf8575_IN2.read(2)==0) pcf8575_RL.write(2, LOW); else pcf8575_RL.write(2, HIGH);
if (pcf8575_IN2.read(3)==0) pcf8575_RL.write(3, LOW); else pcf8575_RL.write(3, HIGH);
if (pcf8575_IN2.read(4)==0) pcf8575_RL.write(4, LOW); else pcf8575_RL.write(4, HIGH);
if (pcf8575_IN2.read(5)==0) pcf8575_RL.write(5, LOW); else pcf8575_RL.write(5, HIGH);
if (pcf8575_IN2.read(6)==0) pcf8575_RL.write(6, LOW); else pcf8575_RL.write(6, HIGH);
if (pcf8575_IN2.read(7)==0) pcf8575_RL.write(7, LOW); else pcf8575_RL.write(7, HIGH);

if (pcf8575_RL2.read(0)==0) pcf8575_RL.write(8, LOW); else pcf8575_RL.write(8, HIGH);
if (pcf8575_RL2.read(1)==0) pcf8575_RL.write(9, LOW); else pcf8575_RL.write(9, HIGH);
if (pcf8575_RL2.read(2)==0) pcf8575_RL.write(10, LOW); else pcf8575_RL.write(10, HIGH);
if (pcf8575_RL2.read(3)==0) pcf8575_RL.write(11, LOW); else pcf8575_RL.write(11, HIGH);
if (pcf8575_RL2.read(4)==0) pcf8575_RL.write(12, LOW); else pcf8575_RL.write(12, HIGH);
if (pcf8575_RL2.read(5)==0) pcf8575_RL.write(13, LOW); else pcf8575_RL.write(13, HIGH);
if (pcf8575_RL2.read(6)==0) pcf8575_RL.write(14, LOW); else pcf8575_RL.write(14, HIGH);
if (pcf8575_RL2.read(7)==0) pcf8575_RL.write(15, LOW); else pcf8575_RL.write(15, HIGH);

  // Delay for 500 milliseconds
  delay(200);
}

arduino ino file download: 

.zip   11-input-trigger-output.zip (Size: 1.28 KB / Downloads: 8)
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: 198.08 KB / Downloads: 12)

Print this item

  [arduino code examples for B24M]-10 Print TEXT on SSD1306 OLED displayer
Posted by: admin - 07-31-2025, 07:06 AM - Forum: B24M - Replies (4)

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: 309)
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: 202.58 KB / Downloads: 318)

Print this item

  [arduino code examples for B24]-10 Print TEXT on SSD1306 OLED displayer
Posted by: admin - 07-31-2025, 07:06 AM - Forum: B24 - 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: 306)
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: 202.58 KB / Downloads: 336)

Print this item

  [arduino code examples for B32M]-10 Print TEXT on SSD1306 OLED displayer
Posted by: admin - 07-31-2025, 07:06 AM - Forum: B32M - Replies (4)

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: 312)
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: 202.58 KB / Downloads: 323)

Print this item

  [arduino code examples for B24M]-09 how to communication with Tuya WiFi module
Posted by: admin - 07-31-2025, 07:05 AM - Forum: B24M - 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: 333)
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: 187.17 KB / Downloads: 316)

Print this item

  [arduino code examples for B24]-09 how to communication with Tuya WiFi module
Posted by: admin - 07-31-2025, 07:05 AM - Forum: B24 - 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: 334)
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: 187.17 KB / Downloads: 320)

Print this item

  [arduino code examples for B32M]-09 how to communication with Tuya WiFi module
Posted by: admin - 07-31-2025, 07:05 AM - Forum: B32M - 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: 337)
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: 187.17 KB / Downloads: 319)

Print this item

  [arduino code examples for B24M]-08 Ethernet W5500 chip work with TCP Server mode
Posted by: admin - 07-31-2025, 07:04 AM - Forum: B24M - 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  42
#define W5500_RST_PIN 44
#define W5500_INT_PIN 43
#define W5500_CLK_PIN 1
#define W5500_MOSI_PIN 2
#define W5500_MISO_PIN 41

// 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   8-Ethernet-W5500.zip (Size: 1.23 KB / Downloads: 329)
BIN file (you can use esp32 download tool download to ESP32-S3 with address 0x0 then directly to use) download: 

.zip   8-Ethernet-W5500.ino.merged.zip (Size: 191.07 KB / Downloads: 344)

Print this item