File "Praktikum3.py"

Full Path: /var/www/html/main/lecture-notes/Internet of Things/02 Materi Praktikum - IoT - PDF/File Python/Praktikum3.py
File size: 3.06 KB
MIME-type: text/x-script.python
Charset: utf-8

 
Open Back
from machine import Pin
import time
import network
import ntptime
try:
  import usocket as socket
except:
  import socket
import webpage

SSID = "<SSID>"
Password = "<Password>"
p0 = Pin(2, Pin.OUT)
blinker = 0
reset = False
current_time = None

# Blink Pendek 50ms
def blinkPendek():
    p0.value(0)        
    time.sleep_ms(100)
    p0.value(1)
    time.sleep_ms(100)
    
# Blink Normal 1s
def blinkNormal():
    p0.value(0)        
    time.sleep(1)
    p0.value(1)
    time.sleep(1)
    
# Koneksi Wi-Fi
def connectWLAN():
    wlan0 = network.WLAN(network.STA_IF)
    if reset:
        wlan0.active(True)
            
        # Masukkan SSID dan Password
        wlan0.connect(SSID, Password)
            
        while not wlan0.isconnected():
            wlan0.active(True)
            pass

    status = wlan0.isconnected()
    ip_addr = wlan0.ifconfig()
    return status,ip_addr
    
# Set Tanggal
def setDateTime():
    ntptime.settime()
    
# Get Tanggal
def getDateTime():
    datetime = time.localtime()
    waktu = str(datetime[3]+7)+":"+str(datetime[4])+":"+str(datetime[5])
    tanggal = str(datetime[2])+"/"+str(datetime[1])+"/"+str(datetime[0])
    return waktu,tanggal

def main():
    global blinker,reset
    # Boot Sequence
    print(">> Memulai ESP8266 ... ")
    blinkNormal()
    
    print(">> Menghubungkan ke Wi-Fi ... ")
    blinkNormal()
    status,ip_addr = connectWLAN()
    
    if(status == True):
        print("==>> Sukses")
        while(blinker<3):
            blinkPendek()
            blinker += 1
            
        print("")
        print(">> Mengkonfigurasikan Waktu dan Tanggal ... ")
        blinkNormal()
        setDateTime()
        waktu,tanggal = getDateTime()
        print(">> Jam : ",waktu,", dan Tanggal : ",tanggal)
            
        print("")
        print(">> Memulai Web Server ... ")
        print(">> Akses Web via ",ip_addr[0],"... ")
        blinkNormal()
        
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.bind(('', 80))
        s.listen(5)

        while True:
            blinkPendek()
            waktu,tanggal = getDateTime()
            try:
                if gc.mem_free() < 102000:
                    gc.collect()
                conn, addr = s.accept()
                conn.settimeout(3.0)
                print("")
                print('>> Mendapatkan Koneksi Dari %s' % str(addr))
                request = conn.recv(1024)
                conn.settimeout(None)
                request = str(request)
                print('==>> Content = %s' % request)
                response = webpage.webPage(waktu,tanggal,ip_addr)
                conn.send('HTTP/1.1 200 OK\n')
                conn.send('Content-Type: text/html\n')
                conn.send('Connection: close\n\n')
                conn.sendall(response)
                conn.close()
            except OSError as e:
                conn.close()
                print('Koneksi Tertutup')
    
    else:
        print("==>> Gagal")
        print("==>> Sukses")
        while(blinker<1):
            blinkPendek()
            blinker += 1
            
main()