From 3897e8aeb44c20f02fcf2a37927f374bd52520ec Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
Date: Sat, 20 Nov 2021 18:52:55 +0000
Subject: [PATCH] autoplant: implement mqtt, but it's not tested yet. We
 anticipate most of our trouble will probably be in getting tls to work.....

---
 .gitmodules                  |  3 ++
 autoplant/autoplant.ino      | 87 +++++++++++++++++++++++++++++++++---
 autoplant/settings.h.example | 11 +++++
 lib/pubsubclient             |  1 +
 4 files changed, 95 insertions(+), 7 deletions(-)
 create mode 100644 .gitmodules
 create mode 160000 lib/pubsubclient

diff --git a/.gitmodules b/.gitmodules
new file mode 100644
index 0000000..e5239cb
--- /dev/null
+++ b/.gitmodules
@@ -0,0 +1,3 @@
+[submodule "lib/pubsubclient"]
+	path = lib/pubsubclient
+	url = https://github.com/knolleary/pubsubclient.git
diff --git a/autoplant/autoplant.ino b/autoplant/autoplant.ino
index b8b8a32..2d2b422 100644
--- a/autoplant/autoplant.ino
+++ b/autoplant/autoplant.ino
@@ -1,6 +1,13 @@
 #include <bitset>
 #include <string>
 
+#include <ArduinoJson.h>
+#include <ESP8266WiFi.h>
+#include "../lib/pubsubclient/src/PubSubClient.h"
+#include "../lib/pubsubclient/src/PubSubClient.cpp"
+
+#include "settings.h"
+
 #define MULTIPLEX_PINA	D5
 #define MULTIPLEX_PINB	D6
 #define MULTIPLEX_PINC	D7
@@ -31,7 +38,8 @@ int soilPin = D3;
 
 float referenceVoltage = 3.3;
 
-
+WiFiClientSecure transport;
+PubSubClient mqtt;
 
 float read_analog(int channel) {
 	
@@ -51,26 +59,91 @@ float read_analog(int channel) {
 	return parsedValue;
 }
 
+bool send_mqtt(StaticJsonDocument data) {
+	int length = measureJson(data);
+	char[length] payload;
+	serialiseJson(data, &payload, length);
+	
+	return rmqtt.publish("sensors/data", payload, length)
+}
+
 void setup() {
 	Serial.begin(9600);
-	Serial.println("Analogue Soil Sensor Test");
+	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("autoplant" + String(random(0, 999999)));
+	if(!mqtt.connect(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;
+	
+	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(read_analog(SOIL_A));
+	Serial.println(soil_a);
 	Serial.print("SOIL_B\t");
-	Serial.println(read_analog(SOIL_B));
+	Serial.println(soil_b);
 	Serial.print("WATER_LEVEL_A\t");
-	Serial.println(read_analog(WATER_LEVEL_A));
+	Serial.println(water_level_a);
 	Serial.print("WATER_LEVEL_B\t");
-	Serial.println(read_analog(WATER_LEVEL_B));
+	Serial.println(water_level_b);
 	
 	Serial.println("\n\n\n\n\n");
 	
-	delay(5000);
+	delay(INTERVAL);
 }
diff --git a/autoplant/settings.h.example b/autoplant/settings.h.example
index dbee210..5cd6db6 100644
--- a/autoplant/settings.h.example
+++ b/autoplant/settings.h.example
@@ -1,2 +1,13 @@
 #define WIFI_SSID		"CHANGE_ME"
 #define WIFI_PASSWORD	"CHANGE_ME"
+
+#define MQTT_SERVER		"mqtt.mooncarrot.space"
+// 1883 = unencrypted, 8883 = encrypted
+// TODO: Support MQTTS - ref https://github.com/knolleary/pubsubclient/issues/574#issuecomment-464528162
+#define MQTT_PORT		"8883"
+#define MQTT_USERNAME	"autoplant"
+#define MQTT_PASSWORD	"CHANGE_ME"
+
+
+
+#define INTERVAL		5*60*1000
diff --git a/lib/pubsubclient b/lib/pubsubclient
new file mode 160000
index 0000000..2d228f2
--- /dev/null
+++ b/lib/pubsubclient
@@ -0,0 +1 @@
+Subproject commit 2d228f2f862a95846c65a8518c79f48dfc8f188c
