From add6d2fd1e04e2ad5515f8ed1858837ba307f462 Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
Date: Tue, 14 Dec 2021 15:31:50 +0000
Subject: [PATCH 1/7] Update settings to add enterprise wifi support

....now we just need to update the firmware itself....
---
 autoplant/settings.h.example | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/autoplant/settings.h.example b/autoplant/settings.h.example
index c87ad3f..a582612 100644
--- a/autoplant/settings.h.example
+++ b/autoplant/settings.h.example
@@ -1,13 +1,23 @@
+// The SSID to connect to
 #define WIFI_SSID		"CHANGE_ME"
+// The password to use to connect
 #define WIFI_PASSWORD	"CHANGE_ME"
 
+// Enterprise authentication. Uncomment to enable.
+// #define WIFI_ENTERPRISE_ENABLED
+// #define WIFI_ENTERPRISE_USERNAME	"CHANGE_ME"
+// #define WIFI_ENTERPRISE_PASSWORD	"CHANGE_ME"
+
+// The domain/IP of the MQTTS server
 #define MQTT_SERVER		"mqtt.example.com"
+// The port to connect to.
 // 1883 = unencrypted, 8883 = encrypted
 // Connections are encrypted by default. There isn't currently a way to disable it.
 #define MQTT_PORT		"8883"
+// The username to authenticate with when connecting to the MQTT server.
 #define MQTT_USERNAME	"autoplant"
+// The password to authenticate with when connecting to the MQTT server.
 #define MQTT_PASSWORD	"CHANGE_ME"
 
-
-
+// The interval at which messages should be sent to the MQTT server, in seconds
 #define INTERVAL		5*60*1000

From 68cd7ac8a80ae06629505cf2a8a07fc650fd85cd Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
Date: Tue, 14 Dec 2021 15:35:51 +0000
Subject: [PATCH 2/7] fixup

---
 autoplant/settings.h.example | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/autoplant/settings.h.example b/autoplant/settings.h.example
index a582612..5dcb86c 100644
--- a/autoplant/settings.h.example
+++ b/autoplant/settings.h.example
@@ -13,7 +13,7 @@
 // The port to connect to.
 // 1883 = unencrypted, 8883 = encrypted
 // Connections are encrypted by default. There isn't currently a way to disable it.
-#define MQTT_PORT		"8883"
+#define MQTT_PORT		8883
 // The username to authenticate with when connecting to the MQTT server.
 #define MQTT_USERNAME	"autoplant"
 // The password to authenticate with when connecting to the MQTT server.

From 3d42f3270bdd19526b8e53bf116323198ca6362b Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
Date: Tue, 14 Dec 2021 15:53:49 +0000
Subject: [PATCH 3/7] autoplant: implement enterprise wifi support

---
 autoplant/autoplant.ino | 49 ++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 48 insertions(+), 1 deletion(-)

diff --git a/autoplant/autoplant.ino b/autoplant/autoplant.ino
index 10df9e3..103ec9d 100644
--- a/autoplant/autoplant.ino
+++ b/autoplant/autoplant.ino
@@ -45,6 +45,52 @@ PubSubClient mqtt;
 
 Adafruit_BME280 bme;	// I2C
 
+#ifdef WIFI_ENTERPRISE_ENABLED
+// Ref https://gist.github.com/Matheus-Garbelini/2cd780aed2eddbe17eb4adb5eca42bd6
+extern "C" {
+#include "user_interface.h"
+#include "wpa2_enterprise.h"
+#include "c_types.h"
+}
+#endif
+
+void connect_wifi() {
+	#ifndef WIFI_ENTERPRISE_ENABLED
+	WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
+	#else
+	// Ref https://gist.github.com/Matheus-Garbelini/2cd780aed2eddbe17eb4adb5eca42bd6
+	// Setting ESP into STATION mode only (no AP mode or dual mode)
+	wifi_set_opmode(STATION_MODE);
+
+	struct station_config wifi_config;
+
+	memset(&wifi_config, 0, sizeof(wifi_config));
+	strcpy((char*)wifi_config.ssid, WIFI_SSID);
+	strcpy((char*)wifi_config.password, WIFI_ENTERPRISE_PASSWORD);
+
+	wifi_station_set_config(&wifi_config);
+	// wifi_set_macaddr(STATION_IF,target_esp_mac);
+
+	wifi_station_set_wpa2_enterprise_auth(1);
+
+	// Clean up to be sure no old data is still inside
+	wifi_station_clear_cert_key();
+	wifi_station_clear_enterprise_ca_cert();
+	wifi_station_clear_enterprise_identity();
+	wifi_station_clear_enterprise_username();
+	wifi_station_clear_enterprise_password();
+	wifi_station_clear_enterprise_new_password();
+
+	wifi_station_set_enterprise_identity((uint8*)WIFI_ENTERPRISE_USERNAME, strlen(WIFI_ENTERPRISE_USERNAME));
+	wifi_station_set_enterprise_username((uint8*)WIFI_ENTERPRISE_USERNAME, strlen(WIFI_ENTERPRISE_USERNAME));
+	wifi_station_set_enterprise_password((uint8*)WIFI_ENTERPRISE_PASSWORD, strlen((char*)WIFI_ENTERPRISE_PASSWORD));
+
+
+	wifi_station_connect();
+	#endif
+}
+
+
 float read_analog(int channel) {
 	
 	const char* bin = std::bitset<8>(channel).to_string().c_str();
@@ -96,7 +142,8 @@ void setup() {
 	// 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);
+	
+	connect_wifi();
 	
 	int attempts = 0;
 	while (WiFi.status() != WL_CONNECTED) {

From f6d158236e94b410de250bc591906fc9f1f134e0 Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
Date: Tue, 14 Dec 2021 18:45:45 +0000
Subject: [PATCH 4/7] Add optional notls support

---
 autoplant/autoplant.ino      | 25 +++++++++++++++++++++----
 autoplant/settings.h.example |  3 +++
 2 files changed, 24 insertions(+), 4 deletions(-)

diff --git a/autoplant/autoplant.ino b/autoplant/autoplant.ino
index 103ec9d..40a05dc 100644
--- a/autoplant/autoplant.ino
+++ b/autoplant/autoplant.ino
@@ -40,7 +40,11 @@
 // Ref https://stackoverflow.com/a/523737/1460422
 #define CHECK_BIT(var,pos) ((var) & (1<<(pos)))
 
+#ifndef MQTT_NO_TLS
 WiFiClientSecure transport;
+#else
+WiFiClient transport;
+#endif
 PubSubClient mqtt;
 
 Adafruit_BME280 bme;	// I2C
@@ -55,9 +59,11 @@ extern "C" {
 #endif
 
 void connect_wifi() {
-	#ifndef WIFI_ENTERPRISE_ENABLED
+#ifndef WIFI_ENTERPRISE_ENABLED
+	Serial.print("personal");
 	WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
-	#else
+#else
+	Serial.print("enterprise");
 	// Ref https://gist.github.com/Matheus-Garbelini/2cd780aed2eddbe17eb4adb5eca42bd6
 	// Setting ESP into STATION mode only (no AP mode or dual mode)
 	wifi_set_opmode(STATION_MODE);
@@ -87,7 +93,7 @@ void connect_wifi() {
 
 
 	wifi_station_connect();
-	#endif
+#endif
 }
 
 
@@ -164,11 +170,22 @@ void setup() {
 	}
 	
 	Serial.print("MQTT: ");
+#ifdef MQTT_NO_TLS
+	Serial.print("notls");
+#else
+	Serial.print("tls");
 	transport.setInsecure(); Serial.print(".");
+#endif
+	// mqtt.setBufferSize(1000);
 	mqtt.setClient(transport); Serial.print(".");
 	mqtt.setServer(MQTT_SERVER, MQTT_PORT); Serial.print(".");
+	// Serial.print(MQTT_SERVER);
+	// Serial.print(":");
+	// Serial.print(MQTT_PORT);
+	// Serial.print(".");
+	
 	if(!mqtt.connect(
-		("autoplant" + String(random(0, 999999))).c_str(),
+		("autoplant_" + String(random(0, 999999))).c_str(),
 		MQTT_USERNAME, MQTT_PASSWORD)
 	) {
 		/* Ref https://pubsubclient.knolleary.net/api#state
diff --git a/autoplant/settings.h.example b/autoplant/settings.h.example
index 5dcb86c..57b0155 100644
--- a/autoplant/settings.h.example
+++ b/autoplant/settings.h.example
@@ -18,6 +18,9 @@
 #define MQTT_USERNAME	"autoplant"
 // The password to authenticate with when connecting to the MQTT server.
 #define MQTT_PASSWORD	"CHANGE_ME"
+// Uncomment to disable TLS. NOT RECOMMENED. USE TLS WHEREVER POSSIBLE.
+// PASSWORDS ARE SENT IN PLAIN TEXT WHEN YOUDO NOT USE TLS!!!!!!
+//#define MQTT_NO_TLS
 
 // The interval at which messages should be sent to the MQTT server, in seconds
 #define INTERVAL		5*60*1000

From cfa67aea37dad74fc324ebed8db503028f6d97fe Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
Date: Mon, 10 Jan 2022 14:35:12 +0000
Subject: [PATCH 5/7] =?UTF-8?q?Change=20pin=20numbers=20for=20esp8266=20?=
 =?UTF-8?q?=E2=86=92=20esp32=20change,=20but=20whether=20they=20are=20actu?=
 =?UTF-8?q?ally=20correct=20of=20not=20is=20correctly=20unclear?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

as usual, it's all untested....
---
 analog_channel_scanner/analog_channel_scanner.ino | 12 ++++++------
 autoplant/autoplant.ino                           | 11 +++++++----
 2 files changed, 13 insertions(+), 10 deletions(-)

diff --git a/analog_channel_scanner/analog_channel_scanner.ino b/analog_channel_scanner/analog_channel_scanner.ino
index 2b3b880..04a3cca 100644
--- a/analog_channel_scanner/analog_channel_scanner.ino
+++ b/analog_channel_scanner/analog_channel_scanner.ino
@@ -1,11 +1,13 @@
 #include <bitset>
 #include <string>
 
-#define MULTIPLEX_PINA	D5
-#define MULTIPLEX_PINB	D6
-#define MULTIPLEX_PINC	D7
+// WAS D5 D6 D7
+#define MULTIPLEX_PINA	17
+#define MULTIPLEX_PINB	16
+#define MULTIPLEX_PINC	4
 
-#define ANALOG_PIN		A0
+// WAS A0
+#define ANALOG_PIN		34
 
 /*
 000	0
@@ -27,8 +29,6 @@
 // Ref https://stackoverflow.com/a/523737/1460422
 #define CHECK_BIT(var,pos) ((var) & (1<<(pos)))
 
-int soilPin = D3;
-
 float referenceVoltage = 3.3;
 
 
diff --git a/autoplant/autoplant.ino b/autoplant/autoplant.ino
index 40a05dc..3f24521 100644
--- a/autoplant/autoplant.ino
+++ b/autoplant/autoplant.ino
@@ -13,11 +13,14 @@
 
 #include "settings.h"
 
-#define MULTIPLEX_PINA	D5
-#define MULTIPLEX_PINB	D6
-#define MULTIPLEX_PINC	D7
+// WAS D5 D6 D7
 
-#define ANALOG_PIN		A0
+#define MULTIPLEX_PINA	17
+#define MULTIPLEX_PINB	16
+#define MULTIPLEX_PINC	4
+
+// WAS A0
+#define ANALOG_PIN		34
 
 /*
 000	0

From c5fcf9011956c41f499563f6d24d2afdb4710ff1 Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
Date: Tue, 18 Jan 2022 18:51:56 +0000
Subject: [PATCH 6/7] autoplant: upgrade to esp32, but enterprise wifi isn't
 working right.

---
 autoplant/autoplant.ino | 64 ++++++++++++++++-------------------------
 1 file changed, 25 insertions(+), 39 deletions(-)

diff --git a/autoplant/autoplant.ino b/autoplant/autoplant.ino
index 3f24521..dd7d1ec 100644
--- a/autoplant/autoplant.ino
+++ b/autoplant/autoplant.ino
@@ -2,7 +2,20 @@
 #include <string>
 
 // #include <ArduinoJson.h>
-#include <ESP8266WiFi.h>
+
+#ifdef WIFI_ENTERPRISE_ENABLED
+// Ref https://github.com/martinius96/ESP32-WPA2-enterprise/blob/master/ESP32_WPA2enterprise.ino
+#include "esp_wpa2.h"
+#include "esp_wifi.h"
+#endif
+
+#include <WiFi.h>
+
+#ifdef MQTT_NO_TLS
+#include <WiFiClient.h>
+#else
+#include <WiFiClientSecure.h>
+#endif
 
 #include "./lib/pubsubclient/PubSubClient.h"
 #include "./lib/pubsubclient/PubSubClient.cpp"
@@ -52,50 +65,23 @@ PubSubClient mqtt;
 
 Adafruit_BME280 bme;	// I2C
 
-#ifdef WIFI_ENTERPRISE_ENABLED
-// Ref https://gist.github.com/Matheus-Garbelini/2cd780aed2eddbe17eb4adb5eca42bd6
-extern "C" {
-#include "user_interface.h"
-#include "wpa2_enterprise.h"
-#include "c_types.h"
-}
-#endif
-
 void connect_wifi() {
 #ifndef WIFI_ENTERPRISE_ENABLED
 	Serial.print("personal");
 	WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
 #else
 	Serial.print("enterprise");
-	// Ref https://gist.github.com/Matheus-Garbelini/2cd780aed2eddbe17eb4adb5eca42bd6
-	// Setting ESP into STATION mode only (no AP mode or dual mode)
-	wifi_set_opmode(STATION_MODE);
-
-	struct station_config wifi_config;
-
-	memset(&wifi_config, 0, sizeof(wifi_config));
-	strcpy((char*)wifi_config.ssid, WIFI_SSID);
-	strcpy((char*)wifi_config.password, WIFI_ENTERPRISE_PASSWORD);
-
-	wifi_station_set_config(&wifi_config);
-	// wifi_set_macaddr(STATION_IF,target_esp_mac);
-
-	wifi_station_set_wpa2_enterprise_auth(1);
-
-	// Clean up to be sure no old data is still inside
-	wifi_station_clear_cert_key();
-	wifi_station_clear_enterprise_ca_cert();
-	wifi_station_clear_enterprise_identity();
-	wifi_station_clear_enterprise_username();
-	wifi_station_clear_enterprise_password();
-	wifi_station_clear_enterprise_new_password();
-
-	wifi_station_set_enterprise_identity((uint8*)WIFI_ENTERPRISE_USERNAME, strlen(WIFI_ENTERPRISE_USERNAME));
-	wifi_station_set_enterprise_username((uint8*)WIFI_ENTERPRISE_USERNAME, strlen(WIFI_ENTERPRISE_USERNAME));
-	wifi_station_set_enterprise_password((uint8*)WIFI_ENTERPRISE_PASSWORD, strlen((char*)WIFI_ENTERPRISE_PASSWORD));
-
-
-	wifi_station_connect();
+	
+	// Ref https://github.com/martinius96/ESP32-WPA2-enterprise/blob/master/ESP32_WPA2enterprise.ino
+	// For ESP32 ONLY! For esp8266, see the code with the esp8266-last tag, which is the last commit that supports the esp8266.
+	WiFi.disconnect(true);
+	// 'esp_wifi_sta_wpa2_ent_set_identity' was not declared in this scope :-(
+    esp_wifi_sta_wpa2_ent_set_identity((uint8_t *)WIFI_ENTERPRISE_USERNAME, strlen(WIFI_ENTERPRISE_USERNAME));
+    esp_wifi_sta_wpa2_ent_set_username((uint8_t *)WIFI_ENTERPRISE_USERNAME, strlen(WIFI_ENTERPRISE_USERNAME));
+    esp_wifi_sta_wpa2_ent_set_password((uint8_t *)WIFI_ENTERPRISE_PASSWORD, strlen(WIFI_ENTERPRISE_PASSWORD));
+    esp_wifi_sta_wpa2_ent_enable();
+	
+	WiFi.begin(WIFI_SSID);
 #endif
 }
 

From d4db3ddc349d4e7a88b7458bb04189d944febc4d Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
Date: Fri, 21 Jan 2022 18:41:14 +0000
Subject: [PATCH 7/7] autoplant: attempt to fix crash, but we're just going
 around in circles

We get this crash:

path/to/autoplant/autoplant/autoplant.ino: In function 'void connect_wifi()':
autoplant:85:5: error: 'esp_wifi_sta_wpa2_ent_set_identity' was not declared in this scope
     esp_wifi_sta_wpa2_ent_set_identity((uint8_t *)user, strlen(user));
     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
autoplant:86:5: error: 'esp_wifi_sta_wpa2_ent_set_username' was not declared in this scope
     esp_wifi_sta_wpa2_ent_set_username((uint8_t *)user, strlen(user));
     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
autoplant:87:5: error: 'esp_wifi_sta_wpa2_ent_set_password' was not declared in this scope
     esp_wifi_sta_wpa2_ent_set_password((uint8_t *)pass, strlen(pass));
     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
autoplant:88:2: error: 'esp_wpa2_config_t' was not declared in this scope
  esp_wpa2_config_t config = WPA2_CONFIG_INIT_DEFAULT();
  ^~~~~~~~~~~~~~~~~
path/to/autoplant/autoplant/autoplant.ino:88:2: note: suggested alternative: 'esp_eth_config_t'
  esp_wpa2_config_t config = WPA2_CONFIG_INIT_DEFAULT();
  ^~~~~~~~~~~~~~~~~
  esp_eth_config_t
autoplant:89:32: error: 'config' was not declared in this scope
  esp_wifi_sta_wpa2_ent_enable(&config);
                                ^~~~~~
path/to/autoplant/autoplant/autoplant.ino:89:32: note: suggested alternative: 'confstr'
  esp_wifi_sta_wpa2_ent_enable(&config);
                                ^~~~~~
                                confstr
autoplant:89:2: error: 'esp_wifi_sta_wpa2_ent_enable' was not declared in this scope
  esp_wifi_sta_wpa2_ent_enable(&config);
  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
path/to/autoplant/autoplant/autoplant.ino:89:2: note: suggested alternative: 'gpio_pin_wakeup_enable'
  esp_wifi_sta_wpa2_ent_enable(&config);
  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
  gpio_pin_wakeup_enable
Multiple libraries were found for "WiFi.h"
 Used: /home/USERNAME/.arduino15/packages/esp32/hardware/esp32/2.0.2/libraries/WiFi
 Not used: /home/USERNAME/Downloads/arduino-1.8.16/libraries/WiFi
exit status 1
'esp_wifi_sta_wpa2_ent_set_identity' was not declared in this scope
---
 autoplant/autoplant.ino | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)

diff --git a/autoplant/autoplant.ino b/autoplant/autoplant.ino
index dd7d1ec..bb7fc81 100644
--- a/autoplant/autoplant.ino
+++ b/autoplant/autoplant.ino
@@ -3,13 +3,14 @@
 
 // #include <ArduinoJson.h>
 
+#include <WiFi.h>
+
 #ifdef WIFI_ENTERPRISE_ENABLED
 // Ref https://github.com/martinius96/ESP32-WPA2-enterprise/blob/master/ESP32_WPA2enterprise.ino
 #include "esp_wpa2.h"
 #include "esp_wifi.h"
 #endif
 
-#include <WiFi.h>
 
 #ifdef MQTT_NO_TLS
 #include <WiFiClient.h>
@@ -75,11 +76,17 @@ void connect_wifi() {
 	// Ref https://github.com/martinius96/ESP32-WPA2-enterprise/blob/master/ESP32_WPA2enterprise.ino
 	// For ESP32 ONLY! For esp8266, see the code with the esp8266-last tag, which is the last commit that supports the esp8266.
 	WiFi.disconnect(true);
+	WiFi.mode(WIFI_STA);
+	
+	const char* user = WIFI_ENTERPRISE_USERNAME;
+	const char* pass = WIFI_ENTERPRISE_PASSWORD;
+	
 	// 'esp_wifi_sta_wpa2_ent_set_identity' was not declared in this scope :-(
-    esp_wifi_sta_wpa2_ent_set_identity((uint8_t *)WIFI_ENTERPRISE_USERNAME, strlen(WIFI_ENTERPRISE_USERNAME));
-    esp_wifi_sta_wpa2_ent_set_username((uint8_t *)WIFI_ENTERPRISE_USERNAME, strlen(WIFI_ENTERPRISE_USERNAME));
-    esp_wifi_sta_wpa2_ent_set_password((uint8_t *)WIFI_ENTERPRISE_PASSWORD, strlen(WIFI_ENTERPRISE_PASSWORD));
-    esp_wifi_sta_wpa2_ent_enable();
+    esp_wifi_sta_wpa2_ent_set_identity((uint8_t *)user, strlen(user));
+    esp_wifi_sta_wpa2_ent_set_username((uint8_t *)user, strlen(user));
+    esp_wifi_sta_wpa2_ent_set_password((uint8_t *)pass, strlen(pass));
+	esp_wpa2_config_t config = WPA2_CONFIG_INIT_DEFAULT();
+	esp_wifi_sta_wpa2_ent_enable(&config);
 	
 	WiFi.begin(WIFI_SSID);
 #endif
