python2 > 3, network drives, sql


I finally have some 'free' time :) so i upgraded a few things on the pi server but not without issues…

From jessie to stretch - network drive

For some reason the old command for mounting a network drive on the pi which worked on jessie did not work under stretch. I kept getting a host is down fault yet it was clearly not. The line i had in fstab before was;

//192.168.1.1/Drive /media/nas_documents cifs credentials=/home/drakx/.nas_credentials,sec=ntlmv2,uid=1000,gid=1000,iocharset=utf8 0 0

trying combinations of this and even removing the credentials requirement still failed to mount. Eventually after much searching it appears that it is to do with nltm versions, the following worked;

sudo mount --verbose -t cifs -o sec=ntlm,username=****,password=****,vers=1.0 //192.168.1.1/location /mnt/nas_files

but honestly i dont understand it and need to go away and investigate but for now the drive is mapped and i've other things to look at.

dragelec to python3

Dragelec is still alive and going strong, i've now added code to have all data put into an sql database and converted to python3 which had its own peculiarities…

sql code to create the database

    sqlite_file = '/mnt/usbkey/home_mon_db.sqlite'
    myfile = Path(sqlite_file)
    if myfile.is_file():
        # file exists
        print('file exists')
    else:
        tn = 'home_mon'
        # Connecting to the database file
        conn = sqlite3.connect(sqlite_file)
        c = conn.cursor()
        # create table
        c.execute('CREATE TABLE {tn} ({nf} {ft} PRIMARY KEY AUTOINCREMENT)'.format(tn='home_mon', nf='IDX', ft='INTEGER') )
        c.execute("ALTER TABLE {tn} ADD COLUMN '{cn}' {ct}".format(tn='home_mon', cn="DATE", ct='DATE'))
        c.execute("ALTER TABLE {tn} ADD COLUMN '{cn}' {ct}".format(tn='home_mon', cn="TIME", ct='TIME'))
        c.execute("ALTER TABLE {tn} ADD COLUMN '{cn}' {ct}".format(tn='home_mon', cn="EXTERNAL", ct='REAL'))
        c.execute("ALTER TABLE {tn} ADD COLUMN '{cn}' {ct}".format(tn='home_mon', cn="FRONTROOM", ct='REAL'))
        c.execute("ALTER TABLE {tn} ADD COLUMN '{cn}' {ct}".format(tn='home_mon', cn="BEDROOM", ct='REAL'))
        c.execute("ALTER TABLE {tn} ADD COLUMN '{cn}' {ct}".format(tn='home_mon', cn="KITCHEN", ct='REAL'))

sql code to write data in

        conn = sqlite3.connect(sqlite_file)
        c = conn.cursor()
        c.execute("INSERT INTO {tn}(DATE,TIME,EXTERNAL,FRONTROOM,BEDROOM,KITCHEN) VALUES(?,?,?,?,?,?)".format(tn='home_mon'), (d_now, t_now, sensordict.get('AC'), sensordict.get('AB'), sensordict.get('AE'), sensordict.get('AD')))
        conn.commit()
        conn.close()

python3 decode 'b'

a change that caught me out was the change in how python handles incoming strings when read from the serial port previously i could simply read the incoming and assign direct to a dictionary or variable as python2 ignored the preceeding letter ie the received string looked like;

b'aAETMPA12.45'

reading the python documentation;

A prefix of 'b' or 'B' is ignored in Python 2; it indicates that the literal should become a bytes literal in Python 3 (e.g. when code is automatically converted with 2to3). A 'u' or 'b' prefix may be followed by an 'r' prefix.

So python2 ignored it but under python3 i need to decode it hence;

llapMsg= ser.read(n)
sensordict['msg']=(llapMsg.decode('utf8'))

So dragelec is back up and running with a new sql backend and under python3 next jobs are to improve the decode routine to handle false inputs and of course extract data from the backend for display on the server site.


See also