This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| en:iot:examples:servo [2020/03/17 13:44] – heikopikner | en:iot:examples:servo [2021/03/05 10:00] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 9: | Line 9: | ||
| Needed libraries: | Needed libraries: | ||
| - | < | + | < |
| <code c> | <code c> | ||
| Line 21: | Line 21: | ||
| */ | */ | ||
| + | // Includes global variables and librarys that the servo motor uses | ||
| #include < | #include < | ||
| #include < | #include < | ||
| #include < | #include < | ||
| + | |||
| + | #define MODULE_TOPIC " | ||
| + | #define WIFI_NAME " | ||
| + | #define WIFI_PASSWORD " | ||
| //Pin definition for the Servo (D3) | //Pin definition for the Servo (D3) | ||
| Line 30: | Line 35: | ||
| Servo myservo; | Servo myservo; | ||
| - | // Change the servo position (value between 0 and 180) | + | // Change the servo position (value between 0 and 180) when a message has been received |
| - | // mosquitto_pub -u test -P test -t " | + | // mosquitto_pub -u test -P test -t " |
| void iot_received(String topic, String msg) | void iot_received(String topic, String msg) | ||
| { | { | ||
| Line 40: | Line 44: | ||
| Serial.println(msg); | Serial.println(msg); | ||
| - | | + | |
| - | | + | |
| - | + | myservo.write(msg.toInt()); | |
| - | | + | } |
| } | } | ||
| Line 50: | Line 54: | ||
| { | { | ||
| Serial.println(" | Serial.println(" | ||
| - | | + | |
| - | iot.subscribe(" | + | iot.subscribe(MODULE_TOPIC); |
| iot.log(" | iot.log(" | ||
| } | } | ||
| Line 57: | Line 61: | ||
| void setup() | void setup() | ||
| { | { | ||
| - | Serial.begin(115200); | + | Serial.begin(115200); |
| Serial.println(" | Serial.println(" | ||
| - | // Print json config to serial | + | //iot.setConfig(" |
| - | iot.printConfig(); | + | |
| - | // Initialize IoT library | + | |
| - | iot.setup(); | + | iot.setup(); |
| - | myservo.attach(SERVO_PIN); | + | myservo.attach(SERVO_PIN); |
| } | } | ||
| void loop() | void loop() | ||
| { | { | ||
| - | // IoT behind the plan work, it should be periodically called | + | |
| - | iot.handle(); | + | |
| - | delay(200); | + | |
| } | } | ||
| </ | </ | ||