Skip to content

Commit

Permalink
new sensor calibrate value, mapping percent and add example some rs485
Browse files Browse the repository at this point in the history
  • Loading branch information
BinaryBearzz committed Nov 14, 2023
1 parent 4bfd34c commit 7c1abe4
Show file tree
Hide file tree
Showing 7 changed files with 177 additions and 64 deletions.
Original file line number Diff line number Diff line change
@@ -1,63 +1,80 @@
#include <Arduino.h>
#include <MAGELLAN_SIM7600E_MQTT.h>
#include <AIS_4G_EXTENSION_BOARD.h>
#include <RS485.h>

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);
Expand All @@ -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())
Expand All @@ -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);
Expand Down
7 changes: 5 additions & 2 deletions examples/Soil_moisture/Soil_moisture.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
26 changes: 26 additions & 0 deletions examples/XY_MD02_RS485/XY_MD02_RS485.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <Arduino.h>
#include <RS485.h> //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);
}
50 changes: 46 additions & 4 deletions src/Modules/IO_EXPANDER.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -160,4 +163,43 @@ void IO_EXPANDER::setMode(DIGITAL digitalIO, int mode)
{
return;
}
}
}

// 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;
}

7 changes: 6 additions & 1 deletion src/Modules/IO_EXPANDER.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down
8 changes: 8 additions & 0 deletions src/Modules/SOIL_MOISTURE.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
15 changes: 12 additions & 3 deletions src/Modules/SOIL_MOISTURE.h
Original file line number Diff line number Diff line change
@@ -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 <Arduino.h>
#include "IncludeLibs.h"
#include "../ExtBoardGPIO.h"
Expand All @@ -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;

Expand Down

0 comments on commit 7c1abe4

Please sign in to comment.