Created
September 20, 2023 04:05
-
-
Save fxprime/c77cc44a2d8373d750b215f04d096818 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* ------------------ 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 ---> Power supply | |
* + VCC | |
* - GND | |
* 3.3V VCC | |
* GND GND | |
* 13 RX | |
* 15 TX | |
* 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 node; | |
void setAddress(uint16_t address) | |
{ | |
node.begin(0, Serial1); | |
node.setTransmitBuffer(0, address); | |
node.writeMultipleRegisters(0, 1); | |
delay(100); | |
node.begin(address, Serial1); | |
} | |
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 | |
node.begin(1, Serial1); | |
setAddress(255); | |
while(1) { | |
Serial.println("Reboot and remove this while loop to begin with new id"); | |
delay(1000); | |
} | |
} | |
void loop() | |
{ | |
delay(1000); | |
node.writeSingleCoil(0,1); | |
delay(1000); | |
node.writeSingleCoil(0,0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment