Hi,
Thanks for the post above. This was helping me to understand the System interactions.
I was playing last week a bit around. I found "mysensor" and this was very interesting to me. But not was I searching for.
My last programming was BASIC. After getting some understanding of C I change the code. MQTT publishing looks to be done now. It would be easier if the unit is behind the value, I have to check it.
Now I have to understand how to integrate it to HomeServer.
The Code is (at the moment) as follow:
Code: Select all
#include "EspMQTTClient.h"
#include <Arduino.h>
#include "DFRobot_ESP_PH_WITH_ADC.h"
#include "OneWire.h"
#include "DallasTemperature.h"
#include "Adafruit_ADS1015.h"
#include "EEPROM.h"
DFRobot_ESP_PH_WITH_ADC ph;
Adafruit_ADS1115 ads;
#define ONE_WIRE_BUS 15
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
EspMQTTClient client(
"YOUR SSID",
"YOUR PASSWORT",
"IP of Broker", // MQTT Broker server ip
"MQTTUSER", // Can be omitted if not needed
"MQTTpassword", // Can be omitted if not needed
"DEVICE", // Client name that uniquely identify your device
1883 // The MQTT port, default to 1883. this line can be omitted
);
void setup()
{
Serial.begin(115200);
EEPROM.begin(32);//needed EEPROM.begin to store calibration k in eeprom
ph.begin();
sensors.begin();
ads.setGain(GAIN_ONE);
ads.begin();
// Optionnal functionnalities of EspMQTTClient :
client.enableDebuggingMessages(); // Enable debugging messages sent to serial output
client.enableHTTPWebUpdater(); // Enable the web updater. User and password default to values of MQTTUsername and MQTTPassword. These can be overrited with enableHTTPWebUpdater("user", "password").
client.enableLastWillMessage("TestClient/lastwill", "I am going offline"); // You can activate the retain flag by setting the third parameter to true
}
// This function is called once everything is connected (Wifi and MQTT)
// WARNING : YOU MUST IMPLEMENT IT IF YOU USE EspMQTTClient
void onConnectionEstablished()
{
// Subscribe to "mytopic/test" and display received message to Serial
client.subscribe("mytopic/test", [](const String & payload) {
Serial.println(payload);
});
// Subscribe to "mytopic/wildcardtest/#" and display received message to Serial
client.subscribe("mytopic/wildcardtest/#", [](const String & topic, const String & payload) {
Serial.println("(From wildcard) topic: " + topic + ", payload: " + payload);
});
// Publish a message to "mytopic/test"
client.publish("mytopic/test", "This is a message"); // You can activate the retain flag by setting the third parameter to true
// Execute delayed instructions
client.executeDelayed(5 * 1000, []() {
client.publish("mytopic/wildcardtest/test123", "This is a message sent 5 seconds later");
});
}
float voltage, phValue, temperature = 25;
float readTemperature()
{
//add your code here to get the temperature from your temperature sensor
sensors.requestTemperatures();
return sensors.getTempCByIndex(0);
}
void loop()
{
static unsigned long timepoint = millis();
if (millis() - timepoint > 5000U) //time interval: 1s
{
timepoint = millis();
/**
* index 0 for adc's pin A0
* index 1 for adc's pin A1
* index 2 for adc's pin A2
* index 3 for adc's pin A3
*/
voltage = ads.readADC_SingleEnded(1) / 10; // read the voltage
Serial.print("voltage:");
Serial.println(voltage, 4);
temperature = readTemperature(); // read your temperature sensor to execute temperature compensation
Serial.print("temperature:");
Serial.print(temperature, 1);
Serial.println("^C");
phValue = ph.readPH(voltage, temperature); // convert voltage to pH with temperature compensation
Serial.print("pH:");
Serial.println(phValue, 4);
char phwert[8];
dtostrf(phValue, 1, 2, phwert);
char volt[8];
dtostrf(voltage, 1, 2, volt);
char temp[8];
dtostrf(temperature, 1, 2, temp);
client.publish("mytopic/test", volt); // You can activate the retain flag by setting the third parameter to true
client.publish("mytopic/test", phwert); // You can activate the retain flag by setting the third parameter to true
client.publish("mytopic/test", temp); // You can activate the retain flag by setting the third parameter to true
//loop delay ms
const int interval = 500;
}
ph.calibration(voltage, temperature); // calibration process by Serail CMD
client.loop();
}