new device: pushbutton
This commit is contained in:
		
							
								
								
									
										25
									
								
								things/DeviceButton.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								things/DeviceButton.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,25 @@ | ||||
| #include "DeviceButton.h" | ||||
|  | ||||
| void DeviceButton::deviceSetup() { | ||||
|   pinMode(pin, INPUT); | ||||
| } | ||||
|  | ||||
| void DeviceButton::deviceRegister() { | ||||
|   Homie.registerNode(buttonNode); | ||||
| } | ||||
|  | ||||
| void DeviceButton::deviceLoop() { | ||||
|   byte debounce = 0; | ||||
|   for (int i = 0; i < 3; i++) { | ||||
|     if (digitalRead(pin) == state) { | ||||
|       return; | ||||
|     } | ||||
|     delay(10); | ||||
|   } | ||||
|   state = !state; | ||||
|   Serial.print("button: "); | ||||
|   Serial.println(state); | ||||
|   if (!Homie.setNodeProperty(buttonNode, "state", String(state), false)) { | ||||
|     Serial.println("Sending failed"); | ||||
|   } | ||||
| } | ||||
							
								
								
									
										18
									
								
								things/DeviceButton.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								things/DeviceButton.h
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,18 @@ | ||||
| #pragma once | ||||
|  | ||||
| #include "Device.h" | ||||
| #include <Homie.h> | ||||
|  | ||||
| class DeviceButton : public Device { | ||||
|   public: | ||||
|     inline DeviceButton(byte buttonPin) { | ||||
|       pin = buttonPin; | ||||
|     } | ||||
|     virtual void deviceSetup(); | ||||
|     virtual void deviceRegister(); | ||||
|     virtual void deviceLoop(); | ||||
|   private: | ||||
|     byte pin; | ||||
|     bool state = LOW; | ||||
|     HomieNode buttonNode = HomieNode("button", "button"); | ||||
| }; | ||||
| @@ -5,6 +5,7 @@ | ||||
| #include "DeviceDht.h" | ||||
| #include "DeviceIrRx.h" | ||||
| #include "DeviceIrTx.h" | ||||
| #include "DeviceButton.h" | ||||
|  | ||||
| const byte PIN_LED_RED = D8; | ||||
| const byte PIN_LED_GREEN = D6; | ||||
| @@ -25,12 +26,16 @@ DeviceIrRx deviceIrRx(PIN_IRRX, PIN_POWER); | ||||
| const byte PIN_IRTX = D2; | ||||
| DeviceIrTx deviceIrTx(PIN_IRTX); | ||||
|  | ||||
| const byte PIN_BUTTON = D2; | ||||
| DeviceButton deviceButton(PIN_BUTTON); | ||||
|  | ||||
| Device* devices[] = { | ||||
|   &deviceLed, | ||||
|   &deviceLdr, | ||||
|   &deviceDht, | ||||
|   &deviceIrRx, | ||||
|   &deviceIrTx, | ||||
|   &deviceButton, | ||||
| }; | ||||
|  | ||||
| void setupHandler() { | ||||
|   | ||||
		Reference in New Issue
	
	Block a user