Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
et:iot:examples:servo [2023/01/28 13:47] – tekitatud karllallet: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:
 +
 +{{:en:iot:examples:servopicture1.jpg?200|}}
 +
 +{{:en:iot:examples:servopicture2.jpg?200|}}
 +
 +Vajaminevad teegid:
 +<code>lib_deps = ITTIoT</code>
 +
 +<code c>
 +/*
 + * IoT Servo example
 + *
 + * This example subscribe to the "servo" topic. When a message received, then it
 + * change servo position
 + *
 + * Created 11 Sept 2017 by Heiko Pikner
 + */
 +
 +// Includes global variables and librarys that the servo motor uses
 +#include <Arduino.h>
 +#include <Servo.h>
 +#include <ittiot.h>
 +
 +#define MODULE_TOPIC "ESP30/servo"
 +#define WIFI_NAME "name"
 +#define WIFI_PASSWORD "password"
 +
 +//Pin definition for the Servo (D3)
 +#define SERVO_PIN            D3
 +
 +Servo myservo;  // create servo object to control a servo
 +
 +// Change the servo position (value between 0 and 180) when a message has been received
 +// mosquitto_pub -u test -P test -t "RL/esp-{ESP-ID}/servo" -m "51" = this calls servo motor to change position
 +void iot_received(String topic, String msg)
 +{
 +  Serial.print("MSG FROM USER callback, topic: ");
 +  Serial.print(topic);
 +  Serial.print(" payload: ");
 +  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("MQTT connected callback");
 +
 +  iot.subscribe(MODULE_TOPIC); // Subscribe to the topic "servo"
 +  iot.log("IoT Servo example!");
 +}
 +
 +void setup()
 +{
 +  Serial.begin(115200); // Setting up serial connection parameter
 +  Serial.println("Booting");
 +
 +  //iot.setConfig("wname", WIFI_NAME);
 +  //iot.setConfig("wpass", WIFI_PASSWORD);
 +  iot.printConfig(); // Print json config to serial
 +  iot.setup(); // Initialize IoT library
 +
 +  myservo.attach(SERVO_PIN); // Setting the servo control pin
 +}
 +
 +void loop()
 +{
 +  iot.handle(); // IoT behind the plan work, it should be periodically called  
 +}
 +
 +</code> 
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