1

I want to remote control sockets in my room manual without an extra library on my raspberry pi. I want to use the UART interface in C. The socket has 433 Mhz receiver and I use a 433 Mhz transmitter. In other librarys you type something like this: send 11111 1 1. (socket code, socket number, condition). But how to format this command in C with the write() function? The 10 is for number of characters. I use this code below. I tested the output via minicom, that works fine. But how the receiver now knows that it was adressed?

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

int main(int argc, char ** argv) {
  int fd;
  // Open the Port. We want read/write, no "controlling tty" status, and open i$
  fd = open("/dev/ttyAMA0", O_RDWR | O_NOCTTY | O_NDELAY);
  if (fd == -1) {
    perror("open_port: Unable to open /dev/ttyAMA0 - ");
    return(-1);
  }

  // Turn off blocking for reads, use (fd, F_SETFL, FNDELAY) if you want that
  fcntl(fd, F_SETFL, 0);

// Write to the port
  int n = write(fd,"11111 1 1",10);
  if (n < 0) {
    perror("Write failed - ");
    return -1;
  }

  // Don't forget to clean up
  close(fd);
  return 0;
}
Sven
  • 11
  • 1
  • I guess: the protocol provide a filed where you have to put slave address. Or the communication is 1->1 (like RS232) and only 1 slave is allowed. – LPs May 14 '15 at 07:48
  • Well I would assume that each socket has a different code, right? – user253751 May 14 '15 at 08:29
  • yes the main code is 11111 and the socket code goes from A to E, so the A is a 1 I think. and then the condition 1 for "on". – Sven May 14 '15 at 08:46

1 Answers1

0

Do I get it right: you send a command via UART to transmit a message via radio trasmitter. Sniffing the UART output proves your code OK in the sense that TX line sends what you want it to send in the software, and the actual question is "how the rx module gets that message/how to make the rx module get it?"

If so, the main question is what kind of radio TX/RX or TRXs do you actually use (I mean chip/module codes, like CC1120, NRF2401 etc.)? There are plenty of 433MHz radiomodules available, I suppose consulting their datasheet first or at least posting the part number here is the right way to go.

alagner
  • 3,448
  • 1
  • 13
  • 25
  • Yes exactly. I tested the output from the computer pin to the TX, but not from the antenna of the TX to the RX.. I don't know how to send it (in a String or single characters, with space or without..) I use this type of TX: http://goo.gl/HP5PEc . And the sockets are normal remote sockets with 433,92 MHz., they all have the same code i can change, also the remote control has this code so the socket knows that it is adressed and they have a socket number A to E. So the first socket would be adressed like this: 11111(code for all sockets) +1(A=1 as number 1) and 1 (for on). – Sven May 14 '15 at 08:42
  • Is it right that when I power and ground the TX and send data to it, it will immediately transmit it? Just to be sure that it will be transmitted. – Sven May 14 '15 at 08:51
  • All I can recommend now is to try to wire the RPI with the remote control of the sockets or replace the radiomodules inside the sockets with the RXs you have and add some logic to control it. I'd go for the first solution due to safety and (probably) lesser complexity. What you're trying to achieve now is rather impossible without the knowledge of exact RF params of sockets and your TX, and even in such case it's imho not worth the effort. – alagner May 14 '15 at 08:58
  • Tried to edit the previous comment but it was too late: The TX you have is supposed and designed to work with its RX and generally nothing else, so changing the approach to the problem seems to be the best solution here. – alagner May 14 '15 at 09:05
  • No, the TX works with the sockets. I tested it with some python librarys but I thought it would work directly with the UART too, maybe it does. I will search for C library and test a little bit around with it. Thanks for your help, maybe it will work one day ^^. – Sven May 14 '15 at 09:44
  • If so, I suggest looking into Python libs and checking the way it works there. I don't think direct UART will work, 433.92MHz is too popular band to risk random switchings due to transmission errors, so I believe there is (or should be at least) some sort of highler level protocol. – alagner May 14 '15 at 09:56
  • Ok I found a C++ library called rc-switch pi (https://github.com/r10r/rcswitch-pi). With this it works. – Sven May 14 '15 at 11:14