hello!
i am looking for some help with a project,the project I'm currently working on using
- Arduino Mega (MASTER)
- Arduino Nano (SLAVE) with SW420 (Vibration Sensor) and DHT11 (Temperature and Humidity Sensor) (2)
- Max485 Ttl To Rs485 Converter Module (3)
In this project I want to send temperature, humidity, and vibration data from slave to master. After studying for a few days, I tried to code like this:
MASTER NODE
//Master Node - Arduino Mega
#include <SoftwareSerial.h>
#define MAX_PIN 2 //pin enable rx tx
SoftwareSerial RS485(11,12);
String dataslave[10];
String received;
int getaran,suhu,kelembapan;
bool parsing = false;
void setup(){
RS485.begin(9600);
Serial.begin(9600);
RS485.setTimeout(250);
pinMode (MAX_PIN, OUTPUT);
}
void loop(){
SendRequest();
}
void CekPesan(){
while(RS485.available()>0){
received = RS485.readString();
Serial.println ("Data=" + received);
parsing = true;
}
if (parsing){
int x = 0;
for (int i=0;i<10;i++){
dataslave[i] = "";
}
for (int i=0; i<received.length();i++){
if (received[i] == "#"){
x++;
dataslave[x] = "";
}else {
dataslave[x] += received[i];
}
}
}
parsing = false;
Serial.println("Berhasil menerima data");
CekSlave();
}
void SendRequest(){
digitalWrite (MAX_PIN, HIGH);
RS485.print("001");
delay(50);
digitalWrite (MAX_PIN, LOW);
delay(950);
CekPesan();
digitalWrite (MAX_PIN, HIGH);
RS485.print("002");
delay(50);
digitalWrite (MAX_PIN, LOW);
delay(950);
CekPesan();
}
void CekSlave(){
getaran = dataslave[0].toInt();
suhu = dataslave[1].toInt();
kelembapan = dataslave[2].toInt();
CetakData();
}
void CetakData(){
Serial.print("Getaran:");
Serial.println(getaran);
Serial.print("Suhu:");
Serial.println(suhu);
Serial.print("Kelembapan:");
Serial.println(kelembapan);
}
SLAVE NODE (same code, just different slave id)
#include "DHT.h"
#include <SoftwareSerial.h>
#define MAX_PIN 12
// definitions
#define DHTPIN 2 // pin of the arduino where the sensor is connected to
#define DHTTYPE DHT11 // define the type of sensor (DHT11 or DHT22)
SoftwareSerial RS485(10,11);
String id = "001";
String strSuhu,strLembab,strGetar;
int suhu,lembab,getar;
DHT dht(DHTPIN, DHTTYPE, 6);
int vs=9;
void setup(){
RS485.begin(9600);
Serial.begin(9600);
pinMode (vs,INPUT);
pinMode (MAX_PIN, OUTPUT);
dht.begin();
}
void loop(){
jalankan_sensor();
}
int vibration(){
int measurement=pulseIn (vs,HIGH);
return measurement;
}
void jalankan_sensor(){
suhu = dht.readHumidity();
lembab = dht.readTemperature();
getar = vibration();
delay(500);
ubah_string();
}
void ubah_string(){
strSuhu = String (suhu);
strLembab = String (lembab);
strGetar = String (getar);
cekpesan();
}
void cekpesan(){
String DataOut = "";
DataOut = strSuhu + "#" + strLembab + "#" + strGetar;
Serial.println (DataOut);
while (RS485.available()>0){
DataIn = RS485.readString();
Serial.println("Request:" + DataIn);
if (DataIn == id){
digitalWrite(MAX_PIN, HIGH);
RS485.print(DataOut);
delay(50);
digitalWrite(MAX_PIN, LOW);
Serial.println ("Mengirim:" + DataOut);
}
}
}
When I try to run it sensor can fetch temperature, humidity and vibration data on slave, But still can't send data to Master. Are there any suggestions for what I can do and references I should study?
