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


#1

Hello!

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.


#2

Hi @claudio.ascensao,

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

Setup:

#define DOOR_SW 12 //assuming GPIO12 is not in use.
pinMode(DOOR_SW,INPUT); 

In your loop:

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

Cheers,

Craig


#3

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.


#4

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.

Cheers,

Craig


#5

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)

Thanks


#6

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.