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

Username
  

Password
  





Search Forums

(Advanced Search)

Forum Statistics
» Members: 9,523
» Latest member: jemskim0107
» Forum threads: 4,182
» Forum posts: 20,780

Full Statistics

Online Users
There are currently 26 online users.
» 0 Member(s) | 14 Guest(s)
AhrefsBot, Amazonbot, Crawl, Google, PetalBot, bot

Latest Threads
KC868-A16 UART pins
Forum: KC868-A series and Uair Smart Controller
Last Post: meowdokugame
Yesterday, 07:40 AM
» Replies: 2
» Views: 569
KC868-A32 Malfunction Aft...
Forum: KC868-A32/A32 Pro
Last Post: admin
Yesterday, 06:19 AM
» Replies: 3
» Views: 48
KinCony F8 + ESPHome: Una...
Forum: F8
Last Post: admin
08-02-2026, 11:00 PM
» Replies: 2
» Views: 17
F8 ESPHome yaml for home ...
Forum: F8
Last Post: admin
08-02-2026, 11:00 PM
» Replies: 0
» Views: 7
Schema A16V3 for Research...
Forum: KC868-A16v3
Last Post: hamzachaouri
08-02-2026, 10:34 PM
» Replies: 2
» Views: 40
B8 ADS1115 current inputs
Forum: B8
Last Post: admin
07-29-2026, 09:47 PM
» Replies: 6
» Views: 65
KINCONY IN KSA
Forum: DIY Project
Last Post: admin
07-29-2026, 10:43 AM
» Replies: 14
» Views: 1,815
A32 Pro ESPHome yaml for ...
Forum: KC868-A32/A32 Pro
Last Post: admin
07-27-2026, 06:25 AM
» Replies: 1
» Views: 1,210
E8v3 E16v3 E24v3 relay mo...
Forum: Extender module
Last Post: admin
07-27-2026, 01:00 AM
» Replies: 0
» Views: 59
KC868-H32B V5.08 firmware
Forum: News
Last Post: admin
07-25-2026, 11:53 PM
» Replies: 23
» Views: 7,872

  [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 - Replies (1)

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: 675)
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: 680)

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: 666)
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: 732)

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: 669)
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: 698)

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: 743)
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: 709)

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: 717)
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: 722)

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: 646)
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: 643)

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: 666)
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: 682)

Print this item

  [arduino code examples for A32 Pro]-03 Read analog input ports value
Posted by: admin - 09-13-2024, 11:46 PM - Forum: KC868-A32/A32 Pro - No Replies

Code:
/*
* Made by KinCony IoT: https://www.kincony.com
*
* Description:
* This Arduino program reads analog values from four analog input pins (A1, A2, A3, A4)
* and prints the values to the Serial Monitor. The analog inputs are defined with specific
* GPIO pins and the program reads the voltage levels from these pins every 2 seconds.
*
* Pin Definitions:
* - A1: GPIO 7
* - A2: GPIO 6
* - A3: GPIO 5
* - A4: GPIO 4
*/

#define ANALOG_A1   7   // Define GPIO pin for analog input A1
#define ANALOG_A2   6   // Define GPIO pin for analog input A2
#define ANALOG_A3   5   // Define GPIO pin for analog input A3
#define ANALOG_A4   4   // Define GPIO pin for analog input A4

void setup()
{
    Serial.begin(115200); // Initialize serial communication at 115200 baud rate
    delay(500); // Short delay to allow serial communication to start

    pinMode(ANALOG_A1, INPUT); // Set GPIO 5 as an input for analog signal A1
    pinMode(ANALOG_A2, INPUT); // Set GPIO 7 as an input for analog signal A2
    pinMode(ANALOG_A3, INPUT); // Set GPIO 6 as an input for analog signal A3
    pinMode(ANALOG_A4, INPUT); // Set GPIO 4 as an input for analog signal A4
}

void loop()
{
    // Read and print analog values from the defined pins
    Serial.print("A1=");
    Serial.println(analogRead(ANALOG_A1)); // Read and print the value from A1
    Serial.print("A2=");
    Serial.println(analogRead(ANALOG_A2)); // Read and print the value from A2
    Serial.print("A3=");
    Serial.println(analogRead(ANALOG_A3)); // Read and print the value from A3
    Serial.print("A4=");
    Serial.println(analogRead(ANALOG_A4)); // Read and print the value from A4
   
    delay(2000); // Wait for 2 seconds before the next reading
}
arduino ino file download: 
.zip   3-analog-input.zip (Size: 768 bytes / Downloads: 678)
BIN file (you can use esp32 download tool download to ESP32-S3 with address 0x0 then directly to use) download: 
.zip   3-analog-input.ino.merged.zip (Size: 182.47 KB / Downloads: 654)

Print this item

  [arduino code examples for A32 Pro]-02 Read digital input ports state
Posted by: admin - 09-13-2024, 11:44 PM - Forum: KC868-A32/A32 Pro - No Replies

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

  Program functionality:
  This program uses an ESP32-S3 to read inputs from two XL9535 I/O expander chips
  (with I2C addresses 0x24 and 0x25) and one PCF8574 I/O expander (with I2C address 0x23) via I2C.
  The XL9535 chips handle 1-32 channels, while the PCF8574 chip handles channels 33-40.
  The program reads the state of all these inputs and prints them in binary format to the serial monitor.

  Key points:
  - The I2C bus is initialized on GPIO pins 11 (SDA) and 10 (SCL) with a frequency of 40kHz.
  - The XL9535 chips read the state of input pins for channels 1-32.
  - The PCF8574 chip reads the state of input pins for channels 33-40.
  - The state of the inputs is printed to the serial monitor in binary format every second.
*/

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

// Initialize the PCA9555 objects for XL9535 chips (1-32 channels)
PCA9555 ioex1;  // For channels 1-16 (I2C address 0x24)
PCA9555 ioex2;  // For channels 17-32 (I2C address 0x25)

// Define the I2C address for the PCF8574 chip (for channels 33-40)
const int pcf8574_address = 0x23;

void setup() {
    // Start serial communication for debugging
    Serial.begin(115200);
    delay(2000);  // Wait for 2 seconds to ensure everything is ready

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

    // Attach the first PCA9555 device (for channels 1-16)
    ioex1.attach(Wire, 0x24);
    ioex1.polarity(PCA95x5::Polarity::ORIGINAL_ALL);
    ioex1.direction(PCA95x5::Direction::IN_ALL);  // Set all pins as inputs

    // Attach the second PCA9555 device (for channels 17-32)
    ioex2.attach(Wire, 0x25);
    ioex2.polarity(PCA95x5::Polarity::ORIGINAL_ALL);
    ioex2.direction(PCA95x5::Direction::IN_ALL);  // Set all pins as inputs
}

void loop() {
    // Read and print the state of inputs from the first PCA9555 (channels 1-16)
    Serial.print("1-16 input states: ");
    Serial.println(ioex1.read(), BIN);

    // Read and print the state of inputs from the second PCA9555 (channels 17-32)
    Serial.print("17-32 input states: ");
    Serial.println(ioex2.read(), BIN);

    // Read and print the state of inputs from the PCF8574 (channels 33-40)
    Serial.print("33-40 input states: ");
    byte pcf8574_state = readPCF8574();
    Serial.println(pcf8574_state, BIN);

    delay(1000);  // Wait for 1 second before reading again
}

// Function to read the state of the PCF8574 (channels 33-40)
byte readPCF8574() {
    Wire.requestFrom(pcf8574_address, 1);  // Request 1 byte from PCF8574

    if (Wire.available()) {
        return Wire.read();  // Return the byte representing the state of inputs
    } else {
        return 0xFF;  // Return 0xFF if no data is available (all inputs are high)
    }
}
arduino ino file download: 
.zip   2-digital-input.zip (Size: 1.19 KB / Downloads: 655)
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: 187.5 KB / Downloads: 667)

Print this item

  [arduino code examples for A32 Pro]-01 Turn ON/OFF relay
Posted by: admin - 09-13-2024, 11:42 PM - Forum: KC868-A32/A32 Pro - Replies (5)

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

  Program functionality:
  This program uses ESP32-S3 to control two PCA9555 I/O expander chips via I2C,
  controlling a total of 32 relays. The first PCA9555 chip has an I2C address of 0x21
  and controls relays 1-16, while the second PCA9555 chip has an I2C address of 0x22
  and controls relays 17-32.

  The relays are turned ON (set to low) and OFF (set to high) in sequence with a small
  delay between each operation. The program reads and prints the current state of the
  relays to the serial monitor after each change.
*/

#include <PCA95x5.h>

// Initialize two PCA9555 objects for controlling 32 relays
PCA9555 ioex1; // 1-16 relays (I2C address 0x21)
PCA9555 ioex2; // 17-32 relays (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 first PCA9555 chip (1-16 relays)
    ioex1.attach(Wire, 0x21);  // Set I2C address to 0x21
    ioex1.polarity(PCA95x5::Polarity::ORIGINAL_ALL);  // Set polarity to normal (no inversion)
    ioex1.direction(PCA95x5::Direction::OUT_ALL);  // Set all pins as output
    ioex1.write(PCA95x5::Level::H_ALL);  // Initialize all outputs to HIGH (relays OFF)

    // Configure the second PCA9555 chip (17-32 relays)
    ioex2.attach(Wire, 0x22);  // Set I2C address to 0x22
    ioex2.polarity(PCA95x5::Polarity::ORIGINAL_ALL);  // Set polarity to normal (no inversion)
    ioex2.direction(PCA95x5::Direction::OUT_ALL);  // Set all pins as output
    ioex2.write(PCA95x5::Level::H_ALL);  // Initialize all outputs to HIGH (relays OFF)

    delay(50);  // Wait for settings to take effect
}

void loop() {
    // Control the first PCA9555 chip (1-16 relays)
    for (size_t i = 0; i < 16; ++i) {
        Serial.print("set port low (chip 1): ");  // Turning ON the relay
        Serial.println(i);

        ioex1.write((PCA95x5::Port::Port)i, PCA95x5::Level::L);  // Set the relay to LOW (ON)
        Serial.println(ioex1.read(), BIN);  // Print the current state of all relays (in binary)
        delay(50);  // Small delay between each relay activation
    }

    // Control the second PCA9555 chip (17-32 relays)
    for (size_t i = 0; i < 16; ++i) {
        Serial.print("set port low (chip 2): ");  // Turning ON the relay
        Serial.println(i + 16);  // Display relay numbers 17-32

        ioex2.write((PCA95x5::Port::Port)i, PCA95x5::Level::L);  // Set the relay to LOW (ON)
        Serial.println(ioex2.read(), BIN);  // Print the current state of all relays (in binary)
        delay(50);  // Small delay between each relay activation
    }

    // Turn off the first PCA9555 chip (1-16 relays)
    for (size_t i = 0; i < 16; ++i) {
        Serial.print("set port high (chip 1): ");  // Turning OFF the relay
        Serial.println(i);

        ioex1.write((PCA95x5::Port::Port)i, PCA95x5::Level::H);  // Set the relay to HIGH (OFF)
        Serial.println(ioex1.read(), BIN);  // Print the current state of all relays (in binary)
        delay(50);  // Small delay between each relay deactivation
    }

    // Turn off the second PCA9555 chip (17-32 relays)
    for (size_t i = 0; i < 16; ++i) {
        Serial.print("set port high (chip 2): ");  // Turning OFF the relay
        Serial.println(i + 16);  // Display relay numbers 17-32

        ioex2.write((PCA95x5::Port::Port)i, PCA95x5::Level::H);  // Set the relay to HIGH (OFF)
        Serial.println(ioex2.read(), BIN);  // Print the current state of all relays (in binary)
        delay(50);  // Small delay between each relay deactivation
    }
}
arduino ino file download: 
.zip   1-relay.zip (Size: 1.13 KB / Downloads: 736)
BIN file (you can use esp32 download tool download to ESP32-S3 with address 0x0 then directly to use) download: 
.zip   1-relay.ino.merged.zip (Size: 187.64 KB / Downloads: 731)

Print this item