SHARE
TWEET

Untitled

a guest Mar 21st, 2017 48 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2. import minimalmodbus
  3. import requests
  4. import time
  5. import sys
  6. import datetime
  7. import random
  8. import traceback
  9.  
  10. #sys.stdout = open('/home/pi/nilan.log', 'w')
  11. #instrument = minimalmodbus.Instrument('/dev/ttyUSB0', 30) # port name, slave address (in decimal)
  12. #instrument.serial.parity   = minimalmodbus.serial.PARITY_EVEN
  13.  
  14. #sudo strace -ewrite -p 2222   pour voir le stdout quand il tourne en bg
  15.  
  16. hum_hist0 = 0
  17. hum_hist1 = 0
  18. flag_hum = 0
  19.  
  20.  
  21. def lire_nilan (un, deux, trois, quatre):
  22.     success = 0
  23.         while (success != 1):
  24.             try:
  25.                     resultat = instrument.read_register(un, deux, trois, quatre)
  26.                         success = 1
  27.                 except:
  28.             print 'exception lecture'
  29.                     print traceback.format_exc()
  30.                 time.sleep (2)
  31.  
  32.     return resultat
  33.  
  34. def ecrire_nilan (un, deux, trois):
  35.     success = 0
  36.         while (success != 1):
  37.                 try:
  38.                         instrument.write_register(un, deux, trois)
  39.                         success = 1
  40.                 except:
  41.             print 'exception ecriture'
  42.             print traceback.format_exc()
  43.                         time.sleep (2)
  44.  
  45.  
  46. while (1):
  47.     try:
  48.  
  49.         instrument = minimalmodbus.Instrument('/dev/serial/by-id/usb-FTDI_FT232R_USB_UART_A9O3R1L9-if00-port0', 30) # port name, slave address (in decimal)
  50.         instrument.serial.parity   = minimalmodbus.serial.PARITY_EVEN
  51.         instrument.CLOSE_PORT_AFTER_EACH_CALL=True
  52.         print time.strftime('%X %x %Z')
  53.  
  54. #                AirFilterAlarm = lire_nilan(101, 0, 4, False) # Registernumber, number of decimals
  55. #                print 'AirFilterAlarm 101 = ', AirFilterAlarm
  56.  
  57.  
  58.                 Alarm = lire_nilan(400, 0, 4, False) # Registernumber, number of decimals
  59.                 print 'Alarm 400 = ', Alarm
  60.    
  61.  
  62. #                Alarm1 = lire_nilan(401, 0, 4, False) # Registernumber, number of decimals
  63. #                print 'Alarm1 401 = ', Alarm1
  64.  
  65.  
  66. #                Alarm2 = lire_nilan(404, 0, 4, False) # Registernumber, number of decimals
  67. #                print 'Alarm2 404 = ', Alarm2
  68.         try:
  69.             if (Alarm > 0):
  70.                             request = "http://192.168.0.19:9090/json.htm?type=command&param=switchlight&idx=92&switchcmd=On&level=0"
  71.                             r = requests.get(request, timeout=13)
  72.             else:
  73.                             request = "http://192.168.0.19:9090/json.htm?type=command&param=switchlight&idx=92&switchcmd=Off&level=0"
  74.                             r = requests.get(request, timeout=13)
  75.         except requests.RequestException:
  76.             pass
  77.  
  78.  
  79.  
  80.         ## Read temperature (PV = ProcessValue) ##
  81.         temp_maison = lire_nilan(210, 2, 4, True) # Registernumber, number of decimals
  82.         print 'Temp. maison = ', temp_maison
  83.         if temp_maison < 5 :
  84.             os.exit()
  85.  
  86.         temp_puls = lire_nilan(207, 2, 4, True) # Registernumber, number of decimals
  87.         print 'Temp. air pulsee = ', temp_puls
  88.         request = "http://192.168.0.19:9090/json.htm?type=command&param=udevice&idx=41&nvalue=0&svalue=" + str(temp_puls)
  89.         try:
  90.             r = requests.get(request, timeout=13)
  91.         except requests.RequestException:
  92.             pass
  93. #       request = "http://emoncms.org/input/post.json?json={Idx41:" + str(temp_puls) + "}&apikey=mettre_sa_clé"
  94. #                try:
  95. #           r = requests.get(request, timeout=13)
  96. #       except requests.RequestException:
  97. #                        pass
  98.  
  99.         #Outdoor temp
  100.         temp_ext = lire_nilan(208, 2, 4, True) # Registernumber, number of decimals
  101.         print 'Temp. ext. = ', temp_ext
  102.         request = "http://192.168.0.19:9090/json.htm?type=command&param=udevice&idx=38&nvalue=0&svalue=" + str(temp_ext)
  103.         try:
  104.             r = requests.get(request, timeout=13)
  105.         except requests.RequestException:
  106.             pass
  107. #       request = "http://emoncms.org/input/post.json?json={Idx38:" + str(temp_ext) + "}&apikey=mettre_sa_clé"
  108. #                try:
  109. #           r = requests.get(request, timeout=13)
  110. #                except requests.RequestException:
  111. #                        pass
  112.  
  113.    
  114.         temp_air_extrait = lire_nilan(210, 2, 4, True) # Registernumber, number of decimals
  115.         print 'Temp. air extrait = ', temp_air_extrait
  116.        
  117.         temp_haut = lire_nilan(211, 2, 4, True) # Registernumber, number of decimals
  118.         print 'Temp. haut du ballon = ', temp_haut
  119.         request = "http://192.168.0.19:9090/json.htm?type=command&param=udevice&idx=39&nvalue=0&svalue=" + str(temp_haut)
  120.         try:
  121.             r = requests.get(request, timeout=13)
  122.         except requests.RequestException:
  123.             pass
  124. #       request = "http://emoncms.org/input/post.json?json={Idx39:" + str(temp_haut) + "}&apikey=mettre_sa_clé"
  125. #                try:
  126. #           r = requests.get(request, timeout=13)
  127. #                except requests.RequestException:
  128. #                        pass
  129.    
  130.  
  131.         temp_bas = lire_nilan(212, 2, 4, True) # Registernumber, number of decimals
  132.         print 'Temp. bas du ballon = ', temp_bas
  133.         request = "http://192.168.0.19:9090/json.htm?type=command&param=udevice&idx=40&nvalue=0&svalue=" + str(temp_bas)
  134.         try:
  135.             r = requests.get(request, timeout=13)
  136.         except requests.RequestException:
  137.             pass
  138. #       request = "http://emoncms.org/input/post.json?json={Idx40:" + str(temp_bas) + "}&apikey=mettre_sa_clé"
  139. #                try:
  140. #           r = requests.get(request, timeout=13)
  141. #                except requests.RequestException:
  142. #                        pass
  143.  
  144.         #temp_defrost = lire_nilan(204, 2, 4, True) # Registernumber, number of decimals
  145.                
  146.         #request = "http://emoncms.org/input/post.json?json={Degivrage:" + str(temp_defrost) + "}&apikey=mettre_sa_clé"
  147.                 #try:
  148.                 #        r = requests.get(request, timeout=13)
  149.                 #except requests.RequestException:
  150.                 #        pass
  151.  
  152.  
  153.  
  154.         temperature = lire_nilan(215, 2, 4, True) # Registernumber, number of decimals
  155.         print 'Temp. buanderie = ', temperature
  156.         request = "http://192.168.0.19:9090/json.htm?type=command&param=udevice&idx=36&nvalue=0&svalue=" + str(temperature)
  157.         try:
  158.             r = requests.get(request, timeout=13)
  159.         except requests.RequestException:
  160.             pass
  161. #       request = "http://emoncms.org/input/post.json?json={Idx36:" + str(temperature) + "}&apikey=mettre_sa_clé"
  162. #                try:
  163. #           r = requests.get(request, timeout=13)
  164. #                except requests.RequestException:
  165. #                        pass
  166.  
  167.  
  168.         #Ventil_consigne = instrument.read_register(1003, 0, 3, True) # Registernumber, number of decimals
  169.             Ventil_consigne = lire_nilan(1003, 0, 3, True)
  170.         print 'Ventilation 1003', Ventil_consigne
  171.         Ventil_consigne_modif = Ventil_consigne * 10
  172.                 request = "http://192.168.0.19:9090/json.htm?type=command&param=switchlight&idx=117&switchcmd=Set%20Level&level=" + str(Ventil_consigne_modif)
  173.  
  174.                 try:
  175.                         result = requests.get(request, timeout=13)
  176.                 except requests.RequestException:
  177.                         pass
  178.  
  179.  
  180.         #humidite = instrument.read_register(221, 2, 4, True) # Registernumber, number of decimals
  181.         humidite = lire_nilan(221, 2, 4, True)
  182.         print 'Humidite maison = ', humidite
  183.         request = "http://192.168.0.19:9090/json.htm?type=command&param=udevice&idx=37&nvalue=0&svalue=" + str(temp_maison) + ";" + str(humidite) + ";1"
  184.         try:
  185.             result = requests.get(request, timeout=13)
  186.         except requests.RequestException:
  187.             pass
  188. #       request = "http://emoncms.org/input/post.json?json={Idx37t:" + str(temp_maison) + "}&apikey=mettre_sa_clé"
  189.  
  190. #       try:
  191. #           r = requests.get(request, timeout=13)
  192. #           request = "http://emoncms.org/input/post.json?json={Idx37h:" + str(humidite) + "}&apikey=mettre_sa_clé"
  193. #                   r = requests.get(request, timeout=13)
  194. #                except requests.RequestException:
  195. #                        pass
  196.  
  197.  
  198.         if (hum_hist0 == 0) or (hum_hist1 == 0):
  199.             hum_hist1 = humidite;
  200.             hum_hist0 = humidite;
  201.             if (Ventil_consigne > 2):
  202.                 ecrire_nilan(1003,2,0)
  203.  
  204.    
  205.         print 'hum hist 0', hum_hist0
  206.         print 'hum hist 1', hum_hist1
  207.         coef = (3 * (hum_hist0 + hum_hist1*2 + humidite*3) - 6 * (hum_hist0+hum_hist1+humidite)) / 6
  208.         print 'Coef humidite = ', coef
  209.         #request = "http://emoncms.org/input/post.json?json={Coef_hum:" + str(coef) + "}&apikey=mettre_sa_clé"
  210.                 #r = requests.get(request, timeout=13)
  211.  
  212.  
  213.         if flag_hum != 1:
  214.             speed3 = 0
  215.             if coef > 1 and (humidite-hum_hist0) > 0.75:
  216.                 Store_consigne = Ventil_consigne;
  217.                 print 'Store consigne', Store_consigne
  218.                 print 'On passe en vitesse 4'
  219.                 flag_hum = 1
  220.                 debut = datetime.datetime.now()
  221.                 ecrire_nilan(1003,4,0)
  222.    
  223.         else:
  224.             if coef > 1:
  225.                 debut = datetime.datetime.now()
  226.             fin = datetime.datetime.now()
  227.             duree = fin - debut
  228.             print 'duree', duree.seconds
  229.             if speed3 != 1:
  230.                 if (duree.seconds / 60) > 10:
  231.                     speed3 = 1
  232.                     print 'On passe en vitesse 3'
  233.                     ecrire_nilan(1003,3,0)
  234.        
  235.             else:
  236.                 if (duree.seconds / 60) > 40:
  237.                     flag_hum = 0
  238.                     ecrire_nilan(1003,Store_consigne,0)
  239.                     print 'On revient a la vitesse initiale', Store_consigne
  240.                 else:
  241.                     if coef > 1:
  242.                         print 'On repart en vitesse 4'
  243.                         debut = datetime.datetime.now()
  244.                                         ecrire_nilan(1003,4,0)
  245.                         speed3 = 0
  246.                        
  247.         hum_hist0 = hum_hist1
  248.         hum_hist1 = humidite
  249.  
  250.  
  251.  
  252.         Status = lire_nilan(1001, 0, 4, True) # Registernumber, number of decimals
  253.         if Status == 0: print 'Off'
  254.         elif Status == 1: print 'Chauffage'
  255.         elif Status == 2: print 'Refroidissement'
  256.         elif Status == 3: print 'Auto'
  257.  
  258.         Status = lire_nilan(1002, 0, 4, True) # Registernumber, number of decimals
  259.         print Status
  260.         if Status == 0: print 'Off'
  261.         elif Status == 2: print 'Arret'
  262.         elif Status == 3: print 'Demarrage'
  263.         elif Status == 4: print 'Pause'
  264.         elif Status == 5: print 'Ventilation Stop'
  265.         elif Status == 6: print 'Ventilation'
  266.         elif Status == 7: print 'Chauffage'
  267.         elif Status == 8: print 'Refroidissement'
  268.         elif Status == 9: print 'Production Eau Chaude'
  269.         elif Status == 10: print 'Legionelles'
  270.         elif Status == 11: print 'Refroidissement + Eau Chaude'
  271.         elif Status == 12: print 'Chauffage central'
  272.         elif Status == 13: print 'Degivrage'
  273.         elif Status == 15: print 'Service'
  274.         elif Status == 16: print 'Alarme'
  275.  
  276.         if (Status == 9) or (Status == 11):
  277.             request = "http://emoncms.org/input/post.json?json={ProdEauChaude:1}&apikey=mettre_sa_clé"
  278.                     try:
  279.                 r = requests.get(request, timeout=13)
  280.                     except requests.RequestException:
  281.                             pass
  282.  
  283.             request = "http://192.168.0.19:9090/json.htm?type=command&param=switchlight&idx=44&switchcmd=On&level=0"
  284.                     try:
  285.                 r = requests.get(request, timeout=13)
  286.             except requests.RequestException:
  287.                 pass
  288.             print 'ProdEauChaude 1'
  289.         else:
  290.             request = "http://emoncms.org/input/post.json?json={ProdEauChaude:0}&apikey=mettre_sa_clé"
  291.                     try:
  292.                 r = requests.get(request, timeout=13)
  293.                     except requests.RequestException:
  294.                             pass
  295.  
  296.             request = "http://192.168.0.19:9090/json.htm?type=command&param=switchlight&idx=44&switchcmd=Off&level=0"
  297.             try:
  298.                 r = requests.get(request, timeout=13)
  299.             except requests.RequestException:
  300.                 pass
  301.             print 'ProdEauChaude 0'
  302.  
  303.         Compressor = lire_nilan(109, 0, 3, True) # Registernumber, number of decimals
  304.         print 'Compressor 109', Compressor
  305.         if (Compressor == 1) and (Status != 9): #temp_puls > temp_maison
  306.             request = "http://emoncms.org/input/post.json?json={PAC:1}&apikey=mettre_sa_clé"
  307.                     try:
  308.                 r = requests.get(request, timeout=13)
  309.                     except requests.RequestException:
  310.                             pass
  311.  
  312.             request = "http://192.168.0.19:9090/json.htm?type=command&param=switchlight&idx=45&switchcmd=On&level=0"
  313.                     try:
  314.                 r = requests.get(request, timeout=13)
  315.             except requests.RequestException:
  316.                 pass
  317.             print 'PAC 1'
  318.         elif (Compressor == 1) and (Status == 9) and (temp_puls > temp_maison) and (temp_maison > temp_ext):
  319.             request = "http://emoncms.org/input/post.json?json={PAC:1}&apikey=mettre_sa_clé"
  320.                         try:
  321.                 r = requests.get(request, timeout=13)
  322.                     except requests.RequestException:
  323.                             pass
  324.  
  325.                         request = "http://192.168.0.19:9090/json.htm?type=command&param=switchlight&idx=45&switchcmd=On&level=0"
  326.                         try:
  327.                 r = requests.get(request, timeout=13)
  328.                         except requests.RequestException:
  329.                 pass
  330.             print 'PAC 1'
  331.         else:
  332.             request = "http://emoncms.org/input/post.json?json={PAC:0}&apikey=mettre_sa_clé"
  333.                         try:
  334.                 r = requests.get(request, timeout=13)
  335.                     except requests.RequestException:
  336.                             pass
  337.  
  338.             request = "http://192.168.0.19:9090/json.htm?type=command&param=switchlight&idx=45&switchcmd=Off&level=0"
  339.                     try:
  340.                 r = requests.get(request, timeout=13)
  341.             except requests.RequestException:
  342.                 pass
  343.             print 'PAC 0'
  344.  
  345.  
  346.  
  347.         Bypassopen = lire_nilan(102, 0, 3, True) # Registernumber, number of decimals
  348.         print 'BypassOpen 102', Bypassopen
  349.         if (Bypassopen == 1):
  350.             request = "http://emoncms.org/input/post.json?json={Bypass:1}&apikey=mettre_sa_clé"
  351.                         try:
  352.                 r = requests.get(request, timeout=13)
  353.                     except requests.RequestException:
  354.                             pass
  355.  
  356.             try:
  357.                 request = "http://192.168.0.19:9090/json.htm?type=command&param=switchlight&idx=43&switchcmd=On&level=0"
  358.                         r = requests.get(request, timeout=13)
  359.             except requests.RequestException:
  360.                 pass
  361.         else:
  362.             request = "http://emoncms.org/input/post.json?json={Bypass:0}&apikey=mettre_sa_clé"
  363.                         try:
  364.                 r = requests.get(request, timeout=13)
  365.                     except requests.RequestException:
  366.                             pass
  367.  
  368.             request = "http://192.168.0.19:9090/json.htm?type=command&param=switchlight&idx=43&switchcmd=Off&level=0"
  369.                     try:
  370.                 r = requests.get(request, timeout=13)
  371.             except requests.RequestException:
  372.                 pass
  373.    
  374.    
  375.    
  376.    
  377. #       Bypassclose = lire_nilan(103, 0, 3, True) # Registernumber, number of decimals
  378. #       print 'BypassClose 103', Bypassclose
  379.  
  380. #                Air = lire_nilan(104, 0, 3, True) # Registernumber, number of decimals
  381. #                print 'Air heating circ pump 104', Air
  382.  
  383. #                Air = lire_nilan(105, 0, 3, True) # Registernumber, number of decimals
  384. #                print 'Air heating selected 105', Air
  385.  
  386. #                Air = lire_nilan(106, 0, 3, True) # Registernumber, number of decimals
  387. #                print 'Air heater relays 106', Air
  388.    
  389.  
  390.  
  391. #       Status = lire_nilan(116, 0, 3, True) # Registernumber, number of decimals
  392. #       print 'Chauffe Eau 116', Status
  393.    
  394. #       Status = lire_nilan(1002, 0, 3, True) # Registernumber, number of decimals
  395. #       print 'Mode Select 1002', Status
  396.        
  397.        
  398.         Status = lire_nilan(1004, 2, 3, True) # Registernumber, number of decimals
  399.         print 'Temp consigne 1004', Status
  400.                 request = "http://192.168.0.19:9090/json.htm?type=command&param=udevice&idx=78&nvalue=0&svalue=" + str(Status)
  401.                 try:
  402.             r = requests.get(request, timeout=13)
  403.         except requests.RequestException:
  404.             pass
  405. #       Ventil_user = lire_nilan(603, 0, 3, True) # Registernumber, number of decimals
  406. #           print 'user vent 603', Ventil_user
  407.    
  408. #       User_active = lire_nilan(600, 0, 3, True) # Registernumber, number of decimals
  409. #           print 'user active 600', User_active
  410. #       User_function = lire_nilan(601, 0, 3, True) # Registernumb$
  411. #           print 'user function 601', User_function
  412.    
  413. #       Humidity_high_step = lire_nilan(1911, 0, 3, True) # Registernumbe$
  414. #           print 'Humidity hight step 1911', Humidity_high_step
  415.    
  416.             Inlet = lire_nilan(200, 2, 3, True) # Regis$
  417.             print 'Inlet 200', Inlet
  418.    
  419. #           Exhaust = lire_nilan(201, 2, 3, True) # Regis$
  420. #           print 'Exhaust 201', Exhaust
  421.    
  422. #       rendement = lire_nilan(1204,2,4,False)
  423. #       print 'Rendement 1204', rendement
  424.  
  425.         request = "http://emoncms.org/input/post.json?json={Idx41:" + str(temp_puls) + ",Idx38:" + str(temp_ext) + ",Idx39:" + str(temp_haut) + ",Idx40:" + str(temp_bas) + ",Idx36:" + str(temperature) + ",Idx37t:" + str(temp_maison) + ",Idx37h:" + str(humidite) + ",Ventilation:" + str(Inlet) + "}&apikey=mettre_sa_clé"
  426.                 try:
  427.             r = requests.get(request, timeout=13)
  428.                 except requests.RequestException:
  429.                         pass
  430.  
  431.  
  432.         ventil_domoticz = int(round((Inlet *16) / 100))
  433.         print 'Ventil domoticz', ventil_domoticz
  434.         request = "http://192.168.0.19:9090/json.htm?type=command&param=switchlight&idx=47&switchcmd=Set%20Level&level=" + str(ventil_domoticz)
  435. #       print request
  436.             try:
  437.             r = requests.get(request, timeout=13)
  438.         except requests.RequestException:
  439.             pass
  440. #       instrument.serial.close()
  441.         time.sleep(40)
  442.     except:
  443.         print 'Exception'
  444.         print traceback.format_exc()
  445.         sys.exit(0)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top