Exemple #1
0
def mqttSendQueue(inMqttBroker,
                  ioMqttCounter,
                  inMqttSendLimit=CONFIG['mqttSendLimit']):

    uping.ping(inMqttBroker, 1)
    topic = CONFIG["mqttMeasureTopic"]
    msgCount = 0

    mqttClient = MQTTClient(
        GLOBAL_MESSAGE_QUEUE.uniqueID(),
        inMqttBroker,
        ssl=CONFIG["mqttSSL"],
        user=CONFIG["mqttUser"],
        password=CONFIG["mqttPassword"],
    )
    mqttLastWill = ("-1, -1, -1, -1, -1, -1, -1, -1, -1, " +
                    GLOBAL_MESSAGE_QUEUE.uniqueID() + ", " +
                    MESSAGE_TYPES["mqttErrorMessage"] + ", " +
                    "UngracefulDisconnect, " +
                    "Ungraceful disruption of MQTT commit, ")
    printDebug("queue mqttLastWill", mqttLastWill)
    mqttClient.set_last_will(topic, mqttLastWill)

    try:
        mqttClient.connect()
        print("## Connected with MQTT Broker", inMqttBroker)
        printDataDebug("ioMqttData", ioMqttData)

        while (GLOBAL_MESSAGE_QUEUE.lenQ()[0] > 0
               and msgCount < inMqttSendLimit):
            msg = GLOBAL_MESSAGE_QUEUE.getMsg()
            mqttClient.publish(topic, json.dumps(msg))
            msgCount += 1
            ioMqttCounter += 1

    except Exception as e:
        sys.print_exception(e)
        errorMessage = mqttErrorMessage(
            GLOBAL_MESSAGE_QUEUE.uniqueID(),
            "Exception",
            "MQTTClientConnectError",
            str(e),
        )
        GLOBAL_MESSAGE_QUEUE.addMsg(errorMessage, "high")

    try:
        mqttClient.disconnect()
    except Exception as e:
        sys.print_exception(e)
        errorMessage = mqttErrorMessage(
            GLOBAL_MESSAGE_QUEUE.uniqueID(),
            "Exception",
            "MQTTClientDisconnectError",
            str(e),
        )
        GLOBAL_MESSAGE_QUEUE.addMsg(errorMessage, "high")
    return ioMqttCounter
Exemple #2
0
def mqttCommit(ioMqttCounter, inMqttBroker, inMqttTopic, inMqttClientUniqueId,
               ioMqttData):
    uping.ping(inMqttBroker, 1)
    mqttClient = MQTTClient(
        inMqttClientUniqueId,
        inMqttBroker,
        ssl=CONFIG["mqttSSL"],
        user=CONFIG["mqttUser"],
        password=CONFIG["mqttPassword"],
    )
    mqttLastWill = ("-1, -1, -1, -1, -1, -1, -1, -1, -1, " +
                    inMqttClientUniqueId + ", " +
                    MESSAGE_TYPES["mqttErrorMessage"] + ", " +
                    "UngracefulDisconnect, " +
                    "Ungraceful disruption of MQTT commit, ")
    printDebug("mqttLastWill", mqttLastWill)
    mqttClient.set_last_will(inMqttTopic, mqttLastWill)
    try:
        mqttClient.connect()
        print("## Connected with MQTT Broker", inMqttBroker)
        printDataDebug("ioMqttData", ioMqttData)
        ioMqttCounter = mqttPublish(ioMqttCounter, mqttClient, inMqttTopic,
                                    ioMqttData)
    except Exception as e:
        sys.print_exception(e)
        errorMessage = mqttErrorMessage(
            inMqttClientUniqueId,
            "Exception",
            "MQTTClientConnectError",
            str(e),
        )
        ioMqttData.append(errorMessage)
    try:
        mqttClient.disconnect()
    except Exception as e:
        sys.print_exception(e)
        errorMessage = mqttErrorMessage(
            inMqttClientUniqueId,
            "Exception",
            "MQTTClientDisconnectError",
            str(e),
        )
        ioMqttData.append(errorMessage)
    return ioMqttCounter
Exemple #3
0
def ping(host) -> bool:
    try:
        result = uping.ping(host,
                            count=4,
                            timeout=5000,
                            interval=10,
                            quiet=True,
                            size=64)
        return result[1] > 0
    except OSError:
        return False
Exemple #4
0
def do_connect():
    import uping
    wlan = network.WLAN(network.STA_IF)
    wlan.active(True)
    if not wlan.isconnected():
        print('connecting to network...')
        wlan.connect('royter', 'traveller22')
        while not wlan.isconnected():
            pass
    print('network config:', wlan.ifconfig())
    print(uping.ping('google.com'))
 def check_conn(self):
     """
     checks network connection by pinging gateway
     uses uping.py
     """
     print('is connected? (sta_if): ', self.sta_if.isconnected())
     try:
         ping_status = uping.ping(self.gateway)
         if ping_status == (4, 4):
             conn_status = True
             print('ping: connected')
         else:
             conn_status = True
             print('ping: some packets lost')
     except OSError:
         print('ping: not connected')
         conn_status = False
     return conn_status
Exemple #6
0
def getLowestPing(host, samples, size, timeout=5000, quiet=False):
    # Make a list containing the ping results for each of the ping samples
    pings = []
    failCnt = 0
    for x in range(samples):
        ping = uping.ping(host, size, timeout, quiet=quiet)
        if (ping == None):
            failCnt += 1
            if (failCnt >= 2):
                return None
        if (ping != None):
            pings.append(ping)
        not quiet and print(
            "getLowestPing host %s size %u sample# %u result: " %
            (host, size, x), ping)
        utime.sleep_ms(25)
    # Review results for number of successful pings and get the lowest latency
    minPing = 9999

    for ping in pings:
        if (minPing > ping[0]):
            minPing = ping[0]
    return minPing
Exemple #7
0
 def check_for_owner_on_wifi(self, timeout=2): # seconds
     tx, rx = uping.ping(last_sender_addr, count=1, timeout=timeout*1000)
     return rx == tx
Exemple #8
0
def demo():
    r = ping('192.168.0.1', quiet=True)
    print(r)
    r = ping('8.8.8.8', quiet=True)
    print(r)
    print(ping_check('8.8.8.8'))
Exemple #9
0
from network import WLAN
import machine
from uping import ping
wlan = WLAN(mode=WLAN.STA)

nets = wlan.scan()
for net in nets:
    if net.ssid == 'mySSID':
        print('Network found!')
        wlan.connect(net.ssid, auth=(net.sec, 'myPassword'), timeout=5000)
        while not wlan.isconnected():
            machine.idle()  # save power while waiting
        print('WLAN connection succeeded!')
        break

ping("google.com")
Exemple #10
0
# loop until a valid addess is registered (check first  ifConfig value, it will be the assigned IP)

ip = l.ifconfig()[0]
while ip == '0.0.0.0':
    print("Getting IP", ip)
    utime.sleep_ms(1000)
    ip = l.ifconfig()[0]

print("Address Details: ", l.ifconfig())

# utime.sleep_ms(10000)

host = "192.168.1.29"
print("Start pings: ", host)
for x in range(5):
    pingInfo = uping.ping(host, 26)
    if pingInfo == None:
        print("bad ping")
    else:
        print("time %f TTL %u size_on_wire %u" %
              (pingInfo[0], pingInfo[1], pingInfo[2]))
print("End pings")

print("bing ", host, ": ", ubing.bing(host, 5, loopBackAdjustment=True))

print('disconnecting from network...')

l.active(False)
while l.isconnected():
    pass
Exemple #11
0
# Measures magnetic filed with interal hall sensor
# Use a magnet right over the chip, try to turn it if the number does not change
# Be careful, you do not want it to find the pins and short them.

import wlan
import uping

wlan.do_connect()
result = uping.ping('google.com')
if __name__ != "__main__":
    assert (result == (4, 4))
    print('TESTOK')