-
Notifications
You must be signed in to change notification settings - Fork 0
/
3_.py
executable file
·60 lines (45 loc) · 1.16 KB
/
3_.py
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
import paho.mqtt.client as mqtt
import redis
import MyTopicDetails as mtd
from time import time
# get current topic
myTopic = mtd.returnTopic()
# Subscriber
"""
here connect to redis, create timeseries
"""
REDIS_HOST = '****'
REDIS_PORT = 152
REDIS_USERNAME = '***'
REDIS_PASSWORD = '*******'
timeseries_name = "AudioIOT"
redis_client = redis.Redis(
host=REDIS_HOST,
port=REDIS_PORT,
username=REDIS_USERNAME,
password=REDIS_PASSWORD)
is_connected = redis_client.ping()
#redis_client.flushdb()
try:
redis_client.ts().create(timeseries_name, chunk_size=128)
except redis.ResponseError:
pass
def on_connect(client, userdata, flags, rc):
# print("Connected with result code "+str(rc))
client.subscribe(myTopic)
def on_message(client, userdata, msg):
print(msg.topic , msg.payload.decode())
"""
send to redis
name of ts is AudioIOT
"""
timestamp=time()
try:
redis_client.ts().add(timeseries_name,timestamp,msg.payload.decode())
except redis.exceptions.ResponseError as e:
pass
client = mqtt.Client()
client.connect("test.mosquitto.org",1883,60)
client.on_connect = on_connect
client.on_message = on_message
client.loop_forever()