sending string via nrf and the other receivers making trouble - why?
-
Hello,
ich have configured an arduino nano via nrf24L01+ and a LED-Stripe. My Idea is to send a LEDNumber and a HEX-Code for the color to set the LED to Color xy...I use FHEM and a own Perl-Method to convert the HEX Code to an Splittable string. This i send via Network-GW to my arduino as String.
At this moment where the code is sending other receivers like Temp-Sensors sending 12502°C as Temperatur...I dont find the problem.
Can somebody help?The sendet code via FHEM is:
set <NAME> status7 0-0-255#include <MySensor.h> #include <SPI.h> #include <EEPROM.h> #include <MyTransportNRF24.h> #include <MyHwATMega328.h> #include <RemoteReceiver.h> #include <RemoteTransmitter.h> #include <InterruptChain.h> #include "FastLED.h" #define DATA_PIN 3 #define CLOCK_PIN 13 #define NUM_LEDS 19 CRGB leds[NUM_LEDS]; #define NUMBER_OF_OUTLETS 99 #define SN "LEDRahmenEG" #define SV "1.6" const byte SC_CHILD_ID = 0 ; //unsigned long receivedCode = 0 ; MySensor gw; MyMessage scene_on(SC_CHILD_ID, V_LEVEL); MyMessage scene_off(SC_CHILD_ID, V_LEVEL); void setup() { Serial.begin(115200); gw.begin(incomingMessage); gw.sendSketchInfo(SN, SV); FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS); //TEST //leds[9].g=1; //leds[9].b=255; //leds[10].r=255; //leds[11].b=255; //FastLED.show(); // Create a child device for each outlet for (int sensor = 1; sensor <= NUMBER_OF_OUTLETS; sensor++) { gw.present(sensor, S_LIGHT); delay(2); } } void loop() { gw.process(); } String getValue(String data, char separator, int index) { int found = 0; int strIndex[] = {0, -1}; int maxIndex = data.length() - 1; for (int i = 0; i <= maxIndex && found <= index; i++) { if (data.charAt(i) == separator || i == maxIndex) { found++; strIndex[0] = strIndex[1] + 1; strIndex[1] = (i == maxIndex) ? i + 1 : i; } } return found > index ? data.substring(strIndex[0], strIndex[1]) : ""; } //command has been received from gateway void incomingMessage(const MyMessage &message) { Serial.println(message.sensor); Serial.println("......."); Serial.println(message.getInt()); if (message.type == V_LIGHT) { Serial.print("LED ID #: "); Serial.println(message.sensor); Serial.print("Color: "); Serial.println(message.getString()); //receive String like 125-255-111 call getValue and split //.toInt() convert the String Number to int and would be assigned to the LED leds[message.sensor].r = getValue(message.getString(), '-', 0).toInt(); leds[message.sensor].g = getValue(message.getString(), '-', 1).toInt(); leds[message.sensor].b = getValue(message.getString(), '-', 2).toInt(); FastLED.show(); if (message.sensor == 99) { for (int i = 0; i < NUM_LEDS; ++i) { leds[i].r = getValue(message.getString(), '-', 0).toInt(); leds[i].g = getValue(message.getString(), '-', 1).toInt(); leds[i].b = getValue(message.getString(), '-', 2).toInt(); FastLED.show(); } } } delay(50); }The Serial-Monitor shows
- message.sensor = 7
- message.getString() = 0-0-255enEG
Why is there "enEG" at the end?
Thanks
Christian -
OK,
my Mysensors version is 1.5.3 i will update it.
Do i have to update my Gateway too?In the link they talk about issues with the Getters in version 1.5.4. is this what you think that could be the problem?!
Thanks
@choffmann I would be very happy if you didn't mind to also share some of your FHEM code....?
-
i hope that i can update my arduino tomorrow, so that i can test if this will work....
The FHEM code you want:
The Devices:
define LEDRahmen MYSENSORS_DEVICE 102 ... autmatic including des Reading/Devices
define LEDRahmenSwitch dummy -- for setting LED and Color manually
define LEDRahmenSwitchNotify notify LEDRahmenSwitch.* { sendLEDColor($EVTPART0, $EVTPART1) }and the 99_myUtils.pm Mehtods:
(NEW: i have changed the Method from send String like 0-255-0 to 000-255-000 - see Method "changeLengthTo" )sub sendLEDColor($$){ my ($stat,$val) = @_; my $tmp; $tmp .= changeLengthTo(hexstr_to_signed32int(substr($val,0,2)),3); $tmp .= "-"; $tmp .= changeLengthTo(hexstr_to_signed32int(substr($val,2,2)),3); $tmp .= "-"; $tmp .= changeLengthTo(hexstr_to_signed32int(substr($val,4,6)),3); #my $m = hexstr_to_signed32int($tmp); #Log 1, "MeinE Testmethode"; #Log 1, $; fhem("set LEDRahmen status".$stat." ".$tmp.""); } sub changeLengthTo($$){ my($str,$toLength) = @_; my $tmpstr; for(my $i = (length($str)+1); $i <= $toLength; $i++){ $tmpstr.= "0"; } return $tmpstr.$str; } sub hexstr_to_signed32int($) { my ($hexstr) = @_; die "Invalid hex string: $hexstr" if $hexstr !~ /^[0-9A-Fa-f]{1,8}$/; my $num = hex($hexstr); return $num >> 31 ? $num - 2 ** 32 : $num; } -
i hope that i can update my arduino tomorrow, so that i can test if this will work....
The FHEM code you want:
The Devices:
define LEDRahmen MYSENSORS_DEVICE 102 ... autmatic including des Reading/Devices
define LEDRahmenSwitch dummy -- for setting LED and Color manually
define LEDRahmenSwitchNotify notify LEDRahmenSwitch.* { sendLEDColor($EVTPART0, $EVTPART1) }and the 99_myUtils.pm Mehtods:
(NEW: i have changed the Method from send String like 0-255-0 to 000-255-000 - see Method "changeLengthTo" )sub sendLEDColor($$){ my ($stat,$val) = @_; my $tmp; $tmp .= changeLengthTo(hexstr_to_signed32int(substr($val,0,2)),3); $tmp .= "-"; $tmp .= changeLengthTo(hexstr_to_signed32int(substr($val,2,2)),3); $tmp .= "-"; $tmp .= changeLengthTo(hexstr_to_signed32int(substr($val,4,6)),3); #my $m = hexstr_to_signed32int($tmp); #Log 1, "MeinE Testmethode"; #Log 1, $; fhem("set LEDRahmen status".$stat." ".$tmp.""); } sub changeLengthTo($$){ my($str,$toLength) = @_; my $tmpstr; for(my $i = (length($str)+1); $i <= $toLength; $i++){ $tmpstr.= "0"; } return $tmpstr.$str; } sub hexstr_to_signed32int($) { my ($hexstr) = @_; die "Invalid hex string: $hexstr" if $hexstr !~ /^[0-9A-Fa-f]{1,8}$/; my $num = hex($hexstr); return $num >> 31 ? $num - 2 ** 32 : $num; }@choffmann Thanks a lot! Please let us know you if the update solved your issue.
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login