pH value in domoticz with MySensors

Hi,

I try to display the pH value of my water aquarium in my home automation application: "Domoticz".
I already use MySensors probes which communicate with Domoticz via a radio chip.
There is an example code on MySensors but I do not know programming:

/*
 * The MySensors Arduino library handles the wireless radio link and protocol
 * between your home built sensors/actuators and HA controller of choice.
 * The sensors forms a self healing radio network with optional repeaters. Each
 * repeater and gateway builds a routing tables in EEPROM which keeps track of the
 * network topology allowing messages to be routed to nodes.
 *
 * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
 * Copyright (C) 2013-2019 Sensnology AB
 * Full contributor list: https://github.com/mysensors/MySensors/graphs/contributors
 *
 * Documentation: http://www.mysensors.org
 * Support Forum: http://forum.mysensors.org
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 *
 *******************************
 *
 * REVISION HISTORY
 * Version 1.0 - mboyer85
 *
 * DESCRIPTION
 * Example sketch showing how to send PH readings back to the controller
 */

// Enable debug prints to serial monitor
//#define MY_DEBUG

// Enable and select radio type attached
#define MY_RADIO_RF24
//#define MY_RADIO_NRF5_ESB
//#define MY_RADIO_RFM69
//#define MY_RADIO_RFM95

#include <MySensors.h>

#define COMPARE_PH 1 // Send PH only if changed? 1 = Yes 0 = No

uint32_t SLEEP_TIME = 60000; // Sleep time between reads (in milliseconds)
float lastPH;
bool receivedConfig = false;
bool metric = true;
// Initialize PH message
MyMessage msg(0, V_PH);

void setup()
{
	//Setup your PH sensor here (I2C,Serial,Phidget...)
}

float getPH()
{
	//query your PH sensor here (I2C,Serial,Phidget...)
	float dummy = 7;
	return dummy;
}

void presentation()
{
	// Send the sketch version information to the gateway and Controller
	sendSketchInfo("PH Sensor", "1.1");
	present(0, S_WATER_QUALITY);

}

void loop()
{
	float ph = getPH();

#if COMPARE_PH == 1
	if (lastPH != ph) {
#endif

		// Send in the new PH value
		send(msg.set(ph, 1));
		// Save new PH value for next compare
		lastPH = ph;

#if COMPARE_PH == 1
	}
#endif
	sleep(SLEEP_TIME);
}

I also found an interesting article but which does not communicate with domoticz : pH Arduino

Could a specialist help me with this project?

I would like to use this kind of kit: KIT pH

Thank you.

Hi,

A professional could verify this code below.

I combine two codes:

n°1 in order to measure pH:

video : PH Mètre avec un ESP32 et le module PH DFRobot (SEN0161-V2) - YouTube

n°2 in order to send value to my Home automation system Domoticz with API:
https://projetsdiy.fr/esp8266-client-web-envoyer-donnees-domoticz-tcpip-api-json/

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <DHT.h>
#include <Adafruit_BMP085.h>

#define DHTTYPE   DHT22       // DHT type (DHT11, DHT22)
#define DHTPIN    D4          // Broche du DHT / DHT Pin

const char* ssid     = "XXXXXXXX";
const char* password = "XXXXXXXX";
const char* host = "XXX.XXX.XXX.XXX";
const int   port = 8080;
const int   watchdog = 60000; // Fréquence d'envoi des données à Domoticz - Frequency of sending data to Domoticz
unsigned long previousMillis = millis(); 

DHT dht(DHTPIN, DHTTYPE);
Adafruit_BMP085 bmp;
HTTPClient http;

void setup() {
  Serial.begin(115200);
  delay(10);
  
  if ( !bmp.begin() ) {
    Serial.println("BMP180 KO!");
    while (1);
  } else {
    Serial.println("BMP180 OK");
  }
  
  Serial.setDebugOutput(true);  
  Serial.println("Connecting Wifi...");

  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
   
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.print(WiFi.localIP()); 
}

int value = 0;

void loop() {
  unsigned long currentMillis = millis();

  if ( currentMillis - previousMillis > watchdog ) {
    previousMillis = currentMillis;

    if(WiFi.status() != WL_CONNECTED) {
      Serial.println("WiFi not connected !");
    } else {  
      Serial.println("Send data to Domoticz");
      
      float t = dht.readTemperature();
      float h = dht.readHumidity();
      float pa = bmp.readPressure() / 100.0F;
      
      if ( isnan(t) || isnan(h) ) {
        Serial.println("DHT KO");
      } else {
        int hum_stat;
        int bar_for = 0;
        if ( h > 70 ) {
          hum_stat = 3;
        } else if ( h < 30 ) {
          hum_stat = 2; 
        } else if ( h >= 30 & h <= 45 ) {
          hum_stat = 0;
        } else if ( h > 45 & h <= 70 ) {
          hum_stat = 1;
        }

        if ( pa > 1030 ) {
          bar_for = 1;  
        } else if ( pa > 1010 & pa <= 1030 ) {
          bar_for = 2;
        } else if ( pa > 990 & pa <= 1010 ) {
          bar_for = 3;
        } else if ( pa > 970 & pa < 990 ) {
          bar_for = 4;
        }
        
        String url = "/json.htm?type=command&param=udevice&idx=12&nvalue=0&svalue=";
        url += String(t); url += ";";
        url += String(h); url += ";";
        url += String(hum_stat); url += ";";
        url += String(pa);url += ";";
        url += String(bar_for);
     
        sendDomoticz(url);
      }
    }
  }
}

void sendDomoticz(String url){
  Serial.print("connecting to ");
  Serial.println(host);
  Serial.print("Requesting URL: ");
  Serial.println(url);
  http.begin(host,port,url);
  int httpCode = http.GET();
    if (httpCode) {
      if (httpCode == 200) {
        String payload = http.getString();
        Serial.println("Domoticz response "); 
        Serial.println(payload);
      }
    }
  Serial.println("closing connection");
  http.end();
}

And my combination to check is:

#include "DFRobot_ESP_PH.h"
#include <EEPROM.h>

DFRobot_ESP_PH ph;
#define ESPADC 4096.0   //the esp Analog Digital Convertion value
#define ESPVOLTAGE 3300 //the esp voltage supply value
#define PH_PIN 35    //the esp gpio data pin number
float voltage, phValue, temperature = 25;

//connection wifi ESP32
void initWiFi() {
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  Serial.print("Connecting to WiFi ..");
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print('.');
    delay(1000);
  }
  Serial.println(WiFi.localIP());
}

// Replace with your network credentials
const char* ssid = "REPLACE_WITH_YOUR_SSID";
const char* password = "REPLACE_WITH_YOUR_PASSWORD";


void setup()
{
  Serial.begin(115200);
  EEPROM.begin(32);//needed to permit storage of calibration value in eeprom
  ph.begin();
}

void loop()
{
  static unsigned long timepoint = millis();
  if (millis() - timepoint > 1000U) //time interval: 1s
  {
    timepoint = millis();
    //voltage = rawPinValue / esp32ADC * esp32Vin
    voltage = analogRead(PH_PIN) / ESPADC * ESPVOLTAGE; // 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);
  }
  ph.calibration(voltage, temperature); // calibration process by Serail CMD

   String url = "/json.htm?type=command&param=udevice&idx=12&nvalue=0&svalue=";
   url += String(phValue);
     
   sendDomoticz(url);

}


void sendDomoticz(String url){
  Serial.print("connecting to ");
  Serial.println(host);
  Serial.print("Requesting URL: ");
  Serial.println(url);
  http.begin(host,port,url);
  int httpCode = http.GET();
    if (httpCode) {
      if (httpCode == 200) {
        String payload = http.getString();
        Serial.println("Domoticz response "); 
        Serial.println(payload);
      }
    }
  Serial.println("closing connection");
  http.end();
  }

float readTemperature()
{
  //add your code here to get the temperature from your temperature sensor
}

If you do not know programing you will either have to get lucky find what you want, have a friend write it for you, pay somebody to write it, go online and learn programming or abandon it. Start by just getting the sensor to work then expand from there.

that is not a very encouraging message. I just tried to understand the code, but I need help figuring out if I'm on the right track.

I am sorry but there is no one simple answer to your question. It appears you have read something that sounds good and picked up a few terms without any understanding of what each part of the equation is. Try breaking it down part by part and do some online tutorials on that and ask questions to help clarify what you have learned. As far as getting a specialist, most of us are hobbyist, and some professionals doing our best to help. We are all over the world, not in one big thinktank.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.