This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| en:iot:examples:button [2020/07/20 11:26] – external edit 127.0.0.1 | en:iot:examples:button [Unknown date] (current) – external edit (Unknown date) 127.0.0.1 | ||
|---|---|---|---|
| Line 5: | Line 5: | ||
| The code above will send message to log if button pressed | The code above will send message to log if button pressed | ||
| - | < | + | < |
| <code c> | <code c> | ||
| Line 17: | Line 17: | ||
| */ | */ | ||
| + | // Includes global variables and librarys that the Buzzer uses | ||
| #include < | #include < | ||
| #include < | #include < | ||
| #include < | #include < | ||
| + | #define WIFI_NAME " | ||
| + | #define WIFI_PASSWORD " | ||
| - | const byte buttonPin = D3; | + | const byte buttonPin = D3; // TO which pin the button has been assigned |
| int i; | int i; | ||
| Switch button = Switch(buttonPin); | Switch button = Switch(buttonPin); | ||
| - | void iot_received(String topic, String msg) | + | void iot_received(String topic, String msg) {} |
| - | { | + | |
| - | Serial.print(" | + | |
| - | Serial.print(topic); | + | |
| - | Serial.print(" | + | |
| - | Serial.println(msg); | + | |
| - | + | ||
| - | } | + | |
| // Function started after the connection to the server is established. | // Function started after the connection to the server is established. | ||
| Line 40: | Line 36: | ||
| { | { | ||
| Serial.println(" | Serial.println(" | ||
| - | // Subscribe to the topic " | ||
| - | iot.subscribe(" | ||
| iot.log(" | iot.log(" | ||
| } | } | ||
| Line 47: | Line 41: | ||
| void setup() | void setup() | ||
| { | { | ||
| - | Serial.begin(115200); | + | Serial.begin(115200); |
| Serial.println(" | Serial.println(" | ||
| pinMode(buttonPin, | pinMode(buttonPin, | ||
| + | |||
| + | // | ||
| + | // | ||
| // Print json config to serial | // Print json config to serial | ||
| iot.printConfig(); | iot.printConfig(); | ||
| // Initialize IoT library | // Initialize IoT library | ||
| iot.setup(); | iot.setup(); | ||
| - | // Initialize relay pin | ||
| - | |||
| } | } | ||
| Line 63: | Line 58: | ||
| // IoT behind the plan work, it should be periodically called | // IoT behind the plan work, it should be periodically called | ||
| iot.handle(); | iot.handle(); | ||
| - | delay(200); | + | |
| + | // Askes in which state the button is, pressed, long pressed, double click, or released. | ||
| button.poll(); | button.poll(); | ||
| + | // If the button is long pressed, it publishes message “LongPressed” | ||
| if (button.longPress()) { | if (button.longPress()) { | ||
| iot.log(" | iot.log(" | ||
| } | } | ||
| + | // If the button is double clicked, it publishes message “DoubleClick” | ||
| if (button.doubleClick()) { | if (button.doubleClick()) { | ||
| | | ||
| } | } | ||
| - | | ||
| + | // If the button has been released, it publishes message “Released” | ||
| if (button.released()) { | if (button.released()) { | ||
| - | iot.log(" | + | iot.log(" |
| - | } | + | |
| - | + | ||
| - | if (button.pushed()) { | + | |
| - | iot.log(" | + | |
| } | } | ||
| + | // If the button is pushed down, it publishes message “ButtonPushed” | ||
| + | if (button.pushed()) { | ||
| + | iot.log(" | ||
| + | } | ||
| + | | ||
| + | delay(3); | ||
| } | } | ||
| </ | </ | ||