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")