diff --git a/examples/Magellan_extension_stardardcode/Magellan_extension_stardardcode.ino b/examples/Magellan_extension_stardardcode/Magellan_extension_stardardcode.ino index a548596..99a3153 100644 --- a/examples/Magellan_extension_stardardcode/Magellan_extension_stardardcode.ino +++ b/examples/Magellan_extension_stardardcode/Magellan_extension_stardardcode.ino @@ -1,63 +1,80 @@ #include #include #include +#include MAGELLAN_SIM7600E_MQTT magel; AIS_4G_EXTENSION_BOARD extBoard; +#define DRY_MILLIVOLT 1600 // please calibrate by read rawMillivolt on dry and wet for accuracy +#define WET_MILLIVOLT 800 // please calibrate by read rawMillivolt on dry and wet for accuracy unsigned long time_previous = 0; // previous time for using timer with millis() -#define DRY_MILLIVOLT 2038 // please calibrate by read rawMillivolt on dry and wet for accuracy -#define WET_MILLIVOLT 1675 // please calibrate by read rawMillivolt on dry and wet for accuracy void setup() { Serial.begin(115200); + RS485.begin(9600, SERIAL_8N1); extBoard.begin(); // initialize extension board extLightMeter.begin(); // initialize [BH1750]light meter sensor - extSHT20.begin(); // initialize SHT20-x sensor extSoilMoisture.begin(AIN::A1, WET_MILLIVOLT, DRY_MILLIVOLT); // initialize soilMoisture sensor connect on "A1" with value from calibrate dry, wet - + setting.endpoint = "device-entmagellan.ais.co.th"; setting.clientBufferSize = defaultOTABuffer; magel.begin(setting); magel.getControl([](String key, String value) - { - Serial.println("Control incoming - [Key]:"+ key + " [Value]" + value); - //Control turn ON OFF Relay1 - if (key == "Relay1"){ - if (value == "1"){ - extBoard.set(RELAY::CH1, HIGH); //set status ON OFF Relay Channel 1 - } else{ - extBoard.set(RELAY::CH1, LOW); //set status ON OFF Relay Channel 1 - } - } - //Control turn ON OFF Relay2 - if (key == "Relay2"){ - if (value == "1"){ - extBoard.set(RELAY::CH2, HIGH); //set status ON OFF Relay Channel 2 - } else{ - extBoard.set(RELAY::CH2, LOW); //set status ON OFF Relay Channel 2 - } - } - //Control turn ON OFF Relay3 - if (key == "Relay3"){ - if (value == "1"){ - extBoard.set(RELAY::CH3, HIGH); //set status ON OFF Relay Channel 3 - } else{ - extBoard.set(RELAY::CH3, LOW); //set status ON OFF Relay Channel 3 - } - } - //Control turn ON OFF Relay4 - if (key == "Relay4"){ - if (value == "1"){ - extBoard.set(RELAY::CH4, HIGH); //set status ON OFF Relay Channel 4 - } else{ - extBoard.set(RELAY::CH4, LOW); //set status ON OFF Relay Channel 4 - } - } - magel.control.ACK(key, value); // ACKNOWLEDGE Control back to magellan - }); + { + Serial.println("Control incoming - [Key]:" + key + " [Value]" + value); + // Control turn ON OFF Relay1 + if (key == "Relay1") + { + if (value == "1") + { + extBoard.set(RELAY::CH1, HIGH); // set status ON OFF Relay Channel 1 + } + else + { + extBoard.set(RELAY::CH1, LOW); // set status ON OFF Relay Channel 1 + } + } + // Control turn ON OFF Relay2 + if (key == "Relay2") + { + if (value == "1") + { + extBoard.set(RELAY::CH2, HIGH); // set status ON OFF Relay Channel 2 + } + else + { + extBoard.set(RELAY::CH2, LOW); // set status ON OFF Relay Channel 2 + } + } + // Control turn ON OFF Relay3 + if (key == "Relay3") + { + if (value == "1") + { + extBoard.set(RELAY::CH3, HIGH); // set status ON OFF Relay Channel 3 + } + else + { + extBoard.set(RELAY::CH3, LOW); // set status ON OFF Relay Channel 3 + } + } + // Control turn ON OFF Relay4 + if (key == "Relay4") + { + if (value == "1") + { + extBoard.set(RELAY::CH4, HIGH); // set status ON OFF Relay Channel 4 + } + else + { + extBoard.set(RELAY::CH4, LOW); // set status ON OFF Relay Channel 4 + } + } + magel.control.ACK(key, value); // ACKNOWLEDGE Control back to magellan + }); // sync output status on device turn off all to magellan magel.sensor.add("Relay1", 0); @@ -71,16 +88,20 @@ void loop() { magel.loop(); magel.subscribes([]() - { magel.subscribe.control(PLAINTEXT); - }); - + { magel.subscribe.control(PLAINTEXT); }); + // read and update value sensor every 15 seconds magel.interval(15, []() { + //read from RS485 XY MD02 + long temp_int16 = RS485.inputRegisterRead(1, 1); + delay(50); + long humi_int16 = RS485.inputRegisterRead(1, 2); + float temp_float = temp_int16 / 10.0; //buffer read value RS485 XY MD02 + float humi_float = humi_int16 / 10.0; //buffer read value RS485 XY MD02 + float LightMeter = extLightMeter.readLight(); - float Sht20Temp = extSHT20.readTemperature(); - float Sht20Humid = extSHT20.readHumidity(); - float SoilMoisture = extSoilMoisture.readMoistureLevel(); + float SoilMoisture = extSoilMoisture.readMoisturePercent(); float BoardTemp = magel.builtInSensor.readTemperature(); float BoardHumid = magel.builtInSensor.readHumidity(); if (magel.gps.available()) @@ -89,23 +110,22 @@ void loop() magel.sensor.add("GPS", Location); } magel.sensor.add("Light", LightMeter); - magel.sensor.add("AirTemp", Sht20Temp); - magel.sensor.add("AirHumid", Sht20Humid); magel.sensor.add("SoilMoisture", SoilMoisture); magel.sensor.add("BoardTemp", BoardTemp); magel.sensor.add("BoardHumid", BoardHumid); - magel.sensor.report(); - }); + magel.sensor.add("RS485Temp", temp_float); + magel.sensor.add("RS485Humid", humi_float); + magel.sensor.report(); }); // read and update status relay every 5 minutes to magellan if (millis() - time_previous > 300000) { time_previous = millis(); // update time - int statusRelay1 = extBoard.read(RELAY::CH1); //read status Relay Channel 1 - int statusRelay2 = extBoard.read(RELAY::CH2); //read status Relay Channel 2 - int statusRelay3 = extBoard.read(RELAY::CH3); //read status Relay Channel 3 - int statusRelay4 = extBoard.read(RELAY::CH4); //read status Relay Channel 4 + int statusRelay1 = extBoard.read(RELAY::CH1); // read status Relay Channel 1 + int statusRelay2 = extBoard.read(RELAY::CH2); // read status Relay Channel 2 + int statusRelay3 = extBoard.read(RELAY::CH3); // read status Relay Channel 3 + int statusRelay4 = extBoard.read(RELAY::CH4); // read status Relay Channel 4 magel.sensor.add("Relay1", statusRelay1); magel.sensor.add("Relay2", statusRelay2); magel.sensor.add("Relay3", statusRelay3); diff --git a/examples/Soil_moisture/Soil_moisture.ino b/examples/Soil_moisture/Soil_moisture.ino index 8bba928..23bc403 100644 --- a/examples/Soil_moisture/Soil_moisture.ino +++ b/examples/Soil_moisture/Soil_moisture.ino @@ -3,8 +3,8 @@ AIS_4G_EXTENSION_BOARD extBoard; -#define DRY_millivolt 2038 // please calibrate by read rawMillivolt on dry and wet for accuracy -#define WET_millivolt 1675 // please calibrate by read rawMillivolt on dry and wet for accuracy +#define DRY_millivolt 1600 // please calibrate by read rawMillivolt on dry and wet for accuracy +#define WET_millivolt 800 // please calibrate by read rawMillivolt on dry and wet for accuracy void setup() { Serial.begin(115200); @@ -15,5 +15,8 @@ void loop() { Serial.print("Soil Moisture Level: "); Serial.println(extSoilMoisture.readMoistureLevel(), 1); + Serial.print("Soil Moisture Percent: "); + Serial.print(extSoilMoisture.readMoisturePercent()); + Serial.println(" v%"); delay(2000); } \ No newline at end of file diff --git a/examples/XY_MD02_RS485/XY_MD02_RS485.ino b/examples/XY_MD02_RS485/XY_MD02_RS485.ino new file mode 100644 index 0000000..d6e27d6 --- /dev/null +++ b/examples/XY_MD02_RS485/XY_MD02_RS485.ino @@ -0,0 +1,26 @@ +#include +#include //required libraries AIS_IoT_4G + +unsigned long time_previous = 0; // previous time for using timer with millis() +void setup() +{ + RS485.begin(9600, SERIAL_8N1); + Serial.begin(115200); +} +void loop() +{ + long temp_int16 = RS485.inputRegisterRead(1, 1); + delay(50); + long humi_int16 = RS485.inputRegisterRead(1, 2); + + float temp_float = temp_int16 / 10.0; + float humi_float = humi_int16 / 10.0; + + Serial.print("Temperature: "); + Serial.print(temp_float); + Serial.print(" *C\t"); + Serial.print("Humidity: "); + Serial.print(humi_float); + Serial.println(" %RH"); + delay(2000); +} \ No newline at end of file diff --git a/src/Modules/IO_EXPANDER.cpp b/src/Modules/IO_EXPANDER.cpp index 8256c8f..0662aa4 100644 --- a/src/Modules/IO_EXPANDER.cpp +++ b/src/Modules/IO_EXPANDER.cpp @@ -122,13 +122,16 @@ void IO_EXPANDER::set(AIN analogIO, int value) } } -bool readOnBoard(int _pin){ +bool readOnBoard(int _pin) +{ return digitalRead(_pin); } -void writeOnBoard(int _pin, int value){ +void writeOnBoard(int _pin, int value) +{ digitalWrite(_pin, value); } -void setOnBoard(int _pin, int mode){ +void setOnBoard(int _pin, int mode) +{ pinMode(_pin, mode); } @@ -160,4 +163,43 @@ void IO_EXPANDER::setMode(DIGITAL digitalIO, int mode) { return; } -} \ No newline at end of file +} + +// 1.0.1 + +/// @details get raw pin Analog +int IO_EXPANDER::getPinAIN(AIN analogINPUT) +{ + if (analogINPUT == AIN::A1) + { + return A1_PIN; + } + else if (analogINPUT == AIN::A2) + { + return A2_PIN; + } + return -1; +} + +/// @details get raw pin Digital +int IO_EXPANDER::getPinDIO(DIGITAL digitalIO) +{ + if (digitalIO == DIGITAL::D3) + { + return D3_PIN; + } + return -1; +} +int IO_EXPANDER::getPinDIN(DIN digitalINPUT) +{ + if (digitalINPUT == DIN::D1) + { + return D1_PIN; + } + else if (digitalINPUT == DIN::D2) + { + return D2_PIN; + } + return -1; +} + diff --git a/src/Modules/IO_EXPANDER.h b/src/Modules/IO_EXPANDER.h index 680f201..88a6d57 100644 --- a/src/Modules/IO_EXPANDER.h +++ b/src/Modules/IO_EXPANDER.h @@ -4,7 +4,7 @@ #include "IncludeLibs.h" #include "../ExtBoardGPIO.h" #define IO_Ex_verMajor 1 -#define IO_Ex_verMinor 0 +#define IO_Ex_verMinor 1 #define IO_Ex_verRevision 0 class IO_EXPANDER : private PCA9557 @@ -22,6 +22,11 @@ class IO_EXPANDER : private PCA9557 void setMode(DIGITAL digitalIO, int mode); void begin(); + // 1.1.0 + int getPinAIN(AIN analogINPUT); + int getPinDIO(DIGITAL digitalIO); + int getPinDIN(DIN digitalINPUT); + private: PCA9557 *_ext; protected: diff --git a/src/Modules/SOIL_MOISTURE.cpp b/src/Modules/SOIL_MOISTURE.cpp index 8eebbf1..6cee5d4 100644 --- a/src/Modules/SOIL_MOISTURE.cpp +++ b/src/Modules/SOIL_MOISTURE.cpp @@ -54,3 +54,11 @@ float SOIL_MOISTURE::readMoistureLevel() float result = mathCamp(moistureLevel, 0.0f, 10.0f); return result; } + +int SOIL_MOISTURE::readMoisturePercent() +{ + int rawAnalog = readRawMillivolt(); + float moistureLevel = map(rawAnalog, this->_dry_millivolt, this->_wet_millivolt, 0, 100); + int result = mathCamp(moistureLevel, 0.0f, 100.0f); + return result; +} diff --git a/src/Modules/SOIL_MOISTURE.h b/src/Modules/SOIL_MOISTURE.h index 0ccf6b3..7ded5fe 100644 --- a/src/Modules/SOIL_MOISTURE.h +++ b/src/Modules/SOIL_MOISTURE.h @@ -1,6 +1,6 @@ #ifndef SOIL_MOISTURE_h #define SOIL_MOISTURE_h -//Capacitive Soil Moisture Sensor V1.2 +// Capacitive Soil Moisture Sensor V1.2 #include #include "IncludeLibs.h" #include "../ExtBoardGPIO.h" @@ -9,15 +9,24 @@ class SOIL_MOISTURE { private: int _analogPin = -1; - int _wet_millivolt = 3620; - int _dry_millivolt = 1680; + int _wet_millivolt = 1600; + int _dry_millivolt = 800; + public: SOIL_MOISTURE(){}; void begin(AIN anlogINPUT, int WET_MILLIVOLT, int DRY_MILLIVOLT); void begin(AIN anlogINPUT); void calibrateSensor(int WET_MILLIVOLT, int DRY_MILLIVOLT); + + //1.1.0 + //raw analog value millivolt float readRawMillivolt(); + + //level range 0 - 10 float readMoistureLevel(); + + //percent range 0 - 100 % + int readMoisturePercent(); }; extern SOIL_MOISTURE extSoilMoisture;