0

I made a pool monitor with ORP + pH + temp sensor. I had everything working (apart from ORP sensor causing interference with pH sensor, but that is a different topic...) for the past couple days in a temporary enclosure. Today I moved the device into a new enclosure. After rewiring it stopped sending out the probe data via MQTT and I'm struggling figuring out why..

If I open the serial monitor for the NodeMCU I can see the data from the Arduino coming through -

{"ph":"11.44","orp":"-23411","temp":"-196.60"}
...

So, it would appear the serial link is working since I'm getting the data. When listening on my MQTT topic, I get a "hello world", so my MQTT and wifi connections appear to be okay. If I type something in the serial console, it DOES get picked up by the MQTT topic. It seems like there is something wrong with the loop sending out the data, but this is the same loop I had and same loop as other sketches? I've even tried a different sketch on the NodeMCU with no luck. I'm really having trouble understanding what went wrong here. Is there something incredibly obvious that I am just spacing out on here?

Arduino Host w/ probes

int id=99;
#include <SoftwareSerial.h>
#include "orp_grav.h"
#include <OneWire.h>
#include <DallasTemperature.h>

//ph configs
#define SensorPin 0            //pH meter Analog output to Arduino Analog Input 0
#define Offset -2.0            //deviation compensate
unsigned long int avgValue;     //Store the average value of the sensor feedback

//orp configs
Gravity_ORP ORP = Gravity_ORP(A1);
unsigned long int ORPavgValue;     //Store the average value of the sensor feedback

//TEMP CONFIGS, using 2.2k pull-up resistor
#define ONE_WIRE_BUS 5 // Data wire is plugged into digital pin 5 on the Arduino
OneWire oneWire(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with any OneWire device
DallasTemperature sensors(&oneWire); // Pass oneWire reference to DallasTemperature library

//NodeMCU serial comm
SoftwareSerial sw(2, 3); // RX, TX

void setup() {
 Serial.begin(9600);
 Serial.println("Interfacfing arduino with nodemcu");
 sw.begin(9600);
 if(ORP.begin()){
    Serial.println("Loaded EEPROM");
  }
 sensors.begin();
}
 
void loop() {
  /////////////  PH LOOP /////////////
  int buf[10];                //buffer for read analog
  for(int i=0;i<10;i++)       //Get 10 sample value from the sensor for smooth the value
  { 
    buf[i]=1024-analogRead(SensorPin);
    delay(10); 
  }
  
  for(int i=0;i<9;i++)        //sort the analog from small to large
  {
    for(int j=i+1;j<10;j++)
    {
      if(buf[i]>buf[j])
      {
        int temp=buf[i];
        buf[i]=buf[j];
        buf[j]=temp;
      }
    }
  }
  avgValue=0;
  for(int i=2;i<8;i++)                      //take the average value of 6 center sample
    avgValue+=buf[i];
 
  float phValue=(float)avgValue*5.0/1024/6; //convert the analog into millivolt
  phValue=3.5*phValue+Offset;                      //convert the millivolt into pH value
////////////////////////////////////////////

  /////////// ORP LOOP ///////////
  int orpValue;
  int orp_buf[10];                //orp_buffer for read analog
  
  for(int i=0;i<10;i++)       //Get 10 sample value from the sensor for smooth the value
  { 
    orp_buf[i]=(int)ORP.read_orp();
    delay(10); 
  }
  
  for(int i=0;i<9;i++)        //sort the analog from small to large
  {
    for(int j=i+1;j<10;j++)
    {
      if(orp_buf[i]>orp_buf[j])
      {
        int temp=orp_buf[i];
        orp_buf[i]=orp_buf[j];
        orp_buf[j]=temp;
      }
    }
  }
  ORPavgValue=0;
  for(int i=2;i<8;i++)                      //take the average value of 6 center sample
    ORPavgValue+=orp_buf[i];
  orpValue = ORPavgValue/6;
 //////////////////////

 
 ///////////TEMP///////////
 sensors.requestTemperatures(); 
 float tempValue = (sensors.getTempCByIndex(0) * 9.0) / 5.0 + 32.0;
 //////////////////////

 
 //Console debug
 Serial.print(phValue);//offset
 Serial.print("\t");//
 Serial.print((orpValue));
 Serial.print("\t");//
 Serial.println((tempValue));
 
 sw.print("{");
 //
 sw.print("\"ph\":\"");
 sw.print(phValue);//offset
 sw.print("\",");
 
 sw.print("\"orp\":\"");
 sw.print(orpValue);//offset
 sw.print("\",");
 
 sw.print("\"temp\":\"");
 sw.print(tempValue);//offset
 //
 sw.print("\"}");
 delay(5000);
}

NodeMCU MQTT code

#include <ESP8266WiFi.h>
#include <PubSubClient.h>


// Update these with values suitable for your network.
const char* ssid = "SSID";
const char* password = "Password";
const char* mqtt_server = "192.168.1.208";
#define mqtt_port 1883
#define MQTT_USER "user"
#define MQTT_PASSWORD "password"
#define MQTT_SERIAL_PUBLISH_CH "hass/sensor/pool"

WiFiClient wifiClient;

PubSubClient client(wifiClient);

void setup_wifi() {
    delay(10);
    // We start by connecting to a WiFi network
    Serial.println();
    Serial.print("Connecting to ");
    Serial.println(ssid);
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.print(".");
    }
    randomSeed(micros());
    Serial.println("");
    Serial.println("WiFi connected");
    Serial.println("IP address: ");
    Serial.println(WiFi.localIP());
}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    client.publish("outTopic", "hello world");
    // Create a random client ID
    String clientId = "NodeMCUClient-";
    clientId += String(random(0xffff), HEX);
    // Attempt to connect
    if (client.connect(clientId.c_str(),MQTT_USER,MQTT_PASSWORD)) {
      Serial.println("connected");
      //Once connected, publish an announcement...
      client.publish("hass/sensor/pool", "hello world");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}
void setup() {
  Serial.begin(9600);
  Serial.setTimeout(500);// Set time out for 
  setup_wifi();
  client.setServer(mqtt_server, mqtt_port);
  reconnect();
}

void publishSerialData(char *serialData){
  if (!client.connected()) {
    reconnect();
  }
  client.publish(MQTT_SERIAL_PUBLISH_CH, serialData);
}
void loop() {
   client.loop();
   if (Serial.available() > 0) {
     char bfr[101];
     memset(bfr,0, 101);
     Serial.readBytesUntil( '\n',bfr,100);
     publishSerialData(bfr);
   }
 }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.