aboutsummaryrefslogtreecommitdiffstats
path: root/libs/pixtend/v2/pplv2/examples/pixtendv2s_mqtt_demo.py
blob: beffd69eadad94d52e9d4f3a8f96c4d941683d83 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#!/usr/bin/env python
# coding=utf-8

# MIT License
# 
# Copyright (C) 2021 Kontron Electronics GmbH <support@pixtend.de>
# 
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files
# (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge,
# publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
# 
# For further details see LICENSE.txt.

# -----------------------------------------------------------------------------
# Attention:
# The PiXtend Python Library v2 (PPLv2) was developed as a Python 
# library / module to make use of the inheritance functionality of Python.
# However, since the library must access the hardware based SPI bus on the
# Raspberry Pi only ONE single instance of the PiXtendV2S or PiXtendV2L
# class per PiXtend is allowed! The PPLv2 as well as the SPI bus is not 
# capable of aggregating (multiplexing) multiple instances of either
# PiXtend class. Please keep this in mind when developing your application.
# We suggest building one central program which creates the PiXtend object
# and all other programs, functions, threads use inter-process communication
# with the main program to send data to the PiXtend board to manipulate the
# analog and/or digital outputs or to get information from the inputs.
# -----------------------------------------------------------------------------

from __future__ import print_function
# Import Pixtend V2 -S- class
from pixtendv2s import PiXtendV2S
import paho.mqtt.client as mqtt
import time, os, urlparse, sys, re

# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
    # We subcribe to the topics /pixtendv2/relX and wait for the user to update
    # the one of the topics with a message containing a 1 to turn on a relay or
    # a 0 to turn a relay off.
    client.subscribe("/pixtendv2/rel0")
    client.subscribe("/pixtendv2/rel1")
    client.subscribe("/pixtendv2/rel2")
    client.subscribe("/pixtendv2/rel3")

# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
    if ((len(msg.topic) == 15) and (msg.topic[:len(msg.topic)-1] == "/pixtendv2/rel")):
        try:
            relnum = int(msg.topic[-1:])
            try:
                value = bool(int(msg.payload))
                if (value is False or value is True):
                    if (relnum == 0):
                            p.relay0 = value
                    elif (relnum == 1):
                            p.relay1 = value
                    elif (relnum == 2):
                            p.relay2 = value
                    elif (relnum == 3):
                            p.relay3 = value
            except ValueError:
                value = 0
        except ValueError:
            relnum = 0

strSlogan1 = "PiXtend V2 -S- Python Library and MQTT Demo."
strSlogan2 = "PiXtend V2 -S- Python Library and MQTT Demo finished."

# -----------------------------------------------------------------
# Print Art and Slogan
# -----------------------------------------------------------------
print("")
print("    ____  _ _  ____                 __   _    _____        _____")
print("   / __ \\(_) |/ / /____  ____  ____/ /  | |  / /__ \\      / ___/")
print("  / /_/ / /|   / __/ _ \\/ __ \\/ __  /   | | / /__/ / ____ \\__ \\ ")
print(" / ____/ //   / /_/  __/ / / / /_/ /    | |/ // __/ /___/__ / / ")
print("/_/   /_//_/|_\\__/\\___/_/ /_/\\__,_/     |___//____/     /____/  ")
print("")
print(strSlogan1)
print("")    
    
    
# Setup MQTT Client for publish and subcribe
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message

client.username_pw_set(USERNAME_HERE, PASSWORD_HERE)
client.connect(SERVER_HERE, PORT_HERE, 60)

# Non-Blocking call that processes network traffic, dispatches callbacks
client.loop_start()
 
# -----------------------------------------------------------------
# Create instance - SPI communication starts automatically
# -----------------------------------------------------------------
p = PiXtendV2S()

# -----------------------------------------------------
# Main Program
# -----------------------------------------------------
if p is not None:
    print("Running Main Program - Hit Ctrl + C to exit")
    # Set some variables needed in the main loop
    is_config = False
    cycle_counter = 0
    toggle_counter = 0

    while True:
        try:
            # Check if SPI communication is running and the received data is correct
            if p.crc_header_in_error is False and p.crc_data_in_error is False:
                cycle_counter += 1

                if not is_config:
                    is_config = True
                    print("The value False = Off and the value True = On")
                    print("")
                    # Setting the relays to Off (False)
                    p.relay0 = p.OFF
                    p.relay1 = p.OFF
                    p.relay2 = p.OFF
                    p.relay3 = p.OFF

                # Build text with values from all digital outputs und relays
                str_text = "Cycle No.: {0}\n".format(cycle_counter)
                str_text += "Digital Input 0: {0}\n".format(p.digital_in0)
                str_text += "Digital Input 1: {0}\n".format(p.digital_in1)
                str_text += "Digital Input 2: {0}\n".format(p.digital_in2)
                str_text += "Digital Input 3: {0}\n".format(p.digital_in3)
                str_text += "Digital Input 4: {0}\n".format(p.digital_in4)
                str_text += "Digital Input 5: {0}\n".format(p.digital_in5)
                str_text += "Digital Input 6: {0}\n".format(p.digital_in6)
                str_text += "Digital Input 7: {0}\n".format(p.digital_in7)
                str_text += "Relay 0:         {0}\n".format(p.relay0)
                str_text += "Relay 1:         {0}\n".format(p.relay1)
                str_text += "Relay 2:         {0}\n".format(p.relay2)
                str_text += "Relay 3:         {0}\n".format(p.relay3)

                # Print text to console
                print(str_text, end="\r")
                
                # Reset cursor
                for i in range(0, 13, 1):
                    sys.stdout.write("\x1b[A") 

                # MQTT processing...
                if toggle_counter >= 10:
                    client.publish("/pixtendv2/digin0", int(p.digital_in0))
                    client.publish("/pixtendv2/digin1", int(p.digital_in1))
                    client.publish("/pixtendv2/digin2", int(p.digital_in2))
                    client.publish("/pixtendv2/digin3", int(p.digital_in3))
                    client.publish("/pixtendv2/digin4", int(p.digital_in4))
                    client.publish("/pixtendv2/digin5", int(p.digital_in5))
                    client.publish("/pixtendv2/digin6", int(p.digital_in6))
                    client.publish("/pixtendv2/digin7", int(p.digital_in7))
                    toggle_counter = 0
                else:
                    toggle_counter += 1

            # Wait 0.5sec or 500ms
            time.sleep(0.5)
            # Overwrite old text on screen
            sys.stdout.write(re.sub(r"[^\s]", " ", str_text))
            # Reset cursor
            for i in range(0, 13, 1):
                sys.stdout.write("\x1b[A")

        # Catch errors and if an error is caught, leave the program
        except IOError as e:
            # Print out the caught error and leave program
            print ("I/O error({0}): {1}".format(e.errno, e.strerror))
            p.close()
            time.sleep(0.25)
            del p
            p = None
            break
            
        except ValueError as ve:
            # Print out the caught error and leave program
            print ("Value error({0}): {1}".format(ve.errno, ve.strerror))
            p.close()
            time.sleep(0.25)
            del p
            p = None
            break

        except RuntimeError as re:
            # Print out the caught error and leave program
            print ("Runtime error({0}): {1}".format(re.errno, re.strerror))
            p.close()
            time.sleep(0.25)
            del p
            p = None
            break 

        except KeyboardInterrupt:
            # Keyboard interrupt caught, Ctrl + C, now clean up and leave program
            client.loop_stop()
            client.disconnect()
            client = None
            for i in range(0, 14, 1):
                print("")
            print(strSlogan2)
            p.digital_out0 = p.OFF
            p.digital_out1 = p.OFF
            p.digital_out2 = p.OFF
            p.digital_out3 = p.OFF
            p.relay0 = p.OFF
            p.relay1 = p.OFF
            p.relay2 = p.OFF
            p.relay3 = p.OFF
            time.sleep(0.25)
            # We must call the close() function to end the communication with the
            # PiXtend V2 -S- microcontroller, this way no error is thrown and we
            # can start this program again right away.
            p.close()
            # We have to wait for the communication to end
            time.sleep(0.25)
            del p
            p = None
            break
else:
    # Close any possible MQTT connection
    client.loop_stop()
    client.disconnect()
    client = None
    p = None
    # If there was an error creating the PiXtend V2 -S- instance, leave the program.
    print("")
    print("There was a problem creating the Pixtend V2 -S- instance. Quitting.")
    print("")
    print(strSlogan2)
bues.ch cgit interface