https://yonglimlee.tistory.com/entry/485-Serial-%ED%86%B5%EC%8B%A0%ED%95%98%EA%B8%B0?category=734072
1. ํต์ ๋ฐฉ๋ฒ
- RS-485
2. ๊ตฌ์ฑ
- master node 1๊ฐ
- slave node 2๊ฐ
3. master์ธก ์์ค
#include "Definition.h" //๊ตฌ์กฐ์ฒด๊ฐ ์ ์ธ
#define MAX_FAIL_CNT 2
int t = 0;
//Slave ๋
ธ๋์ ์์ด๋์ด๋ฉฐ
//Slave ๋
ธ๋๋ ๊ฐ์์ ์์ด๋๊ฐ์ด ๋ฌ๋ฆฌํด์ ์ ์ฅ๋์ด์์.
int id[NODE_CNT] = {101, 102};
int curIdx = 1;
enum eNode {e101=0, e102};
char ch = 'a';
//์๋ฆฌ์ผ ํต์ ๋ฐ์ดํฐ ์์ ๋ฌธ์์ด
char chAt = '@';
//์๋ฆฌ์ผ ํต์ ๋ฐ์ดํฐ ์ข
๋ฃ ๋ฌธ์์ด
char chExclam = '!';
char buf[BUFFER_SIZE] = {0, };
char reqID[5] = {0, };
String str = "";
bool bTransmitt = false;
bool bRead = false;
int failCnt = 0;
//slave ๋
ธ๋๋ก ๋ถํฐ ์์ ํ๋ ๊ฐ
enum envData {eTemp, eHumidity, eWater, eLight, eCnt};
unsigned long reqTime = 0;
unsigned long curTime = 0;
DATA nodes[NODE_CNT] = {0x00, };
//๋ฐ์ ๋ฐ์ดํฐ๋ฅผ ํ์ฑํ์ฌ ์ ์ฅํ๋ ํจ์
void parseDataFromPkt(String str)
{
char* token = NULL;
char tok[] = ",";
float arrValue[eCnt] = {0,};
DATA node = {0x00, };
char pkt[BUFFER_SIZE] = {0, };
strcpy(pkt, str.c_str());
token = strtok(pkt, tok);
int thisId = atoi(token);
int i = 0;
while(token != NULL && i < eCnt)
{
token = strtok(NULL, tok);
arrValue[i] = atof(token);
i++;
}
token = NULL;
//ํ์ฑํํ ๋ฐฐ์ด์ ์ ์ฅ
node.head.id = thisId;
node.body.temperature = arrValue[eTemp];
node.body.humidity = arrValue[eHumidity];
node.body.water = 0;
node.body.light = arrValue[eLight];
if ( thisId == 101)
nodes[e101] = node;
else if (thisId = 102)
nodes[e102] = node;
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600); // ์ง๋ ฌ ํต์ ์ด๊ธฐํ
Serial.println("I am a master!");
delay(100);
Serial.print("Slave node ID is ");
for(int i = 0 ; i < NODE_CNT ; ++i)
{
Serial.print(id[i]);
Serial.print(", ");
}
Serial.println("\nMaster node start...");
Serial.setTimeout(5000);
}
void loop() {
curTime = millis()/1000;
//์ฒ์์ bTransmitt์ false๋ก ์ด๊ธฐํ๋์ด์์.
if(bTransmitt == false){
int cnt = curIdx%2;
//@101! or @102!์ ํ์์ผ๋ก slave id ์์ฒญ
sprintf(buf, "@%d!", id[cnt]);
Serial.write(buf);
bTransmitt = true;
reqTime = millis()/1000;
}
if(bTransmitt == true && curTime - reqTime > 5){
bTransmitt = false;
curIdx++;
}
if(Serial.available())
{
char ch = Serial.read();
//์๋ด์ด @์ผ๋ก ์์ํ๋ฉด SerialRead ์ฝ๊ธฐ ์์
if (ch == '@')
{
bRead = true;
}
else
{
//๋ง์ง๋ง '!'๋ฅผ ๋ฌธ์๋ฅผ ๋ฐ์ผ๋ฉด
//string ๋ณ์์ ์ ์ฅ๋ ๊ฐ์ parsingํ์ฌ ์ ์ฅ
if (ch == '!')
{
bRead = false;
parseDataFromPkt(str);
str = "";
bTransmitt = false;
curIdx++;
delay(30000);
}
//์์ ๋ ๋ฐ์ดํฐ ๊ณ์ํด์ string ๋ณ์์ ์ ์ฅ
if(bRead)
{
str += ch;
}
}
}
if(curIdx == 10000){
curIdx = 1;
}
}
4. Slave๋ ธ๋ ์์ค
#include <DHT.h> // Including library for dht
#include <Timer.h>
//Slave NODE๋ ๊ฐ๊ฐ ๋ค๋ฅธ MYID ๊ฐ์ ๊ฐ์ง
//ํ
์คํธ์์๋ 101, 102๊ฐ์ ๊ฐ์ง๋ slave node2๊ฐ๋ก ํ
์คํธํจ.
#define MYID 101
#define HEADER_SIZE 4
#define DATA_SIZE 16
#define BUFFER_SIZE 30
#define LIGHT A1
#define WATER A0
bool bRead = false;
String str = "";
char buf[BUFFER_SIZE] = {0, };
bool isMyId(char* pkt);
int water = 1;
int light = 0;
float hum = 0.0f;
float tem = 0.0f;
Timer t;
#define DHTPIN 12 // NodeMCU D7 Pin //pin where the dht11 is connected
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
void readHumTem(void* context)
{
hum = dht.readHumidity();
tem = dht.readTemperature();
light = analogRead(LIGHT);
water = analogRead(WATER);
if(water > 10 )
water = 1;
else
water = 0;
if( isnan(hum) || isnan(tem))
{
Serial.println("Failed to read data from DHT Sensor!!!");
return;
}
}
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(9600);
Serial.print("Slave Id is ");
Serial.println(MYID);
//์จ์ต๋์ผ์ ์ด๊ธฐํ
dht.begin();
Serial.print("DHT is beginning...");
delay(10);
//ํ๊ฒฝ๋ฐ์ดํฐ๋ master node์ ์์ฒญ์ ๋ํ ์๋ต๊ณผ๋ ๋ณ๊ฐ๋ก
//1์ด๋ง๋ค ํ๋ฒ์ฉ ์ ์ญ๋ณ์ ๊ฐ์ ๊ฐฑ์ ํจ.
t.every(1000, readHumTem, 0);
}
void loop() // run over and over
{
if(Serial.available())
{
char ch = Serial.read();
//'@'๊ฐ์ ์ฝ์ผ๋ฉด Serial ๊ฐ์ ์ฝ๊ธฐ ์์ํจ.
if(ch == '@')
{
bRead = true;
}
else
{
if (ch == '!')
{
bRead = false;
//'!' ๋ฌธ์๊ฐ ๋ค์ด์ค๋ฉด ์ฝ๊ธฐ ์ค๋จ.
//ํ์ฌ MYID์ ์์ ๋ ์๋ฆฌ์ผ๋ฐ์ดํฐ์ ID๊ฐ๊ณผ ๋น๊ตํ์ฌ
//๊ฐ์ ๊ฒฝ์ฐ์ ์๋ต์ ๋ณด๋.
if( isMyId(str) == true )
{
memset(buf, 0x00, BUFFER_SIZE);
char chTem[8] = {0, };
char chHum[8] = {0, };
dtostrf(tem, 5, 2, chTem);
dtostrf(hum, 5, 2, chHum);
sprintf(buf, "@%d,%s,%s,%d,%d!", MYID, chTem, chHum, water, light);
Serial.write(buf);
}
str = "";
}
if(bRead)
{
str += ch;
}
}
}
//1์ด๋ง๋ค ํ๊ฒฝ๋ฐ์ดํฐ ์ฝ๊ธฐ
t.update();
}
bool isMyId(String s)
{
char pkt[10] = {0,};
strcpy(pkt, s.c_str());
char* id = strtok(pkt, "!");
int nId = atoi(id);
bool bSame = false;
if( MYID == nId)
bSame = true;
return bSame;
}
๋๊ธ