This shows you the differences between two versions of the page.
| en:iot:encoder [2017/09/22 11:49] – created Somepub | en:iot:encoder [2020/07/20 12:00] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | <code c> | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | #define ENC_PIN_1 12 | ||
| + | #define ENC_PIN_2 13 | ||
| + | |||
| + | Encoder myEnc(ENC_PIN_1, | ||
| + | Ticker encTicker; | ||
| + | |||
| + | long oldPosition | ||
| + | bool encFlag; | ||
| + | |||
| + | void iot_connected() | ||
| + | { | ||
| + | Serial.println(" | ||
| + | iot.log(" | ||
| + | } | ||
| + | |||
| + | void setEncFlag() | ||
| + | { | ||
| + | encFlag=true; | ||
| + | } | ||
| + | |||
| + | void setup() | ||
| + | { | ||
| + | Serial.begin(115200); | ||
| + | Serial.println(" | ||
| + | iot.printConfig(); | ||
| + | iot.setup(); | ||
| + | |||
| + | encTicker.attach(1, | ||
| + | } | ||
| + | |||
| + | 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(" | ||
| + | Serial.println(newPosition); | ||
| + | } | ||
| + | |||
| + | // Send encoder reading to server | ||
| + | if(encFlag) | ||
| + | { | ||
| + | encFlag = false; | ||
| + | String msg = String(newPosition); | ||
| + | iot.publishMsg(" | ||
| + | } | ||
| + | } | ||
| + | |||
| + | </ | ||