Button example

The code above will send message to log if button pressed

lib_deps = ITTIoT, Switch
/*
 * 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.txt · Last modified: 2018/03/23 11:36 by Somepub
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