Saturday, April 25, 2015

Raspberry Pi SCADA: Communitating with S7-200 Using Python

Video Demo

For how to compiling snap7 on the pi click here

Since the S7-200 is a bit different then the newer S7-1500/S7-1200 PLCs it took a bit of work to get it working. The python-snap7 library was missing the connection method for the old TSAP method of connecting. So I forked the library and added the missing function to connect. I did a pull request to get it merged back with the original and it's pending currently. Here is the link to the forked library:
https://github.com/SimplyAutomationized/python-snap7

I've also been working on a din rail enclosure for the pi that wasn't so large.   I'm going to be adding a 24vdc to 5vdc dc-dc converter to the enclosure it so I don't have to bring in usb to it.





Anyway back to the good stuff. I added the Cli_Connection in the Client.py library so the code will now be as follows:

import snap7 as p

plc = p.client.Client()
plc.set_connection_params('10.10.55.250',11,11)
plc.connect()
print plc.get_connected()
if(plc.get_connected()):
    plc.disconnect()
If you import my helper S7200.py library from github the code is as follows:
import S7200 as p
plc = p.S7_200('10.10.55.250',0x1100,0x1100,debug=True)
plc.writeMem('QX0.0',True)
print plc.getMem('freal10')
plc.writeMem('freal10',3.141592653589)
print plc.getMem('freal10')
print plc.getMem('QX0.0')


  • This will turn on output 1 (Q0.0, will turn on if there's no ladder associated with it). 
  • Then read the real number stored in V memory 10. 
  • Then write pi to it ;-). 
  • Then read from it again.
  • Then read if output 1 (Q0.0) is on still\
Github for helper library found here:
https://github.com/SimplyAutomationized/raspberrypi/blob/master/S7-200pi/S7200.py



Friday, April 17, 2015

Raspberry Pi Digital Picture Frame: Part 2 Adding an Accelerometer and some more!

UPDATE (4-17-2015): 

To see "Part 1" of this project please go here
So we have few new features!!

  • Ability to hide/show clock and temperature via phone webapp 
  • Change time between image transitions via webapp
  • Uploading multiple images from the web interface now works.
  • Deleting Images now works.
  • Lazy loading of images when looking through them on the webapp. (loads as you scroll)
  •  I just added a 3-axis sensor to the 7" frame and have it broadcast to the HTML side the angle of rotation.

TODO(Coming Soon)

  • Weather feed option in the config. (see weather every 10 slides, and or outside temperature in right hand corner)
  • DNLA Support for those who have NAS servers with millions of images.
  • Option to only show images of the same aspect ratio as your picture frame rotation.(if you frame is vertical show vertical taken images and visa vera)
  • piframe image for download so no setup is required (except hardware)

Details on the new webapp features

To make it more handy to have a IoT picture frame I've added some handy configurations that load when the frame starts up. 



Details on the Acceleromter

I updated the git repository with a class to communicate via i2c to a MMA7455 accelerometer.
https://github.com/SimplyAutomationized/piframe/blob/master/MMA7455.py
The class has a callback method within it so it can broadcast a tilt change to whomever instantiates the class. Which I'm calling it within the websocket code and broadcasting to the client(the frame) the correct angle.

#I check the config file to see if you have enabled the tilt sensor
if(config['tiltsensor']):
 self.tiltSensor=tilt.TiltSensor(self.rotateFrame)
 self.tiltSensor.start()
#...later in the code we define the callback rotateFrame when rotation changes.
#(when the X axis inverts)
def rotateFrame(self,data):
 rotation=data*270
 self.frameconfig.changeKey('rotation',rotation)
 self.sendNewConfig(self,all=True)
#the code then broadcasts it's new rotation, the CSS on the frame then rotate's the images based off of the new rotation :-) win!




And rotation:

Monday, April 6, 2015

Raspberry Pi getting data from a Koyo DL-06 using python


It was an interesting process to sniff and break apart the ECOM protocol. But with the help of Wireshark I was able to create a simple class to read/write Inputs,Outputs,V Memory, and C Memory Bits.  My class may look a bit hokey to those more skilled at python than I, but I've found it very usefull.

If you're stuck with an old generation H0-ECOM ethernet module and want to make use of it this post may be of use to you.  I was able to get a few from work as we phased in new gear.  We only had a couple ECOM-100 modules (ModbusTCP). So I was driven to get these working with my Raspberry Pi.

With mine I made a raspberry controlled sprinkler system using the Koyo DL-06 as my remote I/O.  There was no logic programmed into the PLC because I wanted my pi to control it 100%. But with this python code anyone could make a raspberry pi powered cheap HMI or web based HMI for their system.

Link to PythonKoyo library:
https://github.com/SimplyAutomationized/PythonKoyo/blob/master/Koyo.py

Example code:

import Koyo as plc

myKoyo = plc.Koyo('192.168.1.26')
myKoyo.WriteOutput(0,1) # write output Y0 to true, (if this isn't tied to any rung in the ladder it should turn on)
print myKoyo.ReadInput(5)
print myKoyo.ReadC(16) #this currently only returns data of the first 4 bytes of C Memory
if(myKoyo.ReadOutput(5)):
   myKoyo.WriteC(16,false)
else:
   myKoyo.WriteC(16,true)

#other helpful commands:
myKoyo.FindKoyo()          #return a list (ip,mac) of koyo plcs on the network
myKoyo.ChangeIP(mac,newIP) #change an ip address of a koyo that's plugged into your subnet

Hope this helps anyone with a Koyo. Other options are trying to install the C Library AutomationDirect provides on the website. I was unable to compile it and found this much more enjoyable.