Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
en:iot:examples:button [2018/03/23 09:32] Somepuben:iot:examples:button [Unknown date] (current) – external edit (Unknown date) 127.0.0.1
Line 1: Line 1:
 ====== Button example ====== ====== Button example ======
 +
 {{:en:iot:examples:buttonshield.png?200|}} {{:en:iot:examples:buttonshield.png?200|}}
 +
 +The code above will send message to log if button pressed
 +
 +<code>lib_deps = ITTIoT, blackketter/Switch</code>
 +
 +<code c>
 +/*
 + * IoT Button example
 + *
 + * This example subscribe to the "button" topic. When a message received, then it
 + * will show the message
 + *
 + * Created 21 Febuary 2018 by Mallor Kingsepp
 + */
 +
 +// Includes global variables and librarys that the Buzzer uses
 +#include <Arduino.h>
 +#include <ittiot.h>
 +#include <Switch.h>
 +
 +#define WIFI_NAME "name"
 +#define WIFI_PASSWORD "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("MQTT connected callback");
 +  iot.log("IoT Button example!");
 +}
 +
 +void setup()
 +{
 +  Serial.begin(115200); // setting up serial connection parameter
 +  Serial.println("Booting");
 +
 +  pinMode(buttonPin, INPUT);
 +
 +  //iot.setConfig("wname", WIFI_NAME);
 +  //iot.setConfig("wpass", WIFI_PASSWORD);
 +  // 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("LongPressed");
 +  }
 +
 +  // If the button is double clicked, it publishes message “DoubleClick”
 +  if (button.doubleClick()) {
 +     iot.log("DoubleClick");
 +  }
 +
 +  // If the button has been released, it publishes message “Released”
 +  if (button.released()) {
 +    iot.log("Released");
 +  }
 +
 +  // If the button is pushed down, it publishes message “ButtonPushed”
 +  if (button.pushed()) {
 +    iot.log("ButtonPushed");
 +  }
 +  
 +  delay(3);
 +}
 +
 +</code>
en/iot/examples/button.1521797536.txt.gz · Last modified: 2020/07/20 09:00 (external edit)
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0