Help for my arduino module

Begonnen von M.K., 03 November 2013, 03:57:48

Vorheriges Thema - Nächstes Thema

M.K.

I am trying to write my own module to access my Arduino by serial connection (supposed to be easiest).
Actually it is a wireless project developed by a british company called ciseco (http://shop.ciseco.co.uk/).

I have a basic module which looks like this:
(http://www.fhemwiki.de/wiki/DevelopmentGuidelines#Mechanismus_fuer_aktive_Readings this helped)

# $Id: 00_INO.pm 2076 2013-10-06

package main;

use strict;
use warnings;
use Time::HiRes qw(gettimeofday sleep);
use SetExtensions;

##################################### #
sub #
INO_Initialize($)     # initialisierung
{
  my ($hash) = @_; #

  require "$attr{global}{modpath}/FHEM/DevIo.pm"; #

  $hash->{DefFn}    = "INO_Define";             #
  $hash->{UndefFn}  = "INO_Undef";     #
  $hash->{AttrList} = "IODev do_not_notify:0,1 showtime:0,1 ".
                      "loglevel:0,1,2,3,4,5,6".
                      $readingFnAttributes;
}

##################################### #
sub #
INO_Define($$)     #
{ #
  my ($hash, $def) = @_; #
  my @a = split("[ \t][ \t]*", $def); #???
  $hash->{INTERVAL} = 10;

  return "wrong syntax: define <name> INO [devicename|none]" #
    if(@a != 3); #

  DevIo_CloseDev($hash); #
  my $name = $a[0]; #
  my $dev = $a[2]; #

  if($dev eq "none") { #
    Log 1, "INO device is none, commands will be echoed only";     #
    return undef; #
  } #
 
  $hash->{DeviceName} = $dev; #
  my $ret = DevIo_OpenDev($hash, 0);             #
  #main::readingsSingleUpdate($hash,"state","Initialized",1);
  InternalTimer(gettimeofday()+$hash->{INTERVAL}, "INO_GetUpdate", $hash, 0);
  return $ret; #
} #

#####################################
sub
INO_GetUpdate($) {
    my ($hash) = @_;

    # start internal timer; do it at the beginning to achieve equal intervals no matter how long it takes to gather data
    InternalTimer(gettimeofday()+$hash->{INTERVAL}, "INO_GetUpdate", $hash, 1);
    # gather data
    DevIo_SimpleWrite($hash, "T",0);
    my $Tmp = DevIo_SimpleRead($hash);
    readingsSingleUpdate($hash,"Temperatur",$Tmp, 1);
   
    return $Tmp;
}

##################################### #
sub #
INO_Undef($$)     #
{ #
  my ($hash, $arg) = @_; #
  DevIo_CloseDev($hash); #
  RemoveInternalTimer($hash); #
  return undef; #
}

1;


I can read data from my Arduino and it is written to the logfile BUT the logfile looks weird:
2013-11-03_11:30:16 MyIno Temperatur: 21.00


2013-11-03_11:30:26 MyIno Temperatur: 21.00


2013-11-03_11:30:36 MyIno Temperatur: 21.00


2013-11-03_11:30:46 MyIno Temperatur: 21.00


2013-11-03_11:30:56 MyIno Temperatur: 21.00


2013-11-03_11:31:06 MyIno Temperatur: 21.00




I dont get why there are 2 free lines!

Also, I cant get a plot working.
I am new to perl and fhem, so am I missing something important for plotting?

My fhem.cfg looks like this:
define MyIno INO COM3@115200
attr MyIno loglevel 5
attr MyIno room wz_Heizung
attr MyIno stateFormat {sprintf("T: %.1f",ReadingsVal("MyIno","Temperatur",0))}
#attr MyIno userReadings Temperatur {ReadingsVal("MyIno","Temperatur",0)}

#attr MyIno userReadings Hum

define fl_Temp FileLog D:\fhem-5.4\log\Temperatur-%Y-%m.log MyIno
attr fl_Temp logtype text
attr fl_Temp room Logs

define wl_Temp weblink fileplot fl_Temp:MyIno:CURRENT


Weblink returns this error
ZitatThis page contains the following errors:

error on line 1 at column 1: Extra content at the end of the document
Below is a rendering of the page up to the first error.

hints please  ;)

Edit1:
OK i giess writing a post helps reflecting things: the 2 lines problem is solved. they were read from the serial port and i need to cut them off.
However plotting still not working!

Edit2: After updating to 5.5 plot works. same settings. well... i will improve my module by time and post again when i feel it is usable

AR@WR

Hi,

as I have an Arduino that is reading several room temperatures and switches a relay I'm also interested in integrating it into my FHEM system.

Have you made any improvements to your code in the meantime ?

Kind Regards

   Arno

bugster_de

Hi,

not exactly to your question, but maybe helpful:

"loglevel:0,1,2,3,4,5,6".
you should have a space after the 6:
"loglevel:0,1,2,3,4,5,6 ".

Zitatmy $Tmp = DevIo_SimpleRead($hash);
I assume, that this code is just working by accident, as the serial line is rather slow. in the read buffer, you might have uncomplete commands in the queue so you should check, if the received data is complete or if this is just a fraction of the code. As said, might work for slow serial connections, but if you move to something faster like Ethernet, that will fail.