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 [2020/07/20 11:26] – external edit 127.0.0.1en:iot:examples:servo [2021/03/05 10:00] (current) – external edit 127.0.0.1
Line 9: Line 9:
  
 Needed libraries: Needed libraries:
-<code>lib_deps = ITTIoT@1.0.5</code>+<code>lib_deps = ITTIoT</code>
  
 <code c> <code c>
Line 21: 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 30: 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 "RL/esp-{ESP-ID}/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 40: 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 50: Line 54:
 { {
   Serial.println("MQTT connected callback");   Serial.println("MQTT connected callback");
-  // Subscribe to the topic "servo" + 
-  iot.subscribe("ESP01/servo");+  iot.subscribe(MODULE_TOPIC); // Subscribe to the topic "servo"
   iot.log("IoT Servo example!");   iot.log("IoT Servo example!");
 } }
Line 57: 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.1595244384.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