File "Praktikum5.py"

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

 
Open Back
from machine import Pin
import time
import network
import ntptime
import urandom as random
import urequests as requests

# Variabel Wi-Fi
SSID = "<SSID>"
Password = "<Password>"
reset = True
current_time = None

# Variabel Mesin
p0 = Pin(2, Pin.OUT)
blinker = 0

# Variabel Blynk
token = "XlBVfsBit864iiGz_0nd9xi75-G8dyuf"
api = "https://sgp1.blynk.cloud/external/api/update?token="

# 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
    print(">> Memulai ESP8266 ... ")
    blinkNormal()
    
    print(">> Menghubungkan ke '{}'".format(SSID))
    blinkNormal()
    status,ip_addr = connectWLAN()
    
    if(status==True):
        print("==>> Terhubung")
        while(blinker<3):
            blinkPendek()
            blinker += 1
        print("==>> IP Address : {}".format(ip_addr[0]))
        print("==>> DNS Address : {}".format(ip_addr[3]))
        
        # Konfigurasikan Waktu dan Tanggal
        print("")
        print(">> Mengkonfigurasikan Waktu dan Tanggal ...")
        blinkNormal()
        setDateTime()
        waktu,tanggal = getDateTime()
        print("==>> Waktu : {} dan Tanggal : {}".format(waktu,tanggal))
        
        
        # Susunan VPIN Blynk
        # v0 = Jam
        # v1 = Menit
        # v2 = Detik
        # v3 = PH
        # v4 = Suhu
        # v5 = RH
        while True:
            print("")
            waktu,tanggal = getDateTime()
            print("==>> Waktu : {} dan Tanggal : {}".format(waktu,tanggal))

            jam = str.split(waktu,":")[0]
            menit = str.split(waktu,":")[1]
            detik = str.split(waktu,":")[2]

            print(">> Mengirim Waktu ke Blynk")
            requests.get(api+token+"&v0="+jam).close()
            requests.get(api+token+"&v1="+menit).close()
            requests.get(api+token+"&v2="+detik).close()
            
            # Buat Data Acak sebagai Sampel Sensor
            ph = random.getrandbits(12)%14
            suhu = random.getrandbits(12)%38
            rh = random.getrandbits(12)%100
        
            print(">> Mengirim Suhu ke Blynk")
            requests.get(api+token+"&v3="+str(int(ph))).close()
            requests.get(api+token+"&v4="+str(int(suhu))).close()
            requests.get(api+token+"&v5="+str(int(rh))).close()
            blinkPendek()
            
    else:
        print("==>> Gagal Terhubung")
        blinkPendek()
        print("")
        print(">> Membatalkan ESP8266 ... ")
main()