From f2d37de5fbb1094061e9c5e732a7031d4a8f54f6 Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
Date: Sun, 28 Nov 2021 14:19:46 +0000
Subject: [PATCH] Use a horrible hack to get it working

---
 autoplant/autoplant.ino      | 50 ++++++++++++++++++++++++------------
 autoplant/settings.h.example |  2 +-
 2 files changed, 35 insertions(+), 17 deletions(-)

diff --git a/autoplant/autoplant.ino b/autoplant/autoplant.ino
index 7f1ba12..c93da43 100644
--- a/autoplant/autoplant.ino
+++ b/autoplant/autoplant.ino
@@ -1,7 +1,7 @@
 #include <bitset>
 #include <string>
 
-#include <ArduinoJson.h>
+// #include <ArduinoJson.h>
 #include <ESP8266WiFi.h>
 #include "./lib/pubsubclient/PubSubClient.h"
 #include "./lib/pubsubclient/PubSubClient.cpp"
@@ -56,19 +56,20 @@ float read_analog(int channel) {
 	return parsedValue;
 }
 
-bool send_mqtt(char* id, char* sensor, float value) {
-	StaticJsonDocument<96> data;
+bool send_mqtt(String id, String sensor, float value) {
+	// StaticJsonDocument<96> data;
+	// 
+	// data["id"] = id;
+	// data["sensor"] = sensor;
+	// data["value"] = value;
 	
-	data["id"] = id;
-	data["sensor"] = sensor;
-	data["value"] = value;
+	String payload = "{\"id\": \""+id+"\", \"sensor\": \""+sensor+"\", \"value\": "+String(value)+"}";
+	// int length = measureJson(&data);
+	// char* payload = new char[length]();
+	// serializeJson(data, payload, length);
 	
-	int length = measureJson(&data);
-	char* payload = new char[length]();
-	serializeJson(data, payload, length);
-	
-	bool result = mqtt.publish("sensors/data", payload, length);
-	delete[] payload;
+	bool result = mqtt.publish("sensors/data", payload.c_str());
+	// delete[] payload;
 	
 	return result;
 }
@@ -84,6 +85,9 @@ void setup() {
 	pinMode(MULTIPLEX_PINC, OUTPUT);
 	Serial.println("ok");
 	
+	// Enterprise WiFi example:
+	// https://gist.github.com/Matheus-Garbelini/2cd780aed2eddbe17eb4adb5eca42bd6
+	// TODO Add support for enterprise WiFi here so we can connect to eduroam
 	Serial.print("WIFI: ");
 	WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
 	
@@ -106,13 +110,27 @@ void setup() {
 	}
 	
 	Serial.print("MQTT: ");
-	mqtt.setClient(transport);
-	mqtt.setServer(MQTT_SERVER, MQTT_PORT);
+	transport.setInsecure(); Serial.print(".");
+	mqtt.setClient(transport); Serial.print(".");
+	mqtt.setServer(MQTT_SERVER, MQTT_PORT); Serial.print(".");
 	if(!mqtt.connect(
 		("autoplant" + String(random(0, 999999))).c_str(),
 		MQTT_USERNAME, MQTT_PASSWORD)
 	) {
-		Serial.println("failed!");
+		/* Ref https://pubsubclient.knolleary.net/api#state
+		-4 : MQTT_CONNECTION_TIMEOUT - the server didn't respond within the keepalive time
+		-3 : MQTT_CONNECTION_LOST - the network connection was broken
+		-2 : MQTT_CONNECT_FAILED - the network connection failed
+		-1 : MQTT_DISCONNECTED - the client is disconnected cleanly
+		0 : MQTT_CONNECTED - the client is connected
+		1 : MQTT_CONNECT_BAD_PROTOCOL - the server doesn't support the requested version of MQTT
+		2 : MQTT_CONNECT_BAD_CLIENT_ID - the server rejected the client identifier
+		3 : MQTT_CONNECT_UNAVAILABLE - the server was unable to accept the connection
+		4 : MQTT_CONNECT_BAD_CREDENTIALS - the username/password were rejected
+		5 : MQTT_CONNECT_UNAUTHORIZED - the client was not authorized to connect
+		*/
+		int error_code = mqtt.state();
+		Serial.println("failed, error code "+String(error_code));
 		while(true) delay(10000);
 	}
 	Serial.println("ok");
@@ -124,7 +142,7 @@ void loop() {
 	float water_level_a = read_analog(WATER_LEVEL_A);
 	float water_level_b = read_analog(WATER_LEVEL_B);
 	
-	StaticJsonDocument<96> data;
+	// StaticJsonDocument<96> data;
 	
 	if(!send_mqtt(
 		"autoplant-a",
diff --git a/autoplant/settings.h.example b/autoplant/settings.h.example
index 5cd6db6..4a41a79 100644
--- a/autoplant/settings.h.example
+++ b/autoplant/settings.h.example
@@ -1,7 +1,7 @@
 #define WIFI_SSID		"CHANGE_ME"
 #define WIFI_PASSWORD	"CHANGE_ME"
 
-#define MQTT_SERVER		"mqtt.mooncarrot.space"
+#define MQTT_SERVER		"mqtt.example.com"
 // 1883 = unencrypted, 8883 = encrypted
 // TODO: Support MQTTS - ref https://github.com/knolleary/pubsubclient/issues/574#issuecomment-464528162
 #define MQTT_PORT		"8883"
