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 <ArduinoJson.h>
|
||||||
#include <ESP8266WiFi.h>
|
#include <ESP8266WiFi.h>
|
||||||
#include "../lib/pubsubclient/src/PubSubClient.h"
|
#include "./lib/pubsubclient/PubSubClient.h"
|
||||||
#include "../lib/pubsubclient/src/PubSubClient.cpp"
|
#include "./lib/pubsubclient/PubSubClient.cpp"
|
||||||
|
|
||||||
#include "settings.h"
|
#include "settings.h"
|
||||||
|
|
||||||
|
@ -30,14 +30,11 @@
|
||||||
#define WATER_LEVEL_A 2
|
#define WATER_LEVEL_A 2
|
||||||
#define WATER_LEVEL_B 4
|
#define WATER_LEVEL_B 4
|
||||||
|
|
||||||
|
#define REFERENCE_VOLTAGE 3.3
|
||||||
|
|
||||||
// Ref https://stackoverflow.com/a/523737/1460422
|
// Ref https://stackoverflow.com/a/523737/1460422
|
||||||
#define CHECK_BIT(var,pos) ((var) & (1<<(pos)))
|
#define CHECK_BIT(var,pos) ((var) & (1<<(pos)))
|
||||||
|
|
||||||
int soilPin = D3;
|
|
||||||
|
|
||||||
float referenceVoltage = 3.3;
|
|
||||||
|
|
||||||
WiFiClientSecure transport;
|
WiFiClientSecure transport;
|
||||||
PubSubClient mqtt;
|
PubSubClient mqtt;
|
||||||
|
|
||||||
|
@ -54,17 +51,26 @@ float read_analog(int channel) {
|
||||||
digitalWrite(MULTIPLEX_PINC, b0);
|
digitalWrite(MULTIPLEX_PINC, b0);
|
||||||
|
|
||||||
int rawValue = analogRead(ANALOG_PIN);
|
int rawValue = analogRead(ANALOG_PIN);
|
||||||
float parsedValue = ((float)rawValue) / 1024.0 * referenceVoltage;
|
float parsedValue = ((float)rawValue) / 1024.0 * REFERENCE_VOLTAGE;
|
||||||
|
|
||||||
return parsedValue;
|
return parsedValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool send_mqtt(StaticJsonDocument data) {
|
bool send_mqtt(char* id, char* sensor, float value) {
|
||||||
int length = measureJson(data);
|
StaticJsonDocument<96> data;
|
||||||
char[length] payload;
|
|
||||||
serialiseJson(data, &payload, length);
|
|
||||||
|
|
||||||
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() {
|
void setup() {
|
||||||
|
@ -101,8 +107,11 @@ void setup() {
|
||||||
|
|
||||||
Serial.print("MQTT: ");
|
Serial.print("MQTT: ");
|
||||||
mqtt.setClient(transport);
|
mqtt.setClient(transport);
|
||||||
mqtt.setServer("autoplant" + String(random(0, 999999)));
|
mqtt.setServer(MQTT_SERVER, MQTT_PORT);
|
||||||
if(!mqtt.connect(MQTT_USERNAME, MQTT_PASSWORD)) {
|
if(!mqtt.connect(
|
||||||
|
("autoplant" + String(random(0, 999999))).c_str(),
|
||||||
|
MQTT_USERNAME, MQTT_PASSWORD)
|
||||||
|
) {
|
||||||
Serial.println("failed!");
|
Serial.println("failed!");
|
||||||
while(true) delay(10000);
|
while(true) delay(10000);
|
||||||
}
|
}
|
||||||
|
@ -117,22 +126,43 @@ void loop() {
|
||||||
|
|
||||||
StaticJsonDocument<96> data;
|
StaticJsonDocument<96> data;
|
||||||
|
|
||||||
data["id"] = "autoplant-a";
|
if(!send_mqtt(
|
||||||
data["sensor"] = "soil";
|
"autoplant-a",
|
||||||
data["value"] = soil_a;
|
"soil",
|
||||||
if(!send_mqtt(data)) Serial.println("[mqtt] Failed to send message");
|
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["id"] = "autoplant-a";
|
||||||
data["value"] = water_level_a;
|
// data["sensor"] = "soil";
|
||||||
if(!send_mqtt(data)) Serial.println("[mqtt] Failed to send message");
|
// data["value"] = soil_a;
|
||||||
|
// if(!send_mqtt(&data)) Serial.println("[mqtt] Failed to send message");
|
||||||
data["id"] = "autoplant-b";
|
//
|
||||||
data["value"] = water_level_a;
|
// data["sensor"] = "water-level";
|
||||||
if(!send_mqtt(data)) Serial.println("[mqtt] Failed to send message");
|
// data["value"] = water_level_a;
|
||||||
|
// if(!send_mqtt(&data)) Serial.println("[mqtt] Failed to send message");
|
||||||
data["sensor"] = "soil";
|
//
|
||||||
data["value"] = soil_a;
|
// data["id"] = "autoplant-b";
|
||||||
if(!send_mqtt(data)) Serial.println("[mqtt] Failed to send message");
|
// 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.print("SOIL_A\t");
|
||||||
Serial.println(soil_a);
|
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