This is an old revision of the document!


Button example

The code above will send message to log if button pressed

lib_deps = ITTIoT@1.0.5, 2947
/*
 * 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
 */
 
#include <Arduino.h>
#include <ittiot.h>
#include <Switch.h>
 
 
const byte buttonPin = D3;
int i;
 
Switch button = Switch(buttonPin);
 
void iot_received(String topic, String msg)
{
  Serial.print("MSG FROM USER callback, topic: ");
  Serial.print(topic);
  Serial.print(" payload: ");
  Serial.println(msg);
 
}
 
// Function started after the connection to the server is established.
void iot_connected()
{
  Serial.println("MQTT connected callback");
  // Subscribe to the topic "button"
  iot.subscribe("button");
  iot.log("IoT Button example!");
}
 
void setup()
{
  Serial.begin(115200);
  Serial.println("Booting");
 
  pinMode(buttonPin, INPUT);
  // Print json config to serial
  iot.printConfig();
  // Initialize IoT library
  iot.setup();
  // Initialize relay pin
 
}
 
void loop()
{
  // IoT behind the plan work, it should be periodically called
  iot.handle();
  delay(200);
  button.poll();
 
 
  if (button.longPress()) {
    iot.log("LongPressed");
  }
 
  if (button.doubleClick()) {
     iot.log("DoubleClick");
  }
 
 
  if (button.released()) {
    iot.log("released\n");
}
 
 if (button.pushed()) {
    iot.log("Button Pushed");
  }
 
}
en/iot/examples/button.1595244384.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