Use a horrible hack to get it working

This commit is contained in:
Starbeamrainbowlabs 2021-11-28 14:19:46 +00:00
parent 25afba6819
commit f2d37de5fb
Signed by: sbrl
GPG key ID: 1BE5172E637709C2
2 changed files with 35 additions and 17 deletions

View file

@ -1,7 +1,7 @@
#include <bitset> #include <bitset>
#include <string> #include <string>
#include <ArduinoJson.h> // #include <ArduinoJson.h>
#include <ESP8266WiFi.h> #include <ESP8266WiFi.h>
#include "./lib/pubsubclient/PubSubClient.h" #include "./lib/pubsubclient/PubSubClient.h"
#include "./lib/pubsubclient/PubSubClient.cpp" #include "./lib/pubsubclient/PubSubClient.cpp"
@ -56,19 +56,20 @@ float read_analog(int channel) {
return parsedValue; return parsedValue;
} }
bool send_mqtt(char* id, char* sensor, float value) { bool send_mqtt(String id, String sensor, float value) {
StaticJsonDocument<96> data; // StaticJsonDocument<96> data;
//
// data["id"] = id;
// data["sensor"] = sensor;
// data["value"] = value;
data["id"] = id; String payload = "{\"id\": \""+id+"\", \"sensor\": \""+sensor+"\", \"value\": "+String(value)+"}";
data["sensor"] = sensor; // int length = measureJson(&data);
data["value"] = value; // char* payload = new char[length]();
// serializeJson(data, payload, length);
int length = measureJson(&data); bool result = mqtt.publish("sensors/data", payload.c_str());
char* payload = new char[length](); // delete[] payload;
serializeJson(data, payload, length);
bool result = mqtt.publish("sensors/data", payload, length);
delete[] payload;
return result; return result;
} }
@ -84,6 +85,9 @@ void setup() {
pinMode(MULTIPLEX_PINC, OUTPUT); pinMode(MULTIPLEX_PINC, OUTPUT);
Serial.println("ok"); 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: "); Serial.print("WIFI: ");
WiFi.begin(WIFI_SSID, WIFI_PASSWORD); WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
@ -106,13 +110,27 @@ void setup() {
} }
Serial.print("MQTT: "); Serial.print("MQTT: ");
mqtt.setClient(transport); transport.setInsecure(); Serial.print(".");
mqtt.setServer(MQTT_SERVER, MQTT_PORT); mqtt.setClient(transport); Serial.print(".");
mqtt.setServer(MQTT_SERVER, MQTT_PORT); Serial.print(".");
if(!mqtt.connect( if(!mqtt.connect(
("autoplant" + String(random(0, 999999))).c_str(), ("autoplant" + String(random(0, 999999))).c_str(),
MQTT_USERNAME, MQTT_PASSWORD) 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); while(true) delay(10000);
} }
Serial.println("ok"); Serial.println("ok");
@ -124,7 +142,7 @@ void loop() {
float water_level_a = read_analog(WATER_LEVEL_A); float water_level_a = read_analog(WATER_LEVEL_A);
float water_level_b = read_analog(WATER_LEVEL_B); float water_level_b = read_analog(WATER_LEVEL_B);
StaticJsonDocument<96> data; // StaticJsonDocument<96> data;
if(!send_mqtt( if(!send_mqtt(
"autoplant-a", "autoplant-a",

View file

@ -1,7 +1,7 @@
#define WIFI_SSID "CHANGE_ME" #define WIFI_SSID "CHANGE_ME"
#define WIFI_PASSWORD "CHANGE_ME" #define WIFI_PASSWORD "CHANGE_ME"
#define MQTT_SERVER "mqtt.mooncarrot.space" #define MQTT_SERVER "mqtt.example.com"
// 1883 = unencrypted, 8883 = encrypted // 1883 = unencrypted, 8883 = encrypted
// TODO: Support MQTTS - ref https://github.com/knolleary/pubsubclient/issues/574#issuecomment-464528162 // TODO: Support MQTTS - ref https://github.com/knolleary/pubsubclient/issues/574#issuecomment-464528162
#define MQTT_PORT "8883" #define MQTT_PORT "8883"