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

Username
  

Password
  





Search Forums

(Advanced Search)

Forum Statistics
» Members: 8,406
» Latest member: tailoredargentinainfo
» Forum threads: 3,660
» Forum posts: 18,959

Full Statistics

Online Users
There are currently 16 online users.
» 1 Member(s) | 8 Guest(s)
Amazonbot, PetalBot, bot, thomasbester04

Latest Threads
N30 Energy entry not work...
Forum: N30
Last Post: admin
4 hours ago
» Replies: 30
» Views: 540
flash Kincony software to...
Forum: DIY Project
Last Post: admin
4 hours ago
» Replies: 11
» Views: 60
KC868-A6 - how to connect...
Forum: KC868-A6
Last Post: admin
6 hours ago
» Replies: 8
» Views: 131
KC868-A16 v1 with KCS v2....
Forum: "KCS" v2 firmware system
Last Post: admin
6 hours ago
» Replies: 7
» Views: 41
Separate +12V to Kincony,...
Forum: T128M
Last Post: admin
6 hours ago
» Replies: 1
» Views: 13
linux command line / bash...
Forum: TA
Last Post: almman
7 hours ago
» Replies: 2
» Views: 24
KC868-A16 ethernet work w...
Forum: KC868-A16
Last Post: admin
Yesterday, 12:53 PM
» Replies: 14
» Views: 15,947
KC868-M16v2 configure yam...
Forum: KC868-M16 / M1 / MB / M30
Last Post: admin
Yesterday, 11:24 AM
» Replies: 145
» Views: 26,549
KC868-HAv2 work with F24 ...
Forum: KC868-HA /HA v2
Last Post: admin
Yesterday, 11:19 AM
» Replies: 9
» Views: 681
how to compile new tasmot...
Forum: KC868-A series and Uair Smart Controller
Last Post: admin
Yesterday, 11:16 AM
» Replies: 5
» Views: 3,834

  [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: 463)
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: 442)

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: 458)
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: 445)

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: 394)
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: 390)

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: 399)
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: 414)

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: 395)
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: 388)

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: 382)
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: 389)

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: 449)
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: 433)

Print this item

  communication with I2C sensor
Posted by: remiasz - 09-13-2024, 01:47 PM - Forum: KC868-A series and Uair Smart Controller - Replies (3)

Does the KCS firmware allow communication with sensors connected to the I2C port? 
How?

Print this item

  Get PWM out, even by sacrificing other functionality
Posted by: tarajas - 09-12-2024, 09:50 PM - Forum: KC868-A4 - Replies (7)

Hi,

I have a 1.2.4 version A4 board and I was wondering if there are some quick and dirty adjustments to be made potentially even with some small amount of soldering to get a PWM output on the board maybe?

Like I could sacrifice the 433MHz related functionality for examle?

Is there maybe such a modification?

Thanks for the help in advance

Print this item

  AIO board has failed
Posted by: telewizard13 - 09-12-2024, 09:06 PM - Forum: KC868-AIO - Replies (8)

My 3 month old KC868-AIO board has failed. It no longer communicates with my Home Assistant over ethernet, doesn't appear on WIFI.  Can't connect via USB and the ESP chip on the board is VERY hot. There is no activity on the ethernet connector. Not sure what failed or why, because it was working perfectly even earlier today. The power supply is fine and measures 12.45 volts and the power LEDs are lit, but it appears to be brain dead. I powered it down and disconnected all the I/O plugs and there was no change. 
-Telewizard 13 



Attached Files Image(s)
   
Print this item