Differences

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

Link to this comparison view

en:iot:encoder [2017/09/22 11:49] – created Somepuben:iot:encoder [2020/07/20 12:00] (current) – external edit 127.0.0.1
Line 1: Line 1:
 +<code c>
 +#include <Arduino.h>
 +#include <ittiot.h>
 +#include <Ticker.h>
  
 +#include <Encoder.h>
 +
 +#define ENC_PIN_1 12
 +#define ENC_PIN_2 13
 +
 +Encoder myEnc(ENC_PIN_1, ENC_PIN_2);
 +Ticker encTicker;
 +
 +long oldPosition  = -999;
 +bool encFlag;
 +
 +void iot_connected()
 +{
 +  Serial.println("MQTT connected callback");
 +  iot.log("IoT encoder example!");
 +}
 +
 +void setEncFlag()
 +{
 +    encFlag=true;
 +}
 +
 +void setup()
 +{
 +  Serial.begin(115200);
 +  Serial.println("Booting");
 +  iot.printConfig(); // print json config to serial //Peale Serial.begin ja enne iot.setup
 +  iot.setup();
 +
 +  encTicker.attach(1, setEncFlag);
 +}
 +
 +void loop()
 +{
 +  iot.handle();
 +
 +  // Send encoder reading to the serial port, when it changes
 +  long newPosition = myEnc.read();
 +  if (newPosition != oldPosition)
 +  {
 +    oldPosition = newPosition;
 +    Serial.print("Encoder: ");
 +    Serial.println(newPosition);
 +  }
 +
 +  // Send encoder reading to server
 +  if(encFlag)
 +  {
 +      encFlag = false;
 +      String msg = String(newPosition);
 +      iot.publishMsg("enc", msg.c_str());
 +  }
 +}
 +
 +</code>
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