CadioSerial Library
About
CadioSerial arduino library allows you to connect external MCUs to the ESP in order to add extra GPIOs, or to do any tasks not natively supported.
CadioSerial arduino library for arduino ide is available to download from Downloads center.
Example
- In this example we have arduino board connected with ESP8266.
- The ESP8266 flashed with CADIO firmware and software serial option is activated.
- On arduino board we have 8 switches and 8 relays.
- We are using
A4
andA5
as software serial pins on the arduino board to communicate with the ESP8266.
Wiring
ESP8266 | Arduino |
---|---|
GPIO 4 | A4 |
GPIO 5 | A5 |
GND | GND |
ESP info file settings
- Enabling software serial option.
- Adding 8 ON/OFF devices, and select their GPIOs as SERIAL.
Arduino code
#define CADIO_RX A4 //define custom software serial pins (before including <CadioSerial.h>)
#define CADIO_TX A5 //define custom software serial pins (before including <CadioSerial.h>)
#include <CadioSerial.h> //include CadioSerial library
CadioSerial cadio; //create instance
int relay[8] = {2, 3, 4, 5, 6, 7, 8, 9}; //output pins
int sw[8] = {10, 11, 12, 13, A0, A1, A2, A3}; //switches pins
byte swLast[8]; //for saving last switches status
void cadioOrder(int index, int value) { //new order form esp
if (value > 0) {
digitalWrite(relay[index], HIGH); //change relay status
} else {
digitalWrite(relay[index], LOW); //change relay status
}
}
void sw_check() { //check real switches changes after ignoring noise
for (int i = 0; i < 8; i++) {
if (digitalRead(sw[i]) != swLast[i]) {
bool confirm = true;
unsigned long deNoise = millis();
while (millis() - deNoise < 100) {
if (digitalRead(sw[i]) == swLast[i]) {
confirm = false;
break;
}
}
if (confirm) {
swLast[i] = digitalRead(sw[i]);
sw_changed(i);
}
}
}
}
void sw_changed(int i) { //switch status changed
digitalWrite(relay[i], !digitalRead(relay[i])); //change relay status
if (digitalRead(relay[i])) {
cadio.set(i, 255); //send new status to esp
} else {
cadio.set(i, 0); //send new status to esp
}
}
void setup() {
for (int i = 0; i < 8; i++) {
pinMode(relay[i], OUTPUT); //set relays pinMode
pinMode(sw[i], INPUT_PULLUP); //set switches pinMode
swLast[i] = digitalRead(sw[i]); //save last switches digitalRead
}
cadio.begin(); //start cadio library
cadio.onOrder(cadioOrder); //get orders
cadio.getCurrentStatus(); //request all current status from esp
}
void loop() {
sw_check(); //check switches status
cadio.loop(); //check for cadio orders
}
API usage
Defining software serial pins
#define CADIO_RX A4
#define CADIO_TX A5
- Must be defined before including the library.
- If not added,
RX
andTX
pins will be used.
Including CadioSerial library
#include <CadioSerial.h>
Creating instance
CadioSerial cadio;
Begin
cadio.begin();
- This function should be called in
setup()
.
Handling communications
cadio.loop();
- This function should be added at the end of
loop()
.
Listening for CADIO orders
cadio.onOrder(cadioOrder);
- This function should be called in
setup()
.
cadioOrder
callback function:
void cadioOrder(int index, int value)
- index: the device index.
- value: a number from 0 to 255.
- for ONOFF devices
OFF = 0
andON = 255
.
- for ONOFF devices
Sending device status to CADIO
cadio.set(int index, int value);
- index: the device index.
- value: a number from 0 to 255.
- for ONOFF devices
OFF = 0
andON = 255
.
- for ONOFF devices
Requesting all devices status form CADIO
cadio.getCurrentStatus();
- response will received as normal orders.
Listening for CADIO cooling fan orders
cadio.onFanOrder(fanOrder);
- This function should be called in
setup()
.
fanOrder
callback function:
void fanOrder(int value)
- value:
OFF = 0
andON = 255
.