Use a horrible hack to get it working
This commit is contained in:
parent
25afba6819
commit
f2d37de5fb
2 changed files with 35 additions and 17 deletions
|
@ -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",
|
||||
|
|
|
@ -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"
|
||||
|
|
Loading…
Reference in a new issue