def broker_on_message(client, userdata, msg): # @UnusedVariable #print( msg ) # if (len(blackList) > 0) and (re.match( blackList, msg.topic )): if cfg.check_black_list(msg.topic, blackList): log.info("To UDP BLACKLIST: " + msg.topic + " " + str(msg.payload)) #print("To UDP BLACKLIST: "+ msg.topic+" "+str(msg.payload)) return if ilock.broker_to_udp(msg.topic, msg.payload): me.send_publish(msg.topic, msg.payload) log.info("To UDP: " + msg.topic + "=" + str(msg.payload)) #print("To UDP: "+msg.topic+"="+str(msg.payload)) else: #print("BLOCKED to UDP: "+msg.topic+"="+str(msg.payload)) log.info("BLOCKED to UDP: " + msg.topic + "=" + str(msg.payload))
def run(): global tstump (t, h, p, v, vs, msg) = measure() vbat = v if FAKE_SLEEP: print("No watchdog") else: wdt = machine.WDT(timeout=int(DEEP_SLEEP + DEEP_SLEEP / 2) * 1000) while True: tstump = '%s-%.2d-%.2d %.2d:%.2d:%.2d' % time.localtime()[0:6] if POSITIONING != 'NO': position() (t, h, p, v, vs, msg) = measure() print("TZ: %s LONGITUDE: %s" % (cfg['tz'], cfg['longitude'])) dat = update_data([ t, h, p, v, vs, paz.duty(), palt.duty(), machine.wake_reason(), msg ]) try: config = { "sleep": DEEP_SLEEP, "fake_sleep": FAKE_SLEEP, "ts_cfg": tstump, "Vsun": LVL_SUNPIN } print("Publish config: %s %s" % (MY_ID, config)) me.send_publish('weather/%s/config' % MY_ID, json.dumps(config)) dat.reverse() print("Publish id: %s Length:%s" % (MY_ID, len(dat))) for line in dat: me.send_publish('weather/' + MY_ID, json.dumps(to_dict(line))) except Exception as e: print("Publish exception: %s" % e) stime = DEEP_SLEEP - (time.time() - lib.boot_time) if stime < 0: stime = 1 elif stime > DEEP_SLEEP: stime = DEEP_SLEEP if FAKE_SLEEP: stime = stime / 20 print("Fake sleep for %s sec" % stime) time.sleep(stime) lib.boot_time = time.time() else: print('Deepsleep for %s sec.' % stime) wdt.feed() machine.deepsleep(stime * 1000) print()
def publish_for(topic_of_topic, data): """ Send message using configurable topic Get value of "$SYS/conf/{MY_ID}/topic_of_topic" and use it as topic to send data @param #string topic_of_topic name of parameter holding topic used to send message @param #string data data to send """ key = "topic/" + topic_of_topic if not __conf_items.__contains__(key): #print( "no configured value (topic) for topic_of topic() "+key+"'" ) return item = __conf_items[key] mq.send_publish(item, data)
def send_one_item(k, v): """ Send one remote config item value """ mq.send_publish(full_topic(k), v)
import mqttudp.engine as me #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! # # NB! Used in regress tests. Do not modify! # #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! if __name__ == "__main__": parser = argparse.ArgumentParser( description='Send MQTT/UDP publish message', prog='test_pub') parser.add_argument('topic', help='topic to send to') parser.add_argument('value', help='value to be sent') parser.add_argument('-s', '--signature', dest='signature', action='store', help='digital signature key') args = parser.parse_args() if args.signature != None: me.set_signature(args.signature) print("Will publish to '" + sys.argv[1] + "' value '" + sys.argv[2] + "'") me.send_publish(args.topic, args.value) print("Sent ok")
#!/usr/bin/env python3 ''' This program will generate random data and send to MQTT/UDP topic once a 5 sec ''' # will work even if package is not installed import sys sys.path.append('..') #sys.path.append('../mqttudp') #import threading import mqttudp.engine as me import random import time TOPIC = "random_data" if __name__ == "__main__": print("Will send MQTT/UDP packets with random number as a payload") print("Topic is '" + TOPIC + "'") #me.set_broadcast_address( "192.168.255.255" ) while True: n = str(random.randint(0, 9)) print("Send " + n) me.send_publish(TOPIC, n) time.sleep(2)