Skip to content

Commit

Permalink
send durations array as json
Browse files Browse the repository at this point in the history
  • Loading branch information
8cH9azbsFifZ committed Nov 16, 2023
1 parent 1d21d23 commit da109d4
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 26 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down
62 changes: 41 additions & 21 deletions beep.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pygame, numpy, pygame.sndarray
#import pygame, numpy, pygame.sndarray
import json

class Beep:
def __init__(self, speed = 20):
Expand All @@ -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
6 changes: 4 additions & 2 deletions config.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -12,4 +13,5 @@
MQTT_HOST = "broker.hivemq.com"
MQTT_PORT = 1883

TOPIC = "/moip/udp2mqtt/mopp"
TOPIC = "/moip/udp2mqtt/mopp"
TOPICDURATIONS = "/moip/udp2mqtt/durations"
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
paho-mqtt
paho-mqtt
12 changes: 10 additions & 2 deletions udp2mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import socket
import time
import sys
from beep import *

logging.basicConfig(level=logging.DEBUG, format='%(message)s', )

Expand Down Expand Up @@ -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...
Expand All @@ -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()


Expand Down

0 comments on commit da109d4

Please sign in to comment.