So you're a Noob? Post your questions here until you graduate! Don't be shy.

User avatar
By Cozmik
#62570 Hi all,

I'm trying to make a simple circuit that tests if the main house water pump is running. There is a light connected to the main 120v power that turns on whenever the pump is running, so I decided to wire a photoresistor next to the light and then cover the light up with a pill bottle covered in black tape so that whenever the light is on, the photoresistor would trigger the input pin on my ESP8266.

As for the circuit. I have VCC 3.3v (ESP82266 dev board) ---> Photoresistor --- > 2k ohm resistor --- > Ground (ESP8266 dev board). In between the photoresister and the 2k ohm resistor, i have a lead going to input pin 6 of the ESP8266.

This makes it so that when the light turns on, the voltage goes up to 3.27v and when the light is off, it's pulled down to somewhere between 0.0v and 0.20v.

At first I was getting a lot of bouncing. I could see that my MQTT function was sending two or three 'on' and 'off' states each time a switch the power. So i implemented some software debouncing and now it only sends one off and one on per signal.

Everything seems to work ok when I test it with a outlet strip, flipping the power on and off to make the light go on and off and in turn send the "on" and "off" states to my Home Assistant setup. BUT, when I go plug the light back into the pump, it will work sporadically and it will reverse itself randomly and sometimes it will go to ON, but then never turn off.

Also, something else I noticed that was really strange and I hope someone can figure it out. When I open my microwave door in the house, it triggers the ESP input state! I know the house wiring it not the best, but how does this happen and is there a way to fix it? None of this makes much sense to me. Keep in mind, I am not very experienced in this and am just learning.
User avatar
By rudy
#62572 What are you using for an ESP board? What are you using for a power supply for the ESP? A picture would help as well.

It sounds like you have power supply issues even without the photo sensor input. You need to resolve that one before you look further. Maybe that is also the cause for any other issues.
User avatar
By Cozmik
#62573 Hey! thanks so much for the quick reply!

I'm not sure which board it is honestly. I believe it's an ESP8266 12-E Dev? Here's a picture anyway of both the power supply and the board. I had a 5.25v adapter plugged into it but I swapped it out for the 4.75v one but it didn't make a difference. When I open the microwave door, nothing happens, but when I close it, it still triggers the pin and changes the current state to on or off depending on the previous state.

I also have an LED hooked up to pin 7 as an OUTPUT, that's what the extra two wires are for, in case you were wondering.


Image


Image

Image
Don't pay attention to the board in the schematic, it's different than mine but only one i could find in the online schematic drawing. I think I got the basic circuitry correct though.

I have two other near identical ESP8266's with magnetic reed switches and PIR sensors powered by the same type of 5v phone chargers and they are all working extremely well.

Here's my LUA script.

Code: Select allprint("Start MQTT Conn")
broker = "192.168.1.113"
port = 1883
keepalive = 120
username = "rob"
password = "XXXXX"
pumppin = 6
LEDpin = 7
clientid = "Hic03"

-- State OPEN or CLOSED with 750ms Debounce
function powersenseup(level)
   state_topic = "/home-assistant/pump"
   state = "OPEN"
   gpio.trig(pumppin, "none")
   tmr.alarm(3,750, 0, function()
       gpio.write(LEDpin,gpio.HIGH)
     gpio.trig(pumppin, "down", powersensedown)
   end)
   m:publish(state_topic, state, 2, 1, function(client) print("sentpumpUp") end)
end
function powersensedown(level)
   state_topic = "/home-assistant/pump"
   state = "CLOSED"
   gpio.trig(pumppin, "none")
   tmr.alarm(2,750, 0, function()
     gpio.trig(pumppin, "up", powersenseup)
    gpio.write(LEDpin,gpio.LOW)
   end)
   m:publish(state_topic, state, 2, 1, function(client) print("sentpumpDown") end)
end

-- WIFI Reconnect Function
function reconn()
   tmr.alarm(4, 30000, 0, function()
      m:connect(broker, port, 0, function(client) print("connected") end,     
                           function(client, reason) print("failed reason: "..reason) end)
   end)
end

m = mqtt.Client(clientid, keepalive, username, password)
m:lwt("/lwt", "offline...", 0, 0)
m:on("connect", function(client) print ("connected") end)
m:on("offline", reconn)
print("about to connect to broker")
m:connect(broker, port, 0, function(client) print("connected") end,     
                               function(client, reason) print("failed reason: "..reason) end)

gpio.mode(pumppin, gpio.INT)
gpio.trig(pumppin, "up", powersenseup)

gpio.mode(LEDpin, gpio.OUTPUT)

-- WIFI Reconnect
tmr.alarm(1, 30000, 1, function()
  if DEBUG then
    print('IP: ',wifi.sta.getip())
  end

  if wifi.sta.getip() == nil then
    restart = true
  else
    if restart == true then
      restart = false
     reconn()
    end
  end
end)
User avatar
By Cozmik
#62582 I know it's not a power supply issue to the ESP8266 as I've tried three different adapters and even a 3.7v lipo battery plugged directly to Vin (which has an AMS1117 voltage regulator tied to it to drop it to 3.3v).

I did noticed however when I pull the photoresistor further away from the light bulb, the microwave door no longer causes it to change state, so it has to be some kind of interference between the power cord that goes to the pump and the photoresistor or the wires that the photoresistor is connected to.

The only issue with pulling the resistor further away from the light source, is that it no longer has enough power (light) to switch the GPIO to high. I'm about to give up. So frustrated with this supposedly simple project.