Tutorial Modbus RS485 Arduino : Nurse Call
Pada kesempatan kali ini saya akan menjelaskan mengenai bagaimana cara membuat sebuah alat dengan menggunakan modbus RS485 untuk alat Nurse Call, jadi alat ini berfungsi untuk memanggil perawat agar datang keruangan yang dibutuhkan misalnya ruangan pasien yang membutuhkan bantuan maka alat ini dapat digunakan. untuk lebih jelasnya berikut adalah koding dan komponennya.
a. Skema Alat
b. Master Nurse Call
c. Nurse Call Slave
d. Program Arduino IDE Master Nurse Call
#include <ModbusRtu.h>
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3);
#define slaveID 11
int tombol1 = 10;
int tombol2 = 11;
int tombol3 = 12;
int tombol1x;
int tombol2x;
int tombol3x;
uint16_t nilainya = 0;
unsigned long lastPrint = 0;
// data array for modbus network sharing
uint16_t au16data[4] = {
slaveID, 225, 8888, 9999
};
Modbus slave(slaveID, mySerial, 4); // this is slave @1 and RS-232 or USB-FTDI
void setup() {
Serial.begin(9600);
mySerial.begin( 19200 ); // baud-rate at 19200
slave.start();
delay(10);
pinMode(tombol1,INPUT_PULLUP);
pinMode(tombol2,INPUT_PULLUP);
pinMode(tombol3,INPUT_PULLUP);
}
void loop() {
slave.poll( au16data, 4 );
if (millis() - lastPrint > 200) {
Serial.print(au16data[0]); Serial.print(":");
Serial.print(au16data[1]); Serial.print(":");
Serial.print(au16data[2]); Serial.println();
lastPrint = millis();
}
readSensor(); //for ultrasonic sensor
}
void readSensor() {
tombol1x = digitalRead(tombol1);
tombol2x = digitalRead(tombol2);
tombol3x = digitalRead(tombol3);
if(tombol1x == 0){
nilainya = 1;
}
if(tombol2x == 0){
nilainya = 2;
}
if(tombol3x == 0){
nilainya = 3;
}
au16data[1] = nilainya; //data to be sent to slave device
}
e. Program Arduino IDE Slave Nurse Call
// modbus RS485 read and write register
// V1.1
// 1 master 2 slave,
// 1 slave : 1 input 1 output
#include <Wire.h>
#include <ModbusRtu.h>
#define slaveNumber 5
#define delayCom 15
#define maxQuery 2*2//slaveNumer*2
#include <SoftwareSerial.h>
#include <LiquidCrystal_I2C.h>
SoftwareSerial mySerial(2, 3);
LiquidCrystal_I2C lcd(0x27,16,2);
uint8_t u8state; //!< machine state
uint8_t u8query; //!< pointer to message query
uint16_t dataBus[slaveNumber*3];
uint16_t lastPrint=100;
int slaveID[slaveNumber] = {11,12,13,14,15}; //id = 0,1,2,3,4
/**
* Modbus object declaration
* u8id : node id = 0 for master, = 1..247 for slave
* port : Serial1 port
* u8txenpin : 0 for RS-232 and USB-FTDI
* or any pin number > 1 for RS-485
*/
Modbus master(0,mySerial,4); // ID, seriapNumber, enablePin
/**
* This is an structe which contains a query to an slave device
*/
modbus_t telegram[slaveNumber*2];
unsigned long u32wait;
void init_modBus(){
int num=0;
int addr=0;
////SLAVE 1
// Read 1 data from Slave 11
telegram[num].u8id = slaveID[0]; // slave address
telegram[num].u8fct = 3; // function code (this one is registers read)
telegram[num].u16RegAdd = 0; // start address in slave
telegram[num].u16CoilsNo = 2; // number of elements (coils or registers) to read
telegram[num].au16reg = dataBus; // pointer to a memory array in the Arduino
num+=1;
addr+=2;
// Write 1 data to Slave 11
telegram[num].u8id = slaveID[0]; // slave address
telegram[num].u8fct = 6; // function code (this one is write a multiple register)
telegram[num].u16RegAdd = 2; // start address in slave
telegram[num].u16CoilsNo = 1; // number of elements (coils or registers) to write
telegram[num].au16reg = dataBus+2; // pointer to a memory array in the Arduino
num+=1;
addr+=1;
master.start();
master.setTimeOut( 100 ); // if there is no answer in 100 ms, roll over
u32wait = millis() + 40;
u8state = u8query = 0;
}
void rtuState(){
switch( u8state ) {
case 0:
if (millis() >= u32wait) u8state++; // wait state
break;
case 1:
master.query( telegram[u8query] ); // send query (only once)
u8state++;
u8query++;
if (u8query >= maxQuery)
u8query = 0;
break;
case 2:
master.poll(); // check incoming messages if communication in idle state
if (master.getState() == COM_IDLE) {
u8state = 0;
u32wait = millis() + delayCom; //delay for next state
}
break;
}
}
void printData(){
if (millis() - lastPrint>200){
//print data to validate
Serial.print(dataBus[0]); Serial.print(":");
Serial.print(dataBus[1]); Serial.print(":");
Serial.print(dataBus[2]); Serial.print("\t:\t");
//Serial.print(dataBus[3]); Serial.print(":");
//Serial.print(dataBus[4]); Serial.print(":");
//Serial.print(dataBus[5]);
Serial.println();
}
}
void processData(){
//data to be sent so slave 1 based on slave 2 data
dataBus[2] = dataBus[4]*0.25; //convert 1023 to 255
//data to be sent so slave 2 based on slave 1 data
//dataBus[5] = dataBus[1]*0.25;
if(dataBus[1] == 0){
lcd.setCursor(0, 0);
lcd.print(" ");
}
if(dataBus[1] == 1){
Serial.println("RUANG DOKTER");
lcd.setCursor(0, 0);
lcd.print("RUANG DOKTER ");
}
if(dataBus[1] == 2){
Serial.println("RUANG PASIEN");
lcd.setCursor(0, 0);
lcd.print("RUANG PASIEN ");
}
if(dataBus[1] == 3){
Serial.println("RUANG RAWAT");
lcd.setCursor(0, 0);
lcd.print("RUANG RAWAT ");
}
}
void setup() {
lcd.begin();
lcd.noCursor();
lcd.clear();
Serial.begin (9600); //baud rate of Serial PC
mySerial.begin( 19200 ); // baud-rate of RS485
init_modBus();
}
void loop() {
rtuState();
printData();
processData();
}
f. VIDEO HASILNYA
No comments:
Post a Comment