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

Username
  

Password
  





Search Forums

(Advanced Search)

Forum Statistics
» Members: 6,635
» Latest member: brevno
» Forum threads: 2,947
» Forum posts: 15,637

Full Statistics

Online Users
There are currently 18 online users.
» 0 Member(s) | 6 Guest(s)
Amazonbot, Bytespider, Google, PetalBot, bot

Latest Threads
How to uses sensors with ...
Forum: "KCS" v3 firmware
Last Post: twostar
30 minutes ago
» Replies: 12
» Views: 52
M30 configure yaml for ES...
Forum: KC868-M16 / M1 / MB / M30
Last Post: davidemarino33@gmail.com
32 minutes ago
» Replies: 19
» Views: 2,070
all relay controllers sel...
Forum: Schematic and diagram
Last Post: admin
42 minutes ago
» Replies: 5
» Views: 10
Driver recommendation for...
Forum: DIY Project
Last Post: admin
44 minutes ago
» Replies: 5
» Views: 16
A16v3 low voltage inputs
Forum: KC868-A16
Last Post: jaydubnz
3 hours ago
» Replies: 6
» Views: 34
KC868-A16 - Maximum outpu...
Forum: KC868-A16
Last Post: admin
6 hours ago
» Replies: 1
» Views: 5
"KCS" v3.9.1 firmware BIN...
Forum: "KCS" v3 firmware
Last Post: admin
7 hours ago
» Replies: 0
» Views: 16
MB Specs - 8A per channel...
Forum: KC868-M16 / M1 / MB / M30
Last Post: admin
Today, 12:12 AM
» Replies: 7
» Views: 26
KC868-A4S and meanwell EL...
Forum: KC868-A series and Uair Smart Controller
Last Post: admin
Today, 12:09 AM
» Replies: 1
» Views: 5
KC868-A6 ESP32 I/O pin de...
Forum: KC868-A6
Last Post: admin
Yesterday, 10:19 PM
» Replies: 7
» Views: 8,845

  KC868-A6 tasmota digital/analog input integration to Loxone
Posted by: sigituning - 01-15-2025, 02:10 PM - Forum: KinCony integrate with Loxone home automation - Replies (3)

Hello,

can you provide info how to integrate http commands for loxone to read digital and analog inputs via tasmota firmware?
Or how we can read DI/AI inputs from this board in the loxone?
Can we use e.g. UDP commands somehow in the tasmota and loxone?

Thanks

Print this item

  KC868-A6 with waveshare LAN8720
Posted by: llast - 01-15-2025, 10:17 AM - Forum: KC868-A6 - Replies (3)

Hello,

I am trying to setup KC868-A6 with LAN using waveshare lan8720 controller - I used the following configuration:

```ethernet:
  type: LAN8720
  mdc_pin: GPIO23
  mdio_pin: GPIO18
  phy_addr: 0
```
but I keep on getting the folowing error on esphome:
[11:11:41][C][ethernet:041]: Setting up Ethernet...
[11:11:41][V][esp-idf:000]: E (1589) esp.emac: emac_esp32_init(371): reset timeout
[11:11:41]
[11:11:41][V][esp-idf:000]: E (1589) esp_eth: esp_eth_driver_install(228): init mac failed

would you be able to support this configuration?

Print this item

  Connect LD2420 Presence detection sensor to A8 controller.
Posted by: vijjav - 01-15-2025, 07:03 AM - Forum: KC868-A8 - Replies (5)

Hi,

I want to connect my LD2420 presence detection sensor to A8 board. Please share me the pinout details.

LD2420 has RX, OT1, GND and 3v Pins

LD2420 24Ghz mmWave Radar Sensor — ESPHome

Print this item

  [arduino code examples for A16v3]-12 digital INPUT trigger OUTPUT directly
Posted by: admin - 01-15-2025, 06:14 AM - Forum: KC868-A16v3 - No Replies

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

// Define I2C pins
#define SDA 9           // SDA pin
#define SCL 10           // SCL pin
TwoWire I2Cone = TwoWire(0);

PCF8574 pcf8574_I1(&I2Cone, 0x21, SDA, SCL);
PCF8574 pcf8574_I2(&I2Cone, 0x22, SDA, SCL);

PCF8574 pcf8574_R1(&I2Cone, 0x24, SDA, SCL);
PCF8574 pcf8574_R2(&I2Cone, 0x25, SDA, SCL);

void setup() {
 
  // Initialize serial communication
  Serial.begin(115200);
 
  // Initialize input and relay modules
  // Set pinMode to OUTPUT
pcf8574_R1.pinMode(P0, OUTPUT);
pcf8574_R1.pinMode(P1, OUTPUT);
pcf8574_R1.pinMode(P2, OUTPUT);
pcf8574_R1.pinMode(P3, OUTPUT);
pcf8574_R1.pinMode(P4, OUTPUT);
pcf8574_R1.pinMode(P5, OUTPUT);
pcf8574_R1.pinMode(P6, OUTPUT);
pcf8574_R1.pinMode(P7, OUTPUT);

pcf8574_R2.pinMode(P0, OUTPUT);
pcf8574_R2.pinMode(P1, OUTPUT);
pcf8574_R2.pinMode(P2, OUTPUT);
pcf8574_R2.pinMode(P3, OUTPUT);
pcf8574_R2.pinMode(P4, OUTPUT);
pcf8574_R2.pinMode(P5, OUTPUT);
pcf8574_R2.pinMode(P6, OUTPUT);
pcf8574_R2.pinMode(P7, OUTPUT);

pcf8574_I1.pinMode(P0, INPUT);
pcf8574_I1.pinMode(P1, INPUT);
pcf8574_I1.pinMode(P2, INPUT);
pcf8574_I1.pinMode(P3, INPUT);
pcf8574_I1.pinMode(P4, INPUT);
pcf8574_I1.pinMode(P5, INPUT);
pcf8574_I1.pinMode(P6, INPUT);
pcf8574_I1.pinMode(P7, INPUT);

pcf8574_I2.pinMode(P0, INPUT);
pcf8574_I2.pinMode(P1, INPUT);
pcf8574_I2.pinMode(P2, INPUT);
pcf8574_I2.pinMode(P3, INPUT);
pcf8574_I2.pinMode(P4, INPUT);
pcf8574_I2.pinMode(P5, INPUT);
pcf8574_I2.pinMode(P6, INPUT);
pcf8574_I2.pinMode(P7, INPUT);

  Serial.print("Init pcf8574_R1...");
  if (pcf8574_R1.begin()){
    Serial.println("PCF8574_R1_OK");
  }else{
    Serial.println("PCF8574_R1_KO");
  }

  Serial.print("Init pcf8574_R2...");
  if (pcf8574_R2.begin()){
    Serial.println("PCF8574_R2_OK");
  }else{
    Serial.println("PCF8574_R2_KO");
  }

  Serial.print("Init pcf8574...");
  if (pcf8574_I1.begin()){
    Serial.println("pcf8574_I1_OK");
  }else{
    Serial.println("pcf8574_I1_KO");
  }

  Serial.print("Init pcf8574...");
  if (pcf8574_I2.begin()){
    Serial.println("pcf8574_I2_OK");
  }else{
    Serial.println("pcf8574_I2_KO");
  }

  pcf8574_R1.digitalWrite(P0, HIGH);
  pcf8574_R1.digitalWrite(P1, HIGH);
  pcf8574_R1.digitalWrite(P2, HIGH);
  pcf8574_R1.digitalWrite(P3, HIGH);
  pcf8574_R1.digitalWrite(P4, HIGH);
  pcf8574_R1.digitalWrite(P5, HIGH);
  pcf8574_R1.digitalWrite(P6, HIGH);
  pcf8574_R1.digitalWrite(P7, HIGH);
 

  pcf8574_R2.digitalWrite(P0, HIGH);
  pcf8574_R2.digitalWrite(P1, HIGH);
  pcf8574_R2.digitalWrite(P2, HIGH);
  pcf8574_R2.digitalWrite(P3, HIGH);
  pcf8574_R2.digitalWrite(P4, HIGH);
  pcf8574_R2.digitalWrite(P5, HIGH);
  pcf8574_R2.digitalWrite(P6, HIGH);
  pcf8574_R2.digitalWrite(P7, HIGH);

}

void loop() {
uint8_t val1 = pcf8574_I1.digitalRead(P0);
uint8_t val2 = pcf8574_I1.digitalRead(P1);
uint8_t val3 = pcf8574_I1.digitalRead(P2);
uint8_t val4 = pcf8574_I1.digitalRead(P3);
uint8_t val5 = pcf8574_I1.digitalRead(P4);
uint8_t val6 = pcf8574_I1.digitalRead(P5);
uint8_t val7 = pcf8574_I1.digitalRead(P6);
uint8_t val8 = pcf8574_I1.digitalRead(P7);

uint8_t val9 = pcf8574_I2.digitalRead(P0);
uint8_t val10 = pcf8574_I2.digitalRead(P1);
uint8_t val11 = pcf8574_I2.digitalRead(P2);
uint8_t val12 = pcf8574_I2.digitalRead(P3);
uint8_t val13 = pcf8574_I2.digitalRead(P4);
uint8_t val14 = pcf8574_I2.digitalRead(P5);
uint8_t val15 = pcf8574_I2.digitalRead(P6);
uint8_t val16 = pcf8574_I2.digitalRead(P7);

//------------------------------------
if (val1==LOW) pcf8574_R1.digitalWrite(P0, LOW); else pcf8574_R1.digitalWrite(P0, HIGH);
if (val2==LOW) pcf8574_R1.digitalWrite(P1, LOW); else pcf8574_R1.digitalWrite(P1, HIGH);
if (val3==LOW) pcf8574_R1.digitalWrite(P2, LOW); else pcf8574_R1.digitalWrite(P2, HIGH);
if (val4==LOW) pcf8574_R1.digitalWrite(P3, LOW); else pcf8574_R1.digitalWrite(P3, HIGH);
if (val5==LOW) pcf8574_R1.digitalWrite(P4, LOW); else pcf8574_R1.digitalWrite(P4, HIGH);
if (val6==LOW) pcf8574_R1.digitalWrite(P5, LOW); else pcf8574_R1.digitalWrite(P5, HIGH);
if (val7==LOW) pcf8574_R1.digitalWrite(P6, LOW); else pcf8574_R1.digitalWrite(P6, HIGH);
if (val8==LOW) pcf8574_R1.digitalWrite(P7, LOW); else pcf8574_R1.digitalWrite(P7, HIGH);

if (val9==LOW) pcf8574_R2.digitalWrite(P0, LOW); else pcf8574_R2.digitalWrite(P0, HIGH);
if (val10==LOW) pcf8574_R2.digitalWrite(P1, LOW); else pcf8574_R2.digitalWrite(P1, HIGH);
if (val11==LOW) pcf8574_R2.digitalWrite(P2, LOW); else pcf8574_R2.digitalWrite(P2, HIGH);
if (val12==LOW) pcf8574_R2.digitalWrite(P3, LOW); else pcf8574_R2.digitalWrite(P3, HIGH);
if (val13==LOW) pcf8574_R2.digitalWrite(P4, LOW); else pcf8574_R2.digitalWrite(P4, HIGH);
if (val14==LOW) pcf8574_R2.digitalWrite(P5, LOW); else pcf8574_R2.digitalWrite(P5, HIGH);
if (val15==LOW) pcf8574_R2.digitalWrite(P6, LOW); else pcf8574_R2.digitalWrite(P6, HIGH);
if (val16==LOW) pcf8574_R2.digitalWrite(P7, LOW); else pcf8574_R2.digitalWrite(P7, HIGH);

  // Delay for 500 milliseconds
  delay(50);
}
arduino ino file download:
.zip   12-input-trigger-output.zip (Size: 1 KB / Downloads: 63)
BIN file (you can use esp32 download tool download to ESP32-S3 with address 0x0 then directly to use) download:
.zip   12-input-trigger-output.ino.merged.zip (Size: 191.78 KB / Downloads: 57)

Print this item

  [arduino code examples for A16v3]-11 Print TEXT on SSD1306 OLED displayer
Posted by: admin - 01-15-2025, 06:12 AM - Forum: KC868-A16v3 - 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 IO9
*
* 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 = IO9)
U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0,  10, 9, 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   11-oled-ssd1306.zip (Size: 1.11 KB / Downloads: 62)
BIN file (you can use esp32 download tool download to ESP32-S3 with address 0x0 then directly to use) download:
.zip   11-oled-ssd1306.ino.merged.zip (Size: 201.23 KB / Downloads: 58)

Print this item

  [arduino code examples for A16v3]-10 RF433MHz sender
Posted by: admin - 01-15-2025, 06:10 AM - Forum: KC868-A16v3 - No Replies

Code:
/*
  Example for different sending methods
 
  https://github.com/sui77/rc-switch/
 
*/

#include <RCSwitch.h>

RCSwitch mySwitch = RCSwitch();

void setup() {

  Serial.begin(9600);
 
  // Transmitter is connected to Arduino Pin #10 
  mySwitch.enableTransmit(18);
 
  // Optional set protocol (default is 1, will work for most outlets)
  // mySwitch.setProtocol(2);

  // Optional set pulse length.
  // mySwitch.setPulseLength(320);
 
  // Optional set number of transmission repetitions.
  // mySwitch.setRepeatTransmit(15);
 
}

void loop() {

  /* See Example: TypeA_WithDIPSwitches */
  mySwitch.switchOn("11111", "00010");
  delay(1000);
  mySwitch.switchOff("11111", "00010");
  delay(1000);

  /* Same switch as above, but using decimal code */
  mySwitch.send(5393, 24);
  delay(1000); 
  mySwitch.send(5396, 24);
  delay(1000); 

  /* Same switch as above, but using binary code */
  mySwitch.send("000000000001010100010001");
  delay(1000); 
  mySwitch.send("000000000001010100010100");
  delay(1000);

  /* Same switch as above, but tri-state code */
  mySwitch.sendTriState("00000FFF0F0F");
  delay(1000); 
  mySwitch.sendTriState("00000FFF0FF0");
  delay(1000);

  //delay(20000);
}
arduino ino file download:
.zip   10-RF433M-sender.zip (Size: 664 bytes / Downloads: 58)
BIN file (you can use esp32 download tool download to ESP32-S3 with address 0x0 then directly to use) download:
.zip   10-RF433M-sender.ino.merged.zip (Size: 181.71 KB / Downloads: 55)

Print this item

  [arduino code examples for A16v3]-09 RF433MHz decode
Posted by: admin - 01-15-2025, 06:08 AM - Forum: KC868-A16v3 - No Replies

Code:
/*
  Simple example for receiving
 
  https://github.com/sui77/rc-switch/
*/

#include <RCSwitch.h>

RCSwitch mySwitch = RCSwitch();

void setup() {
  Serial.begin(9600);
  mySwitch.enableReceive(digitalPinToInterrupt(8));
}

void loop() {
  if (mySwitch.available()) {
   
    Serial.print("Received ");
    Serial.print( mySwitch.getReceivedValue() );
    Serial.print(" / ");
    Serial.print( mySwitch.getReceivedBitlength() );
    Serial.print("bit ");
    Serial.print("Protocol: ");
    Serial.println( mySwitch.getReceivedProtocol() );

    mySwitch.resetAvailable();
  }
}
arduino ino file download:
.zip   9-RF433M-decode.zip (Size: 451 bytes / Downloads: 52)
BIN file (you can use esp32 download tool download to ESP32-S3 with address 0x0 then directly to use) download:
.zip   9-RF433M-decode.ino.merged.zip (Size: 181.08 KB / Downloads: 50)

Print this item

  [arduino code examples for A16v3]-08 Ethernet W5500 chip work with TCP Server mode
Posted by: admin - 01-15-2025, 06:06 AM - Forum: KC868-A16v3 - No Replies

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    // Close the connection when done
    client.stop();
    Serial.println("Client disconnected");
  }
}
arduino ino file download:
.zip   8-Ethernet-W5500.zip (Size: 1.23 KB / Downloads: 61)
BIN file (you can use esp32 download tool download to ESP32-S3 with address 0x0 then directly to use) download:
.zip   8-Ethernet-W5500.ino.merged.zip (Size: 188.94 KB / Downloads: 59)

Print this item

  [arduino code examples for A16v3]-07 how to DS3231 RTC clock
Posted by: admin - 01-15-2025, 06:04 AM - Forum: KC868-A16v3 - No Replies

Code:
/*
* Made by KinCony IoT: https://www.kincony.com
*
* DS3231 RTC with Arduino
*
* This program demonstrates how to use the DS3231 RTC (Real-Time Clock) module with the Arduino.
* It includes functionality to:
* - Initialize the DS3231 RTC module
* - Read the current date and time from the RTC
* - Set the RTC time based on a serial command:Command format: DYYYY-MM-DDTHH:MM:SS
*    Set date and time command example: D2024-09-19T11:50:22
*    print current date and time command: current time
*
*
* Hardware Connections:
* - SDA: GPIO 9
* - SCL: GPIO 10
*/

#include <DS3231.h>
#include <Wire.h>

String serial_cmd_rcv = ""; // Serial port receiver

typedef struct
{
  byte year;    // Last two digits of the year, library adds 2000.
  byte month;
  byte day;
  byte hour;
  byte minute;
  byte second;
} MY_DATE_STR;

MY_DATE_STR my_date_str = {0};

// Define constants for relay control
#define OPEN_RLY_DATA    26
#define OPEN_RLY_MONTH   4
#define CLOSE_RLY_DATA   2
#define CLOSE_RLY_MONTH  5

// Define pin connections
#define SDA_PIN   9
#define SCL_PIN   10

DS3231 rtc; // Create an instance of the DS3231 RTC
bool h12Flag;
bool pmFlag;
static bool bCentury = false;
static bool old_level_high = false;
static bool old_level_low = false;


/**
* @brief Print the current time from the RTC to the Serial Monitor.
*/
static void PrintfCurTime()
{
  Serial.print("Current time is: ");
  int year = rtc.getYear() + 2000;
  Serial.print(year);
  Serial.print("-");

  Serial.print(rtc.getMonth(bCentury), DEC);
  Serial.print("-");

  Serial.print(rtc.getDate(), DEC);
  Serial.print(" ");

  Serial.print(rtc.getHour(h12Flag, pmFlag), DEC);
  Serial.print(":");
  Serial.print(rtc.getMinute(), DEC);
  Serial.print(":");
  Serial.println(rtc.getSecond(), DEC);
}

/**
* @brief Process serial commands to set the RTC time.
* Command format: DYYYY-MM-DDTHH:MM:SS
*/
static void GetSerialCmd()
{
  if (Serial.available() > 0)
  {
    delay(100);
    int num_read = Serial.available();
    while (num_read--)
      serial_cmd_rcv += char(Serial.read());
  }
  else return;

  serial_cmd_rcv.trim();

  if (serial_cmd_rcv == "current time")
  {
    PrintfCurTime();
    serial_cmd_rcv = "";
    return;
  }

  Serial.print("Received length: ");
  Serial.println(serial_cmd_rcv.length());

  int indexof_d = serial_cmd_rcv.indexOf('D');
  int indexof_t = serial_cmd_rcv.indexOf('T');

  Serial.print("D index: ");
  Serial.print(indexof_d);
  Serial.print(" T index: ");
  Serial.println(indexof_t);

  if (serial_cmd_rcv.length() != 20 ||
      serial_cmd_rcv.substring(0, 1) != "D" ||
      serial_cmd_rcv.substring(11, 12) != "T") 
  {
    Serial.println(serial_cmd_rcv);
    serial_cmd_rcv = "";
    return;
  }

  Serial.println("Setting time...");

  my_date_str.year = (byte)serial_cmd_rcv.substring(3, 5).toInt();
  my_date_str.month = (byte)serial_cmd_rcv.substring(6, 8).toInt();
  my_date_str.day = (byte)serial_cmd_rcv.substring(9, 11).toInt();
  my_date_str.hour = (byte)serial_cmd_rcv.substring(12, 14).toInt();
  my_date_str.minute = (byte)serial_cmd_rcv.substring(15, 17).toInt();
  my_date_str.second = (byte)serial_cmd_rcv.substring(18).toInt();

  rtc.setYear(my_date_str.year);
  rtc.setMonth(my_date_str.month);
  rtc.setDate(my_date_str.day);
  rtc.setHour(my_date_str.hour);
  rtc.setMinute(my_date_str.minute);
  rtc.setSecond(my_date_str.second);

  serial_cmd_rcv = "";

  Serial.println("Time set.");
}

void setup() {
  // Initialize the I2C interface
  Wire.begin(SDA_PIN, SCL_PIN, 40000);
 
  // Initialize Serial communication
  Serial.begin(115200);
   
  // Set the RTC to 24-hour mode
  rtc.setClockMode(false); // 24-hour format

  // Print current time to Serial Monitor
  PrintfCurTime();

  // Clear any remaining serial data
  while (Serial.read() >= 0) {}
}

void loop() {
  // Process incoming serial commands
  GetSerialCmd();
  delay(1000); // Delay for 1 second
}
arduino ino file download:
.zip   7-DS3231-RTC.zip (Size: 1.56 KB / Downloads: 60)
BIN file (you can use esp32 download tool download to ESP32-S3 with address 0x0 then directly to use) download:
.zip   7-DS3231-RTC.ino.merged.zip (Size: 191.07 KB / Downloads: 59)

Print this item

  [arduino code examples for A16v3]-06 How to use SD Card
Posted by: admin - 01-15-2025, 06:02 AM - Forum: KC868-A16v3 - No Replies

Code:
/*
* Made by KinCony IoT: https://www.kincony.com
*
* SD Card File Operations
*
* This program demonstrates basic file operations on an SD card using the ESP32.
* It includes functionality to:
* - Initialize and test the SD card
* - Read from, write to, append to, and delete files on the SD card
* - Measure file read and write performance
*
* Hardware Connections:
* - SCK: GPIO 13
* - MISO: GPIO 14
* - MOSI: GPIO 12
* - CS: GPIO 11
*/

#include "FS.h"
#include "SD.h"
#include "SPI.h"

// Pin definitions for SD card
#define SCK  13
#define MISO 14
#define MOSI 12
#define CS   11

/**
* @brief Reads the contents of a file from the SD card and prints it to the serial monitor.
*
* @param fs File system to use (in this case, SD).
* @param path Path of the file to read.
*/
void readFile(fs::FS &fs, const char * path) {
  Serial.printf("Reading file: %s\n", path);

  File file = fs.open(path);
  if (!file) {
    Serial.println("Failed to open file for reading");
    return;
  }

  Serial.print("Read from file: ");
  while (file.available()) {
    Serial.print((char)file.read());
  }
  file.close();
}

/**
* @brief Writes a message to a file on the SD card.
*
* @param fs File system to use (in this case, SD).
* @param path Path of the file to write.
* @param message Message to write to the file.
*/
void writeFile(fs::FS &fs, const char * path, const char * message) {
  Serial.printf("Writing file: %s\n", path);

  File file = fs.open(path, FILE_WRITE);
  if (!file) {
    Serial.println("Failed to open file for writing");
    return;
  }
  if (file.print(message)) {
    Serial.println("File written");
  } else {
    Serial.println("Write failed");
  }
  file.close();
}

/**
* @brief Appends a message to a file on the SD card.
*
* @param fs File system to use (in this case, SD).
* @param path Path of the file to append.
* @param message Message to append to the file.
*/
void appendFile(fs::FS &fs, const char * path, const char * message) {
  Serial.printf("Appending to file: %s\n", path);

  File file = fs.open(path, FILE_APPEND);
  if (!file) {
    Serial.println("Failed to open file for appending");
    return;
  }
  if (file.print(message)) {
    Serial.println("Message appended");
  } else {
    Serial.println("Append failed");
  }
  file.close();
}

/**
* @brief Deletes a file from the SD card.
*
* @param fs File system to use (in this case, SD).
* @param path Path of the file to delete.
*/
void deleteFile(fs::FS &fs, const char * path) {
  Serial.printf("Deleting file: %s\n", path);
  if (fs.remove(path)) {
    Serial.println("File deleted");
  } else {
    Serial.println("Delete failed");
  }
}

/**
* @brief Tests file read and write performance.
*
* @param fs File system to use (in this case, SD).
* @param path Path of the file to test.
*/
void testFileIO(fs::FS &fs, const char * path) {
  File file = fs.open(path);
  static uint8_t buf[512];
  size_t len = 0;
  uint32_t start = millis();
  uint32_t end = start;

  if (file) {
    len = file.size();
    size_t flen = len;
    start = millis();
    while (len) {
      size_t toRead = len;
      if (toRead > 512) {
        toRead = 512;
      }
      file.read(buf, toRead);
      len -= toRead;
    }
    end = millis() - start;
    Serial.printf("%u bytes read for %u ms\n", flen, end);
    file.close();
  } else {
    Serial.println("Failed to open file for reading");
  }

  file = fs.open(path, FILE_WRITE);
  if (!file) {
    Serial.println("Failed to open file for writing");
    return;
  }

  size_t i;
  start = millis();
  for (i = 0; i < 2048; i++) {
    file.write(buf, 512);
  }
  end = millis() - start;
  Serial.printf("%u bytes written for %u ms\n", 2048 * 512, end);
  file.close();
}

void setup() {
  // Initialize serial communication
  Serial.begin(115200);
 
  // Initialize SPI and SD card
  SPIClass spi = SPIClass(HSPI);
  spi.begin(SCK, MISO, MOSI, CS);

  if (!SD.begin(CS, spi, 80000000)) {
    Serial.println("Card Mount Failed");
    return;
  }

  uint8_t cardType = SD.cardType();

  if (cardType == CARD_NONE) {
    Serial.println("No SD card attached");
    return;
  }

  Serial.print("SD Card Type: ");
  if (cardType == CARD_MMC) {
    Serial.println("MMC");
  } else if (cardType == CARD_SD) {
    Serial.println("SDSC");
  } else if (cardType == CARD_SDHC) {
    Serial.println("SDHC");
  } else {
    Serial.println("UNKNOWN");
  }

  uint64_t cardSize = SD.cardSize() / (1024 * 1024);
  Serial.printf("SD Card Size: %lluMB\n", cardSize);
  delay(2000);

  // Perform file operations
  deleteFile(SD, "/hello.txt");
  writeFile(SD, "/hello.txt", "Hello ");
  appendFile(SD, "/hello.txt", "World!\n");
  readFile(SD, "/hello.txt");
  testFileIO(SD, "/test.txt");
  Serial.printf("Total space: %lluMB\n", SD.totalBytes() / (1024 * 1024));
  Serial.printf("Used space: %lluMB\n", SD.usedBytes() / (1024 * 1024));
}

void loop() {
  // No operation in loop
}
arduino ino file download:
.zip   6-SD.zip (Size: 1.53 KB / Downloads: 44)
BIN file (you can use esp32 download tool download to ESP32-S3 with address 0x0 then directly to use) download:
.zip   6-SD.ino.merged.zip (Size: 221.26 KB / Downloads: 46)

Print this item