Showing posts with label raspberry pi. Show all posts
Showing posts with label raspberry pi. Show all posts

Friday, 17 February 2017

Raspberry Pi problem with default gateway being lost when rebooting.

On my raspberry pi, I was having a problem where the default gateway was being lost every time that I rebooted it. I was able to temporarily fix the problem by using the following command:

sudo route add default gw 192.168.100.1

However when power was lost or I rebooted then I would be back to having network connection problems, which is an issue when trying to automate something.

The solution that I found was to edit the /etc/dhcpcd.conf file using the following command:

sudo nano /etc/dhcpcd.conf

Then at the bottom of the file I added the following line:

static routers=192.168.100.1

Note that if you have the same problem, your IP address might need to be different. If you enter the command ifconfig, you should get an output similar to the following:



You need to look at the section for the network you are expecting to connect to. wlan0 is provided from the built in wifi hardware which comes on the newer raspberry pis. eth0 is the ethernet connection (wired). lo is the localhost which won't be the one that you need. I was connecting with eth0, so I looked under this section at the inet addr parameter, and I tool this number and changed the last digit to a 1, which is how I got my default gateway.


The file /etc/network/interfaces did not seem to make a difference to anything. Now the only command specifically for the eth0 connection is the following:

iface eth0 inet dhcp

There are a couple of other things that I had tried with no luck.

I tried writing a shell script to add the default gateway, and then tried to use crontab to run this at reboot using the @reboot command. This did not work for some reason. Running the shell script alone was fine but it was not possible for crontab to do this. I also tried adding in a 60 second pause in the crontab before the script was ran, but this also did not work.

I also tried making modifications to the /etc/network/interfaces file, which also did not help.

I tried the following commands together as a pair:

post-up route add default gw 192.168.100.1 eth0
pre-down route del default gw 192.168.100.1 eth0

This did nothing of course. Neither did adding a line with the gateway parameter.

gateway 192.168.100.1

So all in all I'm not sure exactly where the problem came from, just I now know the /etc/network/interfaces file is the wrong place to try and fix this, and instead it needs to be changed in /etc/dhcpcd.conf

Wednesday, 22 October 2014

Python Find Serial Ports - Windows and Raspberry Pi

I started with code taken from stack overflow at the following page: http://stackoverflow.com/questions/12090503/listing-available-com-ports-with-python with the code posted by user "Thomas" (code has since been modified). I tested the code on Windows 7 and a Raspberry Pi. The original 'unix' code did not work for me, so I found a method that did.
import sys
import glob
import serial
def ScanPorts():
    """
    Returns a generator for all available serial ports
    """
    if os.name == 'nt':
        # windows
        for i in range(256):
            try:
                s = serial.Serial(i)
                s.close()
                yield 'COM' + str(i + 1)
            except serial.SerialException:
                pass
    else:
        # unix
        ports = glob.glob('/dev/tty[A-Za-z]*')
       
        for port in ports:
            yield port
To run this on the Raspberry Pi, first the python serial package must be installed by running:
sudo apt-get install python-serial
Also the software package "Device Monitoring Studio" found online here: http://freeserialanalyzer.com/ was useful for peeking into what messages were being sent and received from another program. The free version has a 22 minute time limit.