Skip to content

Commit 9a2ef93

Browse files
authored
Merge pull request #5 from myDevicesIoT/feature/multivalue
Add new data types and units.
2 parents 00f7490 + 79962d8 commit 9a2ef93

File tree

5 files changed

+114
-36
lines changed

5 files changed

+114
-36
lines changed

cayenne/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "1.0.1"
1+
__version__ = "1.0.2a1"

cayenne/client.py

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,12 @@
1-
import paho.mqtt.client as mqtt
1+
"""
2+
This module contains the main agent client for connecting to the Cayenne server. The client connects
3+
to server, sends data to the server and handles commands from the server.
4+
"""
5+
26
import time
7+
import paho.mqtt.client as mqtt
38
from cayenne import __version__
4-
5-
# Data types
6-
TYPE_BAROMETRIC_PRESSURE = "bp" # Barometric pressure
7-
TYPE_BATTERY = "batt" # Battery
8-
TYPE_LUMINOSITY = "lum" # Luminosity
9-
TYPE_PROXIMITY = "prox" # Proximity
10-
TYPE_RELATIVE_HUMIDITY = "rel_hum" # Relative Humidity
11-
TYPE_TEMPERATURE = "temp" # Temperature
12-
TYPE_VOLTAGE = "voltage" # Voltage
13-
14-
# Unit types
15-
UNIT_UNDEFINED = "null"
16-
UNIT_PASCAL = "pa" # Pascal
17-
UNIT_HECTOPASCAL = "hpa" # Hectopascal
18-
UNIT_PERCENT = "p" # % (0 to 100)
19-
UNIT_RATIO = "r" # Ratio
20-
UNIT_VOLTS = "v" # Volts
21-
UNIT_LUX = "lux" # Lux
22-
UNIT_CENTIMETER = "cm" # Centimeter
23-
UNIT_METER = "m" # Meter
24-
UNIT_DIGITAL = "d" # Digital (0/1)
25-
UNIT_FAHRENHEIT = "f" # Fahrenheit
26-
UNIT_CELSIUS = "c" # Celsius
27-
UNIT_KELVIN = "k" # Kelvin
28-
UNIT_MILLIVOLTS = "mv" # Millivolts
9+
from cayenne.datatypes import *
2910

3011
# Topics
3112
COMMAND_TOPIC = "cmd"
@@ -52,7 +33,7 @@ def on_connect(client, cayenne, flags, rc):
5233
# reconnect then subscriptions will be renewed.
5334
cayenne.connected = True
5435
cayenne.reconnect = False
55-
command_topic = cayenne.getCommandTopic();
36+
command_topic = cayenne.getCommandTopic()
5637
print("SUB %s\n" % command_topic)
5738
client.subscribe(command_topic)
5839
cayenne.mqttPublish("%s/sys/model" % cayenne.rootTopic, "Python")
@@ -255,6 +236,30 @@ def hectoPascalWrite(self, channel, value):
255236
"""
256237
self.virtualWrite(channel, value, TYPE_BAROMETRIC_PRESSURE, UNIT_HECTOPASCAL)
257238

239+
def accelWrite(self, channel, x=UNIT_UNDEFINED, y=UNIT_UNDEFINED, z=UNIT_UNDEFINED):
240+
"""Send an acceleration value list to Cayenne.
241+
242+
channel is the Cayenne channel to use.
243+
x is the acceleration on the X-axis.
244+
y is the acceleration on the Y-axis.
245+
z is the acceleration on the Z-axis.
246+
"""
247+
value = [x, y, z]
248+
value = ''.join(c for c in repr(value) if c not in (" ", "'"))
249+
self.virtualWrite(channel, value, TYPE_ACCELERATION, UNIT_G)
250+
251+
def gpsWrite(self, channel, latitude=UNIT_UNDEFINED, longitude=UNIT_UNDEFINED, altitude=UNIT_UNDEFINED):
252+
"""Send a GPS value list to Cayenne.
253+
254+
channel is the Cayenne channel to use.
255+
latitude is the latitude in degrees.
256+
longitude is the longitude in degrees.
257+
altitude is the altitude in meters.
258+
"""
259+
value = [latitude, longitude, altitude]
260+
value = ''.join(c for c in repr(value) if c not in (" ", "'"))
261+
self.virtualWrite(channel, value, TYPE_GPS, UNIT_METER)
262+
258263
def mqttPublish(self, topic, payload):
259264
"""Publish a payload to a topic
260265

cayenne/datatypes.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
"""
2+
This module contains the data types used when sending data to the Cayenne server.
3+
"""
4+
5+
# Data types
6+
TYPE_ACCELERATION = 'accel' # Acceleration, units: UNIT_G
7+
TYPE_ANALOG_ACTUATOR = 'analog_actuator' # Analog Actuator, units: UNIT_ANALOG
8+
TYPE_ANALOG_SENSOR = 'analog_sensor' # Analog Sensor, units: UNIT_ANALOG
9+
TYPE_BAROMETRIC_PRESSURE = 'bp' # Barometric pressure, units: UNIT_PASCAL, UNIT_HECTOPASCAL
10+
TYPE_BATTERY = 'batt' # Battery, units: UNIT_PERCENT, UNIT_RATIO, UNIT_VOLTS
11+
TYPE_CO2 = 'co2' # Carbon Dioxide, units: UNIT_PPM
12+
TYPE_COUNTER = 'counter' # Counter, units: UNIT_ANALOG
13+
TYPE_CURRENT = 'current' # Current, units: UNIT_AMP, UNIT_MAMP
14+
TYPE_DIGITAL_ACTUATOR = 'digital_actuator' # Digital Actuator, units: UNIT_DIGITAL
15+
TYPE_DIGITAL_SENSOR = 'digital_sensor' # Digital Sensor, units: UNIT_DIGITAL
16+
TYPE_ENERGY = 'energy' # Energy, units: UNIT_KWH
17+
TYPE_EXT_WATERLEAK = 'ext_wleak' # External Waterleak, units: UNIT_ANALOG
18+
TYPE_FREQUENCY = 'freq' # Frequency, units: UNIT_HERTZ
19+
TYPE_GPS = 'gps' # GPS, units: UNIT_GPS
20+
TYPE_GYROSCOPE = 'gyro' # Gyroscope, units: UNIT_ROTATION_PER_MINUTE, UNIT_DEGREE_PER_SEC
21+
TYPE_LUMINOSITY = 'lum' # Luminosity, units: UNIT_LUX, UNIT_VOLTS, UNIT_PERCENT, UNIT_RATIO
22+
TYPE_MOTION = 'motion' # Motion, units: UNIT_DIGITAL
23+
TYPE_POWER = 'pow' # Power, units: UNIT_WATT, UNIT_KILOWATT
24+
TYPE_PROXIMITY = 'prox' # Proximity, units: UNIT_CENTIMETER, UNIT_METER, UNIT_DIGITAL
25+
TYPE_RAIN_LEVEL = 'rain_level' # Rain Level, units: UNIT_CENTIMETER, UNIT_MILLIMETER
26+
TYPE_RELATIVE_HUMIDITY = 'rel_hum' # Relative Humidity, units: UNIT_PERCENT, UNIT_RATIO
27+
TYPE_RESISTANCE = 'res' # Resistance, units: UNIT_OHM
28+
TYPE_RSSI = 'rssi' # Received signal strength indicator, units: UNIT_DBM
29+
TYPE_SNR = 'snr' # Signal Noise Ratio, units: UNIT_DB
30+
TYPE_SOIL_MOISTURE = 'soil_moist' # Soil Moisture, units: UNIT_PERCENT
31+
TYPE_SOIL_PH = 'soil_ph' # Soil pH, units: UNIT_ANALOG
32+
TYPE_SOIL_WATER_TENSION = 'soil_w_ten' # Soil Water Tension, units: UNIT_KILOPASCAL, UNIT_PASCAL
33+
TYPE_TANK_LEVEL = 'tl' # Tank Level, units: UNIT_ANALOG
34+
TYPE_TEMPERATURE = 'temp' # Temperature, units: UNIT_FAHRENHEIT, UNIT_CELSIUS, UNIT_KELVIN
35+
TYPE_VOLTAGE = 'voltage' # Voltage, units: UNIT_VOLTS, UNIT_MILLIVOLTS
36+
TYPE_WIND_SPEED = 'wind_speed' # Wind Speed, units: UNIT_KM_PER_H
37+
38+
# Unit types
39+
UNIT_UNDEFINED = 'null' # Undefined unit type
40+
UNIT_AMP = 'a' # Ampere
41+
UNIT_ANALOG = 'null' # Analog
42+
UNIT_CELSIUS = 'c' # Celsius
43+
UNIT_CENTIMETER = 'cm' # Centimeter
44+
UNIT_DB = 'db' # Decibels
45+
UNIT_DBM = 'dbm' # dBm
46+
UNIT_DEGREE_PER_SEC = 'dps' # Degree per second
47+
UNIT_DIGITAL = 'd' # Digital (0/1)
48+
UNIT_FAHRENHEIT = 'f' # Fahrenheit
49+
UNIT_G = 'g' # Acceleration
50+
UNIT_GPS = 'm' # GPS
51+
UNIT_HECTOPASCAL = 'hpa' # Hectopascal
52+
UNIT_HERTZ = 'hz' # Hertz
53+
UNIT_KELVIN = 'k' # Kelvin
54+
UNIT_KILOPASCAL = 'kpa' # Kilopascal
55+
UNIT_KILOWATT = 'kw' # Kilowatts
56+
UNIT_KM_PER_H = 'kmh' # Kilometer per hour
57+
UNIT_KWH = 'kwh' # Killowatt Hour
58+
UNIT_LUX = 'lux' # Lux
59+
UNIT_MAMP = 'ma' # Milliampere
60+
UNIT_METER = 'm' # Meter
61+
UNIT_MILLIMETER = 'mm' # Millimeter
62+
UNIT_MILLIVOLTS = 'mv' # Millivolts
63+
UNIT_OHM = 'ohm' # Ohm
64+
UNIT_PASCAL = 'pa' # Pascal
65+
UNIT_PERCENT = 'p' # Percent (%)
66+
UNIT_PPM = 'ppm' # Parts per million
67+
UNIT_RATIO = 'r' # Ratio
68+
UNIT_ROTATION_PER_MINUTE = 'rpm' # Rotation per minute
69+
UNIT_VOLTS = 'v' # Volts
70+
UNIT_WATT = 'w' # Watts
Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env python
2-
import cayenne.client
2+
from cayenne import client, datatypes
33
import time
44

55
# Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
@@ -13,19 +13,20 @@ def on_message(message):
1313
print("message received: " + str(message))
1414
# If there is an error processing the message return an error string, otherwise return nothing.
1515

16-
client = cayenne.client.CayenneMQTTClient()
17-
client.on_message = on_message
18-
client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID)
16+
cayenne_client = client.CayenneMQTTClient()
17+
cayenne_client.on_message = on_message
18+
cayenne_client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID)
1919

2020
i=0
2121
timestamp = 0
2222

2323
while True:
24-
client.loop()
24+
cayenne_client.loop()
2525

2626
if (time.time() > timestamp + 10):
27-
client.celsiusWrite(1, i)
28-
client.luxWrite(2, i*10)
29-
client.hectoPascalWrite(3, i+800)
27+
cayenne_client.celsiusWrite(1, i)
28+
cayenne_client.luxWrite(2, i*10)
29+
cayenne_client.hectoPascalWrite(3, i+800)
30+
cayenne_client.virtualWrite(4, i%2, datatypes.TYPE_DIGITAL_SENSOR, datatypes.UNIT_DIGITAL)
3031
timestamp = time.time()
3132
i = i+1

tests/Test-01-TestClient.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ def on_message(message):
4343
client.luxWrite(4, 4)
4444
client.pascalWrite(5, 5)
4545
client.hectoPascalWrite(6, 6)
46+
client.accelWrite(7, 7.01, 7.02, -7.03)
47+
client.gpsWrite(8, 27.986065, 86.922623, 8848)
4648

4749
print("Test receiving commands")
4850
client.mqttPublish(client.rootTopic + '/cmd/10', 'senderror,0')

0 commit comments

Comments
 (0)