From 9b280d8b94910360fb378cd7e63770f74a3b3551 Mon Sep 17 00:00:00 2001 From: ramanaditya Date: Sun, 6 Oct 2019 22:57:05 +0530 Subject: [PATCH 1/4] Browser using API to control LED using browser --- 05_browser_using_AP/browser_using_AP.py | 78 +++++++++++++++++++++++++ 05_browser_using_AP/readme.md | 24 ++++++++ 2 files changed, 102 insertions(+) create mode 100644 05_browser_using_AP/browser_using_AP.py create mode 100644 05_browser_using_AP/readme.md diff --git a/05_browser_using_AP/browser_using_AP.py b/05_browser_using_AP/browser_using_AP.py new file mode 100644 index 0000000..fbd652b --- /dev/null +++ b/05_browser_using_AP/browser_using_AP.py @@ -0,0 +1,78 @@ +import usocket as socket +from machine import UART + +uart = UART(0, 115200) # init with given baudrate +uart.init(115200, bits=8, parity=None, stop=1) # init with given parameters +import network +import machine +import time + +import esp +esp.osdebug(None) + +import gc +gc.collect() + +ssid = 'ESP8266' +password = 'nodeesp8266' + +ap = network.WLAN(network.AP_IF) +ap.active(True) +ap.config(essid=ssid, password=password) + +while ap.active() == False: + pass + +print('Connection successful') +print(ap.ifconfig()) + +def web_page(): + html = """ +

Hello, World!

+ + + + """ + return html + + +s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) +s.bind(('', 80)) +s.listen(5) + +blue = machine.Pin(14, machine.Pin.OUT) +red = machine.Pin(12, machine.Pin.OUT) +green = machine.Pin(13, machine.Pin.OUT) + +for i in range(2): + green.on() + time.sleep_ms(500) + green.off() + blue.on() + time.sleep_ms(500) + blue.off() + red.on() + time.sleep_ms(500) + red.off() +while True: + conn, addr = s.accept() + print('Got a connection from %s' % str(addr)) + request = conn.recv(1024) + print('Content = %s' % str(request)) + print("conn = %s" % str(conn)) + str_conn = str(request) + if "red" in str_conn: + red.on() + blue.off() + green.off() + if "blue" in str_conn: + red.off() + blue.on() + green.off() + if "green" in str_conn: + red.off() + blue.off() + green.on() + response = web_page() + conn.send(response) + conn.close() diff --git a/05_browser_using_AP/readme.md b/05_browser_using_AP/readme.md new file mode 100644 index 0000000..f7e2fe6 --- /dev/null +++ b/05_browser_using_AP/readme.md @@ -0,0 +1,24 @@ +# 04_ACCESS_POINT +To run the program use the following ampy command + +## Pre-requisite +- This program is for controlling RGB LED using NodeMCU +- Run this program after setting up the hardware part. For more information visit : [Controlling RGB LED using NOdeMCU](https://blog.thepodnet.com/nodemcu-rgb-led-using-micropython/) + +## MAC OS +```bash + ampy --port /dev/tty.SLAB_USBtoUART run browser_using_AP.py +``` + +## Linux +```bash + ampy --port /dev/ttyUSB0 run browser_using_AP.py +``` + +## Connect to WiFi +- search for ESP8266 +- password ```nodeesp8266``` + +## Open the browser + +## Visit ```192.168.4.1``` in your browser and select the color to change the color of LED \ No newline at end of file From 5126c373ef508abff3aa6b0baee5f989b06b2244 Mon Sep 17 00:00:00 2001 From: Aditya Raman Date: Mon, 7 Oct 2019 04:15:44 +0530 Subject: [PATCH 2/4] Updated the code with comments --- 05_browser_using_AP/browser_using_AP.py | 86 +++++++++++++++++++++---- 1 file changed, 72 insertions(+), 14 deletions(-) diff --git a/05_browser_using_AP/browser_using_AP.py b/05_browser_using_AP/browser_using_AP.py index fbd652b..79ec723 100644 --- a/05_browser_using_AP/browser_using_AP.py +++ b/05_browser_using_AP/browser_using_AP.py @@ -1,8 +1,4 @@ import usocket as socket -from machine import UART - -uart = UART(0, 115200) # init with given baudrate -uart.init(115200, bits=8, parity=None, stop=1) # init with given parameters import network import machine import time @@ -13,9 +9,10 @@ import gc gc.collect() -ssid = 'ESP8266' -password = 'nodeesp8266' +ssid = 'ESP8266' # Name of your network +password = 'nodeesp8266' # password of your network +# Making NodeMCU Hotspot ap = network.WLAN(network.AP_IF) ap.active(True) ap.config(essid=ssid, password=password) @@ -26,12 +23,32 @@ print('Connection successful') print(ap.ifconfig()) +# Web Template def web_page(): - html = """ -

Hello, World!

- - - + html = """ + + + +

The Podnet

+

Press any button to blink LED of yout choice

+ + + + + + """ return html @@ -40,10 +57,12 @@ def web_page(): s.bind(('', 80)) s.listen(5) +# Initialization of the pins for different colours blue = machine.Pin(14, machine.Pin.OUT) red = machine.Pin(12, machine.Pin.OUT) green = machine.Pin(13, machine.Pin.OUT) +# Starter blinking to authenticate node is working properly for i in range(2): green.on() time.sleep_ms(500) @@ -54,6 +73,8 @@ def web_page(): red.on() time.sleep_ms(500) red.off() + +# To continue detecting devices while True: conn, addr = s.accept() print('Got a connection from %s' % str(addr)) @@ -61,18 +82,55 @@ def web_page(): print('Content = %s' % str(request)) print("conn = %s" % str(conn)) str_conn = str(request) - if "red" in str_conn: + if "colourred" in str_conn: red.on() blue.off() green.off() - if "blue" in str_conn: + if "colourblue" in str_conn: red.off() blue.on() green.off() - if "green" in str_conn: + if "colourgreen" in str_conn: red.off() blue.off() green.on() + if "colourpink" in str_conn: + red.on() + blue.on() + green.off() + if "colouryellow" in str_conn: + red.on() + blue.off() + green.on() + if "colourcyan" in str_conn: + red.off() + blue.on() + green.on() + if "colourblink" in str_conn: + for i in range(5): + red.off() + blue.off() + green.on() + time.sleep_ms(300) + green.off() + red.on() + blue.on() + time.sleep_ms(300) + red.off + green.off() + blue.on() + time.sleep_ms(300) + blue.off() + green.on() + red.on() + time.sleep_ms(300) + blue.off() + green.off() + red.on() + time.sleep_ms(300) + red.off() + blue.on() + green.on() response = web_page() conn.send(response) conn.close() From da94286b40a7a4b0d48cecfb9c69b8c3fc2cf7e5 Mon Sep 17 00:00:00 2001 From: Aditya Raman Date: Mon, 7 Oct 2019 04:17:29 +0530 Subject: [PATCH 3/4] Rename 05_browser_using_AP/browser_using_AP.py to 05_led_control_using_wifi/led_control_using_wifi.py --- .../led_control_using_wifi.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename 05_browser_using_AP/browser_using_AP.py => 05_led_control_using_wifi/led_control_using_wifi.py (100%) diff --git a/05_browser_using_AP/browser_using_AP.py b/05_led_control_using_wifi/led_control_using_wifi.py similarity index 100% rename from 05_browser_using_AP/browser_using_AP.py rename to 05_led_control_using_wifi/led_control_using_wifi.py From 08f63ebd9dbab8e9259e54b1749cd15b9f6fb960 Mon Sep 17 00:00:00 2001 From: Aditya Raman Date: Mon, 7 Oct 2019 04:18:08 +0530 Subject: [PATCH 4/4] Rename 05_browser_using_AP/readme.md to 05_led_control_using_wifi/readme.md --- {05_browser_using_AP => 05_led_control_using_wifi}/readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename {05_browser_using_AP => 05_led_control_using_wifi}/readme.md (95%) diff --git a/05_browser_using_AP/readme.md b/05_led_control_using_wifi/readme.md similarity index 95% rename from 05_browser_using_AP/readme.md rename to 05_led_control_using_wifi/readme.md index f7e2fe6..04681b5 100644 --- a/05_browser_using_AP/readme.md +++ b/05_led_control_using_wifi/readme.md @@ -21,4 +21,4 @@ To run the program use the following ampy command ## Open the browser -## Visit ```192.168.4.1``` in your browser and select the color to change the color of LED \ No newline at end of file +## Visit ```192.168.4.1``` in your browser and select the color to change the color of LED