|
Post by papa on Feb 12, 2019 15:58:13 GMT
A Servo Motor Node & OpenHAB 2's MQTT Binding 2.xxWith 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.
Some forum members have used servos to control window blinds. Forum member greginkansas used an Automotive Gauge Stepper Motor (range of 315 degrees) to replace analog weather indications on an antique weather station. He was working to indicate temperature, humidity, etc on existing dials. One might tailor dials to a servo's range of motion. Next, Attach a Servo Motor to an RFM69 End Node
|
|
|
Post by papa on Feb 12, 2019 15:58:49 GMT
Attach a Servo Motor to an RFM69 End Node Node Schematic, Updated: March 17, 2019
<< 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.
Next, Node Software
|
|
|
Post by papa on Feb 13, 2019 15:16:09 GMT
Control a Servo Motor with a User Interface Slider Node SoftwareGo this thread for the latest choose_nodes sketch where I tweaked code for servos provided by Joshua. From that thread, do the initial node customization, except use NODEID 05 as I do below or change the node reference below to match the NODEID you choose.
In the sketch, also uncomment #define SERVO
Next, OpenHAB Configurations: Thing & Channel
|
|
|
Post by papa on Feb 13, 2019 15:19:28 GMT
Control a Servo Motor with a User Interface Slider OpenHAB Configurations: Thing & ChannelIf you do not already have a Node05 Thing, use PaperUI to create one.
In the Node05 Thing, also create a Channel. channel id: Node05Channel34 label: Node05_Servo The channel's state topic is home/rfm_gw/nb/node05/dev34.
The command topic is home/rfm_gw/sb/node05/dev34
Next, More OpenHAB Configurations: Items
|
|
|
Post by papa on Feb 14, 2019 21:37:21 GMT
Control a Servo Motor with a User Interface Slider OpenHAB Configurations: ItemsIn OpenHAB's conf\items\ folder have a Node05Servo.items file that includes these entries: Number servoCmd05 "servoCmd05" { channel="mqtt:embedded-mqtt-broker:topic:mything:Node05Channel34" } Dimmer servoAngle05 "servoAngle05"
Next, OpenHAB Configuration: Link Item to Channel
|
|
|
Post by papa on Feb 14, 2019 21:45:30 GMT
Control a Servo Motor with a User Interface Slider OpenHAB Configuration: Link Item to Channel
At PaperUI\Configuration\Things\Node05, Link the Node05_Servo Channel to the servoCmd05 item
Next, OpenHAB Configuration: Rule
|
|
|
Post by papa on Feb 14, 2019 21:48:32 GMT
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
Next, OpenHAB Sitemap & Servo Operation
|
|
|
Post by papa on Feb 14, 2019 21:53:17 GMT
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:
Slider item=servoAngle05
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
|
|
|
Post by papa on Feb 15, 2019 14:58:29 GMT
Control a Servo Motor with a DS18B Temperature Sensor or Other Data 1) Possibly Changing the Choose_Nodes SketchFor a proof of concept example, I am using a DS18 sensor which is wired like the DS18B schematic in 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); I used an OpenHAB rule (Option 2) which is Next.
|
|
|
Post by papa on Feb 15, 2019 15:12:14 GMT
Control a Servo Motor with a DS18B Temperature Sensor or Other Data Option 2) Using an OpenHAB RuleIn the Node05 Thing, be sure you have a channel (type: Number) with the label: Node05_DS18temp Channel id: Node05Channel50 In an . items file (Node05.items ?), have this entry: Number TempDS18_Node05 "DS18Temp05 [%.1f °F]" <temperature> { channel="mqtt:embedded-mqtt-broker:topic:mything:Node05Channel50" } Link the Node05_DS18temp channel to the DS18Temp05 item.
Next, Option 2 Continues
|
|
|
Post by papa on Feb 15, 2019 15:16:25 GMT
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) end
Next, Comments
|
|
|
Post by papa on Feb 15, 2019 15:21:18 GMT
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.
|
|