Fix a bunch of bugs, but we aren't finished yet
This commit is contained in:
parent
3897e8aeb4
commit
25afba6819
3 changed files with 61 additions and 29 deletions
|
@ -3,8 +3,8 @@
|
|||
|
||||
#include <ArduinoJson.h>
|
||||
#include <ESP8266WiFi.h>
|
||||
#include "../lib/pubsubclient/src/PubSubClient.h"
|
||||
#include "../lib/pubsubclient/src/PubSubClient.cpp"
|
||||
#include "./lib/pubsubclient/PubSubClient.h"
|
||||
#include "./lib/pubsubclient/PubSubClient.cpp"
|
||||
|
||||
#include "settings.h"
|
||||
|
||||
|
@ -30,14 +30,11 @@
|
|||
#define WATER_LEVEL_A 2
|
||||
#define WATER_LEVEL_B 4
|
||||
|
||||
#define REFERENCE_VOLTAGE 3.3
|
||||
|
||||
// Ref https://stackoverflow.com/a/523737/1460422
|
||||
#define CHECK_BIT(var,pos) ((var) & (1<<(pos)))
|
||||
|
||||
int soilPin = D3;
|
||||
|
||||
float referenceVoltage = 3.3;
|
||||
|
||||
WiFiClientSecure transport;
|
||||
PubSubClient mqtt;
|
||||
|
||||
|
@ -54,17 +51,26 @@ float read_analog(int channel) {
|
|||
digitalWrite(MULTIPLEX_PINC, b0);
|
||||
|
||||
int rawValue = analogRead(ANALOG_PIN);
|
||||
float parsedValue = ((float)rawValue) / 1024.0 * referenceVoltage;
|
||||
float parsedValue = ((float)rawValue) / 1024.0 * REFERENCE_VOLTAGE;
|
||||
|
||||
return parsedValue;
|
||||
}
|
||||
|
||||
bool send_mqtt(StaticJsonDocument data) {
|
||||
int length = measureJson(data);
|
||||
char[length] payload;
|
||||
serialiseJson(data, &payload, length);
|
||||
bool send_mqtt(char* id, char* sensor, float value) {
|
||||
StaticJsonDocument<96> data;
|
||||
|
||||
return rmqtt.publish("sensors/data", payload, length)
|
||||
data["id"] = id;
|
||||
data["sensor"] = sensor;
|
||||
data["value"] = value;
|
||||
|
||||
int length = measureJson(&data);
|
||||
char* payload = new char[length]();
|
||||
serializeJson(data, payload, length);
|
||||
|
||||
bool result = mqtt.publish("sensors/data", payload, length);
|
||||
delete[] payload;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void setup() {
|
||||
|
@ -101,8 +107,11 @@ void setup() {
|
|||
|
||||
Serial.print("MQTT: ");
|
||||
mqtt.setClient(transport);
|
||||
mqtt.setServer("autoplant" + String(random(0, 999999)));
|
||||
if(!mqtt.connect(MQTT_USERNAME, MQTT_PASSWORD)) {
|
||||
mqtt.setServer(MQTT_SERVER, MQTT_PORT);
|
||||
if(!mqtt.connect(
|
||||
("autoplant" + String(random(0, 999999))).c_str(),
|
||||
MQTT_USERNAME, MQTT_PASSWORD)
|
||||
) {
|
||||
Serial.println("failed!");
|
||||
while(true) delay(10000);
|
||||
}
|
||||
|
@ -117,22 +126,43 @@ void loop() {
|
|||
|
||||
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");
|
||||
if(!send_mqtt(
|
||||
"autoplant-a",
|
||||
"soil",
|
||||
soil_a
|
||||
)) Serial.println("[mqtt] Failed to send message");
|
||||
if(!send_mqtt(
|
||||
"autoplant-a",
|
||||
"water-level",
|
||||
water_level_a
|
||||
)) Serial.println("[mqtt] Failed to send message");
|
||||
if(!send_mqtt(
|
||||
"autoplant-b",
|
||||
"soil",
|
||||
soil_b
|
||||
)) Serial.println("[mqtt] Failed to send message");
|
||||
if(!send_mqtt(
|
||||
"autoplant-b",
|
||||
"water-level",
|
||||
water_level_b
|
||||
)) 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");
|
||||
// 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(soil_a);
|
||||
|
|
1
autoplant/lib/pubsubclient/PubSubClient.cpp
Symbolic link
1
autoplant/lib/pubsubclient/PubSubClient.cpp
Symbolic link
|
@ -0,0 +1 @@
|
|||
../../../lib/pubsubclient/src/PubSubClient.cpp
|
1
autoplant/lib/pubsubclient/PubSubClient.h
Symbolic link
1
autoplant/lib/pubsubclient/PubSubClient.h
Symbolic link
|
@ -0,0 +1 @@
|
|||
../../../lib/pubsubclient/src/PubSubClient.h
|
Loading…
Reference in a new issue