vom Python Sript daten an device überteragen

Begonnen von saschaev, 24 November 2019, 13:51:44

Vorheriges Thema - Nächstes Thema

Wernieman

Da Ihr über telnet daten versendet, warum nicht gleich "Hardcore" über nc?

Spart recourcen ...
- Bitte um Input für Output
- When there is a Shell, there is a Way
- Wann war Dein letztes Backup?

Wie man Fragen stellt: https://tty1.net/smart-questions_de.html

saschaev

 perl /opt/fhem/fhem.pl 7072 "setreading Zisterne_Inhalt Fuelstand 100"
Can't connect to localhost:7072

amenomade

@Wernieman: Hatte ich für später bei Seite gelassen. Ich möchte zuerst verstehen, wo es bei ihm hängt. Im Moment kann ich mir nicht vorstellen, wie/was/wo er macht

@saschaev: na dann musst Du gucken, ob in deinem Fhem eine telent Instanz definiert ist. Man mal über die Fhem Oberfläsche ein "list TYPE=telnet"
Pi 3B, Alexa, CUL868+Selbstbau 1/2λ-Dipol-Antenne, USB Optolink / Vitotronic, Debmatic und HM / HmIP Komponenten, Rademacher Duofern Jalousien, Fritz!Dect Thermostaten, Proteus

saschaev

Wenn ich
list TYPE=telnet
Eingebe kommt nichts
Also hab ich es nicht eingerichtet?

saschaev

Hab jetzt das Device angelegt

Ausgabe bei Fhem

Internals:
   CFGFN     
   CONNECTS   1
   DEF        7072 global
   FD         15
   FUUID      5ddbd046-f33f-c00a-765a-9a553c7c276da2d3
   NAME       telnetPort
   NR         1304
   PORT       7072
   STATE      Initialized
   TYPE       telnet
   READINGS:
     2019-11-25 13:59:50   state           Initialized
Attributes:

saschaev

So habe jetzt das in die Konsole eingeben

perl /opt/fhem/fhem.pl 7072 "setreading Zisterne_Inhalt Fuelstand 100"


Dann zeigt er mir in den dummy Ziterne_Inhalt bei Readings 100 an . Aber da muss ich auf den Dummy gehen.
Hier mal die Raw Definition

defmod Zisterne_Inhalt dummy
attr Zisterne_Inhalt room Zisterne
attr Zisterne_Inhalt stateFormat Wasserstand
attr Zisterne_Inhalt userReadings Zisterne Fuelhoehe

setstate Zisterne_Inhalt Wasserstand
setstate Zisterne_Inhalt 2019-11-25 17:23:09 Fuelstand 100

CoolTux

stateFormat

Und bitte die Dokumentation lesen. Oder wegen meiner YouTube Videos schauen.
Du musst nicht wissen wie es geht! Du musst nur wissen wo es steht, wie es geht.
Support me to buy new test hardware for development: https://www.paypal.com/paypalme/MOldenburg
My FHEM Git: https://git.cooltux.net/FHEM/
Das TuxNet Wiki:
https://www.cooltux.net

amenomade

OK das ist schon mal gut. Du kannst von ausserhalb Fhem einen Wert in Fhem setzen.

Wie sieht es jetzt mit python aus?
Ich glaube immer noch nicht, dass ('.......')  eine richtige Syntax ist, um ein shell Kommando aus einem Python Skript zu starten. Es soll eher so gemacht werden:
cmd = 'setreading Zisterne_Inhalt Fuelstand ' + str(Fuelstand)
import subprocess
subprocess.run(['perl', '/opt/fhem/fhem.pl', '7072', cmd])

Wie gesagt, bin aber kein python Entwickler.

Letztens musst Du entscheiden, wo genau Du diesen Wert in Fhem speichern möchtest. Wenn Du es als state des Dummys willst, dann statt
setreading Zisterne_Inhalt Fuelstand <Wert>
eher
set Zisterne_Inhalt <Wert>

Oder wie CoolTux sagt, stateFormat benutzen.


Pi 3B, Alexa, CUL868+Selbstbau 1/2λ-Dipol-Antenne, USB Optolink / Vitotronic, Debmatic und HM / HmIP Komponenten, Rademacher Duofern Jalousien, Fritz!Dect Thermostaten, Proteus

amenomade

Und wie Wernieman geschrieben hat, noch besser wäre es mit nc
cmd = 'setreading Zisterne_Inhalt Fuelstand ' + str(Fuelstand)
import subprocess
p1 = subprocess.run(['echo', cmd], stdout=subprocess.PIPE)
p2 = subprocess.run(['nc', '127.0.0.1', '7072'], stdin=p1.stdout)

nicht getestet, und vielleicht gibt es elegantete Art und Weise.
Pi 3B, Alexa, CUL868+Selbstbau 1/2λ-Dipol-Antenne, USB Optolink / Vitotronic, Debmatic und HM / HmIP Komponenten, Rademacher Duofern Jalousien, Fritz!Dect Thermostaten, Proteus

saschaev

Bei

cmd = 'setreading Zisterne_Inhalt Fuelstand ' + str(Fuelstand)
import subprocess
subprocess.run(['perl', '/opt/fhem/fhem.pl', '7072', cmd])

Bekomme ich Fehler

                                                                                ^
SyntaxError: invalid syntax
sh: 1: Value: not found
2019.11.25 18:07:43 3: actWasserstandZisterne return value: -1
sudo: /etc/sudoers.d ist für alle beschreibbar (world writable)
  File "/home/pi/zisterne.py", line 103
    import subprocess
    ^
IndentationError: unexpected unindent

amenomade

Zeig dein komplettes Skript (bitte in Code Tags - das # Zeichen im Edit Menu des Forums). Da sind anscheinend mehrere Fehler drin
Pi 3B, Alexa, CUL868+Selbstbau 1/2λ-Dipol-Antenne, USB Optolink / Vitotronic, Debmatic und HM / HmIP Komponenten, Rademacher Duofern Jalousien, Fritz!Dect Thermostaten, Proteus

saschaev

Sorry es waren die Leerzeichen, bin auf der Arbeit mit dem Handy am gange.Ist ein wenig unübersichtlich.

Jetzt kommt der Fehler.

IndentationError: unexpected unindent
sh: 1: Value: not found
2019.11.25 18:27:22 3: actWasserstandZisterne return value: -1
sudo: /etc/sudoers.d ist für alle beschreibbar (world writable)
2019-11-25_18:27:27 Zisterne Fuelhoehe: 180.7 cm Liter: 3614 l
Traceback (most recent call last):
  File "/home/pi/zisterne.py", line 126, in <module>
    main()
  File "/home/pi/zisterne.py", line 104, in main
    subprocess.run(['perl', '/opt/fhem/fhem.pl', '7072', cmd])
AttributeError: 'module' object has no attribute 'run'

saschaev


#!/usr/bin/env python
# -*- coding: utf-8 -*-


# import required modules
import time
import datetime
import RPi.GPIO as GPIO


# define GPIO pins
GPIOTrigger = 18
GPIOEcho    = 24


# function to measure the distance
def MeasureDistance():
  # set trigger to high
  time.sleep(0.2)
  GPIO.output(GPIOTrigger, True)


  # set trigger after 10Ã,µs to low
  time.sleep(0.0001)
  GPIO.output(GPIOTrigger, False)


  # store initial start time
  StartTime = time.time()


  # store start time
  while GPIO.input(GPIOEcho) == 0:
    StartTime = time.time()


  # store stop time
  while GPIO.input(GPIOEcho) == 1:
    StopTime = time.time()


  # calculate distance
  TimeElapsed = StopTime - StartTime
  Distance = (TimeElapsed * 34400) / 2





  return Distance


# main function
def main():
  try:
#    while True:
      Distance0 = MeasureDistance()
      Distance01 = MeasureDistance()
      Distance02 = MeasureDistance()
      Distance03 = MeasureDistance()
      Distance04 = MeasureDistance()
      Distance05 = MeasureDistance()
      Distance06 = MeasureDistance()
      Distance07 = MeasureDistance()
      Distance08 = MeasureDistance()
      Distance09 = MeasureDistance()
      Distance10 = MeasureDistance()
      Distance11 = MeasureDistance()
      Distance12 = MeasureDistance()
      Distance13 = MeasureDistance()
      Distance14 = MeasureDistance()
      Distance15 = MeasureDistance()
      Distance16 = MeasureDistance()
      Distance17 = MeasureDistance()
      Distance18 = MeasureDistance()
      Distance19 = MeasureDistance()
      Distance20 = MeasureDistance()
      Distance_sum = Distance01 + Distance02 + Distance03 + Distance04 + Distance05 + Distance06 + Distance07 + Distance08 + Distance09 + Distance10 + Distance11 + Distance12 + Distance13 + Distance14 + Distance15 + Distance16 + Distance17 + Distance18 + Distance19 + Distance20
      Distance = round(Distance_sum / 20,1)

#    Meine Tanks haben Maximal 5000 Liter bei 220 cm Füllhöhe
#    Zusätzlich 7 cm Offset vom Einbauort des Sensors
      Fuelstand = 250 - Distance
      Liter = 5000 / 250 * Fuelstand
      Zeit = time.time()

      ZeitStempel = datetime.datetime.fromtimestamp(Zeit).strftime('%Y-%m-%d_%H:%M:%S')

      print (ZeitStempel),("Zisterne Fuelhoehe: %.1f cm" % Fuelstand),("Liter: %.0f l" % Liter)


      time.sleep(2)


#Werte an FHEM uebergeben



      cmd = 'setreading Zisterne_Inhalt Fuelstand ' + str(Fuelstand)
      import subprocess
      subprocess.run(['perl', '/opt/fhem/fhem.pl', '7072', cmd])

     

# reset GPIO settings if user pressed Ctrl+C
  except KeyboardInterrupt:
    print("Measurement stopped by user")


if __name__ == '__main__':
  # set GPIO disable warnings
      GPIO.setwarnings(False)
      GPIO.cleanup()
  # use GPIO pin numbering convention
      GPIO.setmode(GPIO.BCM)
  # set up GPIO pins
      GPIO.setup(GPIOTrigger, GPIO.OUT)
      GPIO.setup(GPIOEcho, GPIO.IN)
  # set trigger to false
      GPIO.output(GPIOTrigger, False)

  # call main function
main()


amenomade

Pi 3B, Alexa, CUL868+Selbstbau 1/2λ-Dipol-Antenne, USB Optolink / Vitotronic, Debmatic und HM / HmIP Komponenten, Rademacher Duofern Jalousien, Fritz!Dect Thermostaten, Proteus

saschaev