gauravsharma0190
Posts: 125
Joined: Tue Oct 28, 2014 6:36 am

Connection with clilent to server like esp32

Fri Jul 05, 2019 9:33 am

Hello there,
i am working on raspberry pi's as a client and server.
i have done my coding on python. my idea of project is , a server is fixed used as a hot spot and client is movable.
i used some scripts to show the client present in the network. but now, i want to see it in the client side also like we use esp32 wifi
as we define ssid and pass in the code.
so what client do , it connect to the wifi if present in the range with id and passwd and also show the ip of the access point from which it is connected with.
i can share you some code of this.

Code: Select all

#!/bin/bash

# show_wifi_clients.sh
# Shows MAC, IP address and any hostname info for all connected wifi devices
# written for openwrt 12.09 Attitude Adjustment
#

printf  "%-20s %-30s %-20s\n" "IP address" "lease name" "MAC address"
leasefile=/var/lib/misc/dnsmasq.leases
# list all wireless network interfaces 
# (for MAC80211 driver; see wiki article for alternative commands)
for interface in `iw dev | grep Interface | cut -f 2 -s -d" "`
do
  # for each interface, get mac addresses of connected stations/clients
  maclist=`iw dev $interface station dump | grep Station | cut -f 2 -s -d" "`
  # for each mac address in that list...
  for mac in $maclist
  do
    # If a DHCP lease has been given out by dnsmasq,
    # save it.
    ip="UNKN"
    host=""
    ip=`cat $leasefile | cut -f 2,3,4 -s -d" " | grep $mac | cut -f 2 -s -d" "`
    host=`cat $leasefile | cut -f 2,3,4 -s -d" " | grep $mac | cut -f 3 -s -d" "`
    # ... show the mac address:
    printf "  %-20s %-30s %-20s\n" $ip $host $mac
  done
done

*******************************************SERVER CODE ****************************************


def getIPs():    
    myCmd = os.popen('./show_client.sh').read()
    IPs = myCmd.split()
    r = re.compile(r"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$")
    IPs = (filter(r.match, IPs))
    print("Current IPs:", IPs)
    return set(IPs)
    
def getNeighbours():
    global new_list, old_list, hello, byebye
    new_list = getIPs()
    hello = set(new_list) - set(old_list)
    byebye = set(old_list) - set(new_list)
    
    if len(hello)>0 and len(byebye)>0:
        print("The Following IP's joined new\n", hello)
        ip_val = hello
        print("value of Ip\n", new_list)
        print("The Following IP's disconnected new\n", byebye)
        old_list = set(new_list)
    elif len(hello)>0:
        print("The Following IP's joined new\n", hello)
        old_list = set(new_list)
    elif len(byebye)>0:
        print("The Following IP's disconnected new\n", byebye)
        old_list = set(new_list)
    else:
        print("No changes")
    for ips in hello:
        clientAddress=ips
        clientPort=12345
        newthread = ClientThread(clientAddress, clientPort)
        newthread.start()
        
def WifiListener():
    print("WifiListener  started")
    while(True):
        getNeighbours()
        time.sleep(5)

LOCALHOST = "127.0.0.1"
#clientPort = 12345
#clientAddress = '' #used for all communication
PORT= 12345
old_list=set()
new_list=set()
hello=set()
byebye=set()
thread1 = threading.Thread(target = WifiListener)
thread1.start()
#WifiListener()
print("Server started")
print("Waiting for client request..") 
 *****************************************************************************************
now this server will find all the ip with in the network and tells us the ip status.
but if the client is movable then it has to be connect to other hotspot( in this case i am assuming all the hotspot is same ssid) , then it also has to know that it is connected to that access point like in esp32 wifi example, as it search the wifi and connected and share some info: if not found then found it again and again.
please tell me how to do this.

Return to “Networking and servers”