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:encoder [2020/07/20 11:26] – external edit 127.0.0.1en:iot:examples:encoder [2021/03/05 10:00] (current) – external edit 127.0.0.1
Line 1: Line 1:
 ====== Encoder example ====== ====== Encoder example ======
  
-The encoder sensor device has to be connected to the sensor module. Encoder sensor device has a two 3,5 mm plugs what has to be connected to the sensor module 3,5 mm input jackets. The controller and sensor modules must be connected. <fc #ff0000>Do not use this example to configure the IoT module. +The encoder sensor device has to be connected to the sensor module. Encoder sensor device has a two 3,5 mm plugs what has to be connected to the sensor module 3,5 mm input jackets. The controller and sensor modules must be connected.
-</fc>+
  
 {{:en:iot:examples:encoderpicture1.jpg?200|}} {{:en:iot:examples:encoderpicture1.jpg?200|}}
Line 13: Line 12:
  
 Needed libraries Needed libraries
-<code>lib_deps = ITTIoT@1.0.5, ClickEncoder</code>+<code>lib_deps = ITTIoT, ClickEncoder</code>
  
-The example code will print out encoder current position value +The example code will print out and send to the broker encoder’s current position value. The value increases when rotating in one direction and decreases when rotating in another direction. Additionally, the encoder wheel has a switch, and if it is pushed, then the message will be sent to the serial port.
-<code c>+
  
 +<code c>
 #include <Arduino.h> #include <Arduino.h>
 #include <ittiot.h> #include <ittiot.h>
 #include <Ticker.h> #include <Ticker.h>
- 
 #include <ClickEncoder.h> #include <ClickEncoder.h>
  
 +#define WIFI_NAME "name"
 +#define WIFI_PASSWORD "password"
 +
 +// Defining pins as need for the encoder
 #define ENC_PINA 12 #define ENC_PINA 12
 #define ENC_PINB 13 #define ENC_PINB 13
 #define ENC_BTN   0 #define ENC_BTN   0
 #define ENC_STEPS_PER_NOTCH 4 #define ENC_STEPS_PER_NOTCH 4
- 
 ClickEncoder encoder = ClickEncoder(ENC_PINA, ENC_PINB, ENC_BTN, ENC_STEPS_PER_NOTCH); ClickEncoder encoder = ClickEncoder(ENC_PINA, ENC_PINB, ENC_BTN, ENC_STEPS_PER_NOTCH);
  
Line 35: Line 36:
 bool encFlag; bool encFlag;
 uint16_t button; uint16_t button;
 +
 +// Function started after the connection to the server is established.
 void iot_connected() void iot_connected()
 { {
Line 48: Line 51:
 void setup() void setup()
 { {
-  Serial.begin(115200);+  Serial.begin(115200); // setting up serial connection parameter
   Serial.println("Booting");   Serial.println("Booting");
-  iot.printConfig(); // print json config to serial //Peale Serial.begin ja enne iot.setup 
-  iot.setup(); 
  
 +  //iot.setConfig("wname", WIFI_NAME);
 +  //iot.setConfig("wpass", WIFI_PASSWORD);
 +  iot.printConfig(); // print json config to serial
 +  iot.setup(); // Initialize IoT library
 +
 +  // Activating additional button functions
   encoder.setButtonHeldEnabled(true);   encoder.setButtonHeldEnabled(true);
   encoder.setDoubleClickEnabled(true);   encoder.setDoubleClickEnabled(true);
- 
  
   encTicker.attach(1, setEncFlag);   encTicker.attach(1, setEncFlag);
 } }
  
 +//Main code, which runs in loop
 void loop() void loop()
 { {
-  iot.handle(); +  iot.handle(); // IoT behind the plan work, it should be periodically called 
-  delay(10);+  delay(10); // wait for 0.01 second
   static uint32_t lastService = 0;   static uint32_t lastService = 0;
-  // Button+ 
 +  // Sending message “Button”, when the analogue value is bigger less then 100
   if(analogRead(A0) < 100)   if(analogRead(A0) < 100)
   {   {
     Serial.println( "button");     Serial.println( "button");
   }   }
-  +
   if (lastService + 1000 < micros())   if (lastService + 1000 < micros())
   {   {
Line 79: Line 87:
   static int16_t last, value;   static int16_t last, value;
   value += encoder.getValue();   value += encoder.getValue();
-  // Send encoder reading to server 
  
 +  // Send encoder reading to serial monitor, if it has changed
   if(value != last)   if(value != last)
   {   {
Line 87: Line 95:
     Serial.println(value);     Serial.println(value);
   }   }
 +  // Publishing encoder value in MQTT broker
   if(encFlag)   if(encFlag)
   {   {
Line 94: Line 103:
   }   }
 } }
- 
- 
  
 </code> </code>
en/iot/examples/encoder.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