Showing posts with label Modbus. Show all posts
Showing posts with label Modbus. Show all posts

Sunday, February 19, 2017

Raspberry Pi - pymodbus Youtube Series

I've been out of the game lately on my blog and wanted to give an update of my latest work on youtube.. I've been able to post a few video tutorials on pymodbus and wanted to show a preview of my youtube tutorial roadmap.

Pymodbus ModbusTCP - Reading Holding Registers
Pymodbus ModbusTCP - Reading & Writing Holding Registers
Pymodbus Raspberry Pi as Modbus Serial<->TCP Bridge
Pymodbus - RPi as ModbusTCP Slave Temperature Sensor
SCADA Datalogger - snap7 and Pymodbus
Pymodbus ModbusTCP - Reading And Writing Raspberry PI GPIO using ModbusTCP
Raspberry Pi - Tutorials - Snap7 Python - Raspberry Pi as S7 Device
Raspberry Pi - Tutorials - Snap7 Python - Reading & Writing Rpi GPIO using snap7 and Kepsever
Raspberry Pi - Tutorials - SCADA SQL Logger - snap7 and Pymodbus MSSQL

Dates are not yet decided.  I hope to get two of these a month. Let me know if you wish to see another one
So far these 4 are done:

Raspberry Pi - Tutorials - Pymodbus ModbusTCP - Setup & Quick Example (Writing To Twido M Memory)

 

Monday, July 7, 2014

Raspberry Pi SCADA Part 2, Modbus TCP PWM Controller

Raspberr Pi SCADA Part 2, Modbus PWM Controller

Since finding a cheap alternative to PLC whilst using an industrial protocol is a popular idea the Raspberry Pi has caught many eyes on doing this.  I posted once on reading a temperature sensor and serving it up on the Pi using ModbusTCP. This time I expound on it and show you how to control something. In this case it will be a PC 12v fan.

Parts:

  • 1 x Raspberry pi
  • 1 x Darlington Transistor
  • 1 x PC Fan (with about  500ma load)
  • 1 x 12v DC Power Supply


Setup:

  1. Installing pymodbus and dependencies:
    • sudo apt-get install python-pymodbus python-twisted-conch
  2. Wire up your Fan, LED, or motor


  1. View my code from Github.
Video of what I did:


Source Code:
from pymodbus.server.async import StartTcpServer
from pymodbus.device import ModbusDeviceIdentification
from pymodbus.datastore import ModbusSequentialDataBlock, ModbusSlaveContext, ModbusServerContext
from pymodbus.transaction import ModbusRtuFramer, ModbusAsciiFramer
from twisted.internet.task import LoopingCall
from threading import Thread
import pid
import threading
from time import sleep
import RPi.GPIO as GPIO
import os
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
#set up Raspberry GPIO 
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(25,GPIO.OUT)
pwm = GPIO.PWM(25,60)
pwmDutyCycle=100
pwm.start(pwmDutyCycle)
temperaturePoll = None

class Temp(Thread):
    """
     A class for getting the current temp of a DS18B20
    """
    def __init__(self, fileName=''):
        Thread.__init__(self)
        super(Temp, self).__init__()
        self._stop = threading.Event()
        self.tempDir = '/sys/bus/w1/devices/'
        list = os.listdir(self.tempDir)
        if(list[0][:2]=="28"):
         fileName=list[0]
        self.fileName = fileName
        self.currentTemp = -999
        self.correctionFactor = 1;
        self.enabled = True
        self.Run=True
    def run(self):
        while self.isEnabled():
   try:
    f = open(self.tempDir + self.fileName + "/w1_slave", 'r')
   except IOError as e:
    print "Error: File " + self.tempDir + self.fileName + "/w1_slave" + " does not exits"
    return;
   lines=f.readlines()
   crcLine=lines[0]
   tempLine=lines[1]
   result_list = tempLine.split("=")
   temp = float(result_list[-1])/1000 # temp in Celcius
   temp = temp + self.correctionFactor # correction factor
   #if you want to convert to Celcius, comment this line
   temp = (9.0/5.0)*temp + 32
   if crcLine.find("NO") > -1:
    temp = -999
   self.currentTemp = temp
   #print "Current: " + str(self.currentTemp) + " " + str(self.fileName)
   sleep(.5)
    #returns the current temp for the probe
    def getCurrentTemp(self):
        return self.currentTemp
    #setter to enable this probe
    def setEnabled(self, enabled):
        self.enabled = enabled
    #getter
    def isEnabled(self):
        return self.enabled


def updating_writer(a):
 context  = a[0]
 register = 3
 slave_id = 0x00
 address  = 0x00
 global pwmDutyCycle,temp
 #uncomment to debug temperature
 print temp.getCurrentTemp()
 values = [int(pwmDutyCycle),temp.getCurrentTemp()*100]
 context[slave_id].setValues(register,address,values)
def read_context(a):
 context  = a[0]
 register = 3
 slave_id = 0x00
 address  = 0x00
 value = context[slave_id].getValues(register,address)[0]
 global pwmDutyCycle
 if(value!=pwmDutyCycle):
  print value
  pwmDutyCycle=value
  pwm.ChangeDutyCycle(pwmDutyCycle)
def main():

 store = ModbusSlaveContext(
  di = ModbusSequentialDataBlock(0, [0]*100),
  co = ModbusSequentialDataBlock(0, [0]*100),
  hr = ModbusSequentialDataBlock(0, [0]*100),
  ir = ModbusSequentialDataBlock(0, [0]*100))
 context = ModbusServerContext(slaves=store, single=True)
 identity = ModbusDeviceIdentification()
 identity.VendorName  = 'pymodbus'
 identity.ProductCode = 'PM'
 identity.VendorUrl   = 'http://github.com/simplyautomationized'
 identity.ProductName = 'pymodbus Server'
 identity.ModelName   = 'pymodbus Server'
 identity.MajorMinorRevision = '1.0'
 time = 5 # 5 seconds delaytime = 5 # 5 seconds delay
 writer = LoopingCall(read_context,a=(context,))
 loop = LoopingCall(updating_writer, a=(context,))
 loop.start(.5) # initially delay by time
 writer.start(.1)
 StartTcpServer(context, identity=identity)#, address=("localhost", 502))
 #cleanup async tasks
 temp.setEnabled(False)
 loop.stop()
 writer.stop()
 GPIO.cleanup()
if __name__ == "__main__":
 temp = Temp()
 temp.start()
 main()

Tuesday, September 17, 2013

Home Automation Project #2 Rpi Light Switch

Lightswitch Replacement

Parts: PiFace $30,
Rpi Model A $25,
Wifi Adapter $10,
120v to 5vdc adapter $5ish,
Low voltage 2 gange wall box $5
Blank wall plate $2
2x low profile sanwa arcade buttons $4

This was one of my first pi projects in my home automation endeavor. I wanted something simple and that wasn't too expensive. I decided to go with the PiFace add-on board.  
One really important thing I wanted was to be able to control the light switch via the web. I ran into a problem with the webapp though. Every time I opened it up it would reset the piface card. I found it was reinitializing the card every time it opened my cgi script. I then decided I needed a python program that would run on boot that would listen for commands to execute against the piface card. I then came upon Pyro, which came in very handy.

Source for my pyro piface

import Pyro.core
import piface.pfio as cmd
import os
from time import sleep
from sys import exit
from os import fork,chdir,setsid,umask


class Command(Pyro.core.ObjBase):
        def __init__(self):
          Pyro.core.ObjBase.__init__(self)
        def outputStatus(self):
          list1=cmd.read_output()
          status = '{0:08b}'.format(list1)[::-1]
          return status
 #output command for piface outputs
        def outputCmd(self,pin,value):
             cmd.digital_write(pin,value)
             return self.outputStatus()
#command to return input statuses
        def inputStatus(self):
          list1 = cmd.read_input()
          status = '{0:08b}'.format(list1)[::-1]
          return status
#read cpu temperature
        def pitemperature(self):
          return open('/sys/class/thermal/thermal_zone0/temp','r').read()
def main():
        Pyro.core.initServer()
        cmd.init()
        daemon = Pyro.core.Daemon()
        uri = daemon.connect(Command(),"cmd")
        print "cmd port: ", daemon.port
        print "cmd uri:", uri
        daemon.requestLoop()

if __name__ == "__main__":
  try:
    pid = fork()
    if pid > 0:
      exit(0)
  except OSError, e:
    exit(1)

  chdir("/")
  setsid()
  umask(0)

  try:
    pid = fork()
    if pid > 0:
      exit(0)
  except OSError, e:
    exit(1)
  main()


I then have either my button listener script(which runs as a daemon on boot) or the web cgi script. Here's the lightswitch.py daemon code:

import sys
from pymodbus.client.sync import ModbusTcpClient as ModbusClient
from os import fork,chdir,setsid,umask
from sys import exit
from time import sleep
import time
import os
import Pyro.core

def main():
        loop=True
#connect to the piface pyro daemon we created earlier
        cmd = Pyro.core.getProxyForURI("PYROLOC://localhost:7766/cmd")
        time_pushed=0
        pushed_1=False
        pushed_2=False
        longpush_2=False
        count=0
        client = ModbusClient('ip_address_of_modbus_IO')
        client.close()
        while loop:
                sleep(.05)
                status = cmd.outputStatus()
                inputstatus = cmd.inputStatus()
#toggle button turn relay 1 on
                if inputstatus[0]=='1' and pushed_1 != True:
                        if status[0]=='0':
                           cmd.outputCmd(0,1)
                        else:
                           cmd.outputCmd(0,0)
                        pushed_1=True
                if inputstatus[0]=='0' and pushed_1:
                        pushed_1=False
#toggle button turn relay 2 on
                      if inputstatus[1]=='1' and pushed_2 !=True:
                        pushed_2=True
                        longpush_2=False
                        time_pushed=time.time()
#if both buttons are pressed for 10 second the reboot the pi
                      while inputstatus[0]=='1' and inputstatus[1]=='1':
                        sleep(1)
                        count=count+1
                        if(count==10):
                          os.system("sudo reboot")


#button released
                     if inputstatus[1]=='0' and pushed_2:
#calculate the time the button was pressed to determine what to do
                        time_taken = time.time()-time_pushed
                        pushed_2=False
                        if(time_taken<.5):
                         if status[1]=='0':
                           cmd.outputCmd(1,1)
                         else:
                           cmd.outputCmd(1,0)
#send command to remote i/o using modbus to turn on some lamps if you hold the button longer than 1/2 second
                        if (time_taken>.5):
                         try:
                          if(client.connect()):
                           if(client.read_discrete_inputs(200,1).bits[0]==True):
                             client.write_coil(0,True)
                           else:
                             client.write_coil(2,True)
                           client.close()
                         except:
                           e = sys.exc_info()[0]
                           f=open('error.log','w+')
                           f.write(e+'\n')
                           f.close()
                        time_pushed=0
#turn off light at certain time
                        hour=time.localtime(time.time())[3]
                        minute=time.localtime(time.time())[4]
                        second=time.localtime(time.time())[5]

                if(hour==5 and minute == 0 and second == 0 and status[0]=='1'):
                  cmd.outputCmd(1,0)


#useful daemon-like code:
if __name__ == "__main__":
  try:
    pid = fork()
    if pid > 0:
      exit(0)
  except OSError, e:
    exit(1)

  chdir("/")
  setsid()
  umask(0)

  try:
    pid = fork()
    if pid > 0:
      exit(0)
  except OSError, e:
    exit(1)
  main()



I threw in a bit of modbus to talk to some of my remote i/o to control some lamps throughout the room. The web code is similar to the lightswitch code (without listening for button presses of course). It response to AJAX requests to give the statuses of the outputs. If desired I can post the web code later.

Pics





see part one to this series here:Sprinkler Control

Saturday, September 14, 2013

Raspberry Pi SCADA Part 1, Modbus Temperature Sensor

Raspberry Pi SCADA Part 1, Modbus Temperature Sensor.

One great thing about the Pi is that it is so cost effective in some SCADA applications. With several different languages to be able to present your data. In the many crazy off the wall things I will do in my series here I will start off with using pymodbus,the DS18b20, and a 4.7k resistor temperature probe to get building temperature. Now there are known security flaws with modbus since it is an open protocol, but with this example you can only read data from the addresses.  This will be in holding the register 0x00 and will need to be scaled by 100 afterwords.

My total cost:
$25.00
.05
.20
$10.00
$3.00
Total
$38.25

SETUP

get pymodbus and dependencies:
sudo apt-get install python-pymodbus python-twisted-conch 

from pymodbus.server.async import StartTcpServer
from pymodbus.device import ModbusDeviceIdentification
from pymodbus.datastore import ModbusSequentialDataBlock
from pymodbus.datastore import ModbusSlaveContext, ModbusServerContext
from pymodbus.transaction import ModbusRtuFramer, ModbusAsciiFramer
from twisted.internet.task import LoopingCall
from threading import Thread
from time import sleep
import os
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
temperature =0
class Temp(Thread):
    """
     A class for getting the current temp of a DS18B20
    """

    def __init__(self, fileName=''):
        Thread.__init__(self)
        self.tempDir = '/sys/bus/w1/devices/'
        list = os.listdir(self.tempDir)
        if(list[0][:2]=="28"):
         fileName=list[0]
        self.fileName = fileName
        self.currentTemp = -999
        self.correctionFactor = 1;
        self.enabled = True

    def run(self):
        while True:
            if self.isEnabled():
                try:
                    f = open(self.tempDir + self.fileName + "/w1_slave", 'r')
                except IOError as e:
                    print "Error: File " + self.tempDir + self.fileName + "/w1_slave" + " does$
                    return;

                lines=f.readlines()
                crcLine=lines[0]
                tempLine=lines[1]
                result_list = tempLine.split("=")

                temp = float(result_list[-1])/1000 # temp in Celcius
                temp = temp + self.correctionFactor # correction factor
                #if you want to convert to Celcius, comment this line
                temp = (9.0/5.0)*temp + 32

                if crcLine.find("NO") > -1:
                    temp = -999
                self.currentTemp = temp
                #print "Current: " + str(self.currentTemp) + " " + str(self.fileName)
            sleep(1)

    #returns the current temp for the probe
    def getCurrentTemp(self):
        return self.currentTemp

    #setter to enable this probe
    def setEnabled(self, enabled):
        self.enabled = enabled
    #getter
    def isEnabled(self):
        return self.enabled

def updating_writer(a):
    context  = a[0]
    register = 3
    slave_id = 0x00
    address  = 0x00
    #print pi.getCurrentTemp(),str(int(pi.getCurrentTemp()*10))
    values = [int(pi.getCurrentTemp()*100)]
    context[slave_id].setValues(register,address,values)

store = ModbusSlaveContext(
    di = ModbusSequentialDataBlock(0, [0]*100),
    co = ModbusSequentialDataBlock(0, [0]*100),
    hr = ModbusSequentialDataBlock(0, [0]*100),
    ir = ModbusSequentialDataBlock(0, [0]*100))
context = ModbusServerContext(slaves=store, single=True)

identity = ModbusDeviceIdentification()
identity.VendorName  = 'pymodbus'
identity.ProductCode = 'PM'
identity.VendorUrl   = 'http://github.com/bashwork/pymodbus/'
identity.ProductName = 'pymodbus Server'
identity.ModelName   = 'pymodbus Server'
identity.MajorMinorRevision = '1.0'
pi = Temp()
pi.start()
time = 5 # 5 seconds delaytime = 5 # 5 seconds delay
loop = LoopingCall(f=updating_writer, a=(context,))
loop.start(time, now=False) # initially delay by time
StartTcpServer(context, identity=identity, address=("localhost", 502))
#change localhost to your ip address.


Check out how to control Raspberry Pi outputs using this library here