Merge branch 'main' of git.starbeamrainbowlabs.com:sbrl/autoplant into main

This commit is contained in:
Starbeamrainbowlabs 2022-02-03 18:34:23 +00:00
commit 3eb49a813e
Signed by: sbrl
GPG Key ID: 1BE5172E637709C2
3 changed files with 89 additions and 16 deletions

View File

@ -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;

View File

@ -2,7 +2,21 @@
#include <string>
// #include <ArduinoJson.h>
#include <ESP8266WiFi.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
#ifdef MQTT_NO_TLS
#include <WiFiClient.h>
#else
#include <WiFiClientSecure.h>
#endif
#include "./lib/pubsubclient/PubSubClient.h"
#include "./lib/pubsubclient/PubSubClient.cpp"
@ -13,11 +27,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
@ -40,11 +57,42 @@
// 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
void connect_wifi() {
#ifndef WIFI_ENTERPRISE_ENABLED
Serial.print("personal");
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
#else
Serial.print("enterprise");
// 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 *)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
}
float read_analog(int channel) {
const char* bin = std::bitset<8>(channel).to_string().c_str();
@ -96,7 +144,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) {
@ -117,11 +166,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

View File

@ -1,13 +1,26 @@
// 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"
#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"
// 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