Chicken Coop Controller

Hey All,
I’m trying to build a chicken coop controller to keep the water from freezing. I would also like it report the temperature back through MQTT. I’m using a water proof DS1820B sensor and esp8266 Nodemcu v0.9.
My coding skills are a bit weak and a lot of this is copy/paste. I am learning :). I would like to keep my relay control script separate so I created void thermostat(). When ever I execute it in void loop() my MQTT stops working. I know the MQTT relay control needs work…I need somehow to switch from auto to manual. That is a future issue. Rate now I just want the client.publish(“Coop/1/waterF”,DStempf );
to work with the thermostat script. MQTT keeps reconnecting also. I use MQTT spy to monitor.

Thanks for any advice

Code was two large, had to attach.

Jeff

MQTT_Relay_Noinfo.ino (10.2 KB)

Holy unnecessary whitespace batman!

Hard to advise without seeing the broken code.

Note though that the EEPROM has a finite lifetime on write - ~100,000 writes to a location and you can expect that cell to start failing. As you have it, you're writing on it every half a second or so which means that it'll last a little less than two and a half days. Better to write only when the temperature set point changes.

Thanks for replying wildbill,

Does the whitespace effect the program or just sloppy programming?

How do I see the "broken code" ?

As I indicated the code is a lot of copy and paste and the EEPROM was a lot of that. I'm not concerned about remember the "last state" and can remove EEPROM writes.

I agree with setpoints being saved in EEPROM on change. I will have to figure that out in the future as I also need to figure changing the setpoint over MQTT.

Jeff

The compiler doesn't care about whitespace for the most part so as long as your code compiles it doesn't impact how it functions, but it makes it more difficult for humans to read. Your indentation suffers from the same issue.

The broken code I'm referring to is that you say that you created a thermostat function which broke your MQTT connectivity and that you want the client.publish("Coop/1/waterF",DStempf ); to work.

However, none of that appears in the code you posted, so I'm not sure how to help.

wildbill,

I cleaned up the white space and corrected the indentation to the best of my ability. I also removed the EEPROM code for now. This code compiles OK and loads OK. It still won't publish client.publish("Coop/1/waterF",DStempf ); in my void loop()

not sure how to debug to see what is stopping the client publish. Serial.print is working fine.

SERIAL MONITOR

Celsius temperature: 23.00 - Fahrenheit temperature: 73.40
Realtime Temp: 73.40 73.40 73.40 73.40 73.40 73.40 73.40 73.40 73.40 73.40
Average Temp is 73.40
Temperature is Over 34, Alarm is off
Temperature is Over 40, HEATER is OFF

Celsius temperature: 23.00 - Fahrenheit temperature: 73.40
Realtime Temp: 73.40 73.40 73.40 73.40 73.40 73.40 73.40 73.40 73.40 73.40
Average Temp is 73.40
Temperature is Over 34, Alarm is off
Temperature is Over 40, HEATER is OFF

Celsius temperature: 23.00 - Fahrenheit temperature: 73.40
Attempting MQTT connection...connected
Realtime Temp: 73.40 73.40 73.40 73.40 73.40 73.40 73.40 73.40 73.40 73.40
Average Temp is 73.40
Temperature is Over 34, Alarm is off
Temperature is Over 40, HEATER is OFF

Celsius temperature: 23.00 - Fahrenheit temperature: 73.40
Realtime Temp: 73.40 73.40 73.40 73.40 73.40 73.40 73.40 73.40 73.40 73.40
Average Temp is 73.40
Temperature is Over 34, Alarm is off
Temperature is Over 40, HEATER is OFF

Celsius temperature: 23.00 - Fahrenheit temperature: 73.40
Realtime Temp: 73.40 73.40 73.40 73.40 73.40 73.40 73.40 73.40 73.40 73.40
Average Temp is 73.40
Temperature is Over 34, Alarm is off
Temperature is Over 40, HEATER is OFF

Post the code with the client.publish. Really hard to debug without seeing it.

wildbill:
Post the code with the client.publish. Really hard to debug without seeing it.

Hey Wildbill,
Sorry if I’m confusing you. client.publish comes from
#include <PubSubClient.h>
It is the MQTT library that I am using. So I’m not sure what you are looking for ? I’ve copied all my Code.

maybe a Arduino simulator may help ?

This was the Note from the Code I copied. It may be my issue ??

It will reconnect to the server if the connection is lost using a blocking
reconnect function. See the ‘mqtt_reconnect_nonblocking’ example for how to
achieve the same result without blocking the main loop.

Hi Jeff,

For a couple of years I have been running some chicken coop control code that works, but unfortunately, it is running on an Uno clone and currently has no remote capabilities. I am planning to do some stuff over RF but I haven't truly begun the code yet. I just wanted to weigh in that you client.publish is in loop as you say, but I came to a question.

client.publish("Coop/1/waterF",DStempf );

This is specifically the Fahrenheit publish...does that mean the Celsius call is working fine in MQTT, and only the F is not working? If so, that is a major hint!

-fab

fabelizer:
Hi Jeff,

For a couple of years I have been running some chicken coop control code that works, but unfortunately, it is running on an Uno clone and currently has no remote capabilities. I am planning to do some stuff over RF but I haven't truly begun the code yet. I just wanted to weigh in that you client.publish is in loop as you say, but I came to a question.

This is specifically the Fahrenheit publish...does that mean the Celsius call is working fine in MQTT, and only the F is not working? If so, that is a major hint!

-fab

Hi Fab,
Both were not working, I relocated call for thermostat() to the bottom of my loop and it is working but I'm still getting disconnects on my MQTT broker. As I am at work now my plans tonight are to rework the code so I can have an manual/auto mode. I believe my Code for PubSubClient isn't used correctly for my application.