This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
en:iot:examples:servo [2017/12/04 11:56] – created Somepub | en:iot:examples:servo [2021/03/05 10:00] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Servo ====== | + | ====== Servo example====== |
+ | |||
+ | Servo motor has to be connected to the sensor module. Motor cable pins has to be connected to left side of the sensor pins, and also the brown cable(GRND) has to be connected to the left side of the pin. | ||
+ | |||
+ | {{: | ||
+ | |||
+ | {{: | ||
+ | |||
+ | Needed libraries: | ||
+ | < | ||
+ | |||
+ | <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(); | ||
+ | } | ||
+ | |||
+ | </ |