File "Praktikum6.py"

Full Path: /var/www/html/main/lecture-notes/Internet of Things/02 Materi Praktikum - IoT - PDF/File Python/Praktikum6.py
File size: 3.54 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

token = "1901469256:AAHz0864vwPsAS6HWu68GZ4uoQ8k_FS0YU8"
chat_id = "1362682845"
url = "https://maulanahirzan.pythonanywhere.com/sendMessage"

# 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("==>> Sukses")
        
        print("")
        print(">> ESP8266 Memulai Pengiriman Data ... ")
        while True:
            print(">> Mengirim Data ke Telegram Bot")

            # Ambil Data Waktu dan Tanggal
            waktu,tanggal = getDateTime()
            print("==>> Waktu : {} dan Tanggal : {}".format(waktu,tanggal))
            
            # Buat Data Acak sebagai Sampel Sensor
            ph = random.getrandbits(12)%14
            suhu = random.getrandbits(12)%38
            rh = random.getrandbits(12)%100
            print("==>> PH : {}, Suhu : {}, dan RH {}".format(ph,suhu,rh))
            
            # Susun URL Web Service
            post_token = "?token="+token
            post_chatid = "&chatid="+chat_id
            post_waktu = "&waktu="+waktu
            post_tanggal = "&tanggal="+tanggal
            post_ph = "&ph="+str(int(ph))
            post_suhu = "&suhu="+str(int(suhu))
            post_rh = "&rh="+str(int(rh))
            url_api1 = url+post_token+post_chatid+post_waktu+post_tanggal
            url_api2 = url_api1+post_ph+post_suhu+post_rh
            print(url_api2)
            req = requests.get(url_api2)
            result = req.status_code
            
            if(result == 200):
                print("==>> Sukses")
                req.close()
                blinkPendek()
            else:
                print("==>> ESP8266 gagal mengirim ... ")
            
            print("")
            
    else:
        print("==>> Gagal Terhubung")
        blinkPendek()
        print("")
        print(">> Membatalkan ESP8266 ... ")

main()