|
Post by keshin on Jan 30, 2018 10:35:49 GMT
papa: The following posts are about an Alternate Ethernet Node programming that keshin found & then I offered some tweaking. As you can see, keshin wanted something that could control his 16 relay board.
This Alternate Ethernet Node could be built similar to the other Ethernet Node, but more digital pins on the Arduino Mega are connected to activating several relays on the board. Moreover, this node's programming uses a different MQTT message structure than the earlier Ethernet Node. The following posts (through Feb 3, 2018) were previously part of the Ethernet Node thread, but since that was confusing, I moved the posts to this thread. As long as the OpenHAB item entries follow the MQTT message structure that this node needs, this node can function alongside other nodes documented by this forum.
============================== keshin: what i have: *an arduino mega with ethernet shield & *a 16 relay board. *i installed openhab and mosquitto on a windows 7 pc. *openhab connects to ios openhab app alrighty folks, i have came across a simple version of the arduino, ethernet, openhab. attached at the end of this post is the sketch Add this line to your .item file Switch Light_PIN5 "Turn On Pin 5 Relay" {mqtt=">[mosquitto:arduino/lightControl:command:ON:5_1],>[mosquitto:arduino/lightControl:command:OFF:5_0],<[mosquitto:arduino/lightControl/5:state:default]", autoupdate="false" } To control multiple pins, simply copy and paste that line again, changing the first digit (PIN 5) to the pin you wish to control. HOWEVER: There is a problem and i cannot figure it out, only single digit pins are being recognized in openhab. What this means is i cannot get pins 10 through 53 to read. Also pins 10, 53, 52, 51, and 50 appear to already be producing output with this sketch. if anyone can help i would very much like to be able to use more pins to control more (eg 16 + relays) working_ethernet_simple.ino (2.13 KB)
|
|
|
Post by papa on Jan 30, 2018 14:40:35 GMT
keshin, above you wrote, "(papa's Ethernet Node ??) coding worked and have successfully run it on my mega with Ethernet shield." I'm interested in what more results you got with the Ethernet Node & sample OpenHAB entries for it that I supplied in the other thread. In some ways, I can understand your looking for something "simple," but you're introducing a different mqtt message structure which could be confusing. My understanding is that mqtt message structure is arbitrary, BUT the coding for message senders & their message receivers must parse messages the same way. At the same time, after thinking, I believe I see what you are trying to do: To have one sketch through which OpenHAB can toggle multiple pins (& their relays) without adding multiple device numbers to keep track of those pins. If you want OpenHAB to be a control board for this, working_ethernet_simple.ino might work for this. OpenHAB rules could also toggle these pins at scheduled times. However, this sketch has no code for toggling pins based on sensor readings. As you deal with glitches, keep in mind that you are using an Arduino Mega instead of an Arduino Uno compatible (like this forum typically uses). I know little about Megas. From my understanding, some shields (like some versions of the w5100 Ethernet Shield) truly work with Megas, & some do not (even if they say they do). At least on an Uno compatible, the w5100 Ethernet Shield uses pin D10 for its own purposes. I don't know what Mega pin the Ethernet Shield might use. Please supply a screen shot of the serial monitor results you go from this sketch. I found the source of working_ethernet_simple.ino here. The provider could have documented & commented it better. keshin, when you borrow code, please provide a link so we might look for related documentation & coding. I don't feel very knowledgeable about coding that sets up & handles message structure, but I'll give this a shot: It appears this sketch's parsing in the callback function (lines 24--) handles only one digit pin numbers. It looks like lines 26-33 pull in a message (a string) & add a null end-of-string ('/0') at the end of the message. I've added comments to the lines in the callback function to indicate what I think they are doing. Perhaps this will help you understand & tweak. if(length==3) // only accepts message length of 3 (plus null): 1) one-digit-pin-number 2) underscore 3) one-digit-command-number { //int number = atoi( buff[1] ); // ?? previous attempt at converting ascii to integer ?? int number = buff[2] - '0'; // command number at byte 2, convert from ascii code for the number to integer int channel = buff[0] - '0'; // pin number at ONLY byte 0 of message buffer (one digit), convert from ascii to integer Serial.println(number); Serial.println(channel); char feedbackTopic[256]; sprintf(feedbackTopic, "arduino/lightControl/%i",channel); pinMode(channel, OUTPUT); // set the designated pin to output digitalWrite(channel, number); // turn designated pin to ON (with 1) or OFF (with 0) I believe you'll need to rewrite lines 37-41 to accommodate 2 digit pin numbers without losing the option of using pin numbers with only one digit. Maybe the simplest is to require all pin numbers to be 2 digits & (in OpenHAB items) add a leading zero to single digit pins. Given the above hints in the comments, adjust the message structure to handle 2 digits, maybe something like this ... if(length== 4) // only accepts message length of 4 (plus null): 1&2) two-digit-pin-number 3) underscore 4) one-digit-command-number { int number = buff[ 3] - '0'; // command number at byte 3 of message buffer, convert from ascii code for the number to integer int channel = (buff[0] - '0') * 10 ; // 10's digit of pin number at byte 0 of message buffer, convert from ascii character code to integer channel = channel + (buff[1] - '0') ; // add 1's digit of pin number from byte 1 of message buffer, convert from ascii to integer Note: When it works, working_ethernet_simple.ino functions similarly to the Ethernet Node that I provided above. However, they use different mqtt message structures. Again, keshin, I would like to know more of your results from the Ethernet Node I supplied in the other thread.
|
|
|
Post by papa on Jan 31, 2018 15:21:05 GMT
keshin: "Would you like to know my results here so far or in pm?"
papa: Generally, post in the public forum notes so all can benefit from what is learned or confirmed (positive or negative). Use personal messages for personal communication or information that has security concerns.
keshin: "my apologies i forgot to credit the author"
keshin: "in regards to your code [tweaks in working_ethernet_simple.ino] for 2 digit pins, ... it is working, i can now call on double digit pins, however the script seems to ignore the "0" being placed in front of single digit pins (this is not a concern as i dont need to use those pins anyway however i thought i should let you know)"
papa: Say more about "the script seems to ignore the "0" being placed in front of single digit pins" Do you mean that with the tweaked code, OpenHAB Switch entries no longer toggle single digit pins even when the entry has a leading zero before the single digit?
With no access to an Ethernet Node for a few days, I cannot test the code at the moment.
|
|
|
Post by keshin on Feb 1, 2018 3:28:53 GMT
papa: Say more about "the script seems to ignore the "0" being placed in front of single digit pins" Do you mean that with the tweaked code, OpenHAB Switch entries no longer toggle single digit pins even when the entry has a leading zero before the single digit? keshin: yes OpenHAB Switch entries no longer toggle single digit pins even when the entry has a leading zero before the single digits however i currently have pins 22 through 37 working correctly. i have attached the sketch with the code you supplied inserted, working_ethernet_simple_double_digit_pins.ino (2.54 KB) below is my openhab items file list (these all work but if i add a switch aseg "05" it doesn't acknowledge) My.items (3.51 KB)
|
|
|
Post by papa on Feb 1, 2018 15:41:00 GMT
keshin: yes OpenHAB Switch entries no longer toggle single digit pins even when the entry has a leading zero
papa: Ok, perhaps I will study this more when I get time & access to my Ethernet Node. Maybe the OpenHAB logs will provide hints. ============================================
I did notice a couple things that concerned me.
1) Your My.items have the format:
Switch Light_PIN22 "Turn On Pin 22 Relay" {mqtt=">[mosquitto:arduino/lightControl:command:ON:22_0],>[mosquitto:arduino/lightControl:command:OFF:22_1],<[mosquitto:arduino/lightControl/22:state:default]", autoupdate="false" }
Earlier you listed the item like this (which I believe is closer to correct): Switch Light_PIN5 "Turn On Pin 5 Relay" {mqtt=">[mosquitto:arduino/lightControl:command:ON:5_1],>[mosquitto:arduino/lightControl:command:OFF:5_0],<[mosquitto:arduino/lightControl/5:state:default]", autoupdate="false" }
The sketch works like your previous format: 1 turns the pin on & 0 turns it off
Previously on this forum, we learned that if these two mqtt bindings are used, they function more completely if the "state" binding is before the "command" binding.
2) When we included autoupdate="false" in an OpenHAB 1.x item & clicked the virtual UI switch, the switch showed momentarily on & then returned to off position. Since I've moved to OpenHAB 2, I don't believe autoupdate="false" has worked very reliably.
If my comments above are correct, that could change your items' format to:
Switch Light_PIN22 "Toggle Pin 22 Relay" {mqtt="<[mosquitto:arduino/lightControl/22:state:default],>[mosquitto:arduino/lightControl:command:ON:22_1],>[mosquitto:arduino/lightControl:command:OFF:22_0]" }
|
|
|
Post by keshin on Feb 3, 2018 11:24:32 GMT
keshin: yes OpenHAB Switch entries no longer toggle single digit pins even when the entry has a leading zero papa: Ok, perhaps I will study this more when I get time & access to my Ethernet Node. Maybe the OpenHAB logs will provide hints. ============================================ I did notice a couple things that concerned me. 1) Your My.items have the format: Switch Light_PIN22 "Turn On Pin 22 Relay" {mqtt=">[mosquitto:arduino/lightControl: command: ON:22_ 0],>[mosquitto:arduino/lightControl:command: OFF:22_ 1],<[mosquitto:arduino/lightControl/22: state:default]", autoupdate="false" } Earlier you listed the item like this (which I believe is closer to correct): Switch Light_PIN5 "Turn On Pin 5 Relay" {mqtt=">[mosquitto:arduino/lightControl:command: ON:5_ 1],>[mosquitto:arduino/lightControl:command: OFF:5_ 0],<[mosquitto:arduino/lightControl/5: state:default]", autoupdate="false" } The sketch works like your previous format: 1 turns the pin on & 0 turns it off Previously on this forum, we learned that if these two mqtt bindings are used, they function more completely if the "state" binding is before the "command" binding. 2) When we included autoupdate="false" in an OpenHAB 1.x item & clicked the virtual UI switch, the switch showed momentarily on & then returned to off position. Since I've moved to OpenHAB 2, I don't believe autoupdate="false" has worked very reliably. If my comments above are correct, that could change your items' format to: Switch Light_PIN22 "Toggle Pin 22 Relay" {mqtt="<[mosquitto:arduino/lightControl/22: state:default],>[mosquitto:arduino/lightControl:command: ON:22_ 1],>[mosquitto:arduino/lightControl:command: OFF:22_ 0]" }
|
|
|
Post by papa on Feb 7, 2018 20:59:45 GMT
Reserved
|
|
|
Post by papa on Feb 8, 2018 1:35:24 GMT
Now having access to an Ethernet Node & OpenHAB 2, I uploaded I installed working_ethernet_simple_double_digit_pins.ino
On the Ethernet Node, I installed LEDs (plus 100 ohm resistor) between GND & D7 AND GND & D7.
I tested these items Switch Light_PIN07 "Toggle Pin 07 Relay" {mqtt="<[mosquitto:arduino/lightControl/07:state:default],>[mosquitto:arduino/lightControl:command:ON:07_1],>[mosquitto:arduino/lightControl:command:OFF:07_0]" } Switch Light_PIN05 "Toggle Pin 05 Relay" {mqtt="<[mosquitto:arduino/lightControl/05:state:default],>[mosquitto:arduino/lightControl:command:ON:05_1],>[mosquitto:arduino/lightControl:command:OFF:05_0]" }
I tested the items with these sitemap entries Switch item=Light_PIN07 Switch item=Light_PIN05
Then on the OpenHAB User Interface, I successfully used the virtual switches (Light_PIN07 & Light_PIN05) to toggle the LEDs on D7 & D5.
I also installed LEDs (plus resistors) between GND & D10, D11, D12. When the node was powered, the LEDs on each of those pins already became lit.
Using comparable items & sitemap entries, the Serial Monitor showed the node received the message from clicking the virtual switches. However, clicking the UI switches did NOT affect the state of the LEDs on D10-D12.
I know that the W5100 Ethernet Shield uses D10 for its own purposes & now I suspect it also uses D11, D12 & maybe D13.
Since I have only a Buono Uno (Arduino Uno compatible), whose digital pins top out at D13, I am not able to test double digit pin numbers, but I believe that keshin's previous results suggest that double digit pins work on Arduino Megas, but perhaps not with D10-D13 when an Ethernet Shield is installed.
|
|
|
Post by papa on Feb 13, 2018 2:42:21 GMT
I just received an Arduino Mega today to test this Alternate Ethernet Node sketch that requires double digit pins (adding a leading zero to single digit pins).
I tested these items Switch Light_PIN07 "Toggle Pin 07 LED" {mqtt="<[mosquitto:arduino/lightControl/07:state:default],>[mosquitto:arduino/lightControl:command:ON:07_1],>[mosquitto:arduino/lightControl:command:OFF:07_0]" }
Switch Light_PIN22 "Toggle Pin 22 LED" {mqtt="<[mosquitto:arduino/lightControl/22:state:default],>[mosquitto:arduino/lightControl:command:ON:22_1],>[mosquitto:arduino/lightControl:command:OFF:22_0]" }
I tested the items with these sitemap entries Switch item=Light_PIN07 Switch item=Light_PIN22
The resulting virtual switches on the OpenHAB User Interface worked just fine to toggle the LEDs on both the single digit pin (with leading zero) & the double digit pin.
|
|
|
Post by struisraspi on Jan 20, 2019 16:30:58 GMT
Hi
Will the code and information in this post still be valid for openhab2 , MQTT v2 broker and an Arduino uno with an ethernet shield. I have tried it and no luck. I know my broker is working because I can control sonoffs flashed with tasmota code. I use a .things file to describe the functions of the sonoffs.
|
|
|
Post by papa on Jan 20, 2019 20:55:59 GMT
struisraspi: "Will the code and information in this post still be valid for openhab2 , MQTT v2 broker, and an Arduino uno with an Ethernet shield?"
papa: For me, it worked with OpenHAB2, Mosquitto MQTT v3.1 broker, & an Buono Uno Arduino compatible (at 3.3 volts) & an Ethernet shield?
|
|
|
Post by struisraspi on Jan 21, 2019 18:34:21 GMT
Thanks for the response papa. I tried it but somehow do not get the correct results. I think a lot changed when openhab moved to the mqtt 2.4 binding or I am missing some crucial setting. I will try again.
|
|
|
Post by papa on Jan 21, 2019 19:21:19 GMT
struisraspi: "I think a lot changed when openhab moved to the mqtt 2.4 binding"
papa: Oh, that may be the problem with following my documentation. I used & am still using mqtt 1 legacy binding which has worked well for me.
I'm in the middle of installing a more recent OpenHAB2 & updating related documentation, but perhaps along with that (or after) I'll look at mqtt 2 binding.
|
|
|
Post by papa on Jan 22, 2019 20:41:36 GMT
struisraspi, for months, I have been using OpenHAB 2.1.0 which worked fine with the mqtt 1.13 binding & Mosquitto MQTT message broker. (On a Windows machine), I am now working through an install of OpenHAB 2.4.0. As part of that, I am looking at documentation on using the new MQTT 2.4.0 Binding & will try on this forum to document what I'm seeing & learning. community.openhab.org/t/migrating-mqtt1-items-to-mqtt2-4-items/60502/6This community OpenHAB thread seems interesting & could help upgrade my MQTT1 stuff to MQTT 2. However, one thing I noted in this post is that we can install & use both MQTT bindings. So one thing you might try for now is install the Mosquitto MQTT message broker (on your computer) & the MQTT 1.13 binding & follow my documentation. As we better understand MQTT binding 2.4.0, we can transition to that.
|
|