Halo teman-teman, Ketemu lagi nih dengan tulisan lanjutan tentang pemrograman pada arduino board wemos ESP8266. Bagi kamu yang belum tahu apa itu wemos , saya sarankan untuk membaca tulisan saya tentang “otomasi murah dan hemat dengan esp8266” . dan “cara mudah memprogram arduino” tentunya.
Sesuai janji saya pada tulisan sebelumnya, kali ini kita akan membahas tentang pemrograman python pada wemos ESP8266.
Apa itu python ? python adalah bahasa pemrograman yang benar-benar simple terutama untuk math dan science dan ajaibnya micropython dapat dijalankan pada board berbasis ESP8266, keren banget kan, dengan python, ga bakal kamu ruwet lagi mikirin coding c++ untuk melakukan otomasi tentunya.
Untuk tutorial kali ini pastikan Python telah terinstall pada laptopmu. Bila kamu menggunakan mac os seperti saya ada baiknya membaca tulisan “cara cerdas menjalankan pythonscript pada jupiter network” berikut Untuk menginstall python pada macbook kamu. Untuk environment windows dapat download installer pada python.org.
Jika python, sudah terinstall pastikan python dapat dipanggil pada terminal (macos) atau cmd.exe (windows) dengan perintah
python
Tulisan kali ini akan membahasa beberapa bagian yaitu:
- tools yang dibutuhkan untuk develop Python pada wemos D1
- pengecekan port/com serial dan driver
- flash firmware esp8266 yang support python intepreter
- cara menjalankan script python langsung pada console wemos
- menaktifkan koneksi wifi client pada wemos
- mengaktifkan websocket webrpl untuk transfer file ke wemos D1
- menjalankan autostart program pada wemos dan contoh code
- cara lain mengupload script python dengan console
- reboot device
- mencoba script python web server
A. Tools yang dibutuhkan untuk develop Python pada wemos
Baiklah langsung saja kita bahas satu persatu ya. Pertama kali kita harus menginstall beberapa requirement tools selain python interpreter
#install screen console (screen ini berguna untuk koneksi ke console wemos)
brew install screen
#install esptool
pip install esptool
#install adafruit-ampy
pip install adafruit-ampy
B. Pengecekan Port/COM Serial dan Driver
Driver CH341 harus terlebih dahulu di-install (cara install dapat dibaca postingan sebelumnya) dan pastikan Port telah terdeteksi pada console agar bisa diprogram. Pada Mac OS Biasanya ada di /dev/tty.wchusbserial1410 pada windows bisa dicek COM port tertentu misalnya :COM1.
ls /dev/| grep tty.wch
C. Flash firmware esp8266 yang support python intepreter
Supaya esp8266 dapat menjalankan script/program python yang kita tulis, maka kita harus melakukan re-flash firmwarenya dan mengganti dengan versi terbaru dari esp8266. List download firmware dapat dilihat tautan berikut:
https://micropython.org/download/all/
Untuk wemos D1 karena memorynya kecil saya memilih menggunakan firmware esp8266-1m-20200902-v1.13.bin
mkdir micropython
cd micropython
wget -c https://micropython.org/resources/firmware/esp8266-1m-20200902-v1.13.bin
ls
Cara melakukan flashing menggunakan tools esp ( esptool.py ) yang telah kita install sebelumnya diatas dengan pip. syntaknya adalah esptool.py [portcom] [arguments] [filefirmware] , Oiya jangan lupa wemos harus sudah dihubungkan via kabel microusb. Contohnya sebagai berikut:
esptool.py --port /dev/tty.wchusbserial1410 erase_flash
esptool.py --port /dev/tty.wchusbserial1410 --baud 921600 write_flash \
--flash_size=4MB -fm dio 0 esp8266-1m-20200902-v1.13.bin
Tunggu sampai flashing selesai dalam beberapa detik.
Jika flashing telah selesai maka langkah berikutnya adalah mencoba menjalankan script test pada console wemos.
D. Cara menjalankan script python langsung pada console wemos
Untuk menjalankan script python secara langsung kita menggunakan tool screen untuk memanggil console python pada embeded python yang ada pada wemos.
screen /dev/tty.wchusbserial1410 115200
Setelah muncul console python ketikan perintah
print("hello world")
Bila muncul hasilnya “Hello World” , berarti instalasi / flash firmware esp8266 dengan embeded python telah berhasil.
E. Mengaktifkan koneksi wifi client pada wemos
Wemos mengunakan chipset esp8266 embeded wifi yang dapat berfungsi sebagai client maupun accesspoint. Pada tahap ini kita akan memfungsikan wemos menjadi client pada jaringan wifi kita, sehingga nanti dapat mengirimkan data sensor langsung ke server cloud atau server di jaringan LAN ataupun difungsikan standalone sebagai web server.
Pada console python yang tadi telah dibuka kita ketikan perintah sebagai berikut:
import network
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.scan()
wlan.connect('namassid', 'password')
(ganti parameter namassid dan password dengan kondisi jaringan wifi yang ada)
Tunggu sampai Wemos Terkoneksi dan mendapat IP dari DHCP server wifi.
kemudian tekan tombol Enter (atau Return) dan ketikan perintah selanjutnya.
wlan.isconnected()
wlan.ifconfig()
Hasil dari wlan.ifconfig() bagian pertama : 192.168.100.43 adalah Assign IP yang didapat dari DHCP Server Wifi.
Bila ada hasil seperti diatas berarti wemos kita sudah siap digunakan di jaringan. Tahapan selanjutnya adalah melakukan transfer file ke wemos D1 via websocket.
F. Mengaktifkan websocket webrpl untuk transfer file ke wemos D1
Webrpl merupakan library bawan dari firmware esp8266 yang berisi script untuk menjalankan websocket server yang defaultnya ada di port 8266. Pada console python kita ketikan perintah sebagai berikut:
import webrepl
import webrepl_setup
jika ada pertanyaan enable as boot ketik E yang artinya enable webrpl saat booting wemos (auto startup).
kemudian akan diminta mengetikan 4-9 huruf password. masukan sembarang misalnya: 1234
jika ada pertanyaan lagi untuk reboot, pilih y
tunggu sampai reboot dengan sempurna
G. Menjalankan autostart program pada wemos
Pada wemos esp8266 ada 2 file yang akan dijalankan otomatis saat boot dengan urutan sequential sebagai berikut:
(1) boot.py
(2) main.py
Boot.py biasanya digunakan untuk melakukan inisialisasi koneksi wifi , jadi pastikan program yang anda upload diberi nama : main.py
Berikut adalah contoh kode untuk sebuah web server python yang dijalankan wemos.
(pastikan spasi dan tab sesuai, Kode program python sensitif terhadap tab dan space).
import machine
pins = [machine.Pin(i, machine.Pin.IN) for i in (0, 2, 4, 5, 12, 13, 14, 15)]
html = """<!DOCTYPE html>
<html>
<head> <title>ESP8266 Pins</title> </head>
<body> <h1>ESP8266 Pins</h1>
<table border="1"> <tr><th>Pin</th><th>Value</th></tr> %s </table>
</body>
</html>
"""
import socket
addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]
s = socket.socket()
s.bind(addr)
s.listen(1)
print('listening on', addr)
while True:
cl, addr = s.accept()
print('client connected from', addr)
cl_file = cl.makefile('rwb', 0)
while True:
line = cl_file.readline()
if not line or line == b'\r\n':
break
rows = ['<tr><td>%s</td><td>%d</td></tr>' % (str(p), p.value()) for p in pins]
response = html % '\n'.join(rows)
cl.send('HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n')
cl.send(response)
cl.close()
simpan kode diatas menjadi. main.py
Buka console webrpl menggunakan tools, buka alamat (pada chrome)
http://micropython.org/webrepl/
Kemudian pada address masukan alamat IP : 192.168.100.43 Port: 8266
atau jika menjadi lengkapnya. (ws://192.168.100.43:8266/ )
Klik tombol connect, masukan password: 1234 (yang sudah dibuat sebelumnya) dan
seharusnya akan muncul prompt python.
Untuk testing coba ketik helloworld berikut :
print("Hello World")
Untuk Mengupload file kedalam micropython wemos gunakan tombol [Choose File] disebelah kanan dan pilih
file main.py kemudian klik tombol. [Send to Device]
Tunggu sampai file di write ke devices selesai.
H. Cara lain mengupload script python dengan console
Download webrepl client micropython.
wget -c https://github.com/micropython/webrepl/archive/master.zip
unzip master.zip
cd webrepl-master
Cara mengupload code:
python webrepl_cli.py main.py 192.168.100.43:/main.py
I. Reboot wemos D1
untuk melakukan reboot ketikan kode berikut pada console python
import machine
machine.reset()
J. Mencoba hasil script webserver python
untuk mencoba output dari script main.py diatas yang berisi kode untuk menjalankan webserver yang sudah diupload ke device wemos kita bisa mengetikan alamat. http://192.168.100.43/ pada browser chrome kamu.
Dan Voilaa…. Ajaib kan !!!. Webserver python secara ajaib sudah running otomatis di device wemos D1 seharga 35 rb gaes…..
Kode main.py diatas dapat diisi sembarang kode yang ingin dijalankan sebagai autostart script python.
Demikian tulisan singkat saya tentang Having Fun python coding pada esp8266, Semoga bermanfaat.
Sampai jumpa di tulisan lainnya yang lebih Fun lagi.
HAPPY CODING !
Referensi:
- https://www.instructables.com/id/MicroPython-on-Cheap-3-ESP8266-WeMos-D1-Mini-for-T/
- https://micropython-on-wemos-d1-mini.readthedocs.io/en/latest/setup.html
- https://pythonforundergradengineers.com/upload-py-files-to-esp8266-running-micropython.html
- https://bigl.es/tooling-tuesday-wemos-d1-mini-micropython/
- https://randomnerdtutorials.com/esp32-esp8266-micropython-web-server/
Right here is the perfect web site for anyone who hopes to find out about this topic. You understand a whole lot its almost hard to argue with you (not that I actually would want toÖHaHa). You definitely put a new spin on a topic that has been discussed for decades. Excellent stuff, just great!
Id like to thank you for the efforts youve put in penning this site. I am hoping to check out the same high-grade content from you in the future as well. In fact, your creative writing abilities has encouraged me to get my own site now 😉
Hello there! This is my first comment here so I just wanted
to give a quick shout out and tell you I truly enjoy reading your blog posts.
Can you recommend any other blogs/websites/forums that go over the same topics?
Thanks a lot!
scientific editing servicesdomestic violence research paper
Hello there, I found your blog by means of Google even as searching for a similar topic, your web site got here up, it appears to be like good.
I have bookmarked it in my google bookmarks.
Hi there, just was aware of your blog via Google,
and located that it’s truly informative. I am going
to be careful for brussels. I’ll appreciate if you proceed this in future.
Lots of folks will be benefited out of your writing. Cheers!
Im thankful for the article.Really thank you! Great.
Can I use your phone? furacin soluble dressing merhem ne merhemi It also includes a seven-day disabled list for concussions and protocols for allowing players to return to play.
Very good post.Thanks Again. Keep writing.
Im thankful for the blog article.Really looking forward to read more. Awesome.
Great, thanks for sharing this article post.Much thanks again. Really Cool.
Amazing issues here. I’m very glad to see your post.Thanks so much and I’m looking forward to touch you. Will you please drop me a mail?
Hi there i am kavin, its my first occasion to commenting anywhere, when i read this piece of writing i thought i could also create comment due to this sensible paragraph.
Awesome article post.Really looking forward to read more. Great.
Thank you so much intended for letting me know what My partner and i didn’t know. I anticipate working with you.
I’m really impressed with your writing skills aswell as with the layout on your blog. Is this a paid theme or didyou customize it yourself? Either way keep up the nice quality writing, it’s rareto see a nice blog like this one these days.
Thank you for your article.Thanks Again. Really Cool.
It’s actually a cool and helpful piece of info. I’m glad that you shared this useful info with us. Please keep us informed like this. Thanks for sharing.
It’s going to be ending of mine day, but before end I am reading thisimpressive article to increase my experience.
This is one awesome article.Much thanks again. Really Cool.
Very informative post.Much thanks again. Awesome.
Really appreciate you sharing this post.
A big thank you for your blog post. Much obliged.
This is one awesome article.Really looking forward to read more.
Really enjoyed this article post.Really looking forward to read more. Much obliged.
This is one awesome post.Really thank you! Great.
Thanks for sharing, this is a fantastic blog post.Really looking forward to read more. Cool.
Your style is unique in comparison to other people I have read stuff from. Thank you for posting when you have the opportunity, Guess I will just book mark this blog.
Enjoyed every bit of your article. Keep writing.
what is cefdinir antibiotic cefdinir seizure cefdinir generic
This is an excellent tip significantly to These new towards the blogosphere. Simple but extremely correct details… Many thanks for sharing this 1. Essential study posting!
Pretty nice post. I just stumbled upon your blog and wished to say that I’ve really enjoyed browsing your blog posts. After all I’ll be subscribing to your rss feed and I hope you write again very soon!
Pretty section of content. I just stumbled upon your blog and in accession capital to assert thatI acquire actually enjoyed account your blog posts. Anyway I’ll be subscribing to your augment and even I achievement you access consistently quickly.
cyprus online pharmacy uk online pharmacy international delivery
My brother recommended I would possibly like this blog.He was once entirely right. This submit truly made my day.You cann’t believe simply how a lot time I had spent for this information! Thank you!
Great blog post.Really looking forward to read more. Keep writing.
I really like and appreciate your article post.Really looking forward to read more. Fantastic.
Hello there! Do you use Twitter? I’d like to follow you if that would be ok.I’m definitely enjoying your blog and look forward to new updates.
Hi there! Do you know if they make any plugins to help with SEO? I’m trying to get my blog to rank for some targeted keywords but I’m not seeing very good gains. If you know of any please share. Thanks!
Thank you for your post.Really looking forward to read more. Fantastic.
ivermectin australia stromectol – ivermectin 12
Major thanks for the blog.Really looking forward to read more. Keep writing.
I loved your article post. Really Cool.
Fantastic article post.Thanks Again. Cool.
Enjoyed every bit of your blog.Really looking forward to read more. Awesome.
Very informative blog article.Much thanks again.
Awesome article.Really looking forward to read more. Fantastic.
I really like your writing style, wonderful info , appreciate it for putting up : D.
ivermectina walmart ivermectin humans Loading…
Awesome blog.Really looking forward to read more. Awesome.
A big thank you for your article post.Much thanks again. Want more.
I really like your writing style, fantastic information, thanks for posting : D.
I am so grateful for your article. Cool.
Hello! This is my first visit to your blog! We are a group of volunteers andstarting a new initiative in a community in the same niche.Your blog provided us beneficial information to work on. You have done a wonderful job!
Thank you for your blog article.Really thank you! Awesome.
Thank you, I have recently been looking for information approximately this topic for a long time and yours is the best I have found out so far. But, what concerning the bottom line? Are you positive concerning the supply?
It’s an awesome paragraph designed for all the internet visitors; they will take advantage from it I am sure.
Greetings! Very helpful advice within this post! It is the little changes which will make the most significant changes. Many thanks for sharing!
Fantastic blog post.Really looking forward to read more. Awesome.
Fantastic blog.Really looking forward to read more. Will read on…
I am so grateful for your post.Much thanks again. Really Cool.
You have got some real insight. Why not hold some sort of contest for your readers?
magnificent points altogether, you simply gained a new reader.What could you suggest about your put up that you madea few days in the past? Any sure?
meds from india: generic pills india india pharmacy mail order
Very good blog.Really thank you! Keep writing.
😍ฝาก•ถอน ระบบ𝗔𝗨𝗧𝗢💸🤩เว็บมั่นคง ปลอดภัย💰🤡🥳ดูแลตลอด2️⃣4️⃣ชม.💡🥰ฝาก-ถอน 2-3วินาที💷😘เว็บตรงไม่ผ่านเอเย่น💴🤗รองรับทุกธนาคาร🏧😝ฝาก-ถอนไม่มีขั้นต่ำ⚔️🔥สมัครฟรี คลิกเลย👉👉
I do believe all of the ideas you’ve offered in your post.They’re very convincing and can definitely work. Still, the postsare too short for novices. May you please prolong them abit from subsequent time? Thank you for the post.
Very informative article.Really thank you! Awesome.
I value the article. Really Great.
I appreciate you sharing this post.Thanks Again. Really Great.
Well I really liked reading it. This tip offered by you is very practical for good planning.
Im thankful for the blog post.Much thanks again. Great.
Asking questions are in fact nice thing if you are not understanding anything fully, but this post gives pleasantunderstanding even.
academic writers online review online professional resume writing services
lasix 40 mg cost
Im grateful for the post. Awesome.
Looking forward to reading more. Great blog.Thanks Again.
Many thanks , I have not too long ago been attempting to find details about this topic for years and yours is the best I’ve identified to date. But, How about the conclusion? Have you been confident regarding the source?
diflucan uk online
Nice read, I just passed this onto a colleague who was doinga little research on that. And he actually bought me lunch becauseI found it for him smile So let me rephrase that: Thank you for lunch!Visit my blog :: seed sprouts
I really liked your post.Much thanks again. Much obliged.