Read relay state

Moderators: adafruit_support_bill, adafruit

Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.
Locked
User avatar
axiomnote
 
Posts: 27
Joined: Sat Jan 11, 2020 4:10 pm

Read relay state

Post by axiomnote »

Hi there
i am using Nodemcu 12E to control 2 ch relay board. Using MQTT adafruit to turn on/off relays remotely. For this i am using IoT app on mobile phone. I am wondering is there any possibility that i receive a message that when switch on adafruit is set to ON, relay state is changed from off to on. Now, i cannot see that my command is executed or not. Because on the side where relays are, maybe there is no electricity or internet or maybe is the problem with the router or nodemcu or anything.
So how to be sure that my command from IoT app or from Adafruit website dashboard was executed successfully?

User avatar
Hexen_Wulf
 
Posts: 27
Joined: Tue Aug 02, 2022 9:49 am

Re: Read relay state

Post by Hexen_Wulf »

You could create a feed that sends the state of the relay to your dashboard at specific intervals. Or add some code that updates a feed with a "Everything's Okay!" message if your on/off button press is detected.

User avatar
axiomnote
 
Posts: 27
Joined: Sat Jan 11, 2020 4:10 pm

Re: Read relay state

Post by axiomnote »

thanks @Hexen_Wulf for your reply, it sounds good to me, but i dont know how to do it
please help me

User avatar
Hexen_Wulf
 
Posts: 27
Joined: Tue Aug 02, 2022 9:49 am

Re: Read relay state

Post by Hexen_Wulf »

I guess it would depend on what language you're using. If you're doing this in Arduino, I can't help you at all I'm afraid as I haven't even touched it. If you're using CircuitPython (It looks like that microcontroller is compatible with MicroPython, so maybe you're using CircuitPython) you could have part of your main while loop checking the on/off feed. When it sees that it's on, you can then instruct it to activate the relay.

I used something like this to control NeoPixels with a PyPortal Titano. I had a feed setup that I could send information to that the Titano was actively reading every 30 seconds or so. When the feed updated the on/off state (I used 1 and 0 for feed values, though you can technically use anything) I had the Titano check to see whether or not this was the same state it was currently in. If it is, it ignores the message. If it isn't, it sets the NeoPixels to whatever the feed said, on or off.

Once you have that working, you could then have your device send information to another feed, something like Device State, and have that point to a dashboard block (Maybe an indicator light next to the on/off switch?)

Sorry I'm not providing specific coding examples, but I find that coding is half figuring out the logic, the rest is just figuring out how to write that in whatever language you're using.

I hope this helped some. I'm a beginner myself, so I'd love to check out your code if you don't mind sharing it here.

User avatar
axiomnote
 
Posts: 27
Joined: Sat Jan 11, 2020 4:10 pm

Re: Read relay state

Post by axiomnote »

Everything is on arduino written. When i turn switch ON in mqtt adafruit dashboard it turns ON relay through nodemcu, but when i am outside of home i cannot be sure if the relay is switched on (maybe internet connection is lost, or electricity is gone or something else)
must be some way to see that nodemcu was executed the action

User avatar
Hexen_Wulf
 
Posts: 27
Joined: Tue Aug 02, 2022 9:49 am

Re: Read relay state

Post by Hexen_Wulf »

Have you looked at the Power-Ups for Adafruit IO? You could maybe use If This, Then That to send you a message when it's turned on.

User avatar
axiomnote
 
Posts: 27
Joined: Sat Jan 11, 2020 4:10 pm

Re: Read relay state

Post by axiomnote »

well in that case i will get an info that mqtt feed reacted on my command but will not receive message from the nodemcu (esp8266) that command is executed and relay is turned on/off

User avatar
Franklin97355
 
Posts: 23910
Joined: Mon Apr 21, 2008 2:33 pm

Re: Read relay state

Post by Franklin97355 »

I have a question. How are you going to tell if the relay is actually switching power to the device? If the power is off to the relay but the nodeMCU is powered the relay would switch a dead circuit. Just wondering.

User avatar
axiomnote
 
Posts: 27
Joined: Sat Jan 11, 2020 4:10 pm

Re: Read relay state

Post by axiomnote »

well thats a good question @Franklin97355, but i was thinking before about it to avoid that situation...so the relay is powered through nodemcu so when nodemcu has power the relay has it too....

User avatar
Franklin97355
 
Posts: 23910
Joined: Mon Apr 21, 2008 2:33 pm

Re: Read relay state

Post by Franklin97355 »

That's not what I meant. The nodeMCU and the relay has power but does the relay have power to its contacts? I think the nmcu can send the state of the relay but you might have to add more sensors to tell if the device is getting power. What are you trying to control and how are you communicating with the nodeMCU? Thanks for the info, it is an interesting problem.

User avatar
axiomnote
 
Posts: 27
Joined: Sat Jan 11, 2020 4:10 pm

Re: Read relay state

Post by axiomnote »

thanks, yes i think that this can help lot of people to get notification that relay is turned on/off...relays are contrilling lights (220V) and led stripe light (12V)..i do not know how to force nodemcu to send me email or notification somehow that relay is turned on/off
its easy when i am near relays and watch execution of commands
but problem is when i am outside and router is stacked and nodemcu cannot receive command from IoT app from my android phone or from website Mqtt broker....

Locked
Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.

Return to “Internet of Things: Adafruit IO and Wippersnapper”