A Servo Motor Node & OpenHAB 2's MQTT Binding 2.xx
With DIY Home Automation, sometimes we want to gather information (as from a sensor). Sometimes we want to control something, such as an electrical appliance. With a servo motor, we can control movement precisely within a small range. We can move the motor's shaft (or an attached "horn") to points within 180 degrees or half of a full circle. Later we might want to gather information from a sensor & use that information to control an arm on a servo motor so it points to the temperature on a printed scale.
<< Forum Members (free registration), click on pic for larger view.
^^ Servo control pin on Arduino changed to D7 from D9. Capacitor added to smooth servo power.
Note: Be sure you connect servo power to Arduino 5 volts. Also when the Node's RFM69 Radio transmits data (which taxes the Arduino's current supply), sometimes the servo hiccups a bit. An electrolytic capacitor between the servo's power & Gnd might smooth this out.
Control a Servo Motor with a User Interface Slider OpenHAB Configuration: Rule
In OpenHAB's conf\rules\ folder create a Node05Servo.rules file with this content:
rule servoSlider05 when Item servoAngle05 changed then var Number prcnt = servoAngle05.state // convert slider's 0-100 percent to 0-180 DEGREES on servo var Number angl = prcnt * 1.8 servoCmd05.sendCommand( angl ) // use converted angl variable to move the servo
// Uncomment the next lines to put debugging info in the log // logInfo("Servo05", "****Percent = " + prcnt) // logInfo("Servo05", "****Angle = " + angl) end
Control a Servo Motor with a User Interface Slider OpenHAB Sitemap & Servo Operation
In OpenHAB's conf\sitemaps\ folder add the following entry to your .sitemap file:
Moving the User Interface slider should also rotate the servo shaft the degrees that are proportional to the slider's percentage. According to the rule above, moving the slider 1% will turn the servo shaft 1.8 degrees. Moving the slider 100%, turns the servo shaft 180 degrees, the maximum for the servo.
Note if the above does not work, stopping & restarting OpenHAB & then trying again may help. Between stopping & restarting, you may also delete \user\cache & \user\tmp folders to force OpenHAB to use changed configurations. (OpenHAB takes longer to reboot when those folders are deleted.)
Next, Control a Servo Motor with Temperature Sensor or Other Data
Control a Servo Motor with a DS18B Temperature Sensor
or Other Data
1) Possibly Changing the Choose_Nodes Sketch
For a proof of concept example, I am using a DS18 sensor which is wired like the DS18B schematicin this post, except omitting the optional DHT sensor. Also wire the servo motor like the schematic in this link above.
For both options 1) & 2) below, beyond the initial customization to the choose_nodes sketch, add: UNcomment ONE: #define FAHR or #define CELS UNcomment: #define DS18 AND #define DS18B
Option 1) We might change the choose_nodes sketch code to set the servo rotation angle according to the DS18 temperature. Untested hints: we might add code near the end of the getDS18temps() function. The temp variable holds the acquired temperature reading.
So adding the following sketch lines might be a good start:
servoVal = temp ; // < as is, servo rotation would = temperature.
// ^^ Added math to this line might spread the temp data over larger servo rotation
myservo.write(servoVal); // tell servo to go to position in variable 'servoVal' delay(15);
Control a Servo Motor with a DS18B Temperature Sensor or Other Data Option 2) Using an OpenHAB Rule, Continued
In a .rules file (Node05Servo.rules ?), have this rule
rule servoDS18B05 when Item TempDS18_Node05 changed then var Number temp05 = TempDS18_Node05.state // ^^ Added math to this line might spread temp data over larger servo rotation servoCmd05.sendCommand( temp05 ) // Uncomment next line for debugging info in the log logInfo("Servo05", "****05DS18 = " + temp05)
Control a Servo Motor with a DS18B Temperature Sensor or Other Data, Comments
Every 60 seconds the node will send OpenHAB the (raw or math-tweaked) temperature data. If it is a change, via the rule, that temp number will become the new angle for the servo arm. As I did with the above rule for the slider, you could multiply or divide to convert to a needed range of angles to spread typical results over the full or desired angle range of the servo.
Similar rule-based approaches could be used for other sensor data: DHT humidity %, sump cistern level, etc.
I have tested this rule-based approach & it works. When we are watching waiting 60 seconds seems long, but normal un-watched operation should be fine. At the same time, by changing the choose_nodes sketch, the servo should more quickly respond to changes in temperature data.