mrajiullah / IoT

0 stars 0 forks source link

Creating socket #2

Open mrajiullah opened 5 years ago

mrajiullah commented 5 years ago
from machine import reset
import network
import time
import machine
import urequests

class httpPostTest():
    def __init__(self):
        self.apn = None
        self.band = None
        self.LTEisAttached = False
        self.LTEisAttachedLast = False
        self.LTEisConnected = False
        self.LTEisConnectedLast = False
        #self.wdt = machine.WDT(timeout=300000)
        self.start = True

    def initLTE(self, band, apn):
        self.apn = apn
        self.band = band
        print("INFO", "Starting initialization of LTE")
        time.sleep(2)
        try:
            self.lte = network.LTE()
            print("INFO", "LTE initialized")
        except:
            print("WARNING", "LTE init command failed")

        print("LTE is attached after init:", self.lte.isattached())
        print("LTE is connected after init:", self.lte.isconnected())

    def LTEgetPhyStats(self):
        isConnected = self.lte.isconnected()

        if isConnected:
            self.lte.pppsuspend()

        response = self.lte.send_at_cmd('AT!="showphy"').split('\r\n')

        if isConnected:
            self.lte.pppresume()

        returnDict = {}
        for line in response:
            try:
                line = line.split(": ")
                key = line[0]

                key = key.replace("(dB)","")
                key = key.replace("(dBm)","")
                key = key.replace("(dBFs)","")
                key = key.replace("(Hz)","")
                key = key.strip(" ")

                value = line[1]
                try:
                    value = int(value)
                except:

                    try:
                        value = float(value)
                    except:
                        pass

                returnDict[key] = value

            except:
                pass

        return returnDict

    def run(self, url):
        n = 0
        while True:

            self.LTEisAttached = self.lte.isattached()

            if not self.LTEisAttached and (self.LTEisAttachedLast or self.start):
                if not self.start:
                    print("WARNING", "Dettached from LTE network")
                try:
                    self.lte.attach(band = self.band, apn = self.apn)
                    #self.lte.send_at_cmd('AT+CGDCONT=1,"IP","%s"' % (self.apn))
                    #self.lte.attach(band = self.band)
                    print("INFO", "LTE attach command sent (band:%s APN:%s)" % (self.band, self.apn))
                    self.start = False
                except:
                    print("WARNING", "LTE attach command failed")

            if self.LTEisAttached and not self.LTEisAttachedLast:
                print("INFO", "Attached to LTE network")
                try:
                    self.lte.connect()
                    print("INFO", "LTE connect command sent")
                except:
                    print("WARNING", "LTE connect command failed")

            self.LTEisAttachedLast = self.LTEisAttached

            self.LTEisConnected = self.lte.isconnected()

            if self.LTEisConnected and not self.LTEisConnectedLast:
                print("INFO", "Connected to LTE network")

            elif not self.LTEisConnected and self.LTEisConnectedLast:
                print("WARNING", "Disconnected from LTE network")
                try:
                    self.lte.connect()
                    print("INFO", "LTE connect command sent")
                except:
                    print("WARNING", "LTE connect command failed")

            self.LTEisConnectedLast = self.LTEisConnected

            try:
                phyStats = self.LTEgetPhyStats()
                print("LTE", "Attached:%s Connected:%s Synchro state:%s RSRP:%sdBm RSRQ:%sdB" % (self.lte.isattached(), self.lte.isconnected(), phyStats["Synchro state"], phyStats["RSRP"], phyStats["RSRQ"]))
            except Exception as e:
                print(e)

            if self.LTEisConnected:
                n+=1
                requestStartTime = time.time()
                try:
                    response = urequests.post(url, headers={"Content-Type": "application/json", "Accept": "application/json"}, data='{"time": "%s"}' % (time.time()))
                    print("INFO", "%s. HTTP POST successful: %s (duration: %ss)" % (n, response.text, time.time() - requestStartTime))
                    response.close()
                except Exception as e:
                    print("WARNING", "%s. HTTP POST failed: %s (duration: %ss)" % (n, e, time.time() - requestStartTime))

                if n == 8:
                    print("Restarting...")
                    time.sleep(0.1)
                    machine.reset()

            #self.wdt.feed()

test = httpPostTest()
test.initLTE(20, "lpwa.telia.iot")
test.run(url = "https://54.234.238.118/post")

Output: Unsuccessful

>>> INFO Starting initialization of LTE
INFO LTE initialized
LTE is attached after init: True
LTE is connected after init: False
INFO Attached to LTE network
INFO LTE connect command sent
INFO Connected to LTE network
LTE Attached:True Connected:True Synchro state:OFF RSRP:-2.147484e+07dBm RSRQ:-15.48dB
WARNING 1. HTTP POST failed: [Errno 113] EHOSTUNREACH (duration: 18s)
LTE Attached:True Connected:True Synchro state:OFF RSRP:-2.147484e+07dBm RSRQ:-15.48dB
WARNING 2. HTTP POST failed: [Errno 113] EHOSTUNREACH (duration: 18s)
LTE Attached:True Connected:True Synchro state:OFF RSRP:-2.147484e+07dBm RSRQ:-15.48dB
WARNING 3. HTTP POST failed: [Errno 113] EHOSTUNREACH (duration: 18s)
LTE Attached:True Connected:True Synchro state:OFF RSRP:-2.147484e+07dBm RSRQ:-15.48dB
WARNING 4. HTTP POST failed: [Errno 113] EHOSTUNREACH (duration: 19s)
LTE Attached:True Connected:True Synchro state:OFF RSRP:-2.147484e+07dBm RSRQ:-15.48dB
WARNING 5. HTTP POST failed: [Errno 113] EHOSTUNREACH (duration: 19s)
LTE Attached:True Connected:True Synchro state:OFF RSRP:-2.147484e+07dBm RSRQ:-15.48dB
WARNING 6. HTTP POST failed: [Errno 113] EHOSTUNREACH (duration: 18s)
LTE Attached:True Connected:True Synchro state:OFF RSRP:-2.147484e+07dBm RSRQ:-15.48dB
WARNING 7. HTTP POST failed: [Errno 113] EHOSTUNREACH (duration: 18s)
LTE Attached:True Connected:True Synchro state:OFF RSRP:-2.147484e+07dBm RSRQ:-15.48dB
WARNING 8. HTTP POST failed: [Errno 113] EHOSTUNREACH (duration: 19s)
Restarting...
mrajiullah commented 5 years ago

>>> Running simple-connect.py

>>>
>>>
>
Pycom MicroPython 1.18.2.r7 [v1.8.6-849-df9f237] on 2019-05-14; FiPy with ESP32
Type "help()" for more information.
>>>
>>> lte.isconnected()
True
>>> import upings
>>> upings.ping('google.com')
PING google.com (32.32.15.128): 64 data bytes
4 packets transmitted, 0 packets received
(4, 0)
>>> upings.ping('kau.se')
PING kau.se (0.0.0.0): 64 data bytes
4 packets transmitted, 0 packets received
(4, 0)
>>> upings.ping('www.kau.se')
PING www.kau.se (32.32.15.128): 64 data bytes
4 packets transmitted, 0 packets received
(4, 0)
>>>
mrajiullah commented 5 years ago
>>> import urequests
>>> response = urequests.get('http://jsonplaceholder.typicode.com/albums/1')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "urequests.py", line 114, in get
  File "urequests.py", line 102, in request
  File "urequests.py", line 60, in request
OSError: [Errno 113] EHOSTUNREACH
mrajiullah commented 5 years ago
>>> import usocket
>>> print(usocket.getaddrinfo("www.google.com", 80)[0][-1])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: -113