Pages

Wednesday, December 28, 2016

Rasberry Pi - Tutorials - S7-1200 & Snap7 Python - Controlling the PLC Using Rpi GPIO


Long awaited video on how to use the Rpi GPIO to control aspects of the PLC.

Intro to controlling Raspberry Pi's GPIO using python:

Raspberry Pi - Tutorials - GPIOZero - Controlling LEDS Using Rpi GPIO



Using the snap7zero and gpiozero to control the S7-1200 PLC with the Raspberry Pi:

Rasberry Pi - Tutorials - S7-1200 & Snap7 Python - Controlling the PLC Using Rpi GPIO

Wednesday, May 11, 2016

snap7 reconnecting code snippet

In some cases your pi may lose connection with your PLC.. here's some helpful code to get it reconnected (I whipped this up pretty quick, and it's currently untested)
def connect(plc,ip):
    while True:
        #check connection
        if plc.get_connected():
            break
        try:
            #attempt connection
            plc.connect(ip,0,0)
        except:
            pass
        sleep(5)
        
plc = snap7.client.Client()
connect(plc,'10.10.55.109')
while True:    
    try:
        #do stuff 
                
    except Snap7Exception as e:
        connect(PLC)
        # break
connect(plc,ip) method puts your pi in an infinite loop till it is connected. make sure you wrap your methods with a try,except.

Monday, February 29, 2016

Raspberry Pi - Python Snap7 - Mapping and Reading Datablocks


import snap7.client
from snap7.snap7types import *
from snap7.util import *


class DBObject(object):
    pass


offsets = { "Bool":2,"Int": 2,"Real":4,"DInt":6,"String":256}

db=\
"""
Temperature Real 0.0
Cold Bool 4.0
RPis_to_Buy Int 6.0
Db_test_String String 8.0
"""

def DBRead(plc,db_num,length,dbitems):
    data = plc.read_area(areas['DB'],db_num,0,length)
    obj = DBObject()
    for item in dbitems:
        value = None
        offset = int(item['bytebit'].split('.')[0])

        if item['datatype']=='Real':
            value = get_real(data,offset)

        if item['datatype']=='Bool':
            bit =int(item['bytebit'].split('.')[1])
            value = get_bool(data,offset,bit)

        if item['datatype']=='Int':
            value = get_int(data, offset)

        if item['datatype']=='String':
            value = get_string(data, offset)

        obj.__setattr__(item['name'], value)

    return obj

def get_db_size(array,bytekey,datatypekey):
    seq,length = [x[bytekey] for x in array],[x[datatypekey] for x in array]
    idx = seq.index(max(seq))
    lastByte = int(max(seq).split('.')[0])+(offsets[length[idx]])
    return lastByte


if __name__ == "__main__":
    plc = snap7.client.Client()
    plc.connect('10.10.55.109',0,0)
    itemlist = filter(lambda a: a!='',db.split('\n'))
    deliminator='\t'
    items = [
        {
            "name":x.split(deliminator)[0],
            "datatype":x.split(deliminator)[1],
            "bytebit":x.split(deliminator)[2]
         } for x in itemlist
    ]
    #get length of datablock
    length = get_db_size(items,'bytebit','datatype')
    meh = DBRead(plc,10,length,items)
    print """
    Cold:\t\t\t{}
    Tempeature:\t\t{}
    RPis_to_Buy:\t{}
    Db_test_String:\t{}
    """.format(meh.Cold,meh.Temperature,meh.RPis_to_Buy,meh.Db_test_String)
    plc.disconnect();

Wednesday, February 3, 2016

Python Snap7 S7-1200 Simple Reading/Writing Memory Example



import snap7.client as c
from snap7.util import *
from snap7.snap7types import *

def ReadMemory(plc,byte,bit,datatype):
    result = plc.read_area(areas['MK'],0,byte,datatype)
    if datatype==S7WLBit:
        return get_bool(result,0,bit)
    elif datatype==S7WLByte or datatype==S7WLWord:
        return get_int(result,0)
    elif datatype==S7WLReal:
        return get_real(result,0)
    elif datatype==S7WLDWord:
        return get_dword(result,0)
    else:
        return None

def WriteMemory(plc,byte,bit,datatype,value):
    result = plc.read_area(areas['MK'],0,byte,datatype)
    if datatype==S7WLBit:
        set_bool(result,0,bit,value)
    elif datatype==S7WLByte or datatype==S7WLWord:
        set_int(result,0,value)
    elif datatype==S7WLReal:
        set_real(result,0,value)
    elif datatype==S7WLDWord:
        set_dword(result,0,value)
    plc.write_area(areas["MK"],0,byte,result)

if __name__=="__main__":
    plc = c.Client()
    plc.connect('10.10.54.2',0,1)
    print ReadMemory(plc,420,0,S7WLReal)
    WriteMemory(plc,420,0,S7WLReal,3.141592)
    print ReadMemory(plc,420,0,S7WLReal)

    #DONE!!

Tuesday, February 2, 2016

25+ Raspberry Compatible 1-Wire Host Boards/Hats

(sortable)

Image Name / Link Channels (Buses) Extras Price Price per channel
1-Wire Driver
DS2482-800
8 ch. --- £21.05 £2.63 / ch
1-Wire Interface 
Module
1 ch. --- £5.93 £5.93 / ch
R-Pi i2c 1-Wire OWFS Expansion Module 1 ch. --- £17.99 £17.99 / ch
I2C counter /
1-wire board
8 ch. 6x 16 bit Counter inputs £69.99 £8.75 / ch
Com Pi 1 ch. RS232 Port £12.99 £12.99 / ch
1 Wire Pi Plus 1 ch. --- £12 £12 / ch
RP1 1-Wire GPIO Host Adapter 1 ch. --- £7.95 £7.95 / ch
RPI2 1-Wire Host Adapter 1 ch. --- £14 £13.95 / ch
RPI3 v1 1-Wire Host Adapter 8 ch. RTC £33 £4.12 / ch
RPI3 v2 1-Wire Host Adapter 8 ch. RTC £33 £4.12 / ch
AbioWire+ 17 ch. --- £42 £5.25 / ch
1-Wire Mini Host 8 ch. --- £15.26 £1.90 / ch
1-Wire Automation System 8 ch. expansion slot £114 £14.25 / ch
Raspberry Pi -9 Channel 1-Wire Board 9 ch. RTC £33 £3.66 / ch
IoT-1Wire-1 module Board 8 ch. --- £34 £4.25 / ch
IoT-1Wire-1 module Board 1 ch. (5 ports) --- £24 £24 / ch
4 USB Hub & I2C AD/DA - 1-Wire   Board 1 ch. RTC, 10bit ADC, 8bit DAC, 4 USB Hub £52 £52 / ch
7 USB Hub & I2C AD/DA - 1-Wire   Board 1 ch. 10bit ADC / 8bit DAC, 7 USB Hub £58 £58 / ch
Step Motor & I2C 1-Wire Board 1 ch. 8 ch output / step motor output  £21 £21 / ch
One-Wire 17 Channels 17 ch. --- £46.99 £2.76 / ch
USB to One Wire converter 1 ch. --- $24.60 $24.60 / ch
S4: USB / 1-Wire Interface Card 1 ch. --- €39 €39 / ch
USB to 1-Wire RJ11 1 ch. --- £26 £26 / ch
I2C 1 Wire Click 8 ch. --- £19.45 £2.43 / ch
UART to 1 Wire Adapter (Retired)  1 ch. --- --- ---
UniPi Expansion Board 1 ch. Relays, Inputs, Outputs, Analog In/Out 0-10v $134 $134 / ch
PIGATOR Onewire 1 ch.  RTC, uplink header for expansion £60 £60 / ch






Tuesday, January 5, 2016

Google Trends - Which Country Loves the Raspberry Pi the Most?

I often visit google.com/trends and decided to do a query on "Raspberry Pi" and here were the results:

Web Search

Image Search


Video Search



News Search


Google Shopping Search

Monday, January 4, 2016

10+ Things the Raspberry Pi Can Do As a Cheaper Replacement in Industrial Automation






The Raspberry Pi has many Industrial Control Applications and many ways to interface with current Industrial Protocols. 

I am starting a  Raspberry Pi Industrialized Google+ Community to gather people smarter than me to answer questions,give tutorials on Raspberry Pi and PLC interfaces, show present and upcoming raspberry pi hardware, and to show off their cool industrial projects using the Raspberry Pi. 

Since I'm a python fan here's a list of Ethernet/Serial Based Protocols I've found:
  1. ModbusTCP/RTU using pymodbus 
  2. *Profinet (a GitHub link
  3. S7 Protocol using snap7 python (tutorial here)
    • (Siemen's S7-300,S7-1500 / S7-200,S7-1200)
  4. Koyo ECOM Protocol, (see my github, and here for example usage)
    • DL05, DL06 PLCs
  5. *EtherNet/IP (Link)
* I've not tested these libraries yet.

Here are the ways the Raspberry Pi can replace or enhance your Industrial Automation Process:

  • Bridging Protocols.  Normally a protocol converter/bridge can cost $100+

    • Modbus RTU <--> Modbus TCP,Siemens S7, Koyo ECOM, EtherNet/IP
    • Modbus TCP <--> Siemens S7, Koyo ECOM, EtherNet/IP
    • Any Other Serial ASCII Device <--> Modbus TCP, Siemens S7, Koyo ECOM, EtherNet/IP
  • Cheap HMI Screen Replacement.  HMI can cost $1000s and since the Raspberry Pi can integrate with several protocols it could be a decent replacement to save some $$$, £££, or € depending on where you live. Of course it may take some knowledge on either HTM5 with websockets or using QT as your front ends. 

  • Remote Relay I/O.


  • Remote Analog I/O


  • Custom Raspberry Pi.  Lastly I am going to mention the customizable Raspberry Pi.  Yes, Customizable!  Element14 can customize the board to fit your process a little better.  Adding Wifi, more GPIO, onboard Flash memory, and others.

Saturday, January 2, 2016

Raspberry Pi - SCADA - Another Video Tutorial Snap7 Python for S7-1200 PLC

Another Simple snap7 Python Tutorial



Covers creating a simple example on turning on and off a PLC output.

 import snap7.client as c
 from snap7.util import *
 from time import sleep


 def WriteOutput(dev,bytebit,cmd):
     byte,bit = bytebit.split('.')
     byte,bit = int(byte),int(bit)
     data = dev.read_area(0x82,0,byte,1)
     set_bool(data,byte,bit,cmd)
     dev.write_area(0x82,byte,data)

 def main():
     myplc = snap7.client.Client()
     myplc.connect('10.10.54.2',0,1)
     for x in range(10):
         WriteOutput(myplc,'0.0',x%2==0) # turns true every other iteration
         sleep(1)

 if __name__ == "__main__":
     main()