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

Username
  

Password
  





Search Forums

(Advanced Search)

Forum Statistics
» Members: 5,367
» Latest member: johnkalvinsmith
» Forum threads: 2,574
» Forum posts: 13,305

Full Statistics

Online Users
There are currently 49 online users.
» 1 Member(s) | 39 Guest(s)
Bing, Crawl, Google, PetalBot, Yandex, bot, johnkalvinsmith

Latest Threads
change wake up name
Forum: KinCony AS
Last Post: gal
3 hours ago
» Replies: 12
» Views: 72
A32 Pro ESPHome yaml incl...
Forum: KC868-A32/A32 Pro
Last Post: xarouli5
4 hours ago
» Replies: 17
» Views: 179
Need help with configurat...
Forum: KC868-HxB series Smart Controller
Last Post: admin
6 hours ago
» Replies: 32
» Views: 390
ESP32 S3 set up issue
Forum: Extender module
Last Post: admin
11 hours ago
» Replies: 10
» Views: 60
KC868-A8 Schematic
Forum: KC868-A8
Last Post: admin
11 hours ago
» Replies: 7
» Views: 45
"KCS" v2.2.8 firmware BIN...
Forum: "KCS" firmware system
Last Post: admin
11 hours ago
» Replies: 2
» Views: 163
Dimensions/drawings of bo...
Forum: Schematic and diagram
Last Post: admin
11 hours ago
» Replies: 1
» Views: 20
how to use AS ESP32-S3 vo...
Forum: KinCony AS
Last Post: admin
12-16-2024, 10:55 PM
» Replies: 12
» Views: 446
Problem with IFTTT automa...
Forum: "KCS" firmware system
Last Post: admin
12-16-2024, 10:53 PM
» Replies: 5
» Views: 34
M16 SHT31 sensor disconne...
Forum: KC868-M16 / M1 / MB / M30
Last Post: bsarra
12-16-2024, 08:36 PM
» Replies: 4
» Views: 38

  [ESP-IDF code examples for A32 Pro]-02 Read digital input ports state
Posted by: admin - 09-14-2024, 12:47 AM - Forum: KC868-A32/A32 Pro - No Replies

Code:
/*
* Made by KinCony IoT: https://www.kincony.com
*
* This program reads inputs from two XL9535 I/O expander chips (I2C addresses 0x24 and 0x25)
* and one PCF8574 I/O expander (I2C address 0x23) via I2C using an ESP32-S3.
* The state of all inputs is printed in binary format to the serial monitor every second.
*
* - The XL9535 chips read inputs from channels 1-32.
* - The PCF8574 chip reads inputs from channels 33-40.
*
* I2C is initialized on GPIO 11 (SDA) and GPIO 10 (SCL) with a frequency of 40kHz.
*/

#include <stdio.h>
#include "driver/i2c.h"
#include "esp_log.h"

// I2C communication settings
#define I2C_MASTER_SCL_IO           10    // GPIO number for I2C clock line (SCL)
#define I2C_MASTER_SDA_IO           11    // GPIO number for I2C data line (SDA)
#define I2C_MASTER_FREQ_HZ          40000  // I2C frequency set to 40kHz
#define I2C_MASTER_PORT             I2C_NUM_0  // I2C port number used in this program

// I2C addresses for XL9535 chips and PCF8574
#define XL9535_ADDR_1               0x24  // Address for XL9535 (channels 1-16)
#define XL9535_ADDR_2               0x25  // Address for XL9535 (channels 17-32)
#define PCF8574_ADDR                0x23  // Address for PCF8574 (channels 33-40)

// XL9535 registers for input ports
#define XL9535_INPUT_PORT_0         0x00  // Input register for port 0 (channels 1-8)
#define XL9535_INPUT_PORT_1         0x01  // Input register for port 1 (channels 9-16)

// Function prototypes
esp_err_t i2c_master_init();
esp_err_t xl9535_read_inputs(uint8_t device_addr, uint8_t *data);
esp_err_t pcf8574_read_inputs(uint8_t *data);
void read_and_print_inputs();

void app_main() {
    // Initialize I2C master
    ESP_ERROR_CHECK(i2c_master_init());

    // Main loop to read and print input states every second
    while (1) {
        read_and_print_inputs();
        vTaskDelay(1000 / portTICK_PERIOD_MS);  // Wait for 1 second
    }
}

// Initialize I2C master interface
esp_err_t i2c_master_init() {
    i2c_config_t conf = {
        .mode = I2C_MODE_MASTER,  // Set I2C mode to master
        .sda_io_num = I2C_MASTER_SDA_IO,  // SDA GPIO pin
        .scl_io_num = I2C_MASTER_SCL_IO,  // SCL GPIO pin
        .sda_pullup_en = GPIO_PULLUP_ENABLE,  // Enable internal pull-up for SDA line
        .scl_pullup_en = GPIO_PULLUP_ENABLE,  // Enable internal pull-up for SCL line
        .master.clk_speed = I2C_MASTER_FREQ_HZ,  // Set I2C clock frequency to 40kHz
    };
    i2c_param_config(I2C_MASTER_PORT, &conf);
    return i2c_driver_install(I2C_MASTER_PORT, conf.mode, 0, 0, 0);
}

// Read inputs from XL9535 I/O expander
esp_err_t xl9535_read_inputs(uint8_t device_addr, uint8_t *data) {
    i2c_cmd_handle_t cmd = i2c_cmd_link_create();
    i2c_master_start(cmd);
    i2c_master_write_byte(cmd, (device_addr << 1) | I2C_MASTER_WRITE, true);
    i2c_master_write_byte(cmd, XL9535_INPUT_PORT_0, true);
    i2c_master_start(cmd);
    i2c_master_write_byte(cmd, (device_addr << 1) | I2C_MASTER_READ, true);
    i2c_master_read_byte(cmd, &data[0], I2C_MASTER_ACK);  // Read port 0 (channels 1-8 or 17-24)
    i2c_master_read_byte(cmd, &data[1], I2C_MASTER_NACK); // Read port 1 (channels 9-16 or 25-32)
    i2c_master_stop(cmd);
    esp_err_t ret = i2c_master_cmd_begin(I2C_MASTER_PORT, cmd, 1000 / portTICK_PERIOD_MS);
    i2c_cmd_link_delete(cmd);
    return ret;
}

// Read inputs from PCF8574 I/O expander
esp_err_t pcf8574_read_inputs(uint8_t *data) {
    i2c_cmd_handle_t cmd = i2c_cmd_link_create();
    i2c_master_start(cmd);
    i2c_master_write_byte(cmd, (PCF8574_ADDR << 1) | I2C_MASTER_READ, true);
    i2c_master_read_byte(cmd, data, I2C_MASTER_NACK);
    i2c_master_stop(cmd);
    esp_err_t ret = i2c_master_cmd_begin(I2C_MASTER_PORT, cmd, 1000 / portTICK_PERIOD_MS);
    i2c_cmd_link_delete(cmd);
    return ret;
}

// Function to read and print the state of inputs from XL9535 and PCF8574 chips
void read_and_print_inputs() {
    uint8_t xl9535_data_1[2];  // Data buffer for XL9535 (channels 1-16)
    uint8_t xl9535_data_2[2];  // Data buffer for XL9535 (channels 17-32)
    uint8_t pcf8574_data;      // Data buffer for PCF8574 (channels 33-40)

    // Read inputs from the first XL9535 (channels 1-16)
    if (xl9535_read_inputs(XL9535_ADDR_1, xl9535_data_1) == ESP_OK) {
        printf("1-16 input states: %02X%02X\n", xl9535_data_1[1], xl9535_data_1[0]);  // Print in binary
    } else {
        printf("Error reading from XL9535 (channels 1-16)\n");
    }

    // Read inputs from the second XL9535 (channels 17-32)
    if (xl9535_read_inputs(XL9535_ADDR_2, xl9535_data_2) == ESP_OK) {
        printf("17-32 input states: %02X%02X\n", xl9535_data_2[1], xl9535_data_2[0]);  // Print in binary
    } else {
        printf("Error reading from XL9535 (channels 17-32)\n");
    }

    // Read inputs from the PCF8574 (channels 33-40)
    if (pcf8574_read_inputs(&pcf8574_data) == ESP_OK) {
        printf("33-40 input states: %02X\n", pcf8574_data);  // Print in binary
    } else {
        printf("Error reading from PCF8574 (channels 33-40)\n");
    }
}
   

Print this item

  [ESP-IDF code examples for A32 Pro]-01 Turn ON/OFF relay
Posted by: admin - 09-14-2024, 12:38 AM - Forum: KC868-A32/A32 Pro - No Replies

Code:
/*
* Made by KinCony IoT: https://www.kincony.com
*
* This program controls two PCA9555 I/O expander chips via I2C to control a total of 32 relays.
* The first PCA9555 (at I2C address 0x21) controls relays 1-16, while the second PCA9555 (at I2C address 0x22)
* controls relays 17-32. The relays are turned ON (LOW) and OFF (HIGH) in sequence.
*
* Each PCA9555 chip has two ports: port 0 controls relays 1-8 (or 17-24), and port 1 controls relays 9-16 (or 25-32).
*
* The program will initialize both chips and turn all relays OFF initially, then turn each relay ON one by one.
* After a delay, all relays are turned OFF again.
*/

#include <stdio.h>
#include <string.h>
#include "driver/i2c.h"
#include "esp_log.h"

// I2C communication settings
#define I2C_MASTER_SCL_IO           10    // GPIO number for I2C clock line (SCL)
#define I2C_MASTER_SDA_IO           11    // GPIO number for I2C data line (SDA)
#define I2C_MASTER_FREQ_HZ          100000 // Frequency for I2C clock (100kHz)
#define I2C_MASTER_PORT             I2C_NUM_0 // I2C port number used in this program

// I2C write/read settings
#define WRITE_BIT                   I2C_MASTER_WRITE // I2C write mode
#define READ_BIT                    I2C_MASTER_READ  // I2C read mode
#define ACK_CHECK_EN                0x1     // Enable I2C ACK checking
#define ACK_CHECK_DIS               0x0     // Disable I2C ACK checking
#define ACK_VAL                     0x0     // I2C ACK value
#define NACK_VAL                    0x1     // I2C NACK value

// PCA9555 I2C addresses (chip 1: controls relays 1-16, chip 2: controls relays 17-32)
#define PCA9555_ADDR_1              0x21    // I2C address for the first PCA9555 (1-16 relays)
#define PCA9555_ADDR_2              0x22    // I2C address for the second PCA9555 (17-32 relays)

// PCA9555 Register addresses
#define PCA9555_REG_OUTPUT_0        0x02    // Output register for port 0 (controls relays 1-8 or 17-24)
#define PCA9555_REG_OUTPUT_1        0x03    // Output register for port 1 (controls relays 9-16 or 25-32)

// Function declarations (prototypes)
esp_err_t i2c_master_init(); // Initializes the I2C master
esp_err_t pca9555_write_byte(uint8_t device_addr, uint8_t reg_addr, uint8_t data); // Writes a byte to a PCA9555 register
void control_relays(); // Main function to control the relays

// Main application entry point
void app_main() {
    // Initialize I2C master interface
    ESP_ERROR_CHECK(i2c_master_init());

    // Set both PCA9555 chips' all pins as output and turn all relays OFF initially (0xFF = all HIGH = OFF)
    ESP_ERROR_CHECK(pca9555_write_byte(PCA9555_ADDR_1, PCA9555_REG_OUTPUT_0, 0xFF)); // Turn off relays 1-8
    ESP_ERROR_CHECK(pca9555_write_byte(PCA9555_ADDR_1, PCA9555_REG_OUTPUT_1, 0xFF)); // Turn off relays 9-16
    ESP_ERROR_CHECK(pca9555_write_byte(PCA9555_ADDR_2, PCA9555_REG_OUTPUT_0, 0xFF)); // Turn off relays 17-24
    ESP_ERROR_CHECK(pca9555_write_byte(PCA9555_ADDR_2, PCA9555_REG_OUTPUT_1, 0xFF)); // Turn off relays 25-32

    // Start controlling relays (turn on one by one, then off)
    control_relays();
}

// Function to initialize the I2C master
esp_err_t i2c_master_init() {
    // I2C configuration structure
    i2c_config_t conf = {
        .mode = I2C_MODE_MASTER,       // Set the I2C mode to master
        .sda_io_num = I2C_MASTER_SDA_IO, // Set the SDA GPIO pin
        .scl_io_num = I2C_MASTER_SCL_IO, // Set the SCL GPIO pin
        .sda_pullup_en = GPIO_PULLUP_ENABLE, // Enable internal pull-up for SDA line
        .scl_pullup_en = GPIO_PULLUP_ENABLE, // Enable internal pull-up for SCL line
        .master.clk_speed = I2C_MASTER_FREQ_HZ, // Set I2C clock frequency to 100kHz
    };
    // Apply I2C configuration to I2C master port
    i2c_param_config(I2C_MASTER_PORT, &conf);
    // Install the I2C driver for the master port
    return i2c_driver_install(I2C_MASTER_PORT, conf.mode, 0, 0, 0);
}

// Function to write a byte of data to a specified register of the PCA9555
// device_addr: I2C address of the PCA9555 chip (0x21 or 0x22)
// reg_addr: Register address to write to (e.g., output register 0 or 1)
// data: The byte of data to write (e.g., relay states: HIGH = OFF, LOW = ON)
esp_err_t pca9555_write_byte(uint8_t device_addr, uint8_t reg_addr, uint8_t data) {
    // Create a new I2C command link
    i2c_cmd_handle_t cmd = i2c_cmd_link_create();
    // Start the I2C communication
    i2c_master_start(cmd);
    // Send the PCA9555's I2C address with the write bit
    i2c_master_write_byte(cmd, (device_addr << 1) | WRITE_BIT, ACK_CHECK_EN);
    // Send the register address where the data should be written
    i2c_master_write_byte(cmd, reg_addr, ACK_CHECK_EN);
    // Send the actual data to be written to the register
    i2c_master_write_byte(cmd, data, ACK_CHECK_EN);
    // Stop the I2C communication
    i2c_master_stop(cmd);
    // Execute the I2C command and return the result
    esp_err_t ret = i2c_master_cmd_begin(I2C_MASTER_PORT, cmd, 1000 / portTICK_PERIOD_MS);
    // Delete the I2C command link after use
    i2c_cmd_link_delete(cmd);
    return ret;
}

// Function to control the relays on both PCA9555 chips
void control_relays() {
    uint8_t relay_state_0 = 0xFE; // Relay 1 ON (LOW), others OFF (HIGH) for port 0
    uint8_t relay_state_1 = 0xFF; // All OFF (HIGH) for port 1 initially

    // Control the first PCA9555 chip (relays 1-16)
    // Port 0 controls relays 1-8
    for (int i = 0; i < 8; ++i) {
        ESP_LOGI("Relay Control", "Setting relay %d ON (chip 1, port 0)", i + 1);
        // Write relay states to port 0 of the first PCA9555 (1-8 relays)
        pca9555_write_byte(PCA9555_ADDR_1, PCA9555_REG_OUTPUT_0, relay_state_0);
        relay_state_0 = (relay_state_0 << 1) | 0x01; // Shift to the next relay
        vTaskDelay(100 / portTICK_PERIOD_MS); // Delay between relay actions
    }

    relay_state_0 = 0xFE; // Reset state for port 0, relay 9 ON

    // Port 1 controls relays 9-16
    for (int i = 0; i < 8; ++i) {
        ESP_LOGI("Relay Control", "Setting relay %d ON (chip 1, port 1)", i + 9);
        // Write relay states to port 1 of the first PCA9555 (9-16 relays)
        pca9555_write_byte(PCA9555_ADDR_1, PCA9555_REG_OUTPUT_1, relay_state_0);
        relay_state_0 = (relay_state_0 << 1) | 0x01; // Shift to the next relay
        vTaskDelay(100 / portTICK_PERIOD_MS); // Delay between relay actions
    }

    relay_state_0 = 0xFE; // Relay 17 ON (LOW), others OFF (HIGH) for port 0

    // Control the second PCA9555 chip (relays 17-32)
    // Port 0 controls relays 17-24
    for (int i = 0; i < 8; ++i) {
        ESP_LOGI("Relay Control", "Setting relay %d ON (chip 2, port 0)", i + 17);
        // Write relay states to port 0 of the second PCA9555 (17-24 relays)
        pca9555_write_byte(PCA9555_ADDR_2, PCA9555_REG_OUTPUT_0, relay_state_0);
        relay_state_0 = (relay_state_0 << 1) | 0x01; // Shift to the next relay
        vTaskDelay(100 / portTICK_PERIOD_MS); // Delay between relay actions
    }

    relay_state_0 = 0xFE; // Reset state for port 1, relay 25 ON

    // Port 1 controls relays 25-32
    for (int i = 0; i < 8; ++i) {
        ESP_LOGI("Relay Control", "Setting relay %d ON (chip 2, port 1)", i + 25);
        // Write relay states to port 1 of the second PCA9555 (25-32 relays)
        pca9555_write_byte(PCA9555_ADDR_2, PCA9555_REG_OUTPUT_1, relay_state_0);
        relay_state_0 = (relay_state_0 << 1) | 0x01; // Shift to the next relay
        vTaskDelay(100 / portTICK_PERIOD_MS); // Delay between relay actions
    }

    // After a short delay, turn all relays OFF
    vTaskDelay(1000 / portTICK_PERIOD_MS);
    // Set all relays on both chips to OFF (HIGH)
    pca9555_write_byte(PCA9555_ADDR_1, PCA9555_REG_OUTPUT_0, 0xFF); // Turn off relays 1-8
    pca9555_write_byte(PCA9555_ADDR_1, PCA9555_REG_OUTPUT_1, 0xFF); // Turn off relays 9-16
    pca9555_write_byte(PCA9555_ADDR_2, PCA9555_REG_OUTPUT_0, 0xFF); // Turn off relays 17-24
    pca9555_write_byte(PCA9555_ADDR_2, PCA9555_REG_OUTPUT_1, 0xFF); // Turn off relays 25-32
}
   

Print this item

  [arduino code examples for A32 Pro]-11 How to use digital input port trigger relay
Posted by: admin - 09-14-2024, 12:12 AM - Forum: KC868-A32/A32 Pro - No Replies

Code:
/*
  Made by KinCony IoT: https://www.kincony.com

  Program functionality:
  This program uses ESP32-S3 to read inputs from two PCA9555 I/O expander chips (I2C addresses 0x24 and 0x25)
  for channels 1-32, and control corresponding relays using two PCA9555 I/O expander chips (I2C addresses 0x21 and 0x22)
  for controlling 32 relays (1-32). When input1 is triggered, relay1 is activated; when input2 is triggered,
  relay2 is activated, and so on.

  The I2C bus is initialized on GPIO pins 11 (SDA) and 10 (SCL) with a frequency of 100kHz.
*/

#include <PCA95x5.h>
#include <Wire.h>

// Initialize the PCA9555 objects for reading inputs (channels 1-32)
PCA9555 input_ioex1;  // For channels 1-16 (I2C address 0x24)
PCA9555 input_ioex2;  // For channels 17-32 (I2C address 0x25)

// Initialize the PCA9555 objects for controlling relays (channels 1-32)
PCA9555 output_ioex1; // For relays 1-16 (I2C address 0x21)
PCA9555 output_ioex2; // For relays 17-32 (I2C address 0x22)

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

    // Initialize the I2C bus with GPIO 11 as SDA and GPIO 10 as SCL, 100kHz frequency
    Wire.begin(11, 10, 100000);

    // Configure the input PCA9555 chips (for inputs 1-32)
    input_ioex1.attach(Wire, 0x24);  // I2C address 0x24 for inputs 1-16
    input_ioex1.polarity(PCA95x5::Polarity::ORIGINAL_ALL);  // No polarity inversion
    input_ioex1.direction(PCA95x5::Direction::IN_ALL);  // Set all pins as inputs

    input_ioex2.attach(Wire, 0x25);  // I2C address 0x25 for inputs 17-32
    input_ioex2.polarity(PCA95x5::Polarity::ORIGINAL_ALL);  // No polarity inversion
    input_ioex2.direction(PCA95x5::Direction::IN_ALL);  // Set all pins as inputs

    // Configure the output PCA9555 chips (for relays 1-32)
    output_ioex1.attach(Wire, 0x21);  // I2C address 0x21 for relays 1-16
    output_ioex1.polarity(PCA95x5::Polarity::ORIGINAL_ALL);  // No polarity inversion
    output_ioex1.direction(PCA95x5::Direction::OUT_ALL);  // Set all pins as outputs
    output_ioex1.write(PCA95x5::Level::H_ALL);  // Initialize all relays to OFF (HIGH)

    output_ioex2.attach(Wire, 0x22);  // I2C address 0x22 for relays 17-32
    output_ioex2.polarity(PCA95x5::Polarity::ORIGINAL_ALL);  // No polarity inversion
    output_ioex2.direction(PCA95x5::Direction::OUT_ALL);  // Set all pins as outputs
    output_ioex2.write(PCA95x5::Level::H_ALL);  // Initialize all relays to OFF (HIGH)

    delay(50);  // Wait for initialization to complete
}

void loop() {
    // Read the input states from the first PCA9555 chip (inputs 1-16)
    uint16_t inputs_1_16 = input_ioex1.read();
   
    // Iterate through each input (1-16) and control corresponding relay output
    for (int i = 0; i < 16; ++i) {
        bool input_state = bitRead(inputs_1_16, i);  // Read the state of each input (0 = triggered, 1 = not triggered)
        if (input_state == 0) {  // If the input is triggered (pressed, i.e., LOW)
            output_ioex1.write((PCA95x5::Port::Port)i, PCA95x5::Level::L);  // Activate corresponding relay (LOW)
        } else {
            output_ioex1.write((PCA95x5::Port::Port)i, PCA95x5::Level::H);  // Deactivate relay (HIGH)
        }
    }

    // Read the input states from the second PCA9555 chip (inputs 17-32)
    uint16_t inputs_17_32 = input_ioex2.read();
   
    // Iterate through each input (17-32) and control corresponding relay output
    for (int i = 0; i < 16; ++i) {
        bool input_state = bitRead(inputs_17_32, i);  // Read the state of each input (0 = triggered, 1 = not triggered)
        if (input_state == 0) {  // If the input is triggered (pressed, i.e., LOW)
            output_ioex2.write((PCA95x5::Port::Port)i, PCA95x5::Level::L);  // Activate corresponding relay (LOW)
        } else {
            output_ioex2.write((PCA95x5::Port::Port)i, PCA95x5::Level::H);  // Deactivate relay (HIGH)
        }
    }

    delay(100);  // Small delay for stability
}
arduino ino file download: 
.zip   11-input-trigger-output.zip (Size: 1.26 KB / Downloads: 44)
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: 187.31 KB / Downloads: 40)

Print this item

  [arduino code examples for A32 Pro]-10 How to test AT command with SIM7600 4G modulde
Posted by: admin - 09-14-2024, 12:09 AM - Forum: KC868-A32/A32 Pro - No Replies

Code:
/*
  Made by KinCony IoT: https://www.kincony.com
  All rights reserved.

  ESP32-S3 with SIM7600 and RS485 Communication
  This program sends an AT command to the SIM7600 4G module every 3 seconds to query its model number.
  The response from the SIM7600 is then sent out through the RS485 interface.

  Connections:
  SIM7600:
    TXD: GPIO18
    RXD: GPIO17
  RS485:
    TXD: GPIO9
    RXD: GPIO8
*/

#include <HardwareSerial.h>

// Create a hardware serial object for SIM7600 communication using UART2
HardwareSerial sim7600(2); // UART2: TXD on GPIO18, RXD on GPIO17

// Create a hardware serial object for RS485 communication using UART1
HardwareSerial rs485(1);   // UART1: TXD on GPIO9, RXD on GPIO8

void setup() {
  // Initialize the default serial port for debugging (Serial Monitor)
  Serial.begin(115200);        // Baud rate of 115200 for debugging

  // Initialize the SIM7600 serial port with baud rate of 115200
  // Set TX to GPIO18 and RX to GPIO17
  sim7600.begin(115200, SERIAL_8N1, 17, 18);

  // Initialize the RS485 serial port with baud rate of 9600
  // Set TX to GPIO9 and RX to GPIO8
  rs485.begin(9600, SERIAL_8N1, 8, 9);

  // Wait for 3 seconds before starting (optional, can be used for initialization delay)
  delay(3000);
}

void loop() {
  // Send the AT command to query SIM7600 module model number
  // "AT+CGMM" is the command used to get the model of the SIM7600
  sim7600.println("AT+CGMM");

  // Wait for 1 second to allow time for the SIM7600 to respond
  delay(1000);

  // Check if there is any data available from the SIM7600 module
  if (sim7600.available()) {
    // Create a string to store the response from the SIM7600
    String simResponse = "";

    // While data is available from the SIM7600, read it byte by byte
    while (sim7600.available()) {
      // Read one byte from the SIM7600 serial
      char c = sim7600.read();
      // Append the byte to the response string
      simResponse += c;
    }
   
    // Send the SIM7600 response to the RS485 serial port
    rs485.print(simResponse);

    // Also print the response to the Serial Monitor for debugging
    Serial.println(simResponse);
  }

  // Wait for 3 seconds before sending the next query
  delay(3000);
}
arduino ino file download: 
.zip   10-4g-sim7600.zip (Size: 1.04 KB / Downloads: 40)
BIN file (you can use esp32 download tool download to ESP32-S3 with address 0x0 then directly to use) download: 
.zip   10-4g-sim7600.ino.merged.zip (Size: 182.27 KB / Downloads: 49)

Print this item

  [arduino code examples for A32 Pro]-09 How to output DC0-10v from GP8403 DAC port
Posted by: admin - 09-14-2024, 12:06 AM - Forum: KC868-A32/A32 Pro - No Replies

Code:
/*
* Made by KinCony IoT: https://www.kincony.com
*
* This Arduino program controls the GP8403 DAC I2C chip using an ESP32-S3 board.
* The program sets the DAC outputs for two channels:
* - Channel 0 (CH0) outputs 5V.
* - Channel 1 (CH1) outputs 10V.
*
* The I2C communication is handled using the Wire library, and the SDA and SCL pins
* are custom defined as GPIO11 (SDA) and GPIO10 (SCL).
*
* GP8403 I2C address: 0x58
*/

#include "DFRobot_GP8403.h"

// Initialize the GP8403 DAC object with the I2C address 0x58
DFRobot_GP8403 dac(&Wire, 0x58);

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

  // Initialize I2C communication with custom SDA and SCL pins
  Wire.begin(11, 10);  // SDA = GPIO11, SCL = GPIO10

  // Try to initialize the DAC, retry if initialization fails
  while(dac.begin() != 0) {
    Serial.println("Initialization failed, retrying...");
    delay(1000);  // Wait for 1 second before retrying
  }

  // If initialization succeeds
  Serial.println("Initialization successful!");

  // Set the DAC output range to 10V (this setting applies to both channels)
  dac.setDACOutRange(dac.eOutputRange10V);

  // Set the output for DAC channel 0 to 5V
  // Range for 10V mode is 0-10000, so 5000 represents 5V
  dac.setDACOutVoltage(5000, 0);  // Set CH0 to 5V
  Serial.println("Channel 0 set to 5V");

  // Set the output for DAC channel 1 to 10V
  dac.setDACOutVoltage(10000, 1);  // Set CH1 to 10V
  Serial.println("Channel 1 set to 10V");

  // Store the data in the chip's non-volatile memory so it persists after power-off
  dac.store();
  Serial.println("DAC values stored in memory");
}

void loop() {
  // No need to continuously write the values, the DAC holds the last set value
}
arduino ino file download: 
.zip   9-dac-GP8403.zip (Size: 975 bytes / Downloads: 37)
BIN file (you can use esp32 download tool download to ESP32-S3 with address 0x0 then directly to use) download: 
.zip   9-dac-GP8403.ino.merged.zip (Size: 187.92 KB / Downloads: 41)

Print this item

  [arduino code examples for A32 Pro]-08 Print TEXT on SSD1306 OLED displayer
Posted by: admin - 09-14-2024, 12:04 AM - Forum: KC868-A32/A32 Pro - 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 IO10
* - SDA (data) on pin IO11
*
* 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 = IO10, SDA = IO11)
U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0,  10, 11, 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   8-oled-ssd1306.zip (Size: 1.11 KB / Downloads: 40)
BIN file (you can use esp32 download tool download to ESP32-S3 with address 0x0 then directly to use) download:
.zip   8-oled-ssd1306.ino.merged.zip (Size: 198.95 KB / Downloads: 41)

Print this item

  [arduino code examples for A32 Pro]-07 Communication between ESP32-S3 and Tuya module
Posted by: admin - 09-14-2024, 12:00 AM - Forum: KC868-A32/A32 Pro - No Replies

Code:
/*
* Copyright (c) 2024 KinCony IoT (https://www.kincony.com)
*
* This Arduino program implements communication between ESP32-S3 and the Tuya module
* via UART (serial communication). It listens for specific packets from the Tuya module
* and responds according to predefined commands. Additionally, it uses an LED connected
* to GPIO45 to indicate the network status based on the Tuya module's response, and a button
* on GPIO21 to initiate a manual quick configuration (EZ mode).
*
* Functionality:
* 1. The ESP32-S3 communicates with the Tuya module via serial (UART).
* 2. The program listens for specific commands from the Tuya module, such as heartbeat, product info requests, work mode requests, and network status.
* 3. When the ESP32-S3 receives a heartbeat packet (0x55 0xAA 00 00 00 00 0xFF), it responds with a heartbeat response (0x55 0xAA 03 00 00 01 00 03).
* 4. When the ESP32-S3 receives a network status update, it controls an LED connected to GPIO45 to indicate the current state:
*    - Fast blink (every 250ms) indicates the device is in quick configuration mode (EZ mode).
*    - Slow blink (every 1500ms) indicates the device is in hotspot configuration mode (AP mode).
*    - Solid ON indicates the device is successfully connected to the cloud.
* 5. A button connected to GPIO21 is used to send a command to the Tuya module to start the quick configuration mode.
*
* Pin definitions:
* - TXD (ESP32 to Tuya module): GPIO15
* - RXD (Tuya module to ESP32): GPIO16
* - LED for network status indication: GPIO45
* - Button for starting quick configuration: GPIO21
*/

#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 with the Tuya module
#define TXD_PIN 15
#define RXD_PIN 16

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

// Define the GPIO for LED and Button
#define LED_PIN 45
#define BUTTON_PIN 21

// Define LED flash intervals (in milliseconds) for different states
#define FAST_FLASH_INTERVAL 250      // Fast blink for EZ mode
#define SLOW_FLASH_INTERVAL 1500     // Slow blink for AP mode
#define LONG_LIGHT_INTERVAL 10000    // Long light for cloud connection (simulating always ON)

// Variables to control LED state
bool ledState = LOW;                 // Tracks the current state of the LED
unsigned long previousMillis = 0;    // Used to track time for blinking the LED
unsigned long flashInterval = LONG_LIGHT_INTERVAL;  // Default interval (long ON)

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

// Heartbeat response: Tuya expects this response after sending a heartbeat
uint8_t heartBeatResponse[] = {0x55, 0xAA, 0x03, 0x00, 0x00, 0x01, 0x00, 0x03};

// Product info response with details (e.g., product ID, firmware version, 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: Sent when the Tuya module asks for the current work mode
uint8_t workModeResponse[] = {0x55, 0xAA, 0x03, 0x02, 0x00, 0x00, 0x04};

// Network status response: Sent when the Tuya module asks for the network status
uint8_t netStatusResponse[] = {0x55, 0xAA, 0x03, 0x03, 0x00, 0x00, 0x05};

// Subsequent heartbeat response: Sent after the first heartbeat response
uint8_t secondHeartBeatResponse[] = {0x55, 0xAA, 0x03, 0x00, 0x00, 0x01, 0x01, 0x04};

// Command to start quick configuration (EZ mode) when the button is pressed
uint8_t quickConfigCommand[] = {0x55, 0xAA, 0x03, 0x05, 0x00, 0x01, 0x00, 0x08};

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);

  // Initialize the GPIO for LED and button
  pinMode(LED_PIN, OUTPUT);       // Set the LED pin as output
  pinMode(BUTTON_PIN, INPUT_PULLUP); // Use internal pull-up resistor for the button

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

void loop() {
  // Handle LED blinking based on the current flash interval
  unsigned long currentMillis = millis();  // Get the current time
  if (currentMillis - previousMillis >= flashInterval) {
    previousMillis = currentMillis;        // Update the time tracker
    ledState = !ledState;                  // Toggle the LED state
    digitalWrite(LED_PIN, ledState);       // Update the LED state
  }

  // Check if the button is pressed (active low)
  if (digitalRead(BUTTON_PIN) == LOW) {
    Serial.println("Button pressed, sending quick config command...");
    sendPacket(quickConfigCommand, sizeof(quickConfigCommand));  // Send quick config command
    delay(500);  // Debounce delay to avoid multiple triggers
  }

  // Check if there is any data 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 module

    // 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) {
        delay(50); // Wait briefly to receive the remaining bytes
        while (tuyaSerial.available()) {
          incomingPacket[bytesRead++] = tuyaSerial.read(); // Continue reading remaining bytes
          if (bytesRead >= 7) break;
        }
      }

      // If the packet is still incomplete, discard it
      if (bytesRead < 7) return;

      // Process the received packet
      processTuyaPacket(incomingPacket, 7);
    } else {
      // If the header is invalid, discard the packet
      tuyaSerial.flush(); // Clear the serial buffer
    }
  }

  // Small delay to prevent CPU overload
  delay(100);
}

// Function to process the received packet from Tuya module and send appropriate responses
void processTuyaPacket(uint8_t* packet, size_t size) {
  // Ensure the packet size is correct and the header is valid
  if (size == 7 && packet[0] == 0x55 && packet[1] == 0xAA) {
    // Check the command byte (packet[2]) to determine the type of request
    switch (packet[2]) {
      case 0x00:
        // Heartbeat request
        if (packet[3] == 0x00 && packet[4] == 0x00 && packet[5] == 0x00 && packet[6] == 0xFF) {
          Serial.println("Heartbeat received.");
          sendPacket(heartBeatResponse, sizeof(heartBeatResponse));  // Send heartbeat response
        }
        break;
      case 0x01:
        // Product info request
        Serial.println("Product info request received.");
        sendPacket(productInfoResponse, sizeof(productInfoResponse));  // Send product info response
        break;
      case 0x02:
        // Work mode request
        Serial.println("Work mode request received.");
        sendPacket(workModeResponse, sizeof(workModeResponse));  // Send work mode response
        break;
      case 0x03:
        // Network status request
        Serial.println("Network status request received.");
        sendPacket(netStatusResponse, sizeof(netStatusResponse));  // Send network status response

        // Change LED flash pattern based on network status (packet[6])
        if (packet[6] == 0x00) {
          flashInterval = FAST_FLASH_INTERVAL;  // Set LED to fast blink for EZ mode
        } else if (packet[6] == 0x01) {
          flashInterval = SLOW_FLASH_INTERVAL;  // Set LED to slow blink for AP mode
        } else if (packet[6] == 0x04) {
          flashInterval = LONG_LIGHT_INTERVAL;  // Set LED to long ON for cloud connection
          digitalWrite(LED_PIN, HIGH);          // Ensure LED is ON
        }
        break;
      default:
        Serial.println("Error: Unhandled command received.");
        break;
    }
  }
}

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

  // Debug: Print the sent packet for logging purposes
  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   7-tuya-wifi-config.zip (Size: 2.89 KB / Downloads: 43)
BIN file (you can use esp32 download tool download to ESP32-S3 with address 0x0 then directly to use) download: 
.zip   7-tuya-wifi-config.ino.merged.zip (Size: 182.85 KB / Downloads: 43)

Print this item

  [arduino code examples for A32 Pro]-06 Ethernet W5500 chip work with TCP Server mode
Posted by: admin - 09-13-2024, 11:55 PM - Forum: KC868-A32/A32 Pro - 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: GPIO44
* - MISO: GPIO40
* - CS: GPIO39
* - RST: GPIO43
* - INT: GPIO41
*
* 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 with the new GPIO assignments
#define W5500_CS_PIN  39
#define W5500_RST_PIN 43
#define W5500_INT_PIN 41
#define W5500_CLK_PIN 42
#define W5500_MOSI_PIN 44
#define W5500_MISO_PIN 40

// 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   6-Ethernet-W5500.zip (Size: 1.25 KB / Downloads: 50)
BIN file (you can use esp32 download tool download to ESP32-S3 with address 0x0 then directly to use) download: 
.zip   6-Ethernet-W5500.ino.merged.zip (Size: 186.82 KB / Downloads: 48)

Print this item

  [arduino code examples for A32 Pro]-05 Read free GPIO state
Posted by: admin - 09-13-2024, 11:51 PM - Forum: KC868-A32/A32 Pro - 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 1
* - GPIO 2
* - GPIO 38
* - GPIO 0
* - GPIO 21
*
* Hardware Requirements:
* - Connect the pins to appropriate devices or pull them to HIGH/LOW for testing
*/

#define GPIO_PIN_1  1
#define GPIO_PIN_2  2
#define GPIO_PIN_38 38
#define GPIO_PIN_0  0
#define GPIO_PIN_21 21

// Store the previous state of the GPIO pins
bool prevState[5] = {false, false, false, false, false}; // Adjusted to 5 elements

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_1, INPUT);
  pinMode(GPIO_PIN_2, INPUT);
  pinMode(GPIO_PIN_38, INPUT);
  pinMode(GPIO_PIN_0, INPUT);
  pinMode(GPIO_PIN_21, INPUT);

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

void loop() {
  // Read the current state of each GPIO pin
  bool currentState[5];
  currentState[0] = digitalRead(GPIO_PIN_1);
  currentState[1] = digitalRead(GPIO_PIN_2);
  currentState[2] = digitalRead(GPIO_PIN_38);
  currentState[3] = digitalRead(GPIO_PIN_0);
  currentState[4] = digitalRead(GPIO_PIN_21);

  // Check for changes in GPIO pin states
  for (int i = 0; i < 5; 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_1 :
                   i == 1 ? GPIO_PIN_2 :
                   i == 2 ? GPIO_PIN_38 :
                   i == 3 ? GPIO_PIN_0 : GPIO_PIN_21);
      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   5-free-gpio-state.zip (Size: 1,011 bytes / Downloads: 42)
BIN file (you can use esp32 download tool download to ESP32-S3 with address 0x0 then directly to use) download:
.zip   5-free-gpio-state.ino.merged.zip (Size: 177.4 KB / Downloads: 34)

Print this item

  [arduino code examples for A32 Pro]-04 RS485 communication test
Posted by: admin - 09-13-2024, 11:49 PM - Forum: KC868-A32/A32 Pro - No Replies

Code:
/*
* Made by KinCony IoT: https://www.kincony.com
*
* RS485 Communication Test with Echo
*
* This program tests RS485 communication using ESP32-S3.
* It sends a message over RS485, reads incoming messages, and echoes them back.
* The TXD pin is defined as GPIO 9 and RXD pin is defined as GPIO 8.
*/

#include <HardwareSerial.h>

// Define RS485 pins
#define RS485_RXD 8
#define RS485_TXD 9

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

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

  // Initialize RS485 Serial communication
  rs485Serial.begin(9600, SERIAL_8N1, RS485_RXD, RS485_TXD);
 
  Serial.println("RS485 Test Start");
}

void loop() {
  // Send a test message periodically
// rs485Serial.println("Hello from KinCony A32 Pro!");

  // Wait for a short period
  delay(100);

  // Check if data is available to read from RS485
  if (rs485Serial.available()) {
    String receivedMessage = "";
    while (rs485Serial.available()) {
      char c = rs485Serial.read();
      receivedMessage += c;
    }
   
    // Print the received message to the serial monitor
    Serial.print("Received: ");
    Serial.println(receivedMessage);
   
    // Echo the received message back over RS485
    rs485Serial.print("Echo: ");
    rs485Serial.println(receivedMessage);
  }

  // Wait before sending the next message
  delay(2000);
}
arduino ino file download: 
.zip   4-RS485-Test.zip (Size: 823 bytes / Downloads: 43)
BIN file (you can use esp32 download tool download to ESP32-S3 with address 0x0 then directly to use) download: 
.zip   4-RS485-Test.ino.merged.zip (Size: 182.27 KB / Downloads: 35)

Print this item