From 4257240200dea2822dc078012ea173966f2bb2a6 Mon Sep 17 00:00:00 2001 From: Aditya Raman Date: Fri, 11 Oct 2019 23:56:45 +0530 Subject: [PATCH 1/9] Create server_client.py --- server_client.py | 176 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 176 insertions(+) create mode 100644 server_client.py diff --git a/server_client.py b/server_client.py new file mode 100644 index 0000000..9972ab6 --- /dev/null +++ b/server_client.py @@ -0,0 +1,176 @@ +import network +from utime import sleep_ms +import usocket as socket + +# Setup WiFi connection: +def connect_wifi(ssid, passwd): + ap = network.WLAN(network.AP_IF) + ap.active(False) + + print("Connecting to WiFi '%s'. This takes some time..." % ssid) + + wifi = network.WLAN(network.STA_IF) + wifi.active(True) + wifi.connect(ssid, passwd) + + while wifi.status() == network.STAT_CONNECTING: + sleep_ms(100) + + if wifi.isconnected(): + print("Connection established. My IP is " + str(wifi.ifconfig()[0])) + return True + else: + status = wifi.status() + if status == network.STAT_WRONG_PASSWORD: + status = "WRONG PASSWORD" + elif status == network.STAT_NO_AP_FOUND: + status = "NETWORK '%s' NOT FOUND" % ssid + else: + status = "Status code %d" % status + print("Connection failed: %s!" % status) + return False + +# Server: +def server(port, max_clients=1): + print("Starting server at port %d..." % port) + + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + sock.bind(('', port)) + sock.listen(max_clients) + + print("Server started. Connect a client now!") + + conn, cl_addr = sock.accept() + print("New connection from %s:%d" % cl_addr) + + while True: + data = conn.recv(1024).decode() + print(" Received from client: " + str(data)) + data = input(">> ") + conn.send(data.encode()) + + sock.close() + print("Server stopped.") + + +# Simple Client: +def client(host, port): + print("Connecting to server %s:%d..." % (host, port)) + + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + sock.connect((host, port)) + + print("Connection established.") + print("Enter 'exit' to close the client.") + + while True: + msg = input(">> ") + if msg.lower().strip() == "exit": + break + + sock.send(msg.encode()) + data = sock.recv(1024).decode() + + print("Received from server: %s" % data) + + sock.close() + print("Client closed.") + + +# Prints available WiFi networks: +def print_wlan_list(sort_by=3, sort_desc=True): + """sort by 0 = network name, 1 = bssid, 2 = ch, 3 = signal, ...""" + + from ubinascii import hexlify + + wlan = network.WLAN(network.STA_IF) + prev_wlan_state = wlan.active() # restore after scan + wlan.active(True) + + table_header = ("network name", "BSSID", "CH", "signal", "authmode", "visibility") + + # safe all networks as tuples in a list, where [0] is a list + # containing the maximum lengths of the subitems for oled; + # - 0: network name + # - 1: bssid (hardware address) + # - 2: channel + # - 3: rssi (signal strength, the higher the better) + # - 4: authmode (most likely WPA/WPA2-PSK) + # - 5: visible/hidden + scan = [[0] * len(table_header)] + + # minimum length is table header + for i in range(len(table_header)): + scan[0][i] = len(table_header[i]) + + # scan + for item in wlan.scan(): + bssid = hexlify(item[1]).decode("ascii") + bssid = ':'.join([bssid[i:i + 2] for i in range(0, len(bssid), 2)]) + + new = (item[0].decode("utf-8"), + bssid, + item[2], + item[3], + ("open", "WEP", "WPA-PSK", "WPA2-PSK", "WPA/WPA2-PSK")[int(item[4])], + ("visible", "hidden")[int(item[5])]) + scan.append(new) + + for i in range(0, len(scan[0])): + len_new = len(str(new[i])) + if len_new > scan[0][i]: + scan[0][i] = len_new + + wlan.active(prev_wlan_state) + + # print table + def center_subitems(ituple): + retlist = [] + for i in range(len(ituple)): + missing_spaces = scan[0][i] - len(str(ituple[i])) + if missing_spaces > 0: + spaces_right = int(missing_spaces / 2) + spaces_left = missing_spaces - spaces_right + retlist.append(' ' * spaces_left + str(ituple[i]) + ' ' * spaces_right) + else: + retlist.append(ituple[i]) + return tuple(retlist) + + header_string = "|| %s || %s | %s | %s | %s | %s ||" % center_subitems(table_header) + print('-' * len(header_string)) + print(header_string) + print('-' * len(header_string)) + + for item in sorted(scan[1:], key=lambda x: x[sort_by], reverse=sort_desc): + print("|| %s || %s | %s | %s | %s | %s ||" % center_subitems(item)) + + print('-' * len(header_string)) + + +# Interface for setting up server/client: +def main(): + while True: + cmd = input("Run server (S) or client (C)? ").lower() + if cmd == 's': + while True: + print("\n") + ssid = input("Enter your WiFi name: ") + passwd = input("Enter WiFi password: ") + if connect_wifi(ssid, passwd): + break + print("Scanning for available WiFi networks...") + print_wlan_list() + server(5678) + break + elif cmd == 'c': + ip = input("Enter the server's IP address (see other ESP): ") + try: + client(ip, 5678) + break + except OSError: + print("Connection failed. Did you already start the server on the other ESP?") + else: + print("Invalid input. Please enter 'S' or 'C'.") + + +main() From 91d455854bc83727da52df463c9118a3b7dab85a Mon Sep 17 00:00:00 2001 From: Aditya Raman Date: Fri, 11 Oct 2019 23:58:10 +0530 Subject: [PATCH 2/9] Rename server_client.py to 06_server_client/server_client.py --- server_client.py => 06_server_client/server_client.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename server_client.py => 06_server_client/server_client.py (100%) diff --git a/server_client.py b/06_server_client/server_client.py similarity index 100% rename from server_client.py rename to 06_server_client/server_client.py From cedfa80d955627baec9de7d212095d631bd018d4 Mon Sep 17 00:00:00 2001 From: Aditya Raman Date: Fri, 11 Oct 2019 23:58:40 +0530 Subject: [PATCH 3/9] Create README.md --- 06_server_client/README.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 06_server_client/README.md diff --git a/06_server_client/README.md b/06_server_client/README.md new file mode 100644 index 0000000..57104bf --- /dev/null +++ b/06_server_client/README.md @@ -0,0 +1 @@ +# Server Client From b1aeaee5a9650f90892553ba51d8d5e5af3ce189 Mon Sep 17 00:00:00 2001 From: ramanaditya Date: Sat, 12 Oct 2019 00:00:11 +0530 Subject: [PATCH 4/9] Readme --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index 336ec39..1e85eb9 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,6 @@ Podnet is a communication network provider for IoT devices. IoT developers/compa [![](https://sourcerer.io/fame/ramanaditya/ramanaditya/nodemcu_examples/images/0)](https://sourcerer.io/fame/ramanaditya/ramanaditya/nodemcu_examples/links/0)[![](https://sourcerer.io/fame/ramanaditya/ramanaditya/nodemcu_examples/images/1)](https://sourcerer.io/fame/ramanaditya/ramanaditya/nodemcu_examples/links/1)[![](https://sourcerer.io/fame/ramanaditya/ramanaditya/nodemcu_examples/images/2)](https://sourcerer.io/fame/ramanaditya/ramanaditya/nodemcu_examples/links/2)[![](https://sourcerer.io/fame/ramanaditya/ramanaditya/nodemcu_examples/images/3)](https://sourcerer.io/fame/ramanaditya/ramanaditya/nodemcu_examples/links/3)[![](https://sourcerer.io/fame/ramanaditya/ramanaditya/nodemcu_examples/images/4)](https://sourcerer.io/fame/ramanaditya/ramanaditya/nodemcu_examples/links/4)[![](https://sourcerer.io/fame/ramanaditya/ramanaditya/nodemcu_examples/images/5)](https://sourcerer.io/fame/ramanaditya/ramanaditya/nodemcu_examples/links/5)[![](https://sourcerer.io/fame/ramanaditya/ramanaditya/nodemcu_examples/images/6)](https://sourcerer.io/fame/ramanaditya/ramanaditya/nodemcu_examples/links/6)[![](https://sourcerer.io/fame/ramanaditya/ramanaditya/nodemcu_examples/images/7)](https://sourcerer.io/fame/ramanaditya/ramanaditya/nodemcu_examples/links/7) -
## Join Our Community From 36ae066248a64d595b67ab82b3929a9502b36b1c Mon Sep 17 00:00:00 2001 From: ramanaditya Date: Sat, 12 Oct 2019 00:01:44 +0530 Subject: [PATCH 5/9] Server Client --- 06_server_client/server_client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/06_server_client/server_client.py b/06_server_client/server_client.py index 9972ab6..35542b5 100644 --- a/06_server_client/server_client.py +++ b/06_server_client/server_client.py @@ -53,7 +53,7 @@ def server(port, max_clients=1): print("Server stopped.") -# Simple Client: +# Client: def client(host, port): print("Connecting to server %s:%d..." % (host, port)) From b7918d1739155d6bd0ab02eb12b2e0a761968aab Mon Sep 17 00:00:00 2001 From: Aditya Raman Date: Sat, 26 Oct 2019 10:53:39 +0530 Subject: [PATCH 6/9] Rename 06_server_client/README.md to 07_server_client/README.md --- {06_server_client => 07_server_client}/README.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename {06_server_client => 07_server_client}/README.md (100%) diff --git a/06_server_client/README.md b/07_server_client/README.md similarity index 100% rename from 06_server_client/README.md rename to 07_server_client/README.md From a7f7e660b4842d8b841fd47ab84224b4afc76335 Mon Sep 17 00:00:00 2001 From: Aditya Raman Date: Sat, 26 Oct 2019 10:54:11 +0530 Subject: [PATCH 7/9] Rename 06_server_client/server_client.py to 07_server_client/server_client.py --- {06_server_client => 07_server_client}/server_client.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename {06_server_client => 07_server_client}/server_client.py (100%) diff --git a/06_server_client/server_client.py b/07_server_client/server_client.py similarity index 100% rename from 06_server_client/server_client.py rename to 07_server_client/server_client.py From a2e43b08cb5dcb591fdec47bffb64a1fa1072f28 Mon Sep 17 00:00:00 2001 From: Aditya Raman Date: Sat, 26 Oct 2019 10:56:41 +0530 Subject: [PATCH 8/9] Update readme.md --- 06.ESP_TEMPERATURE/readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/06.ESP_TEMPERATURE/readme.md b/06.ESP_TEMPERATURE/readme.md index 3a33e60..b78d6ae 100644 --- a/06.ESP_TEMPERATURE/readme.md +++ b/06.ESP_TEMPERATURE/readme.md @@ -1,7 +1,7 @@ # 06_ESP_TEMPERATURE To run the program use the following ampy command -##NOTE +## NOTE The temperature values differ from chip to chip, and the temperature sensor returns a higher value than the room temperature because of the temperature that SoC is operating upon is higher and closer to the sensor. Typically the values are 20 Celsius higher than room temperature. ## MAC OS From 879021386ac3253fded4d43151feb952b3b03a5a Mon Sep 17 00:00:00 2001 From: Aditya Raman Date: Sat, 26 Oct 2019 11:08:16 +0530 Subject: [PATCH 9/9] Update README.md --- 07_server_client/README.md | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/07_server_client/README.md b/07_server_client/README.md index 57104bf..47ffbd9 100644 --- a/07_server_client/README.md +++ b/07_server_client/README.md @@ -1 +1,32 @@ # Server Client +To run the program use the following ampy command + +## Note +- We will use ```ampy``` to put the code in the ESP8266 picocom to run the program inside the ```terminal``` +- Use two different system for each ESP8266 +- Connect the system which you want to make server with the WiFi before starting the server +- This communication will be half duplex and the system will wait until the information is not received or sent + +## MAC OS +```bash + ampy --port /dev/tty.SLAB_USBtoUART put server_client.py main.py +``` + +```bash + picocom /dev/tty.SLAB_USBtoUART -b 115200 + + Press ctrl + C to end the process + import main +``` + +## Linux +```bash + ampy --port /dev/ttyUSB0 run accesspoint.py +``` + +```bash + picocom /dev/ttyUSB0 -b 115200 + + Press ctrl + C to end the process + import main +```