Differences

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

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
en:iot:examples:servo [2017/12/05 09:20] Somepuben:iot:examples:servo [2021/03/05 10:00] (current) – external edit 127.0.0.1
Line 1: Line 1:
-====== Servo ======+====== Servo example======
  
-==== Theory ==== 
  
 +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.
  
 +{{:en:iot:examples:servopicture1.jpg?200|}}
  
-==== Practical ====+{{:en:iot:examples:servopicture2.jpg?200|}}
  
-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>lib_deps = ITTIoT</code>
-{{:en:iot:examples:24825943_1764422926925818_1955531775_o.jpg?300|}} +
- +
-{{:en:iot:examples:24726378_1764422930259151_2085205050_o.jpg?300|}}+
  
 <code c> <code c>
Line 23: Line 21:
  */  */
  
 +// Includes global variables and librarys that the servo motor uses
 #include <Arduino.h> #include <Arduino.h>
 #include <Servo.h> #include <Servo.h>
 #include <ittiot.h> #include <ittiot.h>
 +
 +#define MODULE_TOPIC "ESP30/servo"
 +#define WIFI_NAME "name"
 +#define WIFI_PASSWORD "password"
  
 //Pin definition for the Servo (D3) //Pin definition for the Servo (D3)
Line 32: Line 35:
 Servo myservo;  // create servo object to control a servo Servo myservo;  // create servo object to control a servo
  
-// 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 "ITT/iot/test1/servo" -m "51" = this calls servo motor to change position  +// 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) void iot_received(String topic, String msg)
 { {
Line 42: Line 44:
   Serial.println(msg);   Serial.println(msg);
  
-  Serial.print("Servo: "); +  if(topic == MODULE_TOPIC
-  Serial.println(msg); +  { 
- +    myservo.write(msg.toInt()); 
-  myservo.write(msg.toInt());+  }
 } }
  
Line 52: Line 54:
 { {
   Serial.println("MQTT connected callback");   Serial.println("MQTT connected callback");
-  // Subscribe to the topic "servo" + 
-  iot.subscribe("servo");+  iot.subscribe(MODULE_TOPIC); // Subscribe to the topic "servo"
   iot.log("IoT Servo example!");   iot.log("IoT Servo example!");
 } }
Line 59: Line 61:
 void setup() void setup()
 { {
-  Serial.begin(115200);+  Serial.begin(115200); // Setting up serial connection parameter
   Serial.println("Booting");   Serial.println("Booting");
  
-  // Print json config to serial +  //iot.setConfig("wname", WIFI_NAME); 
-  iot.printConfig(); +  //iot.setConfig("wpass", WIFI_PASSWORD); 
-  // Initialize IoT library +  iot.printConfig(); // Print json config to serial 
-  iot.setup();+  iot.setup(); // Initialize IoT library
  
-  myservo.attach(SERVO_PIN);+  myservo.attach(SERVO_PIN); // Setting the servo control pin
 } }
  
 void loop() void loop()
 { {
-  // IoT behind the plan work, it should be periodically called +  iot.handle(); // IoT behind the plan work, it should be periodically called  
-  iot.handle(); +
-  delay(200);+
 } }
  
 </code> </code>
en/iot/examples/servo.1512465658.txt.gz · Last modified: 2020/07/20 09:00 (external edit)
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