Raspberry Pi 4 connect it to a computer via USB












Problem:

In the Raspberry Pi 4, the USB Type-C power jack can also be used for data transfer. This can be used for various useful project purposes and ideas, for example, how to connect to a computer trough USB port as an OTG device. ( USB OTG introduces the concept of a device performing both master and slave roles – whenever two USB devices are connected and one of them is a USB OTG device  source )

Solution:

1. Turn on a dwc2 driver writing the next line in /boot/config.txt

dtoverlay=dwc2

2. Use the options for using the Raspberry Pi as a USB device connected to a computer

You will be able to create a small FAT32 file system in a file on an SD card that will appear on the computer as a read / write drive

sudo dd if=/dev/folder of=/piusb.bin bs=512 count=2880
sudo mkdosfs /piMyUSB.bin
sudo modprobe g_mass_storage file=/piMyUSB.bin stall=0

For example, using the next Makerfun USB dongle expansion board for your Raspberry Pi Zero, you will be able to attachc your raspberry pi zero to your computer

Download images from amazon from database table image list











Problem:
You need to download a huge list of images from amazon and the list is in a data base table.

Solution:
You can create an easy script using request to access to the image url and shutil to copy one by one each image.

import mysql.connector
import shutil
import requests
conn = mysql.connector.connect(
       host="localhost",
       user="usuario",
       password="******",
       database="DBIMAGES"
       )
cursor = conn.cursor()
cursor.execute('SELECT * FROM z_images ORDER')
rows = cursor.fetchall()
for row in rows:
  resp = requests.get(row[4], stream=True)
  path = row[4]
  firstpos = path.rfind("/")+1
  lastpos=len(path)
  v_filename = path[firstpos:lastpos] 
  local_file = "/home/img/"+v_filename
  with open(local_file, 'wb') as out_file:
    shutil.copyfileobj(resp.raw, out_file)
    print(v_filename)
print("END")

Rapberry pi lens comparation

With the release of the new Raspberry Pi High Quality Camera, you have the option to use different CS-mount senses with your Raspberry Pi. The High Quality Camera is a 12,3 M sensor with a 7.9 diagonal image size, enough for hobby projects and learn about digital photography.




Take a look in the official product page: https://www.raspberrypi.org/products/raspberry-pi-high-quality-camera/?resellerType=home

The camera have a CS-mount ring adapter and a C-mount, I remember the first time that I got the camera, and when playing with the 9 min and 16 min lenses I got frustrated because I don't remember that you have to use the ring adapter for the 16 min.

Check the official documentation to see how to mount the adapter ring for different lenses.


Camera Specification

Sensor:    12,3 megapixels

Output    RAW, COMP8

Back Focus:    12.5 mm - 22.4 mm

Lens Standard:    CS mount, C-mount ( C-CS adapter included )

Tripod mount:    1/4"

LENSES

A lens is a device that have a different amount of lenses inside, divergent  and convergent, and with the most cases you can modify the shutter and the focus, there are 3 main topic that you have to adapt to take in consideration before you capture a photograph:
  • Luminosity,  
  • F number, 
  • Focal distance, 

In this picture, you can see the vision angle of different lens focal distance. You can see the relation between a focal distance of the lens and the vision angle raised by the following type of lenses; the numbers of the bellow diagram represent the following lens type:

1.- Fish Eye, an extreme wide-angle lens with a strongly convex front element.
2.- Wide angle lens
a lens that reproduces perspective that generally looks "wider" than a normal lens.
3.- Normal Lense,  a lens with a focal length about equal to the diagonal size of the film or sensor format, or that reproduces perspective that generally looks "normal" to a human observer.
4.- Telean optically compressed version of the long-focus lens. 
5.- Super Tele,

16mm C-Mount Lense

This lens provides a higher quality image than the 6 mm lens but it offer a narrow angle of view which is more suited to viewing distant objects.
This lense comes with a Back focus ring to adjust with a litter screw, you have to screwed it fully once mounted in the camera, this ring is to ensure that the focus will be hard fixed while you adjust the aperture or focus manually.

  • Resolution: 10 mpx
  • Picture Format: 1"
  • Focus Distance:  17.53mm
  • Aperture:  F1.4-16
  • Mount: C Type
  • Dimensions: 39x50 mm
  • Mode: Manual
  • Minimum Macro Dist:  17.53mm
  • Weigh: 133.7 gr. 

  • Angle View:
    1" > 4.6°× 33.6°
    2/3" > 30.0°× 23.2°
    1/1.8" >  24.7°× 18.6°
    1/2" >  21.8°× 16.4°

6mm CS Mount

This lens is the low cost Wide Range version of a raspberry pi lens for the new high quality camera, it's suitable for basic photography and it offer also the possibility to do macro photography because it can focus objects at very short distance.

  • Resolution: 3 mpx
  • Picture Format: 1/2"
  • Focus Distance:  7.53mm
  • Aperture:  F1.2
  • Mount: CS Type
  • Minimum Macro Dist:  7.53mm
  • Dimensions: 30x34 mm
  • Mode: Manual
  • Weigh: 53 gr. 
  • Angle View: 63º


What we can do with this lenses ?

If you check the previous diagram, you will see that we have 2 lens for our Raspberry, 16 mm and 6 mm, if you try to category by the lens type both have to be places as a type 1, Fish Eye type but there are more factors that we have to consider.

As you can see, the 6 mm lens is a basic one for normal photograph pictures. The 16 mm is a very good one but more expensive but it's not a real Tele lense like a large one that have a focus near to 135 mm.

Even that, if you would like to learn about photography bot are very good for it, and if you try to adjust them dynamically using a python program you will have a loot of options to play with Aperture, ISO, exposure, etc... for example to take long expose pictures.

Check this offer from Waveshare where they have other lenses avairables. 

Waveshare Quality Industrial Telephoto Lens with 16mm Focal Length, Multi Field Angle and C-Mount Compatible with Raspberry Pi Camera…


  • 16mm C-Mount Telephoto Lens For Raspberry Pi High Quality Camera
  • 16mm Focal Length High Definition Telephoto Lens, Capturing Every Tiny Details
  • Fitting The Lens To Raspberry Pi High Quality Camera (C-CS Adapter Required)
  • To use with Raspberry Pi Zero, you need to buy an additional Raspberry Pi Zero v1.3 Camera Cable




Scrap Twitter member list using Tweepy in Raspberrypi

Problem: 
You need to populate date in a website and the source of this information is stored in a Twitter list, you need to read information of members in this list.

Solution:
You can do that scraping the information using Tweepy library, this library help to read information from twitter in a legal way.

First, you will need to create a Twitter app and register the corresponding tokens, etc...
Create you twitter application here: https://developer.twitter.com/en/portal/projects-and-apps 

This is a sample code to download the name of members in a specific list.

#Twitter bot to read information of members in a list
v_access_token = "**********"
v_access_token_secret = "**********"
v_consumer_key = "**********"
v_consumer_secret = "**********"

import tweepy

auth = tweepy.OAuthHandler(v_consumer_key, v_consumer_secret)
auth.set_access_token(v_access_token, v_access_token_secret)

api = tweepy.API(auth)

public_tweets = api.home_timeline()

slug = 'Twiter-List-Name'
owner = 'Twiter-Name'
list_id = 1153378345425632898

members = []

for page in tweepy.Cursor(api.list_members, list_id=list_id).items():
  members.append(page)
  print("@"+page.screen_name)

Tips for using SSH effectively on Raspberry Pi

SSH is a common secure type of remote connection to access to servers or other computers in the same or different network, it's important to remember this few tips for using SSH effectively; you will need it to connect to your Raspberry PI if you don't use a display.
Let's see a few of them:

  • The two-factor authentication for SSH.
  • Use safe agent-forwarding.
  • Exit out of SSH session.
  • leave the terminal open on disconnecting.
  • share a remote connection with friend mates.





Let's start with the first point, you will see that there are different ways to add a second factor for SSH authentication and ensure in this way your connection.


1.- Use hardware tokens (USB Key).

Around February 2020, support for Universal Second Factor tokens was added to OpenSSH connection. This is a great new feature, but there are thinks that we have to consider before to use it.

Around February 2020, support for Universal Second Factor tokens was added to OpenSSH connection. This is a great new feature, but there are thinks that we have to consider before to use it.

Since this update adds new key types to support tokens, it can only be used if you update both the client and server. The current version of the client can be checked with the command ssh -V, for the remote server it depend of the OS, check your OS information to know who to check it.

Two new types of keys were added - ecdsa - skand ed25519 - sk . To create key files, insert your token into your computer and run the command$ ssh -keygen -t ecdsa-sk -f This command will create the public and private keys associated with your token. The private key on the device is used to decrypt the private key file stored on disk.

Also, as a second factor, you can set a password for key files. OpenSSH supports another variant of key generation, a'resident' key. In this case, key files are stored on a token. This way, you will always have your keys with you. 

To transfer a key file to a new machine, insert the key media and execute the command ssh-add -k. You will need to activate your token.

More information: https://www.ssh.com/ssh/key/



2.- Safe use of agent forwarding (UNTIL HERE, DRAFT)

SSH key forwarding gives remote host access to your local SSH agent. When your SSH client uses key forwarding, there are 2 channels in the connection:

- Your interactive session.
- The key forwarding channel.

The local SSH agent creates an IPC socket that connects to the remote host through this channel. This is dangerous because a root user on the remote host has access to your local SSH agent and can potentially use it to access network resources on
your behalf. With the standard SSH agent that comes with OpenSSH, you will never know what happened. But if you use a U2F key, you will be able to stop any attempts to use your SSH agent.

Even with this limitation, periodic use of key forwarding is perfectly acceptable.

Do not use this method for all of your connections. Use it only if you are sure you need it in specific situations.


3.- Exit out of SSH session

SSH sessions often hang due to network interruptions, loss of control of a program being executed, or one of the terminal escape sequences that block keyboard input.

Here are some ways to get out of a frozen session:

Automatic logout in case of network failure.

In your SSH configuration files [.ssh/config] you need to add the next command:

ServerAliveInterval 5
ServerAliveCountMax 1

ssh will test the connection by sending echo requests to the remote host every ServerAliveIntervalseconds. If more than ServerAliveCountMaxsuch requests go unanswered, SSH will close the connection.

In this situation you will have sure about the current situation in your server to avoid that any ssh connection remains open without your control.

Terminate the session.
SSH uses a character ~ as the default escape sequence. The command closes the current connection and returns to the terminal.
(remember that you can enter escape sequences only on a new line).

The command  ~? displays a list of commands that can be used in the current session. If you have a keyboard with multiple languages, you may have to press the button ~ twice to send this character.

Leave the terminal open on disconnecting

There are two options for how to save the session when you switch between networks or want to disconnect for a while:

Use Mosh or External Terminal.

If you really need a connection that doesn't drop even if you switch between networks, use Mosh - the mobile shell. Mosh is a secure shell that uses SSH to initialize a session and then switch to its own encrypted channel. This channel is very stable. It can handle a variety of situations, including internet disconnects, changing your laptop's IP address, high network latency, and more.

To use Mosh, you need to install it on both your server and client and open ports for incoming UDP traffic on your remote host. Then just dial mosh user@server connect.

Mosh works at the level of terminal screens and keystrokes, and this gives it many advantages over SSH, which transfers a binary stream of standard I/O between the client and server. If we need to synchronize only the terminal screen and keystrokes, then the interrupted connection can then be restored much faster. SSH would have to buffer and forward whatever happened, while Mosh would only need to store the keystrokes and synchronize the last state of the terminal window with the client.

More information: https://mosh.org/ 

Use tmux.

If you want to connect and disconnect as you please and keep the same session on the remote host, use the tmux terminal. If your SSH connection drops, just connect again and type tmux attach to return to your session tmux. It has some great additional features in tabs and panels, the same as in the macOS terminal and the ability to share the terminal with friends.

Some people improve tmux with Byobu, a package that adds many handy functions and keyboard shortcuts. Byobu ships with Ubuntu and is easy to install on macOS via the Homebrew package manager.

Read more
https://phoenixnap.com/kb/tmux-tutorial-install-commands#:~:text=Tmux%20is%20a%20Linux%20application,reattach%20to%20a%20running%20process.



Share a remote connection with friends:

When solving a complex problem in servers in different task, It would be useful to share the SSH session with someone else who is in a different location.

tmux is the best terminal sharing tool for that. So, you need to do the following:

Make sure it is tmux installed on your server.

You both need to connect to the server via SSH using the same account.
One of you must run tmux for the tmux session to be created .
Another has to execute the command tmux attach.

Sourcehttps://github.com/tmux/tmux/wiki

It's done ! You have shared the terminal.



Best Raspberry Pi HATs

I fount and article talking about different hat's that you can plug into a Raspberry Pi, I'll create a new page in this blog to collect all information, sources, and utilities of using this hat's; I don't use anyone of them so I can't talk about my own experience; I'll add my feedback once I have the choose to use them. 

This is the wish list:

  • Sense Hat; Acceleratometer, Magnetometer, Air Pressure and a few more
  • Automation Hat; plug and play with diferent type of outputs.
  • Pimoroni Unicorn; Just a led matrix hub, I don't like them a lot because you have tu put very close to the raspberry.
  • Adafruit Servo; as the name tell, control your step and servo motors.
  • pHat Beat; just a little Sound board.
  • HifiBerry DAC; co convert the digital signal to analog.
  • PoE Hat; if you would like to power on your raspberry over the internte, you need this. it's in my wish list.
  • Cluster Hat;to create server cluster
  • Uptime UPS; A funny solution to use as a external battery to protect and power your raspberry if there is any cut in the main power supply.
Links to the page with more detailed information cumming soon:


Next Raspberry Pi 400 board

Picture from pimoroni

We have a new board option in the Raspberry PI family, this new Raspberry Pi 400 is a new design option that offer the user the experience to have integrated in the same keyboard a new raspberry pi 400.


 

After around a year of the last Raspberry pi P4 release, this is a good new for ones and a huge deception for others.

The board is more powerful than the old PI4, and the case is itself a user keyboard in the same "case", as the new board is not squares, all the connectors included the GPIO are available in the same side, that makes this new system more accessible than other boards.

From a point of view of headless System, what we can say, the keyboard it’s totally useless if we follow the headless installation to put the raspberry as a server, as a IOT System, as a spy cam or similar; maybe we will see new plastic cases without he keyboard for a different use, but, it seams that this board don’t have the option to plug a camera directly to the board, you will need to plug it trough USB if you need one.

This new product is more oriented for kids that would like to play with a Linux  computer, it can be more accessible that other computers and/or laptop because you only need to plug the micro SD in the raspberry, plug the HDMI to a tv and start using it. I found it quit expensive compared if you acquired for example the P4 board with an external keyboard, I miss for example an display like we can see in old ebooks and the touch pad to avoid the use of the mouse; if this is a product for help kid to learn about computers, programming, etc… 

You can check it from Pimoroni web site:
https://shop.pimoroni.com/products/raspberry-pi-400-personal-computer-kit?variant=32280730206291
 

Why not to invest in a small and cheapest PI4 rather than more different boards ?

Xmas season, new staff to make gif to our geek friends.


Using scrapy in raspberry pi with python

Problem:

You need to extract some information from a public website an process it to do "thinks"

Solution:

One solution is to use a web scrap tool, to let you know who it works, lets see a simple example to extract metada from  single url in a single program.

1.- Install scrappy and resolve any dependence issue, maybe you need to upload crypto library [sudo pip3 install cryptography==2.8]

sudo pip3 install scrapy

2.- Check scrappy release

scrapy version

3.- Use the next code in file "myscrapbot.py"

#get links from a single page without jump
import scrapy
class QuotesSpider(scrapy.Spider):
    name = "quotes"
    start_urls = [
        'https://raspberrypihack.blogspot.com/',
    ]
    def parse(self, response):
            yield {
                'enlaces': response.xpath('//a/@href').extract(),
            }

4.- Run the program using "scrapy"

scrapy runspider myscrapbot.py -o export.json
4.- The data selected to be scraped will be saved in file export.json

5.- How to extract data from a data base table urls

First, you have to checl the disscussion in this post.

Then you have to addapt the code using this post recomendation from the first answers.

import scrapy

import mysql.connector

class ProductsSpider(scrapy.Spider):
    name = "Products"
    start_urls = []
    def parse(self, response):
 
      print(self.start_urls)
         yield {
              'enlaces': response.xpath('//a/@href').extract(),
        }
    def start_requests(self):
        conn = mysql.connector.connect(
                user='user',
                passwd='_a30e4qK',
                db='DDBB',
                host='localhost',
                charset="utf8",

                use_unicode=True
                )
        cursor = conn.cursor()
        cursor.execute('SELECT links FROM CUSTOMERS;')
        rows = cursor.fetchall()
        for row in rows:
           ProductsSpider.start_urls.append(row[0])
           yield self.make_requests_from_url(row[0])
        conn.close()




X.- How to extract href data?

response.xpath('//a/@href').extract()
response.xpath('//a/text()').extract()
more will continue soon...


Source:

Connect to MySQL using Python

Problem:

You need to connect to a external MySQL database and do thing...

Solution:

1.- Install mysql drivers

sudo python3 -m pip install mysql-connector-python

2.- Create a new program using the next code:
import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="yourusername",
  password="yourpassword",
  database="mydatabase"
)

mycursor = mydb.cursor()
mycursor.execute("SELECT * FROM customers")
myresult = mycursor.fetchall()
for x in myresult:
  print(x)

Sources:

3.- Do thinks... 

https://www.w3schools.com/python/python_mysql_getstarted.asp

Raspberry Pi 4 release 1.1 or 1.2 ? what is the diference ?

Problem:

You need to know the correct release of your Raspberry Pi but it's not possible to identify it in the board, neither the storage box neither the product technical documentation.

Solution:

Run the next command:

cat /proc/cpuinfo

Check the information in the source link to identify the release of your model using the map table in the official Raspberry Pi website

Source:

https://www.raspberrypi.org/documentation/hardware/raspberrypi/revision-codes/README.md

 

How to connect a OLED display with a Raspberry Pi

Problem:

You would like to use a OLED display with a I2C interface with a Raspberry pi using a SH1106 controller , 

Solution:

That type of OLE Display have 4 connections, just follow the next wire schema to connect it to the Raspberry.

Connect the wires directly between the oled display and the Raspberry:

OLED pins >> Raspberry Pi

VDD >> 5V

GND >> GND

SCK >> SCL  ( more information )

SDA >> SDA  ( more information)

*Check the panel in your OLED display to ensure the correct pin definition in the oled panel 

Setup the software:

Activate the I2C Interface option in system configuration, use the command raspi-config and go to interface settings, there you will fine the I2C option.

Install libraries:

Just use the following command: 

sudo apt-get install -y python-smbus 
sudo apt-get install -y i2c-tools
sudo apt-get reinstall build-essential
sudo apt-get install i2c-tools
$ sudo apt-get update
$ sudo apt-get install python3 python3-pip python3-pil libjpeg-dev zlib1g-dev libfreetype6-dev liblcms2-dev libopenjp2-7 libtiff5 -y
$ sudo -H pip3 install luma.oled

Check if the system detect it:

You can check it if you find signal in this interface map, use the
command: sudo i2cdetect -y 1

If it works, you will see something like this:



Working with the Oled and Python:
1.- Clonning the exmaple repository:
$ git clone https://github.com/rm-hull/luma.examples.git
$ cd luma.examples

2.- install libraries
sudo -H pip install -e .

3.- run examples
cd examples
sudo python3 3d_box.py

Hardware:

You are in emergency mode. After logging in,

Problem:

you make a change in the fstab file and after reboot the systm you can see that your raspberry pi don't boot and show the next message:

"You are in emergency mode. After logging in, type "journalctl -xb" to view system logs, "systemctl reboot" to reboot, "systemctl default" or ^D to try again to boot into default mode.
Cannot open acess to console, the root account is locked.
See sulogin(8) man page for more details.
Press Enter to continue"


but, you can't do nothing to skip this message.

Solution:

the solution is to edit fstab system file and remove the last change or remove all and leave it with default values, follow the instruction that Talha Sariyürek explain in this blog post:

https://talhasariyuerek.com/en/linux-raspberry-pi-3b3b4-emergency-mode-root-account-locked-solution/

Control RGB Led Strip using Mosfet

Problem:
You are interested in controlling a led RGB strip with a raspberry but it's not clear how to setup Mosfet because there are people that use 1 resistor, 2 resistors, others without resistors, the scheme is quite different, etc... 


Solution:
If you would like to use Mosfet as a switch to control a RGB led strip,it is recommended to use resistors in the gate port, one to the source and other to pump.
I use and old P30N06LE and it works well, as I can see everyone is using the IRLZ34N, it's important to read the mosfet datasheet Gate to Source Voltage value in the mosfet to ensure the volts needed to activation.

Circuit diagram with 2 resistors

Connection map with 1 resistor in Mosfet gate

Sources:

With 1 resistor:
http://www.knight-of-pi.org/color-mixer-control-a-rgb-led-strip-with-the-raspberry-pi-and-the-n-channel-mosfet-irlb8721/ 

Note:
Take in consideration that using a led strip, it's required to power them with a 12v power supply, if you put any wire in the wrong position,  your raspberry pi will die like mine :-) 


coding python for GPIO

Problem:
How to control electronic component connected to the GIO board ? 

Solution:
Follow the instruction of the electronic component, for exmaple led light need to be connected in serial with a resitant, active buzzers can be connected directed between the gpio output and the gnd pin.
once you have connected the electronic component, follow the next code structure to control it:

import RPi.GPIO as GPIO
import time

v_compo = 10

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(v_compo, GPIO.OUT)

print("LED on")
GPIO.output(v_compo, 1)
time.sleep(v_sleep)

print("LED of")
GPIO.output(v_compo, 0)
time.sleep(v_sleep)


No module named RPi

Problem:
when you try to run the program, you face with error message "No module named 'RPi'"

Solution:
Install missing libraries following the next steps:
sudo apt-get install python-pip
sudo apt-get install python3-rpi.gpio   (on Python 3)
sudo 
pip freeze | grep RPi
sudo apt-get install picap

Source:
https://raspberrypi.stackexchange.com/questions/60774/importerror-no-module-named-rpi

Clone github repository with raspberry pi

Problem:
You are iterested to use a custom development from a github repository in your raspberry pi, after this clone process you can sync your raspberry with the new development.

Solution:

First, install GIT on raspbian:
sudo apt update
sudo apt install git
git --version
Follo the next steps:
1.- git clone https://github.com/raspberrypi/userland.git
2.- cd userland/host_applications/linux/apps/raspicam/
3.- time userland/build/bin/RaspiStill --exposure verylong --shutter 140
00000 -t 16000 -o test.jpg

Examble:
This git is to change functionality of raspberry pi camera, in this case you can get more exposure time.

Run python program on start-up

Problem:
You need to run a python program or report once raspberry pi start-up.

Solution:
You can do this editing you rc-local file, you add the next entry and pay especial attention on bash method to ensure that you run the program with the specific software version.


sudo bash -c '/usr/bin/python3 /home/pi/progra.py > /home/pi/log.txt 2>&1' &
Light  Explanation:
sudo allow you to run the program with admin privilege, 
bash allow to specify the software version pf pyhton
> create a file with the result of the program execution


Remember that the rc.local fine need to end with a exit 0 in the final line

pink colored images on Raspberry PI Noir V2 camera

Problem:
You detect that the Pi Noir V2 camera take pictures in a pink color mask for day pictures.

Solution:
It's fixed after execute the next command:
sudo vcdbg set awb_mode 0 

Source:
https://github.com/raspberrypi/firmware/issues/1167


wifi headless setup

Problem:
you need to setup the wifi connection and you don't have a display to do it trough graphic interface.

Solution:
You can plug the microSD card into a computer,a new Disk Unit will be available, there you can create a new file (wpa_supplicant.conf) for the wifi configuration file with the details example as you can see later in this same post.
If you have access to the command line ( you have a display ), you can do it using the command line tool once you are connected using ssh, just open the file wpa_supplicant.conf using the following commands:
# manual edition of the filesudo nano
  sudo nano /etc/wpa_supplicant/wpa_supplicant.conf
# launch the config setup tool
 sudo raspi-config


Just edit the file with the correct SSID and password in the file that have to lloks like:
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=<Insert 2 letter ISO 3166-1 country code here>
network={
 ssid="<Name of your wireless LAN>"
 psk="<Password for your wireless LAN>"
}

Source:
https://www.raspberrypi.org/documentation/configuration/wireless/headless.md

Hardware:
For old raspberry PI, you will need an wifi USB key like this:
TP-Link TL-WN725N Adaptador WiFi USB inalámbrico Nano, Compatible con Raspberry Pi, N 150 Mbps, Botón WPS, AP soft Windows10/8.1/8/7/XP, Mac OS X 10.7-10.11, Linux, negro

list file content into text file with pyrhon

problem:
You need to add into a text file the name of all files in a folder, usually you can use the following command sudo ls *.jpg > stills.txt but you get an error:
"unable to execute /bin/ls: Argument list too long"
this is because the sentence is too long because linux can not hold all content in memory if the folder have an huge amount of data.

solution:
You can use python as a workarund, use the following code to create a text file fith the name of all files in a folder or directory.

import os
from os import listdir
from os.path import isfile, join
#folder that hold the files, in my case 70.000,00
dirName = '/media/USB128/SHARED/RingBell/pictures/'
#crete and array
fileNames=[f for f in listdir(dirName) if isfile(join(dirName, f))]
#empty the file is it was previouly filled
open('file.txt', 'w').close()
#loop and write line by ñine each filename
for file in os.listdir(dirName):
f = open("./stills.txt", "a+")
f.write("/media/USB128/SHARED/RingBell/pictures/"+file+"\n")
f.flush() 
f.seek(0)
f.close()

Change Time Zone on Raspberry Pi

Problem:
Beside you select the time zone correctly ( most of it ) trough command raspi-config, you detect that the system don't use the correct time, in some countries there is a gap in the time zone calculator of it can be possible to use diferents time zones in the same country.

Solution:
it's as simple as launch the next command and follow the instructions:
sudo dpkg-reconfigure tzdata

Source:

Remote access trought SSH

Problem:
You need to access to your local server from outside of local network using SSH from the command line.

Solution:
You need to map a port in your firewall to allow the firewall pass the remote access connectin from ou public IP access to your local server, for that search port forwarding configuration and map there you raspberry local IP, now you can create an external connection  to your raspberry if you user your public IP and the selected port.

Use the following steps to jump between any raspberry system:

1.- simple remote access connection using the same user.

sudo ssh hostname/ipaddress

2.- change the user

sudo ssh user@hostname

3.- close the session

sudo exit


Mount NFS Client

Problem:
You need to access to a Shared folder from other server from your raspberry PI, this folder is shared troncut the network using a NFS Server

Solution:
Follow the next commands to setup a NFS client in your Raspberry:


1.- Install the required software


sudo apt-get install nfs-common -y

2.- Create the local folder to associate the link between the current system and the remote folder


sudo mkdir -p /media/MyUSB/

3.- Add permission to NFS folder, change owner and group


sudo chown -R pi:pi /media/MyUSB/


4.- mount the folder

sudo mount serveripaddress:/remotefolder /media/MyUSB/
use "df" command to see if the folder is correctly mounted

5.- setup fstab

sudo nano /etc/fstab

add the next line to fstab file:

remoteserveripaddress:/remote/folder/   /media/MyUSB   nfs    rw  0  0
done, now reboot the system and then you will be able to see the shared content in the mounted drive /media/MyUSB

Notes:
- If you are able to see you NFS folder from other computer but you can't write to it, check that all users have the same ID and SGID than the owner for the shared folder in the server
- If you need to make any change, you can use sudo umount /dev/xxx  to unmount the unit and mount it again with the command sudo mount -a
- to list all mount units in the computer use the command df





Mount USB disk

Problem:
You need to use a USB disk with your Raspberry PI.

Solution:
Raspberry PI OS will recognize automatically your USB disk but you need to follow the next configuration steps to use it correctly.

1.- Check your current OS partitions.
sudo fdisk -l
2.- Create a shared forlder.
 sudo mkdir /media/MyUSB
 sudo chmod 777 /media/MyUSB

2.- Setup fstab file, this file store your mount configuration
sudo nano /etc/fstab
 Add one line to configure your USB disk, somethink like:
/dev/stda       /media/MyUSB    vfat    rw  0   0
/dev/stda    path to the USB sick
/media/MyUSB mount path where files are stored
vfat         FAT32 format



3.- Confirm an apply changes.
sudo mount -a
4.- Check current partition.
sudo df
Additional info:
To Remove the disk in save mode:
umount /dev/stda
Notes:
- Remember to change shared folder authorizations to 777, if not you will not be able to share it trough NFS or similar. 

Hardware:
In my test I use a 128 USB flash disk  from Philips and it works really well to manage file between the raspberry system. 

Remember that this will work not only with flash disk, also with external  USB hard disks.