// -----
// SimplePollRotator.ino - Example for the RotaryEncoder library.
// This class is implemented for use with the Arduino environment.
//
// Copyright (c) by Matthias Hertel, http://www.mathertel.de
// This work is licensed under a BSD 3-Clause License. See http://www.mathertel.de/License.aspx
// More information on: http://www.mathertel.de/Arduino
// -----
// 18.01.2014 created by Matthias Hertel
// 04.02.2021 conditions and settings added for ESP8266
// -----
// This example checks the state of the rotary encoder in the loop() function.
// The current position and direction is printed on output when changed.
// Hardware setup:
// Attach a rotary encoder with output pins to
// * A2 and A3 on Arduino UNO.
// * D5 and D6 on ESP8266 board (e.g. NodeMCU).
// Swap the pins when direction is detected wrong.
// The common contact should be attached to ground.
#include <RotaryEncoder.h>
#include <WiFi.h>
#include <PubSubClient.h> // For Arduino IDE
const char* ssid = "Wokwi-GUEST";
const char* password = "";
//hbsxaEHBH6C0Pxta1aY6
const char* mqtt_server = "demo.thingsboard.io"; // Replace with your ThingsBoard server address
const int mqtt_port = 1883;
char thingsboardServer[] = "demo.thingsboard.io";
const char* device_token = "hbsxaEHBH6C0Pxta1aY6"; // Replace with your device token
const char* topic = "v1/devices/me/telemetry"; // Data publishing topic (you can customize)
WiFiClient espClient;
PubSubClient client(espClient);
//float temperature = 25.0; // Replace with your sensor reading function
// Example for ESP8266 NodeMCU with input signals on pin D5 and D6
#define PIN_IN1 26
#define PIN_IN2 27
// Setup a RotaryEncoder with 4 steps per latch for the 2 signal input pins:
// RotaryEncoder encoder(PIN_IN1, PIN_IN2, RotaryEncoder::LatchMode::FOUR3);
// Setup a RotaryEncoder with 2 steps per latch for the 2 signal input pins:
RotaryEncoder encoder(PIN_IN1, PIN_IN2, RotaryEncoder::LatchMode::TWO03);
void setup() {
Serial.begin(115200);
delay(1000);
// Connect to WiFi network
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
Serial.println("Connecting to WiFi...");
delay(500);
}
Serial.println("Connected to the WiFi network");
Serial.println("IP Address: ");
Serial.println(WiFi.localIP());
client.setServer( thingsboardServer, mqtt_port );
client.setCallback(callback);
}
void reconnect() {
// Loop until reconnected
while (!client.connected()) {
Serial.print("Reconnecting MQTT client...");
if (client.connect("ESP32 Device",device_token, NULL)) {
Serial.println("connected");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
static int pos = 0;
encoder.tick();
int newPos = encoder.getPosition();
if (pos != newPos) {
float newPis = (newPos * 3.14 * 5.0)/20;
//newPis = newPis / 20;
Serial.print("pos:");
Serial.print(newPis);
Serial.print(" dir:");
Serial.println((int)(encoder.getDirection()));
pos = newPos;
//int temperature = 25;
String tempString = "{gerbang_1: ";
tempString += newPis;
tempString += "}";
const char* message = tempString.c_str();
// Publish sensor data
//client.publish(topic,String(temperature).c_str());
client.publish(topic,message);
Serial.println("Published value: " + String(newPis));
//delay(5000); // Adjust delay based on your data update frequency
}
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i=0;i<length;i++) {
Serial.print((char)payload[i]);
}
Serial.println();
}
/*
// Example for ESP8266 NodeMCU with input signals on pin D5 and D6
#define PIN_IN1 26
#define PIN_IN2 27
// Setup a RotaryEncoder with 4 steps per latch for the 2 signal input pins:
// RotaryEncoder encoder(PIN_IN1, PIN_IN2, RotaryEncoder::LatchMode::FOUR3);
// Setup a RotaryEncoder with 2 steps per latch for the 2 signal input pins:
RotaryEncoder encoder(PIN_IN1, PIN_IN2, RotaryEncoder::LatchMode::TWO03);
void setup()
{
Serial.begin(9600);
while (! Serial);
Serial.println("SimplePollRotator example for the RotaryEncoder library.");
} // setup()
// Read the current position of the encoder and print out when changed.
void loop()
{
static int pos = 0;
encoder.tick();
int newPos = encoder.getPosition();
if (pos != newPos) {
float newPis = (newPos * 3.14 * 5.0)/20;
//newPis = newPis / 20;
Serial.print("pos:");
Serial.print(newPis);
Serial.print(" dir:");
Serial.println((int)(encoder.getDirection()));
pos = newPos;
} // if
} // loop ()
// The End
*/