From 732f0776ab23486db39c5dc8775343cce5f81fdc Mon Sep 17 00:00:00 2001 From: houaimin Date: Sat, 8 Oct 2022 22:43:22 +0800 Subject: [PATCH] add examples --- .../01-control one relay.ino | 37 ++++++++++++++++ .../02-control multi relay.ino | 37 ++++++++++++++++ .../03-open all relay/03-open all relay.ino | 28 ++++++++++++ .../04-close all relay/04-close all relay.ino | 28 ++++++++++++ .../05-read one relay state.ino | 24 ++++++++++ .../06-read all relay state.ino | 23 ++++++++++ .../07-read one sensor state.ino | 24 ++++++++++ .../08-read all sensor state.ino | 24 ++++++++++ .../09-relay change hook.ino | 29 ++++++++++++ .../10-sensor change hook.ino | 29 ++++++++++++ .../11-normal use.ino} | 10 +++-- src/KC868.cpp | 44 ++++++++++++++----- src/KC868.h | 9 ++-- 13 files changed, 328 insertions(+), 18 deletions(-) create mode 100644 examples/01-control one relay/01-control one relay.ino create mode 100644 examples/02-control multi relay/02-control multi relay.ino create mode 100644 examples/03-open all relay/03-open all relay.ino create mode 100644 examples/04-close all relay/04-close all relay.ino create mode 100644 examples/05-read one relay state/05-read one relay state.ino create mode 100644 examples/06-read all relay state/06-read all relay state.ino create mode 100644 examples/07-read one sensor state/07-read one sensor state.ino create mode 100644 examples/08-read all sensor state/08-read all sensor state.ino create mode 100644 examples/09-relay change hook/09-relay change hook.ino create mode 100644 examples/10-sensor change hook/10-sensor change hook.ino rename examples/{kc868-test/kc868-test.ino => 11-normal use/11-normal use.ino} (73%) diff --git a/examples/01-control one relay/01-control one relay.ino b/examples/01-control one relay/01-control one relay.ino new file mode 100644 index 0000000..22a72c0 --- /dev/null +++ b/examples/01-control one relay/01-control one relay.ino @@ -0,0 +1,37 @@ +#include "KC868.h" + + +KC868 kc868(&Serial2); //create kc868 object + +void setup() { + + Serial.begin(115200); + + delay(2000); + + kc868.begin(115200);//begin kc868 with baud 115200 + + boolean ret = kc868.writeSwitch(2,1); //open relay 2 + Serial.printf("open relay 2 "); + if(ret) + Serial.println("success!"); + else + Serial.println("fail."); + + delay(2000); + + ret = kc868.writeSwitch(2,0); //close relay 2 + Serial.printf("close relay 2 "); + if(ret) + Serial.println("success!"); + else + Serial.println("fail."); + +} + + +void loop(){ + + kc868.poll(); + +} diff --git a/examples/02-control multi relay/02-control multi relay.ino b/examples/02-control multi relay/02-control multi relay.ino new file mode 100644 index 0000000..f2a986b --- /dev/null +++ b/examples/02-control multi relay/02-control multi relay.ino @@ -0,0 +1,37 @@ +#include "KC868.h" + + +KC868 kc868(&Serial2); //create kc868 object + +void setup() { + + Serial.begin(115200); + + delay(2000); + + kc868.begin(115200);//begin kc868 with baud 115200 + + boolean ret = kc868.writeSwitchAll(98304); //open relay 16 and relay 17 ,hex value is 0x00018000 , decimal value is 98304 + Serial.printf("open relay 16 and 17 "); + if(ret) + Serial.println("success!"); + else + Serial.println("fail."); + + delay(2000); + + ret = kc868.writeSwitchAll(0); //close all relay (contain relay 16 and relay 17) + Serial.printf("close all relay "); + if(ret) + Serial.println("success!"); + else + Serial.println("fail."); + +} + + +void loop(){ + + kc868.poll(); + +} \ No newline at end of file diff --git a/examples/03-open all relay/03-open all relay.ino b/examples/03-open all relay/03-open all relay.ino new file mode 100644 index 0000000..7ff949f --- /dev/null +++ b/examples/03-open all relay/03-open all relay.ino @@ -0,0 +1,28 @@ +#include "KC868.h" + + +KC868 kc868(&Serial2); //create kc868 object + +void setup() { + + Serial.begin(115200); + + delay(2000); + + kc868.begin(115200);//begin kc868 with baud 115200 + + boolean ret = kc868.writeSwitchAllON(); //open all relay + Serial.printf("open all relay "); + if(ret) + Serial.println("success!"); + else + Serial.println("fail."); + +} + + +void loop(){ + + kc868.poll(); + +} \ No newline at end of file diff --git a/examples/04-close all relay/04-close all relay.ino b/examples/04-close all relay/04-close all relay.ino new file mode 100644 index 0000000..5a7cbc6 --- /dev/null +++ b/examples/04-close all relay/04-close all relay.ino @@ -0,0 +1,28 @@ +#include "KC868.h" + + +KC868 kc868(&Serial2); //create kc868 object + +void setup() { + + Serial.begin(115200); + + delay(2000); + + kc868.begin(115200);//begin kc868 with baud 115200 + + boolean ret = kc868.writeSwitchAllOFF(); //close all relay + Serial.printf("close all relay "); + if(ret) + Serial.println("success!"); + else + Serial.println("fail."); + +} + + +void loop(){ + + kc868.poll(); + +} \ No newline at end of file diff --git a/examples/05-read one relay state/05-read one relay state.ino b/examples/05-read one relay state/05-read one relay state.ino new file mode 100644 index 0000000..ea7b9ca --- /dev/null +++ b/examples/05-read one relay state/05-read one relay state.ino @@ -0,0 +1,24 @@ +#include "KC868.h" + + +KC868 kc868(&Serial2); //create kc868 object + +void setup() { + + Serial.begin(115200); + + delay(2000); + + kc868.begin(115200);//begin kc868 with baud 115200 + + uint8_t ret = kc868.readSwitch(15); //read relay 15 + Serial.printf("relay 15 state is %d.\n",ret); //ret=1 relay 15 on, ret=0 relay 15 off + +} + + +void loop(){ + + kc868.poll(); + +} \ No newline at end of file diff --git a/examples/06-read all relay state/06-read all relay state.ino b/examples/06-read all relay state/06-read all relay state.ino new file mode 100644 index 0000000..a89d3b7 --- /dev/null +++ b/examples/06-read all relay state/06-read all relay state.ino @@ -0,0 +1,23 @@ +#include "KC868.h" + + +KC868 kc868(&Serial2); //create kc868 object + +void setup() { + + Serial.begin(115200); + + delay(2000); + + kc868.begin(115200);//begin kc868 with baud 115200 + + uint32_t ret = kc868.readSwitchAll(); //read all relay state + Serial.printf("relay state is 0x%08x.\n",ret); //32 bit value,the highest bit is the state of relay 32, and the lowest bit is the state of relay 1 +} + + +void loop(){ + + kc868.poll(); + +} \ No newline at end of file diff --git a/examples/07-read one sensor state/07-read one sensor state.ino b/examples/07-read one sensor state/07-read one sensor state.ino new file mode 100644 index 0000000..9c9ebf3 --- /dev/null +++ b/examples/07-read one sensor state/07-read one sensor state.ino @@ -0,0 +1,24 @@ +#include "KC868.h" + + +KC868 kc868(&Serial2); //create kc868 object + +void setup() { + + Serial.begin(115200); + + delay(2000); + + kc868.begin(115200);//begin kc868 with baud 115200 + + uint8_t ret = kc868.readSensor(5); //read sensor 5 state, + Serial.printf("sensor 5 state is %d.\n",ret); //ret=1,the sensor is tirggered, ret=0,the sensor is not tirggered + +} + + +void loop(){ + + kc868.poll(); + +} \ No newline at end of file diff --git a/examples/08-read all sensor state/08-read all sensor state.ino b/examples/08-read all sensor state/08-read all sensor state.ino new file mode 100644 index 0000000..3ee96c1 --- /dev/null +++ b/examples/08-read all sensor state/08-read all sensor state.ino @@ -0,0 +1,24 @@ +#include "KC868.h" + + +KC868 kc868(&Serial2); //create kc868 object + +void setup() { + + Serial.begin(115200); + + delay(2000); + + kc868.begin(115200);//begin kc868 with baud 115200 + + uint8_t ret = kc868.readSensorAll(); //read all sensor state, + Serial.printf("sensor state is 0x%02x.\n",ret); //8 bit value,the 6st bit is the state of sensor 6, and the lowest bit is the state of sensor 1 + //Which bit is 1, which bit is triggered. +} + + +void loop(){ + + kc868.poll(); + +} \ No newline at end of file diff --git a/examples/09-relay change hook/09-relay change hook.ino b/examples/09-relay change hook/09-relay change hook.ino new file mode 100644 index 0000000..d9aa842 --- /dev/null +++ b/examples/09-relay change hook/09-relay change hook.ino @@ -0,0 +1,29 @@ +#include "KC868.h" + + +KC868 kc868(&Serial2); //create kc868 object + +void SwitchUpdate(uint8_t idx, uint8_t state) +{ + Serial.printf("Switch %d : %d\n",idx,state);//state ,0:switch off , 1: switch on + +} + +void setup() { + + Serial.begin(115200); + + delay(2000); + + kc868.begin(115200);//begin kc868 with baud 115200 + + kc868.setSwitchChangeHook(SwitchUpdate);//set switch change hook + +} + + +void loop(){ + + kc868.poll(); + +} \ No newline at end of file diff --git a/examples/10-sensor change hook/10-sensor change hook.ino b/examples/10-sensor change hook/10-sensor change hook.ino new file mode 100644 index 0000000..7616883 --- /dev/null +++ b/examples/10-sensor change hook/10-sensor change hook.ino @@ -0,0 +1,29 @@ +#include "KC868.h" + + +KC868 kc868(&Serial2); //create kc868 object + +void SensorUpdate(uint8_t idx, uint8_t state) +{ + Serial.printf("Sensor %d : %d\n",idx,state); //state ,0:sensor triggered , 1:sensor not triggered +} + + +void setup() { + + Serial.begin(115200); + + delay(2000); + + kc868.begin(115200);//begin kc868 with baud 115200 + + kc868.setSensorChangeHook(SensorUpdate);//set sensor change hook + +} + + +void loop(){ + + kc868.poll(); + +} \ No newline at end of file diff --git a/examples/kc868-test/kc868-test.ino b/examples/11-normal use/11-normal use.ino similarity index 73% rename from examples/kc868-test/kc868-test.ino rename to examples/11-normal use/11-normal use.ino index f193c5c..fc0925f 100644 --- a/examples/kc868-test/kc868-test.ino +++ b/examples/11-normal use/11-normal use.ino @@ -1,7 +1,7 @@ #include "KC868.h" -KC868 kc868(&Serial2,115200);//create a new kc868 object, arg 1: serial object point , arg 2: baudrate +KC868 kc868(&Serial2);//create a new kc868 object, arg 1: serial object point , arg 2: baudrate void SwitchUpdate(uint8_t idx, uint8_t state) @@ -21,7 +21,7 @@ void setup() { delay(3000); - kc868.open();//open the kc868 first + kc868.begin(115200);//open the kc868 first kc868.setReadMode(0);//mode, 0 :default, Query directly; 1: Query all switch state every 2s @@ -32,13 +32,17 @@ void setup() { Serial.println("Write Switch 1 on."); kc868.writeSwitch(1,1); + int ret = kc868.readSwitch(1); + Serial.printf("The state of Switch 1 is %d .\n",ret); + Serial.println("Delay 1 second."); delay(1000); Serial.println("Write Switch 1 off."); kc868.writeSwitch(1,0); - //kc868.readSwitchAll();// + ret = kc868.readSwitch(1); + Serial.printf("The state of Switch 1 is %d .\n",ret); } diff --git a/src/KC868.cpp b/src/KC868.cpp index b1afd51..6497462 100644 --- a/src/KC868.cpp +++ b/src/KC868.cpp @@ -1,24 +1,24 @@ #include "KC868.h" -KC868::KC868(HardwareSerial *hs, unsigned long baud) +KC868::KC868(HardwareSerial *hs) { _type = dev_serial; _serial = hs; - _baud = baud; } -boolean KC868::open() +boolean KC868::begin(unsigned long baud, uint32_t config, int8_t rxPin, int8_t txPin, bool invert, unsigned long timeout_ms, uint8_t rxfifo_full_thrhd) + { if (_type == dev_serial) { - _serial->begin(_baud); + _serial->begin(baud, config, rxPin, txPin, invert, timeout_ms, rxfifo_full_thrhd); } return true; } -boolean KC868::close() +boolean KC868::end() { if (_type == dev_serial) { @@ -48,8 +48,6 @@ boolean KC868::readSwitch(int idx) // example RELAY-READ-255,1,1,OK\0 sprintf(tmp, "%s,%d,1,OK", READ_RELAY_CMD, idx); //check 'OK' ,otherwise return false ret = checkRet(tmp); - Serial.printf("---%s---",pkt.rxbuf); - Serial.printf("---%d---",ret); if (ret) return true; else @@ -324,13 +322,14 @@ boolean KC868::readSensor(int idx) } value = atoi(cmd.p1); + value = ~value; value = (value>>(idx-1))&0x01; return value; } } } - return 1; + return 0; } @@ -368,6 +367,7 @@ uint8_t KC868::readSensorAll() } value = atoi(cmd.p1); + value = ~value; return value; } } @@ -381,6 +381,30 @@ void KC868::write(char *data, uint16_t len) { if (_type == dev_serial) { + bufferPos=0; + + while (_serial->available()) + { + + uint8_t received = _serial->read(); + // if((received=='\0')||(received=='\n')) + // break; + inputBuffer[bufferPos++] = received; + if (bufferPos >= PKT_MAX_LEN) + { + bufferPos = 0; + } + + } + if(bufferPos>0) + { + //handle one line + handleData(inputBuffer, bufferPos); + + memset(inputBuffer, 0, PKT_MAX_LEN); + bufferPos = 0; + } + memset(pkt.rxbuf, 0, PKT_MAX_LEN); _serial->write(data, len); } @@ -589,7 +613,7 @@ void KC868::handleData(char *data, uint16_t len) idx = atoi(cmd.p1); if(SensorChangeHook!=NULL) - SensorChangeHook(idx,0);//alarm + SensorChangeHook(idx,1);//alarm } @@ -606,7 +630,7 @@ void KC868::handleData(char *data, uint16_t len) idx = atoi(cmd.p1); if(SensorChangeHook!=NULL) - SensorChangeHook(idx,1);//Cancel alarm + SensorChangeHook(idx,0);//Cancel alarm } diff --git a/src/KC868.h b/src/KC868.h index cb3ba0c..f47a4a7 100644 --- a/src/KC868.h +++ b/src/KC868.h @@ -57,10 +57,9 @@ class KC868 _dev_type _type; HardwareSerial *_serial; - int _baud; uint32_t _SwitchCache;//save switch state ,32 bit uint32_t _SwitchCache_last=0x00;//save last switch state ,32 bit - uint8_t _SensorCache=0XFF;//save sensor input state + uint8_t _SensorCache=0x00;//save sensor input state uint8_t _mode; hw_timer_t* tim= NULL; @@ -83,11 +82,11 @@ class KC868 _data_pkt pkt; public: - KC868(HardwareSerial *hs, unsigned long baud); //serial + KC868(HardwareSerial *hs); //serial // KC868(WiFiClient client); // wificlient - boolean open(); - boolean close(); + boolean begin(unsigned long baud, uint32_t config=SERIAL_8N1, int8_t rxPin=-1, int8_t txPin=-1, bool invert=false, unsigned long timeout_ms = 20000UL, uint8_t rxfifo_full_thrhd = 112); + boolean end(); void poll(); boolean readSwitch(int idx); boolean readSwitchCache(int idx);