This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| et:iot:examples:servo [2023/01/28 13:47] – tekitatud karllall | et:iot:examples:servo [Unknown date] (current) – external edit (Unknown date) 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== Servo näidis ====== | ||
| + | |||
| + | Servomootor peab olema ühendatud anduri mooduliga. Oluline on jälgida, et servomootor oleks õigesti ühendatud. Õige ühendamise viis on näha allolevalt pildilt: | ||
| + | |||
| + | {{: | ||
| + | |||
| + | {{: | ||
| + | |||
| + | Vajaminevad teegid: | ||
| + | < | ||
| + | |||
| + | <code c> | ||
| + | /* | ||
| + | * IoT Servo example | ||
| + | * | ||
| + | * This example subscribe to the " | ||
| + | * change servo position | ||
| + | * | ||
| + | * Created 11 Sept 2017 by Heiko Pikner | ||
| + | */ | ||
| + | |||
| + | // Includes global variables and librarys that the servo motor uses | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | #define MODULE_TOPIC " | ||
| + | #define WIFI_NAME " | ||
| + | #define WIFI_PASSWORD " | ||
| + | |||
| + | //Pin definition for the Servo (D3) | ||
| + | #define SERVO_PIN | ||
| + | |||
| + | Servo myservo; | ||
| + | |||
| + | // Change the servo position (value between 0 and 180) when a message has been received | ||
| + | // mosquitto_pub -u test -P test -t " | ||
| + | void iot_received(String topic, String msg) | ||
| + | { | ||
| + | Serial.print(" | ||
| + | Serial.print(topic); | ||
| + | Serial.print(" | ||
| + | Serial.println(msg); | ||
| + | |||
| + | if(topic == MODULE_TOPIC) | ||
| + | { | ||
| + | myservo.write(msg.toInt()); | ||
| + | } | ||
| + | } | ||
| + | |||
| + | // Function started after the connection to the server is established | ||
| + | void iot_connected() | ||
| + | { | ||
| + | Serial.println(" | ||
| + | |||
| + | iot.subscribe(MODULE_TOPIC); | ||
| + | iot.log(" | ||
| + | } | ||
| + | |||
| + | void setup() | ||
| + | { | ||
| + | Serial.begin(115200); | ||
| + | Serial.println(" | ||
| + | |||
| + | // | ||
| + | // | ||
| + | iot.printConfig(); | ||
| + | iot.setup(); | ||
| + | |||
| + | myservo.attach(SERVO_PIN); | ||
| + | } | ||
| + | |||
| + | void loop() | ||
| + | { | ||
| + | iot.handle(); | ||
| + | } | ||
| + | |||
| + | </ | ||