Pages: [1]   Go Down
Author Topic: Controlling a rs-232 Relaisboard via web  (Read 321 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 3
Posts: 133
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi!

I want to connect an rs232 Relais board to my Arduino, thats the first step. The second will be to add an Webserver function, but lets start with the Relaisboard first.

I have two of those Boards (and some older Version which dont support function 4 and 5).

Now my try was the following
Code:
/*
  AnalogReadSerial
 Reads an analog input on pin 0, prints the result to the serial monitor.
 Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.
 
 This example code is in the public domain.
 */
byte a= 3;
byte b =0;
byte c=48;
byte d= a^b^c;

void setup() {
 Serial.begin(19200);
}


void loop() {
  // read the input on analog pin 0:

  // print out the value you read:
  Serial.write(a);
  Serial.write(b);
  Serial.write(c);
  Serial.write(d);
  delay(500);        // delay in between reads for stability
}

But this doesnt work smiley-sad.

What did I do wrong? Here is the Product site
http://www.conrad.de/ce/de/product/197720/Conrad-8fach-Relaiskarte-24-VDC-7-A-Baustein-8-Relaisausgaenge
and here the Manual
http://www.produktinfo.conrad.com/datenblaetter/175000-199999/197720-an-01-ml-8FACH_RELAISKARTE_24V_7A_de_en_fr_nl.pdf
« Last Edit: March 29, 2013, 06:51:56 am by tsaG » Logged

Offline Offline
Edison Member
*
Karma: 58
Posts: 2078
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That board is ment to connect with a PC, not with an Arduino board.
Is a schematic of that board available ?
Is it using the higher voltage RS232 ? Perhaps the Arduino is already damaged. The Arduino can only handle 5V level serial communication.

With so little information it is almost impossible to make it work.
I read that a null-modem is required sometimes.
So the board is too expensive, almost useless, and without proper documentation.

And even if you have the hardware correct, you have to send commands.

You could buy a relay board which can be normally used with an Arduino board.
For example these, http://www.ebay.de/sch/i.html?_nkw=8+channel+relay+module&_sacat=0&_odkw=8+relay+module&LH_BIN=1&_sop=15&_osacat=0&_from=R40&LH_PrefLoc=2

What do you want to turn on and off ?
Logged

Offline Offline
Full Member
***
Karma: 3
Posts: 133
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi!

Thanks for your reply. Yes, the Board is crappy designed but I just got 4 of them for free, so I wanted to give it a try smiley-grin

I managed to get it to work.

There is an Attiny2313 on the board to set the Relais. I already tried to flash the Attiny but it doesnt really work out, so I flashed it back.

The reason why it didnt work to use the RX/TX Output directly on the boards connection is the MAX232 Chip. I soldered my wires directly to the Attiny 2313 and it worked!

Here is my function I used (using Myserial)


Code:
void sendCommand(int command, int cardAddr, int data){
  byte bytes[4];
  bytes[0] = byte(command);
  bytes[1] = byte(cardAddr);
  bytes[2] = byte(data);
  bytes[3] = bytes[0] ^ bytes[1] ^ bytes[2];

  writeBytes(bytes,4);
  delay(250);
}

void writeBytes(byte arr[], int len){
  for(int i = 0; i < len; i++){
    mySerial.write(arr[i]);
  }
}

First you have to Initialise them (1.0.0) then Set them (3.0.x). Works good.

The next step is Adding the Webserver capability. I already tested it using the simpled Webserver youll find when googling "ArduinoWebServerLED".
I wanted to use the SDcard to store my Website, but how can I do that? I already found "TinyWebServer" but even the examples it wont compile smiley-sad

Code:
SimpleWebServer.ino:11:19: warning: Flash.h: No such file or directory
SimpleWebServer.ino: In function 'boolean index_handler(TinyWebServer&)':
SimpleWebServer.ino:38: warning: only initialized variables can be placed into program memory area
SimpleWebServer:38: error: no match for 'operator<<' in 'web_server << (const __FlashStringHelper*)({...})'
SimpleWebServer.ino: In function 'const char* ip_to_str(const uint8_t*)':
SimpleWebServer.ino:48: warning: embedded '\0' in format
SimpleWebServer.ino: In function 'void setup()':
SimpleWebServer.ino:54: warning: only initialized variables can be placed into program memory area
SimpleWebServer:54: error: expected `)' before 'FreeRam'
SimpleWebServer:54: error: expected `)' before ';' token
SimpleWebServer.ino:72: warning: only initialized variables can be placed into program memory area
SimpleWebServer:72: error: no match for 'operator<<' in 'Serial << (const __FlashStringHelper*)({...})'
SimpleWebServer.ino:76: warning: only initialized variables can be placed into program memory area
SimpleWebServer:76: error: no match for 'operator<<' in 'Serial << (const __FlashStringHelper*)({...})'
SimpleWebServer.ino:79: warning: only initialized variables can be placed into program memory area
SimpleWebServer:79: error: no match for 'operator<<' in 'Serial << (const __FlashStringHelper*)({...})'

Are there any Libraries I can use? Is there "ONE" library everyone uses for those Webservers (using SD-Card for switching pins or sth.)?

Logged

Offline Offline
Edison Member
*
Karma: 58
Posts: 2078
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Connecting it to the RX and TX at the logic level is a nice solution !

For I webserver on the SD card, I created it myself.
I started with the Webserver, http://arduino.cc/en/Tutorial/WebServer
Added SD card functionality, http://www.ladyada.net/learn/arduino/ethfiles.html
And I check how someone else did, http://www.instructables.com/id/ServDuino-Arduino-Webserver/?ALLSTEPS
Logged

0
Offline Offline
Tesla Member
***
Karma: 143
Posts: 9586
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Below is some test code that basically stores a "web site" on a microSD card. If your relay board uses a max232 or similar chip to convert the pc rs232 signal to TTL, then connect the arduino tx/rx to the TTL tx/rx lines down stream of the chip.

Code:
//zoomkat 2/26/13
//SD server slider test code
//open serial monitor to see what the arduino receives
//browser address will look like http://192.168.1.102:84/servosld.htm when submited
//for use with W5100 based ethernet shields
//put the servosld.htm, slider.js, bluev_sl.gif,
//and bluev_bg.gif on the SD card
//download flies at:
// http://web.comporium.net/~shb/pix/servosld.htm
// http://web.comporium.net/~shb/pix/slider.js
// http://web.comporium.net/~shb/pix/bluev_bg.gif
// http://web.comporium.net/~shb/pix/bluev_sl.gif
//

#include <SD.h>
#include <SPI.h>
#include <Ethernet.h>

#include <Servo.h>
Servo myservoa, myservob, myservoc, myservod;  // create servo object to control a servo
Servo myservoe, myservof, myservog; // myservoh not used due to lack of another free pin
String readString, pos;

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 102 }; // ip in lan
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(84); //server port

//////////////////////

void setup(){

  Serial.begin(9600);

  // disable w5100 while setting up SD
  pinMode(10,OUTPUT);
  digitalWrite(10,HIGH);
  Serial.print("Starting SD..");
  if(!SD.begin(4)) Serial.println("failed");
  else Serial.println("ok");

  Ethernet.begin(mac, ip, gateway, gateway, subnet);

  //delay(2000);
  server.begin();
  Serial.println("Ready");

  myservoa.attach(2);  //the pin for the servoa control
  myservob.attach(3);  //the pin for the servob control
  myservoc.attach(5);  //the pin for the servoc control
  myservod.attach(6);  //the pin for the servod control
  myservoe.attach(7);  //the pin for the servoa control
  myservof.attach(8);  //the pin for the servob control
  myservog.attach(9);  //the pin for the servoc control
  //myservoh.attach(10);  //the pin for the servod control

}

void loop(){
  // Create a client connection
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();

        //read char by char HTTP request
        if (readString.length() < 100) {
          //store characters to string
          readString += c;
          //Serial.print(c);
        }
        //if HTTP request has ended
        if (c == '\n') {

          ///////////////
          Serial.println(readString); //print to serial monitor for debuging

            //select proper header for file to be sent to browser

          client.println("HTTP/1.1 200 OK"); //send new page
          if(readString.indexOf("servosld") >=0) {
            client.println("Content-Type: text/html");
            client.println();
          }

          if(readString.indexOf("slider") >=0) {
            client.println("Content-Type: application/x-javascript");
            client.println();
          }

          if(readString.indexOf("bluev") >=0) {
            client.println("Content-Type: image/gif");
            client.println();
          }

          //select file to send to browser
          if(readString.indexOf("servosld") >=0) {
            File myFile = SD.open("SERVOSLD.HTM");
            if (myFile) {

              byte clientBuf[64];
              int clientCount = 0;             

              while (myFile.available())
              {
                clientBuf[clientCount] = myFile.read();
                clientCount++;

                if(clientCount > 63)
                {
                  client.write(clientBuf,64);
                  clientCount = 0;
                }               
              }
              if(clientCount > 0) client.write(clientBuf,clientCount);           
              myFile.close();
            }
          }

          if(readString.indexOf("slider") >=0) {
            File myFile = SD.open("slider.js");
            if (myFile) {

              byte clientBuf[64];
              int clientCount = 0;             

              while (myFile.available())
              {
                clientBuf[clientCount] = myFile.read();
                clientCount++;

                if(clientCount > 63)
                {
                  client.write(clientBuf,64);
                  clientCount = 0;
                }               
              }
              if(clientCount > 0) client.write(clientBuf,clientCount);
              myFile.close();
            }
          }

          if(readString.indexOf("bluev_sl") >=0) {
            File myFile = SD.open("bluev_sl.gif");
            if (myFile) {

              byte clientBuf[64];
              int clientCount = 0;             

              while (myFile.available())
              {
                clientBuf[clientCount] = myFile.read();
                clientCount++;

                if(clientCount > 63)
                {
                  client.write(clientBuf,64);
                  clientCount = 0;
                }               
              }
              if(clientCount > 0) client.write(clientBuf,clientCount);
              myFile.close();
            }
          }

          if(readString.indexOf("bluev_bg") >=0) {
            File myFile = SD.open("bluev_bg.gif");
            if (myFile) {

              byte clientBuf[64];
              int clientCount = 0;             

              while (myFile.available())
              {
                clientBuf[clientCount] = myFile.read();
                clientCount++;

                if(clientCount > 63)
                {
                  client.write(clientBuf,64);
                  clientCount = 0;
                }               
              }
              if(clientCount > 0) client.write(clientBuf,clientCount);
              myFile.close();
            }
          }

          delay(1);
          //stopping client
          client.stop();

          //process GET string request from client and and position servo

          pos = readString.substring(8, 12); //get the first four characters         
          //Serial.println(pos);
          int n = pos.toInt();  //convert readString into a number   
          Serial.println(n);
          Serial.println();

          if(readString.indexOf("?0") >0) myservoa.writeMicroseconds(n);
          if(readString.indexOf("?1") >0) myservob.writeMicroseconds(n);
          if(readString.indexOf("?2") >0) myservoc.writeMicroseconds(n);
          if(readString.indexOf("?3") >0) myservod.writeMicroseconds(n);
          if(readString.indexOf("?4") >0) myservoe.writeMicroseconds(n);
          if(readString.indexOf("?5") >0) myservof.writeMicroseconds(n);
          if(readString.indexOf("?6") >0) myservog.writeMicroseconds(n);
          //only seven servo pins, so back to myservoa for testing
          if(readString.indexOf("?7") >0) myservoa.writeMicroseconds(n);

          //clearing string for next read
          readString="";
          pos="";
        }
      }
    }
  }
}

Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Pages: [1]   Go Up
Jump to: