autoplant: implement mqtt, but it's not tested yet.

We anticipate most of our trouble will probably be in getting tls to 
work.....
This commit is contained in:
Starbeamrainbowlabs 2021-11-20 18:52:55 +00:00
parent 4bae1e0fbb
commit 3897e8aeb4
Signed by: sbrl
GPG key ID: 1BE5172E637709C2
4 changed files with 95 additions and 7 deletions

3
.gitmodules vendored Normal file
View file

@ -0,0 +1,3 @@
[submodule "lib/pubsubclient"]
path = lib/pubsubclient
url = https://github.com/knolleary/pubsubclient.git

View file

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

View file

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

1
lib/pubsubclient Submodule

@ -0,0 +1 @@
Subproject commit 2d228f2f862a95846c65a8518c79f48dfc8f188c