diff --git a/README.md b/README.md index 13a1b4c..220a86b 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,7 @@ # Durations testing mosquitto_pub -h broker.hivemq.com -t /moip/udp2mqtt/durations -m '{ "durations": [60, -60, 180, -60, 180, -60] }' +mosquitto_sub -h broker.hivemq.com -t /moip/udp2mqtt/durations # Free accounts # https://mntolia.com/10-free-public-private-mqtt-brokers-for-testing-prototyping/ diff --git a/beep.py b/beep.py index fff2b93..2ecfc37 100644 --- a/beep.py +++ b/beep.py @@ -1,4 +1,5 @@ -import pygame, numpy, pygame.sndarray +#import pygame, numpy, pygame.sndarray +import json class Beep: def __init__(self, speed = 20): @@ -13,32 +14,51 @@ def __init__(self, speed = 20): # Ref for sound: https://gist.github.com/nekozing/5774628 sampleRate = 44100 # 44.1kHz, 16-bit signed, mono - pygame.mixer.pre_init(sampleRate, -16, 1) - pygame.init() + #pygame.mixer.pre_init(sampleRate, -16, 1) + #pygame.init() # 4096 : the peak ; volume ; loudness # 440 : the frequency in hz # ?not so sure? if astype int16 not specified sound will get very noisy, because we have defined it as 16 bit mixer at mixer.pre_init() # ( probably due to int overflow resulting in non continuous sound data) - arr = numpy.array([4096 * numpy.sin(2.0 * numpy.pi * 440 * x / sampleRate) for x in range(0, sampleRate)]).astype(numpy.int16) - self.sound = pygame.sndarray.make_sound(arr) - - def _beep(self, symbol): - if symbol == ".": - self.sound.play(-1) - pygame.time.delay(self.dit_duration) - self.sound.stop() - pygame.time.delay(self.dit_duration) - elif symbol == "-": - self.sound.play(-1) - pygame.time.delay(self.dah_duration) - self.sound.stop() - pygame.time.delay(self.dit_duration) - elif symbol == "C": # EOC - pygame.time.delay(self.eoc_duration) - elif symbol == "W": # EOW - pygame.time.delay(self.eow_duration) + #arr = numpy.array([4096 * numpy.sin(2.0 * numpy.pi * 440 * x / sampleRate) for x in range(0, sampleRate)]).astype(numpy.int16) + #self.sound = pygame.sndarray.make_sound(arr) + + # def _beep(self, symbol): + # if symbol == ".": + # self.sound.play(-1) + # pygame.time.delay(self.dit_duration) + # self.sound.stop() + # pygame.time.delay(self.dit_duration) + # elif symbol == "-": + # self.sound.play(-1) + # pygame.time.delay(self.dah_duration) + # self.sound.stop() + # pygame.time.delay(self.dit_duration) + # elif symbol == "C": # EOC + # pygame.time.delay(self.eoc_duration) + # elif symbol == "W": # EOW + # pygame.time.delay(self.eow_duration) def beep_message(self, message): for s in message: self._beep (s) + # FIXME -> put to a separate class (or lib) + def return_duration_json(self, message): + json_string = '{"durations": []}' + data = json.loads(json_string) + + for symbol in message: + if symbol == ".": + data['durations'].append(self.dit_duration) + data['durations'].append(-self.dit_duration) + elif symbol == "-": + data['durations'].append(self.dah_duration) + data['durations'].append(-self.dit_duration) + elif symbol == "C": # EOC + data['durations'].append(-self.eoc_duration) + elif symbol == "W": # EOW + data['durations'].append(-self.eow_duration) + + updated_json_string = json.dumps(data, indent=2) + return updated_json_string \ No newline at end of file diff --git a/config.py b/config.py index b2e448d..881d024 100644 --- a/config.py +++ b/config.py @@ -1,5 +1,6 @@ # Configuration for UDP connections -SERVER_IP = "mopp_chat_server" #"127.0.0.1" # "10.101.101.14" +#SERVER_IP = "mopp_chat_server" #"127.0.0.1" # "10.101.101.14" +SERVER_IP = "10.101.101.14" #"127.0.0.1" # "" UDP_PORT = 7373 # Configuration for Chat Server @@ -12,4 +13,5 @@ MQTT_HOST = "broker.hivemq.com" MQTT_PORT = 1883 -TOPIC = "/moip/udp2mqtt/mopp" \ No newline at end of file +TOPIC = "/moip/udp2mqtt/mopp" +TOPICDURATIONS = "/moip/udp2mqtt/durations" \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index d173f65..8579e8b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1 @@ -paho-mqtt \ No newline at end of file +paho-mqtt diff --git a/udp2mqtt.py b/udp2mqtt.py index e780895..1cd2b4f 100644 --- a/udp2mqtt.py +++ b/udp2mqtt.py @@ -7,6 +7,7 @@ import socket import time import sys +from beep import * logging.basicConfig(level=logging.DEBUG, format='%(message)s', ) @@ -58,6 +59,7 @@ def on_log(mqttc, obj, level, string): mqttc.on_subscribe = on_subscribe mqttc.connect(config.MQTT_HOST, config.MQTT_PORT, 60) mqttc.subscribe(config.TOPIC, 0) +# FIXME: Listen to durations mqttc.loop_start() last_r = {} # keep track of duplicate messages... @@ -77,13 +79,19 @@ def on_log(mqttc, obj, level, string): # Beep if message received if not "Keepalive" in r: - #b = Beep(speed=r["Speed"]) + b = Beep(speed=r["Speed"]) if not last_r == r: #b.beep_message(r["Message"]) last_r = r + # decode the message + print (r["Speed"]) + print (r["Message"]) + mydecoded = b.return_duration_json(r["Message"]) + # And send mqtt - infot = mqttc.publish(config.TOPIC, data_bytes, qos=2) + #infot = mqttc.publish(config.TOPIC, data_bytes, qos=2) + infot = mqttc.publish(config.TOPICDURATIONS, mydecoded, qos=2) infot.wait_for_publish()