Connecting button to ESP12E and sending information to cayenne (MQTT)



i’m trying to use a esp12E to detect and send (via wifi) button change information to cayenne dashboard.

when reading the cayenne examples the code is for other kind off Sensors such as:

//Write data to Cayenne here. This example just sends the current uptime in milliseconds.
	Cayenne.virtualWrite(0, lastMillis);
	//Some examples of other functions you can use to send data.
	//Cayenne.celsiusWrite(1, 22.0);
	//Cayenne.luxWrite(2, 700);
	//Cayenne.virtualWrite(3, 50, TYPE_PROXIMITY, UNIT_CENTIMETER);

What i’d like to do is read a pin (door sensor) and send the information to the dashboard.

Any help with the code and functions available? (i’m a newbie).

Thank you.


Hi @claudio.ascensao,

What particular board are you using? The NodeMCU for instance changes the pin names so it is harder to map.


#define DOOR_SW 12 //assuming GPIO12 is not in use.

In your loop:

Cayenne.virtualWrite(4,digitalRead(DOOR_SW)); //Sends door switch status on channel 4.




Hello @kreggly

My board is an NodeMCU.

And it worked. Thank you for that.

The reported value of GPI012 is 1, when nothing is connected. Shouldn’t it be 0?

Thank you in advance.


My guess is internal pull-ups or just floating high. Usually if you want
the internal pullups you would do pinmode(PIN,INPUT_PULLUP), but the ESP
conversion to Arduino is known for not following all the rules.




Hello again.

when i connect something to d12 de value descents to 0, but then when i disconnect… it remains 0… the ideia was when closing the door the value should com to the initial state.
Any ideia? (sorry for this questions)



Try the pinmode with INTERNAL_PULLUP. Your pin may be behaving oddly if it is floating.

If this doesn’t work, add your own pullup with a 10k resistor to 3V3.