Connect to wifi using Python on Raspberry Pi - python

I am using Python 2.7 and wifi library (https://wifi.readthedocs.org/en/latest/) on a Raspberry Pi. I have managed to install wifi library using:
sudo pip install wifi
on the terminal. The library seems to work but I can't figure out the way to connect to a wifi using a password. The documentation on the website is a bit difficult to understand, especially if you are a Python noob like me. I used this:
>>> from wifi import Cell, Scheme
>>> Cell.all('wlan0')
and I got all the wifi networks available and also the one I want to connect to called test1. So I am sure that the library works. I followed the steps on the website but got a permission denied error at:
>>> scheme.save()
Also, before that there was this command:
>>> scheme = Scheme.for_cell('wlan0', 'home', cell)
Does anyone know what that 'home' refer to? Is it the SSID name? Can anyone help me connect to a wifi called test1 whose password is passwordtest1? Is there any easier way to connect to wifi through terminal so as not to use Python?. Thanks in advance.

After some research, I didn't find a way to connect easily to a wifi using Python on a Raspberry Pi. So I solved my problem by using the wifi command on the Terminal:
sudo wifi connect --ad-hoc SSID_Name
Which automatically asks me for an input: passkey> .Where you can actually type the password, press enter and after that it automatically connects to the wifi. After that I can run my Python script which needs a connection to the internet in order to run. The wifi command is preferable to other terminal commands when it comes to my problem cause it needs less time to connect to a wifi manually. For example it is preferable to the process which uses this terminal command:
sudo nano /etc/network/interfaces
I hope this helps everyone who has the same problem.

Related

Raspberry Pi unable to connect to Windows Websocket

I am attempting to send data from a Python script running on a Raspberry Pi to a Java Micronaut ServerWebSocket running on a Windows machine, but I am getting asyncio.exceptions.TimeoutError errors during the process.
I attempted to use the websockets library in Python on the Raspberry Pi to establish a websocket connection, but encountered a ConnectionRefusedError. Is this issue related to a network problem as mentioned here, and should I ask somewhere else like Superuser or Serverfault? Or is there another solution to this problem?
As #life888888 suggested, it had something to do with Windows firewall.
But instead of the public firewall being the problem, it was the one for the domain network instead.

Python: How to connect to Bluestacks or another emulator by ppadb?

I'm trying to simulate simple gestures like tap or swipe in BlueStacks emulator by using Python and PPADB. The problem is when I'm trying to connect.
Client(host="127.0.0.1", port=5037)
There is no devices. Emulator have address:
But when I try to connect to it by PPADB, then nothing happened and terminal stops work.
Here is the same ask.
I found working application where someone solved this problem but i dont understand what he exactly did.
Can someone check it and write simple code in one file?
Here is a link to this app and code.
BlueStacks uses port 5037 for ADB. This means that
adb = Client(host='127.0.0.1', port=5555)
should instead be
adb = Client(host='127.0.0.1', port=5037)
As you see in the link you mentioned, this person has an adb folder somewhere, where he runs adb.exe and starts the daemon, so the adb server runs in the localhost 127.0.0.1 on the port 5037 which is the default, so I suggest you try running subprocess.run("/path/to/adb.exe","start-server") and check again

Executing code.py on Raspberry PI, from computer via ethernet connection

How could I launch code.py that only exists on a raspberry, from a python command line on a computer connected by an Ethernet cable. Machine A and the raspberry are connected to each other by a simple ethernet cable, and none of those two endpoints are connected to the internet. They only have the latest version of python installed and can't have anything more installed on them.
It looks like subprocess.Popen() could be a way to tackle it. But with the little knowledge I have I can't understand and know for sure if using this method is suitable !
Thank you for your time :)

Python Wifi Breaks Raspberry Pi

I am currently building an application on Raspberry Pi using Python, and I am trying to connect to the wifi. The package I am using is wifi 0.3.8 installed via pip3.
The code looks like this for connecting:
cell = wifi.Scheme.find('wlan0', ssid)
scheme = wifi.Scheme.for_cell('wlan0', cell.ssid, cell, password)
scheme.save()
scheme.activate()
There are several problems I've encountered:
Uses ifup and ifdown
The current problem with the package is it uses /sbin/ifup and /sbin/ifdown. These are deprecated and do not work. My hack fix was to reroute to use /sbin/ifconfig wlan0 up and /sbin/ifconfig wlan0 down. Yes very hacky but it worked.
Modifies /etc/network/interfaces
The biggest problem I've encountered is modified /etc/network/interfaces by placing a connection string in there with something like:
source-directory /etc/network/interfaces.d
uface wlan0-[network-ssid-name]
Which breaks the wifi completely. I've had to re-type the entire configuration file to get it working again. It SHOULD be modifying /etc/wpa_supplicant/wpa_supplicant.conf only.
Would anyone have any suggestions on how to correctly connect to the wifi using Python?

How to control Raspberry Pi's GPIO pins from separate PC

In my application I want to turn on certain GPIO pins that are on the Raspberry Pi from my PC's C++ script. My PC is currently running on Windows 10 and my Raspberry Pi on Raspbian.
The general idea I had was to write python scripts on the Raspberry Pi itself and run them somehow from my PC when needed. Although, I am not sure how to do this or if it's possible in C++. I know I can ssh into my Raspberry Pi and run scripts manually but the idea of this application is automation. I am able to connect my PC to the Raspberry Pi either by Ethernet or USB cables.
I have seen USB to GPIO modules that would probably work better although for now I am stuck with my Raspberry Pi.
Any knowledge or resources on how to do this is greatly appreciated.
Maybe be what you need is a client and server program to control your GPIO pins. Check out my client and server programs and dont forget to read the answer and its comments and make the change accordingly for the program to work.(My progams motives is to control an LED through the client and server programs , similar to controlling the GPIO pins )
Program getting stuck at accept statement
PS: I hope you are having some knowledge about Threading and sockets.
This answer will be pretty broad so feel free to leave a comment with questions.
The first things that are needed, are the python scripts on the Pi which control the GPIOs in however fashion we like. From then on we are able to execute these commands via ssh from almost any device. Although, we want this process to be automated. In other words, the user doesn't have to enter credentials to ssh into the Pi but rather have the program automatically do so.
Luckily, this library allows us to do so. -> https://www.libssh.org/
There is nice documentation as to how to create ssh sessions and executing the commands that we want. So I won't go into much detail. Although I will comment on one procedure that the documentation recommends but gave me a problem.
When authenticating the user via password we use method ssh_userauth_password(ssh_session session, const char* username, const char* password). The documentation suggests setting the second parameter to NULL. Which was what was giving me the problem.
Simply change this to the login name you use when you normally ssh into the Raspberry Pi. For most the default name is "pi".
ssh_userauth_password(my_ssh_session, "pi", password);

Categories

Resources