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.

Saturday, April 4, 2015

Raspberry Pi Digital Picture Frame (Picture frame, websocket controlled, and more)

To see Part 2 of this project click here

Inside the Pi Frame

In my search (hours upon hours upon days..) through the many picture frame pi tutorials I didn't quite find one that was fine tuned for what I wanted.  I wanted to control it with my phone (upload, change , remove, and modify images).  I tried the following (Pros/Cons):
  •  FBI, Frame Buffer Imageviewer. 
    • Pro: easy to setup and had slideshow modes.
    • Cons:
      • No layering if you wanted to put a clock or temperature on the top.
      • Took lots of ram to buffer all the images up.
      • No web interface.
  • Pygame:
    • Pros:
      • Python based, and I like python, which could give you a web interface. 
      • Can do layering (add a clock layer.
      • Interface with web
    • Cons:
      • Slow
      • Not great for animations
  • HTML5 Web page Kiosk with Slideshow frontend and websocket server backend.
    • Pros:
      • Python websocket API for connecting Phone Apps and web apps
      • Quick animations
      • Layering upon layers upon layers
      • Easy programming
      • Connect it to other things (Weather, Home Automation,etc...)
    • Cons:
      • Takes lots of RAM.


So I took a bit of the FBI, and used some Raspberry Pi kiosk tutorials (can't think of which ones I used, just google some to find it) and mixed it into the mighty HTML5 one!!. Here's the parts list if this is your first RPicFrame:
  • LCD Screen with either Video or HDMI (Ebay/Amazon)
  • A Picture frame the you can attach your screen to. (May be difficult to find)
    • I luckily had a philips 10" digital picframe that I found at a garage sale for $15, gutted the lcd driver board and ordered one compatible with the lcd screen. (not always going to work) Also found the 7" digital picture frame at goodwill and gutted it and found a compatible driver (which barely fit with the Model A+).
    • Some people are skilled with woodworking as I've seen in other rpi pictureframe tutorials. You may be also.
  • Temperature Sensor (DS18B20)
    • I always add these to my pi, very handy, cheap, and gives me an idea of the temperature of that room.
  • Resistive Touch screen.
    • This may be included with your ebay purchase 
  • A Raspberry Pi, Model A, B, B+, A+, and Rpi 2 B.

Steps of creation:

sudo apt-get update && sudo apt-get upgrade -y
sudo apt-get install python-dev python-pip fbi mplayer matchbox libjpeg-dev
sudo pip install autobahn[twisted]
sudo pip install -I pillow
mkdir ~/.config


Now we are going to be creating the booting sequence:

Boot screen: follow tutorial here but replace the fbi cli to this appending mplayer underneath.

/usr/bin/fbi -T 30 -noverbose -a /etc/fbiImage.png & 
/usr/bin/mplayer /etc/pilogo.gif -speed .5 -loop 2 -fs -vo fbdev &

**This will add this spinning raspberry pi to your screen. (you'll need to save this gif to /etc/pilogo.gif)
The fbiImage.png is whatever you want to show after this gif finishes and the kiosk hasn't loaded. I just had a solid black image for mine. 



Controlling the frame with your mobile device



Ok... A couple more things and we're almost there. Create a file called go.sh and refernce it in your rc.local file:

go.sh:
#/bin/bash

# Disable DPMS / Screen blanking
 xset -dpms
 xset s off
 xset s noblank

while true; do
 sudo matchbox-window-manager -use_cursor no -use_titlebar no &
 sudo -u pi epiphany-browser -a --profile ~/.config http://localhost/index.html
 sleep 2s
done;
Append this before exit 0 in your rc.local:
sudo python /home/pi/piframe/piframestreamer.py &
sleep 5s
sudo -u pi xinit /home/pi/go.sh &

Here we are booting matchbox-window-manager as our desktop with epiphany our pictureframe webpage in an app kiosk mode with no title bar.

Now get the git clone of my webpage/websocket stuff and fill the pics folder with your images.
You'll need to make thumbnails to view the image selecting menu on the webpage:

git clone https://github.com/SimplyAutomationized/piframe.git
cd piframe 
python thumbMaker.py www/pics www/thumb

After that it should be ready to roll. I just make a piframe image for the sd card. Let me know if that is what some of you would like. Enjoy!!!
To see Part 2 of this project see here









The ones below are the 10" digital picture frame I can control my lights with. Along with having a raspberry pi camera in it.