Skip to content

Commit 67cce01

Browse files
authored
Update AEX_iobroker_IoT_Framework.ino
1 parent ac09e9c commit 67cce01

File tree

1 file changed

+296
-21
lines changed

1 file changed

+296
-21
lines changed

AEX_iobroker_IoT_Framework.ino

Lines changed: 296 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@
22
33
Shared functions for iobroker IoT Framework
44
5-
Version: V5 (release)
6-
Date: 2020-11-30
5+
Version: V5.1.1
6+
Date: 2020-12-03
77
88
Supported features / sensors:
99
1010
- Wifi/HTTP Client (no tls)
1111
- BME280
1212
- BME680
1313
- SCD30
14+
- SPS30
15+
- WindSensor
1416
1517
https://github.com/AndreasExner/ioBroker-IoT-Framework
1618
@@ -743,42 +745,35 @@ void SCD30_get_data() {
743745

744746
void SCD30_AutoCal() {
745747

746-
if (debug) {
747-
Serial.println("### SCD30_AutoCal");
748-
}
748+
if (debug) {Serial.println("### SCD30_AutoCal");}
749+
750+
String scd30_autoCal_get = bool_to_string(airSensor.getAutoSelfCalibration());
751+
Serial.println(" scd30_autoCal_get = " + scd30_autoCal_get);
749752

750753
HTTPClient http;
751754

752755
String sendURL;
753-
String scd30_autoCal;
754-
755-
sendURL = URL_SCD30_autoCal_get;
756+
sendURL = URL_SCD30_autoCal;
756757
http.begin(sendURL);
757758
http.GET();
758-
scd30_autoCal = http.getString();
759+
760+
String scd30_autoCal = http.getString();
761+
759762
http.end();
760763

761-
if (debug) {
762-
Serial.println(" scd30_autoCal = " + scd30_autoCal);
763-
}
764+
if (debug) {Serial.println(" scd30_autoCal = " + scd30_autoCal);}
764765

765-
if (scd30_autoCalHistory != scd30_autoCal) {
766+
if (scd30_autoCal_get != scd30_autoCal) {
766767
if (scd30_autoCal == "true") {
767768

768769
airSensor.setAutoSelfCalibration(true);
769-
if (debug) {
770-
Serial.println(" Enable SCD30 AutoCalibration");
771-
}
770+
if (debug) {Serial.println(" Enable SCD30 AutoCalibration");}
772771
}
773772
else {
774773
airSensor.setAutoSelfCalibration(false);
775-
if (debug) {
776-
Serial.println(" Disable SCD30 AutoCalibration");
777-
}
774+
if (debug) {Serial.println(" Disable SCD30 AutoCalibration");}
778775
}
779-
780776
airSensor.reset();
781-
scd30_autoCalHistory = scd30_autoCal;
782777
}
783778
}
784779

@@ -793,6 +788,286 @@ void SCD30_serial_output() {
793788
}
794789
#endif
795790

791+
#ifdef SPS30_active
792+
//######################################### SPS30 functions ########################################################
793+
794+
void SPS30_setup() {
795+
796+
int16_t ret;
797+
uint8_t auto_clean_days = 4;
798+
uint32_t auto_clean;
799+
800+
sensirion_i2c_init();
801+
802+
while (sps30_probe() != 0) {
803+
Serial.println("SPS sensor probing failed");
804+
send_ErrorLog("Error: SPS30 sensor probing failed");
805+
delay(1000);
806+
}
807+
808+
ret = sps30_set_fan_auto_cleaning_interval_days(auto_clean_days);
809+
if (ret) {
810+
Serial.println("error setting the auto-clean interval: " + String(ret));
811+
send_ErrorLog("Error: SPS30 setting the auto-clean interval failed");
812+
}
813+
814+
#ifdef SPS30_LIMITED_I2C_BUFFER_SIZE
815+
Serial.println("Your Arduino hardware has a limitation that only allows reading the mass concentrations.");
816+
Serial.println("For more information, please check:");
817+
Serial.println("https://github.com/Sensirion/arduino-sps#esp8266-partial-legacy-support");
818+
send_ErrorLog("Warning: SPS30 Your Arduino hardware has a limitation that only allows reading the mass concentrations.");
819+
delay(2000);
820+
#endif
821+
822+
SPS30_activated = true;
823+
824+
}
825+
826+
void SPS30_start_fan() {
827+
828+
int16_t ret;
829+
830+
Serial.println("starting measurement...");
831+
832+
ret = sps30_start_measurement(); //start measurement
833+
if (ret < 0) {
834+
Serial.println("error starting measurement");
835+
sps30_stop_measurement();
836+
send_ErrorLog("Error: SPS30 error starting measurement (fan)");
837+
reboot_on_error();
838+
}
839+
}
840+
841+
void SPS30_get_data() {
842+
843+
struct sps30_measurement m;
844+
char serial[SPS30_MAX_SERIAL_LEN];
845+
uint16_t data_ready;
846+
int16_t ret;
847+
848+
do {
849+
ret = sps30_read_data_ready(&data_ready);
850+
851+
if (ret < 0) {
852+
Serial.println("error reading data-ready flag: " + String(ret));
853+
sps30_stop_measurement();
854+
send_ErrorLog("Error: SPS30 error reading data-ready flag");
855+
break;
856+
}
857+
else if (!data_ready)
858+
{
859+
Serial.println("data not ready, no new measurement available");
860+
send_ErrorLog("Error: SPS30 data not ready, no new measurement available");
861+
break;
862+
}
863+
else
864+
{
865+
break;
866+
}
867+
868+
delay(100); /* retry in 100ms */
869+
} while (1);
870+
871+
ret = sps30_read_measurement(&m);
872+
873+
if (ret < 0) {
874+
Serial.println("error reading measurement");
875+
sps30_stop_measurement();
876+
send_ErrorLog("Error: SPS30 error reading measurement");
877+
reboot_on_error();
878+
}
879+
else {
880+
Serial.println("stopping measurement...");
881+
ret = sps30_stop_measurement();
882+
if (ret < 0) {
883+
Serial.println("error stopping measurement");
884+
send_ErrorLog("Error: SPS30 error stopping measurement");
885+
reboot_on_error();
886+
}
887+
}
888+
889+
// convert
890+
891+
char m_char[7];
892+
String untrimmed;
893+
894+
dtostrf(m.mc_1p0, 6, 2, m_char);
895+
mc_1p0 = String(m_char);
896+
mc_1p0.trim();
897+
898+
dtostrf(m.mc_2p5, 6, 2, m_char);
899+
mc_2p5 = String(m_char);
900+
mc_2p5.trim();
901+
902+
dtostrf(m.mc_4p0, 6, 2, m_char);
903+
mc_4p0 = String(m_char);
904+
mc_4p0.trim();
905+
906+
dtostrf(m.mc_10p0, 6, 2,m_char);
907+
mc_10p0 = String(m_char);
908+
mc_10p0.trim();
909+
910+
dtostrf(m.nc_0p5, 6, 2, m_char);
911+
nc_0p5 = String(m_char);
912+
nc_0p5.trim();
913+
914+
dtostrf(m.nc_1p0, 6, 2, m_char);
915+
nc_1p0 = String(m_char);
916+
nc_1p0.trim();
917+
918+
dtostrf(m.nc_2p5, 6, 2, m_char);
919+
nc_2p5 = String(m_char);
920+
nc_2p5.trim();
921+
922+
dtostrf(m.nc_4p0, 6, 2, m_char);
923+
nc_4p0 = String(m_char);
924+
nc_4p0.trim();
925+
926+
dtostrf(m.nc_10p0, 6, 2, m_char);
927+
nc_10p0 = String(m_char);
928+
nc_10p0.trim();
929+
930+
dtostrf(m.typical_particle_size, 6, 2, m_char);
931+
typical_particle_size = String(m_char);
932+
typical_particle_size.trim();
933+
}
934+
935+
void SPS30_zero_data() {
936+
937+
mc_1p0 = "0";
938+
mc_2p5 = "0";
939+
mc_4p0 = "0";
940+
mc_10p0 = "0";
941+
nc_0p5 = "0";
942+
nc_1p0 = "0";
943+
nc_2p5 = "0";
944+
nc_4p0 = "0";
945+
nc_10p0 = "0";
946+
typical_particle_size = "0";
947+
948+
}
949+
950+
void SPS30_serial_output() {
951+
952+
String output;
953+
954+
if (SPS30_sensor_active) {
955+
956+
output = String("SPS30 -- ");
957+
output += "mc_1p0 = " + mc_1p0;
958+
output += ", mc_2p5 = " + mc_2p5;
959+
output += ", mc_4p0 = " + mc_4p0;
960+
output += ", mc_10p0 = " + mc_10p0;
961+
output += ", nc_0p5 = " + nc_0p5;
962+
output += ", nc_1p0 = " + nc_1p0;
963+
output += ", nc_2p5 = " + nc_2p5;
964+
output += ", nc_4p0 = " + nc_4p0;
965+
output += ", nc_10p0 = " + nc_10p0;
966+
output += ", tps = " + typical_particle_size;
967+
}
968+
else {output = String("SPS30 -- inactive");}
969+
970+
Serial.println(output);
971+
}
972+
973+
void SPS30_control_heater() {
974+
975+
if (debug) {Serial.println("### SPS30_control_heater");}
976+
977+
HTTPClient http;
978+
979+
int humi_int = humi.toInt();
980+
String send_url;
981+
982+
//------------------------------------ Test mode
983+
984+
if (humi_test > 0) {
985+
humi_int = humi_test;
986+
}
987+
988+
// -----------------------------------------
989+
990+
if (debug) {
991+
Serial.print(" Humidity/high/low: " + String(humi_int) + "/" + String(humi_high) + "/" + String(humi_low));
992+
}
993+
994+
if (humi_int >= humi_high) {
995+
if (debug) {
996+
Serial.print(" -- Activate heater");
997+
Serial.println(" -- Switch GPIO to low: " + String(Relay_A));
998+
}
999+
digitalWrite(Relay_A, LOW);
1000+
send_url = URL_heater + "true";
1001+
}
1002+
else if (humi_int < humi_high && humi_int > humi_low) {
1003+
if (debug) {Serial.println(" -- Do nothing");}
1004+
}
1005+
else if (humi_int <= humi_low) {
1006+
if (debug) {
1007+
Serial.print(" -- Deactivate heater (low)");
1008+
Serial.println(" -- Switch GPIO to high: " + String(Relay_A));
1009+
}
1010+
digitalWrite(Relay_A, HIGH);
1011+
send_url = URL_heater + "false";
1012+
}
1013+
else {
1014+
if (debug) {
1015+
Serial.print(" -- Out of range - deactivate heater");
1016+
Serial.println(" -- Switch GPIO to high: " + String(Relay_A));
1017+
}
1018+
digitalWrite(Relay_A, HIGH);
1019+
send_url = URL_heater + "false";
1020+
send_ErrorLog("Warning: SPS30_control_heater out of range Humidity/high/low: " + String(humi_int) + "/" + String(humi_high) + "/" + String(humi_low));
1021+
}
1022+
http.begin(send_url);
1023+
http.GET();
1024+
http.end();
1025+
}
1026+
1027+
void SPS30_get_heater_config() {
1028+
1029+
HTTPClient http;
1030+
1031+
http.begin(URL_humi_low);
1032+
http.GET();
1033+
humi_low = http.getString().toInt();
1034+
1035+
http.begin(URL_humi_high);
1036+
http.GET();
1037+
humi_high = http.getString().toInt();
1038+
1039+
http.begin(URL_humi_test);
1040+
http.GET();
1041+
humi_test = http.getString().toInt();
1042+
1043+
http.end();
1044+
}
1045+
1046+
void SPS30_get_dynamic_config() {
1047+
1048+
if (debug) {Serial.println("### SPS30_get_dynamic_config");}
1049+
1050+
HTTPClient http;
1051+
1052+
// Get SPS30_sensor_active
1053+
1054+
http.begin(URL_SPS30_sensor_active);
1055+
http.GET();
1056+
if (http.getString() == "true") {
1057+
SPS30_sensor_active = true;
1058+
}
1059+
else {
1060+
SPS30_sensor_active = false;
1061+
}
1062+
1063+
if (debug) {
1064+
Serial.println(" New SPS30_sensor_active setting = " + bool_to_string(SPS30_sensor_active));
1065+
}
1066+
http.end();
1067+
}
1068+
1069+
#endif
1070+
7961071
#ifdef WindSensor_active
7971072
//######################################### wind speed sensor section #######################################################
7981073

0 commit comments

Comments
 (0)