How to Upload a Python Script to Rspberry Pi Zero
Headless Raspberry Pi Zero W Setup with Bonus Python Program and Debugging!
I merely finished setting up my Raspberry Pi Aught W and information technology was both extremely easy and unexpectedly, annoyingly tricky. I'll recap the easy function and record my debugging process for the tricky part here since I did not discover all of information I needed in 1 place.
There are more than robust headless setup tutorials out at that place, including this 1 that I initially used, just like I said — information technology may exist helpful to include all of my steps here in case you run into the same problems.
If you merely desire to ready up your Pi, feel free to follow forth. If yous're here to debug shit, the issue I was solving was Python related, and looks like the post-obit:
AttributeError: module 'lib' has no attribute 'X509_up_ref'
I'll cover what I did to run across and fix that soon.
Initial Setup
I had the Raspberry Pi Goose egg W from Adafruit with Headers, and a boot SD card with Debian Stretch Light pre-installed. If you lot need to flash information technology yourself, I recommend using Etcher and following along hither, every bit I went down this path in my efforts to debug my issues.
If y'all want to get shit running and don't have a keyboard/screen/mouse for your pi, we tin can make that piece of work via SSH and your handy WiFi that's included in the Zero Due west model.
Outset, you want to add some files to your SD card earlier booting up the pi. The following instructions are for Mac Bone:
- Insert the SD carte du jour into your preferred machine (I was using Macbook Pro).
- Open up up your terminal and
cd /Volumes/ && ls
. You should come across a directory calledboot
if everything mounted correctly. -
cd boot
-
touch ssh
— this creates a blank file needed to enable SSH for your pi! -
vi wpa_supplicant.conf
and add the following lines:
land=US
ctrl_interface=DIR=/var/run/wpa_supplicant Grouping=netdev
update_config=1network={
ssid="YOUR WIFI Proper name"
scan_ssid=1
psk="YOUR_WIFI_PASSWORD"
key_mgmt=WPA-PSK
}
It's important you lot get your wifi info right! Only the ssid
and psk
need to exist modified to reverberate your network settings.
Ok, now :wq
and get out of at that place. You're ready to power up!
Booting Upwardly
Insert your SD carte du jour into the pi, then plug in your 5V USB ability. You should see some nice flashy green LCD lights on your pi. Now, it is time for fun!
Open up your terminal again. We're going to SSH into your pi:
ssh pi@raspberrypi.local
You will be asked for a countersign. It is, unsurprisingly: raspberry
You'll want to change that now that you're in by typing passwd
and updating it to something more than secure.
(It sometimes takes a minute for your pi to boot, so if you don't see a server after that long, mayhap start troubleshooting that. I'k not going to include all that information here tho 😔)
Okay! Yous're inside your raspberry pi! Now what?
You can run some demo programs (headlessly, you can actually but run hello globe
) and learn about setting those upward hither. Otherwise, nosotros can just get started with a uncomplicated python plan that will send y'all a text. Why? Because this is ultimately part of a larger programme I will exist building, and it exposed a pretty annoying result that I had to resolve that you may see besides.
Offset of all, you lot want to update and install some basics:
sudo apt-get update && sudo apt-get upgrade -y
sudo apt-become install python-pip
Kewl, now you lot have python. Let's make that simple program. Offset things first: you'll want to get a Twilio trial account. It's pretty straightforward. Become alee, I'll wait.
…
Okay, Now we're set.
First: sudo pip install twilio
Then nosotros write 3 lines of code. You can use vi
if you like, but I preferred to just use nano
as I was experiencing some weird beliefs with Vi on my pi.
nano funTextTest.py
Y'all are at present in a python file! You lot may wish to copy the following:
from twilio.residuum import Client client = Customer(YOUR_ACCOUNT_SSID, YOUR_AUTH_KEY) customer.messages.create(to='+1your_number', from_='+1twilio_number', body='Hi my friend! I am a pi.')
That is it! Pretty straightforward, eh?? Now ^10
to quit, Y
to save, Enter
to confirm.
To come across if you're about to have fun or not, run information technology: py funTextTest.py
If y'all got a text, cool: you are done. Cheerio!
Otherwise…
The Troubles Begin
I immediately ran into an fault, with the virtually relevant bulletin in the stack trace being:
AttributeError: module 'lib' has no attribute 'X509_up_ref'
If you see this, y'all are in luck! I mean, you have a bug, just because I will tell you how to fix it without diving into a billion So answers and forum pleas.
You essentially accept a versioning issue with some dependencies. The reply (found eventually on this thread) is simple:
sudo rm -rf /usr/local/lib/python2.7/dist-packages/OpenSSL/
sudo apt install --reinstall python-openssl
Run your examination again, and you lot should no longer experience whatsoever unfortunate SSL-related bug.
This appeared to be relatively common as an consequence for folks using python libraries, but there were a lot of answers that I had to try before finding one that worked. Hopefully this helps!
If y'all saw a different error, feel free to post it in the comments. I saw a couple other wonky things that I may exist able to indicate y'all in the direction of solving, just you may have your own outcome to solve.
If the above error WAS your problem, and this solution does not work, please comment. I did some other impaired things to try and solve it that did not appear to gene into the solution, just may in fact have played some function in it finally working. I tin can include that information if it appears to be relevant!
Source: https://gerswayne.medium.com/headless-raspberry-pi-zero-w-setup-with-bonus-python-program-and-debugging-9ffc98349915
Posting Komentar untuk "How to Upload a Python Script to Rspberry Pi Zero"