Wednesday, 13 August 2014 12:42

Raspberry Pi ModBus Energy Meter 2

 

Needed 
1 RaspberryPI
1 SDM120C EnergyMeter
1 USB<>RS485 Converter
1 twisted pair cable
recommend install Raspbian

 

 

 

Set speed USB port - 2400 bps, 8E1

 

stty -F /dev/ttyUSB0 2400 cs8 parenb -parodd

 

 

 

 

C code

 

 

 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
 
union
{
char c[4];
float f;
}u;
 
int main(int argc, char* argv[]) {
 
    struct termios serial;
    char inBuff[16];
    char *buffer;
    char choice[5];
    int wcount = 0;
    char cVoltage[] = {0x01,0x04,0x00,0x00,0x00,0x02,0x71,0xCB};
    char cCurrent[] = {0x01,0x04,0x00,0x06,0x00,0x02,0x91,0xCA};
    char cPower[] = {0x01,0x04,0x00,0x0C,0x00,0x02,0xB1,0xC8};
    char cAAPower[] = {0x01,0x04,0x00,0x12,0x00,0x02,0xD1,0xCE};
    char cRAPower[] = {0x01,0x04,0x00,0x18,0x00,0x02,0xF1,0xCC};
    char cPFactor[] = {0x01,0x04,0x00,0x1E,0x00,0x02,0x11,0xCD};
    char cFrequency[] = {0x01,0x04,0x00,0x46,0x00,0x02,0x90,0x1E};
    char cIAEnergy[] = {0x01,0x04,0x00,0x48,0x00,0x02,0xF1,0xDD};
    char cEAEnergy[] = {0x01,0x04,0x00,0x4A,0x00,0x02,0x50,0x1D};
    char cTAEnergy[] = {0x01,0x04,0x01,0x56,0x00,0x02,0x90,0x27};
 
    int fd = open("/dev/ttyUSB2", O_RDWR | O_NOCTTY | O_NDELAY);
 
    if (fd == -1) {
        perror(argv[2]);
        return -1;
    }
 
    if (tcgetattr(fd, &serial) < 0) {
        perror("Getting configuration");
        return -1;
    }
 
    //////////////////// Set up Serial Configuration ////////////
    serial.c_iflag = 0;
    serial.c_oflag = 0;
    serial.c_lflag = 0;
    serial.c_cflag = 0;
 
    serial.c_cc[VMIN] = 0;
    serial.c_cc[VTIME] = 10;
 
    serial.c_cflag = B2400 | CS8 | PARENB | CREAD & ~PARODD;
    fcntl(fd,F_SETFL,0);
    tcsetattr(fd, TCSANOW, &serial); // Apply configuration
    //////////////////////////////////////////////////////////////
 
    
   int rcount;  
   buffer = &inBuff[0];
   int i = 0;
   int j = 0;
 
   //////////////////////////         Voltage                /////////////////////////////
   i = 0; j = 0;
   memset(&inBuff[0],0,sizeof(inBuff));
   write(fd,cVoltage,sizeof(cVoltage));
   buffer = &inBuff[0];
   printf("Sent: ");
   for(j = 0;j < sizeof(cVoltage);j++)
   {
printf("%02x ",cVoltage[j]);
   }
   printf("\r\n");
   printf("Received: " );
while( (rcount = read(fd,buffer,1)) > 0)
{
      if (rcount < 0) {
           perror("Read");
          return -1;
        }
 
      buffer++;
printf("%02x ",inBuff[i]); i++;
  }
    u.c[3] = inBuff[3];
    u.c[2] = inBuff[4];
    u.c[1] = inBuff[5];
    u.c[0] = inBuff[6];
printf("\r\n");
printf("Voltage: %4.2f V \r\n\r\n",u.f);
///////////////////////////////////////////////////////////////////////////////////////////
 
 
 //////////////////////////         Current                /////////////////////////////
   i = 0; j = 0;
   memset(&inBuff[0],0,sizeof(inBuff));
   write(fd,cCurrent,sizeof(cCurrent));
   buffer = &inBuff[0];
   printf("Sent: ");
   for(j = 0;j < sizeof(cCurrent);j++)
   {
        printf("%02x ",cCurrent[j]);
   }
   printf("\r\n");
   printf("Received: " );
        while( (rcount = read(fd,buffer,1)) > 0)
        {
           if (rcount < 0) {
           perror("Read");
           return -1;
        }
 
        buffer++;
        printf("%02x ",inBuff[i]); i++;
  }
    u.c[3] = inBuff[3];
    u.c[2] = inBuff[4];
    u.c[1] = inBuff[5];
    u.c[0] = inBuff[6];
printf("\r\n");
printf("Current: %4.2f A \r\n\r\n",u.f);
///////////////////////////////////////////////////////////////////////////////////////////
 
 
///////////////////////////////         Power               ///////////////////////////////
   i = 0; j = 0;
   memset(&inBuff[0],0,sizeof(inBuff));
   write(fd,cPower,sizeof(cPower));
   buffer = &inBuff[0];
   printf("Sent: ");
   for(j = 0;j < sizeof(cPower);j++)
   {
        printf("%02x ",cPower[j]);
   }
   printf("\r\n");
   printf("Received: " );
        while( (rcount = read(fd,buffer,1)) > 0)
        {
           if (rcount < 0) {
           perror("Read");
           return -1;
        }
 
        buffer++;
        printf("%02x ",inBuff[i]); i++;
  }
    u.c[3] = inBuff[3];
    u.c[2] = inBuff[4];
    u.c[1] = inBuff[5];
    u.c[0] = inBuff[6];
printf("\r\n");
printf("Power: %4.2f W \r\n\r\n",u.f);
///////////////////////////////////////////////////////////////////////////////////////////
 
 
///////////////////////////////   Active Apparent Power     ///////////////////////////////
   i = 0; j = 0;
   memset(&inBuff[0],0,sizeof(inBuff));
   write(fd,cAAPower,sizeof(cAAPower));
   buffer = &inBuff[0];
   printf("Sent: ");
   for(j = 0;j < sizeof(cAAPower);j++)
   {
        printf("%02x ",cAAPower[j]);
   }
   printf("\r\n");
   printf("Received: " );
        while( (rcount = read(fd,buffer,1)) > 0)
        {
           if (rcount < 0) {
           perror("Read");
           return -1;
        }
 
        buffer++;
        printf("%02x ",inBuff[i]); i++;
  }
    u.c[3] = inBuff[3];
    u.c[2] = inBuff[4];
    u.c[1] = inBuff[5];
    u.c[0] = inBuff[6];
printf("\r\n");
printf("Active Apparent Power: %4.2f VA \r\n\r\n",u.f);
///////////////////////////////////////////////////////////////////////////////////////////
 
///////////////////////////////   Reactive Apparent Power     ///////////////////////////////
   i = 0; j = 0;
   memset(&inBuff[0],0,sizeof(inBuff));
   write(fd,cRAPower,sizeof(cRAPower));
   buffer = &inBuff[0];
   printf("Sent: ");
   for(j = 0;j < sizeof(cRAPower);j++)
   {
        printf("%02x ",cRAPower[j]);
   }
   printf("\r\n");
   printf("Received: " );
        while( (rcount = read(fd,buffer,1)) > 0)
        {
           if (rcount < 0) {
           perror("Read");
           return -1;
        }
 
        buffer++;
        printf("%02x ",inBuff[i]); i++;
  }
    u.c[3] = inBuff[3];
    u.c[2] = inBuff[4];
    u.c[1] = inBuff[5];
    u.c[0] = inBuff[6];
printf("\r\n");
printf("Reactive Apparent Power: %4.2f VAr \r\n\r\n",u.f);
///////////////////////////////////////////////////////////////////////////////////////////
 
////////////////////////////////////   Power Factor     ///////////////////////////////////
   i = 0; j = 0;
   memset(&inBuff[0],0,sizeof(inBuff));
   write(fd,cPFactor,sizeof(cPFactor));
   buffer = &inBuff[0];
   printf("Sent: ");
   for(j = 0;j < sizeof(cPFactor);j++)
   {
        printf("%02x ",cPFactor[j]);
   }
   printf("\r\n");
   printf("Received: " );
        while( (rcount = read(fd,buffer,1)) > 0)
        {
           if (rcount < 0) {
           perror("Read");
           return -1;
        }
 
        buffer++;
        printf("%02x ",inBuff[i]); i++;
  }
    u.c[3] = inBuff[3];
    u.c[2] = inBuff[4];
    u.c[1] = inBuff[5];
    u.c[0] = inBuff[6];
printf("\r\n");
printf("Power Factor: %4.2f \r\n\r\n",u.f);
///////////////////////////////////////////////////////////////////////////////////////////
 
/////////////////////////////////////   Frequency     /////////////////////////////////////
   i = 0; j = 0;
   memset(&inBuff[0],0,sizeof(inBuff));
   write(fd,cFrequency,sizeof(cFrequency));
   buffer = &inBuff[0];
   printf("Sent: ");
   for(j = 0;j < sizeof(cFrequency);j++)
   {
        printf("%02x ",cFrequency[j]);
   }
   printf("\r\n");
   printf("Received: " );
        while( (rcount = read(fd,buffer,1)) > 0)
        {
           if (rcount < 0) {
           perror("Read");
           return -1;
        }
 
        buffer++;
        printf("%02x ",inBuff[i]); i++;
  }
    u.c[3] = inBuff[3];
    u.c[2] = inBuff[4];
    u.c[1] = inBuff[5];
    u.c[0] = inBuff[6];
printf("\r\n");
printf("Frequency: %4.2f Hz \r\n\r\n",u.f);
///////////////////////////////////////////////////////////////////////////////////////////
 
/////////////////////////////  Import Active Energy   /////////////////////////////////////
   i = 0; j = 0;
   memset(&inBuff[0],0,sizeof(inBuff));
   write(fd,cIAEnergy,sizeof(cIAEnergy));
   buffer = &inBuff[0];
   printf("Sent: ");
   for(j = 0;j < sizeof(cIAEnergy);j++)
   {
        printf("%02x ",cIAEnergy[j]);
   }
   printf("\r\n");
   printf("Received: " );
        while( (rcount = read(fd,buffer,1)) > 0)
        {
           if (rcount < 0) {
           perror("Read");
           return -1;
        }
 
        buffer++;
        printf("%02x ",inBuff[i]); i++;
  }
    u.c[3] = inBuff[3];
    u.c[2] = inBuff[4];
    u.c[1] = inBuff[5];
    u.c[0] = inBuff[6];
printf("\r\n");
printf("Import Active Energy: %4.2f KWh \r\n\r\n",u.f);
///////////////////////////////////////////////////////////////////////////////////////////
 
/////////////////////////////  Export Active Energy   /////////////////////////////////////
   i = 0; j = 0;
   memset(&inBuff[0],0,sizeof(inBuff));
   write(fd,cEAEnergy,sizeof(cEAEnergy));
   buffer = &inBuff[0];
   printf("Sent: ");
   for(j = 0;j < sizeof(cEAEnergy);j++)
   {
        printf("%02x ",cEAEnergy[j]);
   }
   printf("\r\n");
   printf("Received: " );
        while( (rcount = read(fd,buffer,1)) > 0)
        {
           if (rcount < 0) {
           perror("Read");
           return -1;
        }
 
        buffer++;
        printf("%02x ",inBuff[i]); i++;
  }
    u.c[3] = inBuff[3];
    u.c[2] = inBuff[4];
    u.c[1] = inBuff[5];
    u.c[0] = inBuff[6];
printf("\r\n");
printf("Export Active Energy: %4.2f KWh \r\n\r\n",u.f);
///////////////////////////////////////////////////////////////////////////////////////////
 
/////////////////////////////  Total  Active Energy   /////////////////////////////////////
   i = 0; j = 0;
   memset(&inBuff[0],0,sizeof(inBuff));
   write(fd,cTAEnergy,sizeof(cTAEnergy));
   buffer = &inBuff[0];
   printf("Sent: ");
   for(j = 0;j < sizeof(cTAEnergy);j++)
   {
        printf("%02x ",cTAEnergy[j]);
   }
   printf("\r\n");
   printf("Received: " );
        while( (rcount = read(fd,buffer,1)) > 0)
        {
           if (rcount < 0) {
           perror("Read");
           return -1;
        }
 
        buffer++;
        printf("%02x ",inBuff[i]); i++;
  }
    u.c[3] = inBuff[3];
    u.c[2] = inBuff[4];
    u.c[1] = inBuff[5];
    u.c[0] = inBuff[6];
printf("\r\n");
printf("Total Active Energy: %4.2f KWh \r\n\r\n",u.f);
///////////////////////////////////////////////////////////////////////////////////////////
 
 
 
close(fd);
}
 

 

 

 

 

 

Download All files - DOWNLOAD

 

Wednesday, 13 August 2014 07:17

Raspberry Pi ModBus Energy Meter 1

 

Needed 
1 RaspberryPI
1 EJS 212 D2AMX Energy meter with ModBus
1 USB<>RS485 Converter
1 twisted pair cable
recommend install Raspbian

 

 

 

Set speed USB port - 9'600 bps, 8E1

 

stty -F /dev/ttyUSB0 9600 cs8 parenb -parodd

 

 

C code

 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
 
int main(int argc, char* argv[]) {
 
    struct termios serial;
    char inBuff[16];
    char *buffer;
    char choice[5];
    int wcount = 0;
    char rdINPUT[8]= {0x22,0x04,0x00,0x00,0x00,0x06,0x77,0x5B};
    int fd = open("/dev/ttyUSB2", O_RDWR | O_NOCTTY | O_NDELAY);
    double tariff1,tariff2,tariff3;
 
    if (fd == -1) {
        perror(argv[2]);
        return -1;
    }
 
    if (tcgetattr(fd, &serial) < 0) {
        perror("Getting configuration");
        return -1;
    }
 
    //////////////////// Set up Serial Configuration ////////////
    serial.c_iflag = 0;
    serial.c_oflag = 0;
    serial.c_lflag = 0;
    serial.c_cflag = 0;
 
    serial.c_cc[VMIN] = 0;
    serial.c_cc[VTIME] = 10;
 
    serial.c_cflag = B9600 | CS8 | PARENB | CREAD & ~PARODD;
    fcntl(fd,F_SETFL,0);
    tcsetattr(fd, TCSANOW, &serial); // Apply configuration
    //////////////////////////////////////////////////////////////
 
    
   int rcount;  
   buffer = &inBuff[0];
   int i = 0;
   int j = 0;
   write(fd,rdINPUT,8);
   buffer = &inBuff[0];
 
   printf("Send: ");
   for(j = 0;j < sizeof(rdINPUT);j++)
   {
printf("%02x ",rdINPUT[j]);
   }
   printf("\r\n");
   printf("Received: " );
while( (rcount = read(fd,buffer,1)) > 0)
{
      if (rcount < 0) {
           perror("Read");
          return -1;
        }
 
      buffer++;
printf("%02x ",inBuff[i]); i++;
  }
       tariff2 =(double)(256*256*256*inBuff[3]) + (double)(256*256*inBuff[4]) + (double)(256*inBuff[5]) + (double)inBuff[6];
       tariff2 =(double)(256*256*256*inBuff[7]) + (double)(256*256*inBuff[8]) + (double)(256*inBuff[9]) + (double)inBuff[10];
       tariff1 =(double)(256*256*256*inBuff[11]) + (double)(256*256*inBuff[12]) + (double)(256*inBuff[13]) + (double)inBuff[14];
       printf("\r\n");
       printf("Peak Tariff 3: %4.3f KWh",tariff3/100);
       printf("\r\n");
       printf("Daily Tariff 2: %4.3f KWh",tariff2/100);
       printf("\r\n");
       printf("Night Tariff 1: %4.3f KWh",tariff1/100);
       printf("\r\n");
 
close(fd);
}
 

 

 

 

 

Input Registers

Address

Value

0x00

Energy tariff 3 - peak (most significant part)

0x01

Energy tariff 3 - peak (low significant part)

0x02

Energy tariff 2 - peak (most significant part)

0x03

Energy tariff 2 - peak (low significant part)

0x04

Energy tariff 1 - peak (most significant part)

0x05

Energy tariff 1 - peak (low significant part)

 

Note: All values ​​of the energy registers are in hundredths of KWh, 1 from the register corresponds to 0.01 KWh

 

 

 

 

 
 
 
 
Tuesday, 12 August 2014 12:11

Raspberry Pi Door Lock 03

 

 

This project will show how to control door lock system using basic free code via keyboard.

 

 

All you need is:
1x Raspberry Pi
1x USB Keyboard
1x Electromechanical Strike
1x USB One Channel Relay controller

 

 

 

Electromechanical Strike
http://www.amig.es/en/strike-body-mod-18-19/g/372

 

 

 

 

 

By entering simple PIN code (example 1234 & ENTER) on the keyboard you can open the door.

 

 File : on.sh

#!/bin/bash
# sends on signal to the USB relay
echo -e "\xFF\x01\x01" > /dev/ttyUSB0 ;sleep 1
echo -e "\xFF\x01\x00" > /dev/ttyUSB0 

 

 

 

File : keyboard.c

 

#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <linux/input.h>
#include <string.h>
#include <stdio.h>
 
static const char *const evval[3] = {
    "RELEASED",
    "PRESSED ",
    "REPEATED"
};
 
char PIN[4] = {79,80,81,75}; //1,2,3,4
char inData[4] = {0,0,0,0}; //Input Buffer
char *ptr;
int cntr = 0; //Counter
 
int main(void)
{
    const char *dev = "/dev/input/event0";
    struct input_event ev;
    ssize_t n;
    int fd;
    ptr = &inData[0];
 
    fd = open(dev, O_RDONLY);
    if (fd == -1) {
        fprintf(stderr, "Cannot open %s: %s.\n", dev, strerror(errno));
        return EXIT_FAILURE;
    }
 
while (1) {
        n = read(fd, &ev, sizeof ev);
        if (n == (ssize_t)-1) {
            if (errno == EINTR)
                continue;
            else
                break;
        } else
        if (n != sizeof ev) {
            errno = EIO;
            break;
        }
  if (ev.type == EV_KEY && ev.value >= 0 && ev.value <= 2)
           // printf("%s 0x%04x (%d)\n", evval[ev.value], (int)ev.code, (int)ev.code);
            if(ev.value == 1){//Pressed
if(ev.code == 96){//Enter
if(memcmp(PIN,inData,4) == 0)
{
printf("Code accepted!\r\n");
popen("exec /home/pi/keyboard/./on.sh","r");
}
ptr = &inData[0];
memset(ptr,0,4);
cntr = 0;
}
else{
if(cntr == 4){//$ digits limit
cntr = 0;
ptr = &inData[0];
}
*ptr = ev.code;
ptr++;
                cntr++;
}
 
}
 
    }
    fflush(stdout);
    fprintf(stderr, "%s.\n", strerror(errno));
    return EXIT_FAILURE;
}

 

 

View how to compile C Files 

http://info.kmtronic.com/how-to-compiling-c-and-c-programs.html

 

 

All Files DOWNLOAD

 

 

 

 

 

 

 

Tuesday, 12 August 2014 08:46

Raspberry Pi Joystick controlled relays

 
I'll show you how to control USB Web Relays via USB Joystick
 
Needed 
1 RaspberryPI
1 USB joystick
USB 485 relay
recommend install Raspbian
 

 

 

 

 

 

 

 

C Code

 

 

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <linux/joystick.h>
 
#define JOY_DEV "/dev/input/js0"
 
int main()
{
int f;
int joy_fd, *axis=NULL, num_of_axis=0, num_of_buttons=0, x;
char *button=NULL, name_of_joystick[80];
struct js_event js;
 
if( ( joy_fd = open( JOY_DEV , O_RDONLY)) == -1 )
{
printf( "Couldn't open joystick\n" );
return -1;
}
 
ioctl( joy_fd, JSIOCGAXES, &num_of_axis );
ioctl( joy_fd, JSIOCGBUTTONS, &num_of_buttons );
ioctl( joy_fd, JSIOCGNAME(80), &name_of_joystick );
 
axis = (int *) calloc( num_of_axis, sizeof( int ) );
button = (char *) calloc( num_of_buttons, sizeof( char ) );
 
printf("Joystick detected: %s\n\t%d axis\n\t%d buttons\n\n"
, name_of_joystick
, num_of_axis
, num_of_buttons );
 
fcntl( joy_fd, F_SETFL, O_NONBLOCK ); /* use non-blocking mode */
 
while( 1 ) /* infinite loop */
{
 
/* read the joystick state */
read(joy_fd, &js, sizeof(struct js_event));
 
/* see what to do with the event */
switch (js.type & ~JS_EVENT_INIT)
{
case JS_EVENT_AXIS:
axis   [ js.number ] = js.value;
break;
case JS_EVENT_BUTTON:
button [ js.number ] = js.value;
break;
}
 
/* print the results */
printf( "X: %6d  Y: %6d  ", axis[0], axis[1] );
 
if( num_of_axis > 2 )
printf("Z: %6d  ", axis[2] );
 
if( num_of_axis > 3 )
printf("R: %6d  ", axis[3] );
 
for( x=0 ; x<num_of_buttons ; ++x )
printf("B%d: %d  ", x, button[x] );
 
                        if(button[0] > 0) {f = popen("exec bash myscript.bash", "r");}
 
printf("  \r");
fflush(stdout);
}
 
close( joy_fd ); /* too bad we never get here */
return 0;
}

 

 

 

 

 

 

 

Download  all files - DOWNLOAD

Tuesday, 12 August 2014 06:13

Raspberry Pi as a DMX light controller

 
 
 
Needed 
1 RaspberryPI
1 KMTronic UART Board
12 volt power
recommend install Raspbian
 

 

 

 

 

 

 

 

....

 

 

 

 

 

 
 
 
Needed 
1 RaspberryPI
1 KMTronic UART Board
2 cables for connect to GPIO on Raspberry PI
12 volt power
recommend install Raspbian
 
 
Connect GPIO 14 (TXD) to  RX on Board

Connect GROUND to + on Board

 
 
 
 

 

 

Set speed USB port

stty -F /dev/ttyAMA0 9600

 

 

Shell script to control relays

echo -e "\xFF\x01\x01" > /dev/ttyAMA0
echo -e "\xFF\x01\x00" > /dev/ttyAMA0
 
echo -e "\xFF\x02\x01" > /dev/ttyAMA0
echo -e "\xFF\x02\x00" > /dev/ttyAMA0
 
echo -e "\xFF\x03\x01" > /dev/ttyAMA0
echo -e "\xFF\x03\x00" > /dev/ttyAMA0
 
echo -e "\xFF\x04\x01" > /dev/ttyAMA0
echo -e "\xFF\x04\x00" > /dev/ttyAMA0
 
echo -e "\xFF\x05\x01" > /dev/ttyAMA0
echo -e "\xFF\x05\x00" > /dev/ttyAMA0
 
echo -e "\xFF\x06\x01" > /dev/ttyAMA0
echo -e "\xFF\x06\x00" > /dev/ttyAMA0
 
echo -e "\xFF\x07\x01" > /dev/ttyAMA0
echo -e "\xFF\x07\x00" > /dev/ttyAMA0
 
echo -e "\xFF\x08\x01" > /dev/ttyAMA0
echo -e "\xFF\x08\x00" > /dev/ttyAMA0

 

 

Download UART shell script - DOWNLOAD

 

 

 

 

 

Thursday, 31 July 2014 11:58

Raspberry Pi Door Lock 02

 

 

I'll show you how with RFID card checker you can open the door.

 

Needed

1 RaspberryPI
1 Strike (body) mod.18-19
1 KMTronic RFID card reader 
1 KMTronic USB One Relay
recommend install Raspbian

 

 

 

Electromechanical Strike
http://www.amig.es/en/strike-body-mod-18-19/g/372

 

 

Set permissions on /dev/ttyUSB0 and ttyUSB1

sudo chmod -R 777 /dev/ttyUSB0

sudo chmod -R 777 /dev/ttyUSB1

 

 

Code RFIDCardReader.c

 

open/create file  RFIDcardReader.c and post code

 

 

 

 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>

int main(void) {

    struct termios serial;
    char rON[3] = {0xFF,0x01,0x01}; // USB Relay ON command
    char rOFF[3] = {0xFF,0x01,0x00}; // USB Relay OFF command    
    char inBuff[16];
    char *buffer;
 
    int rfid = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY);
    int relay = open("/dev/ttyUSB1", O_RDWR | O_NOCTTY | O_NDELAY);
    
    if (rfid == -1) {
        return -1;
    }
    
    if (relay == -1) {
        return -1;
    }

    //////////////////// Set up Serial Configuration ////////////
    serial.c_iflag = 0;
    serial.c_oflag = 0;
    serial.c_lflag = 0;
    serial.c_cflag = 0;

    serial.c_cc[VMIN] = 0;
    serial.c_cc[VTIME] = 10;

    serial.c_cflag = B9600 | CS8 | CREAD;
    fcntl(rfid,F_SETFL,0);
    fcntl(relay,F_SETFL,0);
    tcsetattr(rfid, TCSANOW, &serial); // Apply configuration
    tcsetattr(relay, TCSANOW, &serial); // Apply configuration
    //////////////////////////////////////////////////////////////
 
   int rcount;  
   buffer = &inBuff[0];
    
   while(1)
  {
   buffer = &inBuff[0];    
        while( (rcount = read(rfid,buffer,1)) > 0)
        { 
            if (rcount < 0) {
                perror("Read");
                return -1;
             }
            buffer++;
           

            // Valid Card number is 0x79 0x52 0x33
            if (inBuff[0] == 0x79 && inBuff[1] == 0x52 && inBuff[2] == 0x33)
            {
               printf("%02x %02x %02x \r\n",inBuff[0],inBuff[1],inBuff[2]);
               printf("Card is recognized! \r\n");
               memset(&inBuff[0],0,3);
               write(relay, rON, sizeof(rON));
               sleep(5);
               write(relay, rOFF, sizeof(rON));
            }
            else
            {
              
            }
        }
   }

}

 

 

 

How to Compiling C Files? - View here

 

 

chmod +x RFIDCardReader 

and start file

 

 

 

 

 

 

 

 

All files here - DOWNLOAD

 

 

 

 

 

 
Needed 
1 RaspberryPI
1 KMTronic 2 Relay Board
1 USB cable 
recommend install Raspbian

 

 

 

 

To see ttyusb use do the following:

cat /dev/tty and press TAB twice

 

 

Here we see that ttyUSB0

 

Set speed USB port

 

stty -F /dev/ttyUSB0 9600

 

 

Shell script to control relays

echo -e "\xFF\x01\x01" > /dev/ttyUSB0
echo -e "\xFF\x01\x00" > /dev/ttyUSB0

 

echo -e "\xFF\x02\x01" > /dev/ttyUSB0
echo -e "\xFF\x02\x00" > /dev/ttyUSB0
 

 

 

 

 
 
Needed 
1 RaspberryPI
1 KMTronic 8 Relay Board
1 USB cable 
12 volt power
recommend install Raspbian
 

 

Raspberrapi automatically recognizes it as ttyACM0

 

 

Set speed USB port

stty -F /dev/ttyACM0 9600

 

 

Shell script to control relays

echo -e "\xFF\x01\x01" > /dev/ttyACM0
echo -e "\xFF\x01\x00" > /dev/ttyACM0

 

echo -e "\xFF\x02\x01" > /dev/ttyACM0
echo -e "\xFF\x02\x00" > /dev/ttyACM0
 
echo -e "\xFF\x03\x01" > /dev/ttyACM0
echo -e "\xFF\x03\x00" > /dev/ttyACM0
 
echo -e "\xFF\x04\x01" > /dev/ttyACM0
echo -e "\xFF\x04\x00" > /dev/ttyACM0
 
echo -e "\xFF\x05\x01" > /dev/ttyACM0
echo -e "\xFF\x05\x00" > /dev/ttyACM0
 
echo -e "\xFF\x06\x01" > /dev/ttyACM0
echo -e "\xFF\x06\x00" > /dev/ttyACM0
 
echo -e "\xFF\x07\x01" > /dev/ttyACM0
echo -e "\xFF\x07\x00" > /dev/ttyACM0
 
echo -e "\xFF\x08\x01" > /dev/ttyACM0
echo -e "\xFF\x08\x00" > /dev/ttyACM0

 

 

Download all files - DOWNLOAD

http://www.info.kmtronic.com/software/Raspberry_PI/USBRelayTest.zip

 

 

 

 

Monday, 28 July 2014 10:23

PELCO D commands

 

 

 

 

 

 

 

 

KMTronic PELCO D Test software:

http://www.info.kmtronic.com/software/PTZ/KMTronic_PelcoD_Tester.zip

 

 

 

 

Commands:

 

FF 01 00 00 00 00  - STOP

FF 01 00 04 3F 00  - LEFT
FF 01 00 02 3F 00  - RIGHT
FF 01 00 08 00 3F  - UP
FF 01 00 10 00 3F  - DOWN

FF 01 00 0C 3F 3F  - LEFT-UP
FF 01 00 0A 3F 3F  - RIGHT-UP
FF 01 00 12 3F 3F  - RIGHT-DOWN
FF 01 00 14 3F 3F  - LEFT-DOWN

FF 01 10 00 00 00  - AUTOSCAN

FF 01 00 20 00 00  - ZOOM+
FF 01 00 40 00 00  - ZOOM-
FF 01 00 08 00 00  - FOCUS+
FF 01 01 00 00 00  - FOCUS-


FF 01 00 07 00 <PRESET> - CALL PRESET

 

 

NOTE: You don't need to enter checksum manually. The software automatically calculates checksum and adds it to the main command.

 

 

 

 

Page 6 of 41