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