180 lines
4.0 KiB
C++
180 lines
4.0 KiB
C++
#include <bitset>
|
|
#include <string>
|
|
|
|
#include <ArduinoJson.h>
|
|
#include <ESP8266WiFi.h>
|
|
#include "./lib/pubsubclient/PubSubClient.h"
|
|
#include "./lib/pubsubclient/PubSubClient.cpp"
|
|
|
|
#include "settings.h"
|
|
|
|
#define MULTIPLEX_PINA D5
|
|
#define MULTIPLEX_PINB D6
|
|
#define MULTIPLEX_PINC D7
|
|
|
|
#define ANALOG_PIN A0
|
|
|
|
/*
|
|
000 0
|
|
001 1
|
|
010 2
|
|
011 3
|
|
100 4
|
|
101 5
|
|
110 6
|
|
111 7
|
|
*/
|
|
|
|
#define SOIL_A 0
|
|
#define SOIL_B 6
|
|
#define WATER_LEVEL_A 2
|
|
#define WATER_LEVEL_B 4
|
|
|
|
#define REFERENCE_VOLTAGE 3.3
|
|
|
|
// Ref https://stackoverflow.com/a/523737/1460422
|
|
#define CHECK_BIT(var,pos) ((var) & (1<<(pos)))
|
|
|
|
WiFiClientSecure transport;
|
|
PubSubClient mqtt;
|
|
|
|
float read_analog(int channel) {
|
|
|
|
const char* bin = std::bitset<8>(channel).to_string().c_str();
|
|
|
|
bool b0 = bin[7] == u'1';
|
|
bool b1 = bin[6] == u'1';
|
|
bool b2 = bin[5] == u'1';
|
|
|
|
digitalWrite(MULTIPLEX_PINA, b2);
|
|
digitalWrite(MULTIPLEX_PINB, b1);
|
|
digitalWrite(MULTIPLEX_PINC, b0);
|
|
|
|
int rawValue = analogRead(ANALOG_PIN);
|
|
float parsedValue = ((float)rawValue) / 1024.0 * REFERENCE_VOLTAGE;
|
|
|
|
return parsedValue;
|
|
}
|
|
|
|
bool send_mqtt(char* id, char* sensor, float value) {
|
|
StaticJsonDocument<96> data;
|
|
|
|
data["id"] = id;
|
|
data["sensor"] = sensor;
|
|
data["value"] = value;
|
|
|
|
int length = measureJson(&data);
|
|
char* payload = new char[length]();
|
|
serializeJson(data, payload, length);
|
|
|
|
bool result = mqtt.publish("sensors/data", payload, length);
|
|
delete[] payload;
|
|
|
|
return result;
|
|
}
|
|
|
|
void setup() {
|
|
Serial.begin(9600);
|
|
delay(1000);
|
|
Serial.println("\nAnalogue Soil Sensor Test");
|
|
Serial.println("=========================");
|
|
Serial.print("PIN MODES: ");
|
|
pinMode(MULTIPLEX_PINA, OUTPUT);
|
|
pinMode(MULTIPLEX_PINB, OUTPUT);
|
|
pinMode(MULTIPLEX_PINC, OUTPUT);
|
|
Serial.println("ok");
|
|
|
|
Serial.print("WIFI: ");
|
|
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
|
|
|
|
int attempts = 0;
|
|
while (WiFi.status() != WL_CONNECTED) {
|
|
delay(500);
|
|
Serial.print(".");
|
|
attempts++;
|
|
if(attempts > 25)
|
|
break;
|
|
}
|
|
if(WiFi.status() == WL_CONNECTED) {
|
|
Serial.print("ok, IP: ");
|
|
Serial.println(WiFi.localIP());
|
|
}
|
|
else {
|
|
// Ref https://arduino-esp8266.readthedocs.io/en/latest/esp8266wifi/readme.html#check-return-codes
|
|
Serial.print("failed, error code ");
|
|
Serial.println(WiFi.status());
|
|
}
|
|
|
|
Serial.print("MQTT: ");
|
|
mqtt.setClient(transport);
|
|
mqtt.setServer(MQTT_SERVER, MQTT_PORT);
|
|
if(!mqtt.connect(
|
|
("autoplant" + String(random(0, 999999))).c_str(),
|
|
MQTT_USERNAME, MQTT_PASSWORD)
|
|
) {
|
|
Serial.println("failed!");
|
|
while(true) delay(10000);
|
|
}
|
|
Serial.println("ok");
|
|
}
|
|
|
|
void loop() {
|
|
float soil_a = read_analog(SOIL_A);
|
|
float soil_b = read_analog(SOIL_B);
|
|
float water_level_a = read_analog(WATER_LEVEL_A);
|
|
float water_level_b = read_analog(WATER_LEVEL_B);
|
|
|
|
StaticJsonDocument<96> data;
|
|
|
|
if(!send_mqtt(
|
|
"autoplant-a",
|
|
"soil",
|
|
soil_a
|
|
)) Serial.println("[mqtt] Failed to send message");
|
|
if(!send_mqtt(
|
|
"autoplant-a",
|
|
"water-level",
|
|
water_level_a
|
|
)) Serial.println("[mqtt] Failed to send message");
|
|
if(!send_mqtt(
|
|
"autoplant-b",
|
|
"soil",
|
|
soil_b
|
|
)) Serial.println("[mqtt] Failed to send message");
|
|
if(!send_mqtt(
|
|
"autoplant-b",
|
|
"water-level",
|
|
water_level_b
|
|
)) Serial.println("[mqtt] Failed to send message");
|
|
|
|
// data["id"] = "autoplant-a";
|
|
// data["sensor"] = "soil";
|
|
// data["value"] = soil_a;
|
|
// if(!send_mqtt(&data)) Serial.println("[mqtt] Failed to send message");
|
|
//
|
|
// data["sensor"] = "water-level";
|
|
// data["value"] = water_level_a;
|
|
// if(!send_mqtt(&data)) Serial.println("[mqtt] Failed to send message");
|
|
//
|
|
// data["id"] = "autoplant-b";
|
|
// data["value"] = water_level_a;
|
|
// if(!send_mqtt(&data)) Serial.println("[mqtt] Failed to send message");
|
|
//
|
|
// data["sensor"] = "soil";
|
|
// data["value"] = soil_a;
|
|
// if(!send_mqtt(&data)) Serial.println("[mqtt] Failed to send message");
|
|
|
|
Serial.print("SOIL_A\t");
|
|
Serial.println(soil_a);
|
|
Serial.print("SOIL_B\t");
|
|
Serial.println(soil_b);
|
|
Serial.print("WATER_LEVEL_A\t");
|
|
Serial.println(water_level_a);
|
|
Serial.print("WATER_LEVEL_B\t");
|
|
Serial.println(water_level_b);
|
|
|
|
Serial.println("\n\n\n\n\n");
|
|
|
|
delay(INTERVAL);
|
|
}
|