Skip to content

Instantly share code, notes, and snippets.

@fxprime
Created September 21, 2023 14:52
Show Gist options
  • Save fxprime/fef6d2943c872300ec4c0c44ae7e1458 to your computer and use it in GitHub Desktop.
Save fxprime/fef6d2943c872300ec4c0c44ae7e1458 to your computer and use it in GitHub Desktop.
/* ------------------ Relay Reading/Writing code by ModuleMore.com ----------------- */
/* ------------------------------- วิธีการต่อ ------------------------------- */
/**
* @file main.cpp
* @author ModuleMore.com ([email protected])
* @brief รับส่งค่า relay เวอร์ชั่น RS485 โดยใช้ RS485 to TTL (รุ่นมี tx rx และ gnd ของ rs485)
* สื่อสารผ่าน Serial1 (ที่เรา define ขาไว้เป็น RX = 13, TX = 15) ด้วย Library ModbusMaster
*
* Wiring
* ______
*
* ESP32 ---> RS485 -> Relay Modbus 1 + -> Relay Modbus 2 ---> Power supply
* + + VCC
* - - GND
* 3.3V VCC
* GND GND
* 13 RX
* 15 TX
* A A+
* B B-
* A A+
* B B-
* GND(ภาษาจีน) -(หรือไม่ต่อก็ได้)
* @version 0.1
* @date 2022-06-20
*
* @copyright Copyright (c) 2022
*
*/
#include <Arduino.h>
#include <ModbusMaster.h>
// instantiate ModbusMaster object
ModbusMaster node1;
ModbusMaster node2;
void setup()
{
//สื่อสารออก Terminal ด้วย baudrate 115200
Serial.begin(115200);
//ตั้งค่า Hardware serial เป็นแบบ 8bit no parity 1 stop bit, ขา RX=13, TX=15
Serial1.begin(9600, SERIAL_8N1, 13,15);
// เริ่มสื่อสารกับ modbus slaveid = 1 ด้วย HardwareSerial = Serial1
node1.begin(1, Serial1);
node2.begin(2, Serial1);
}
void loop()
{
delay(1000);
node1.writeSingleCoil(0,1);
node2.writeSingleCoil(0,1);
delay(1000);
node1.writeSingleCoil(0,0);
node2.writeSingleCoil(0,0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment