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)