Mqtt Switch with Nodemcu

Hi..i am a newbie and a mechanical engineer who is trying to learn some IOT. i want to write a code for my nodemcu V2. In the hardware setup I want to connect a normally open rocker switch between Pin 2 and ground of the nodemcu. I wish to write a code to publish a message when the switch is closed( enabled) and when the switch is opened( disabled). I know its very simple but i tried many ways but not suceeded in exactly what i wish. its very simple, please provide me an example of this setup.... :slight_smile: My mqtt broker is mosquito which has both username and password enabled which is running in my RPi 3..please help

Did you connect your switch to pin 2 or GPIO2? You will find that pin 2 and GPIO2 ae different pins.

What line of your code.. Oh wait, you did not post any code, so I cannot help you there. If you, at sometime, decide to post your code, please, use code tags. If you, also, decide to post a schematic of your connections, that would be wonderful.

My advice is to start with a very simple sketch that prints to Serial or switches an LED to indicate the state of the switch. Once you have verified that your switch is working correctly, you can proceed with the MQTT part of the project.

Idahowalker:
Did you connect your switch to pin 2 or GPIO2? You will find that pin 2 and GPIO2 ae different pins.

What line of your code.. Oh wait, you did not post any code, so I cannot help you there. If you, at sometime, decide to post your code, please, use code tags. If you, also, decide to post a schematic of your connections, that would be wonderful.

i have used the code int BUTTON_PIN = 2

sheminasalam:
i have used the code int BUTTON_PIN = 2

ROFL.

What we are trying to say is that many things can be wrong, if you can post your code, and some form of diagram of your circuit, we might be able to assist you in your trouble. What you have done, is like going to the mechanic to fix your car, but left the car at home.

Please post your code, use code tags, make sure before you post you indent your code. The IDE has an auto format Windows is CTL T. MAC CMD T