Home automation aka Domotica

Now thats an idea :slight_smile:

Though maybe overkill as they would be only a couple meters separated.

Is xbee reliable?

Yep, XBees are very reliable.

Very very reliable. I have a PRO 60mw set here. Configured to high transfer mode (I know it’s not allowed… shhhhhhhh :-[)

And I have done a test with the cobra inside the house transferring and walking through the backyard with the domino which was recieving. All the time the data was perfectly transferred.

Make sure u set baud rate to a higher value then 9600.

Not only is Xbee very reliable, but you can configure it to encrypt the transmissions and limit network joining. This is very helpful if you use them to do something like opening your garage door, or are just worried about people listening in on the temperature of your house

Yea, they might think, it’s cuddly warm in there, let’s warm up at his house :smiley:

I made the mistake of not protecting access to a web site to control my TV. I wasn’t a problem until I thought I’d show off my temperature monitoring site. My brother was on the phone with the person looking at my temperature site (explaining how crazy I am) when suddenly my TV channels started changing. She was very disappointed when I killed the site until I got password protection enabled.

Hahahaha that is one cool example of unprotected domotica :smiley: (although you will not agree :D)

Awesome, let the community decide what i’ll be watching on tv tonight :smiley: :smiley:
(lemme first get rid of those 1000+ porn sat channels)

Satellite TV ROCKS (no, not because of the pr0n) :smiley:

Hahaha, what did they change the channel to?

Since I think cheap is a virtue, I didn’t have cable or anything at the time (I eventually caved, but I wouldn’t mind getting rid of it). So all they had the option of doing was selecting the various over the air channels or video inputs, or turning the TV on and off. But when you’re playing a game on your xbox, that can be highly annoying. Honestly, I was laughing through it, since my brother first thought the TV was dieing.

Anyone here with good networking socket experience?

Awesome: [url]Samsung Home Watcher Demo (IFA 2010) - YouTube

Surely there’s a pricey tag attached ???

You can do that with FEZ Cobra easily. Even easier, use smaller FEZ that is connected to your network then you can use your cell to control the house. You can even control your house from ANYWHERE…thanks to the internet :slight_smile:

[quote]Anyone here with good networking socket experience?
[/quote]

There are several members who can help you with sockets. Ask away!

Ok, here goes. I have the following assembled from various places on the internet. But I can’t figure out how to send a message on a button click. Can someone give me some help here?


    private byte[] data = new byte[1024];
    private int size = 1024;
    private Socket server;

    public void AsyncTcpSrvr()
    {
      try
      {
        server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        IPEndPoint iep = new IPEndPoint(IPAddress.Any, 9050);
        server.Bind(iep);
        server.Listen(5);
        server.BeginAccept(new AsyncCallback(AcceptConn), server);
      }
      catch (SocketException e)
      {
        Console.WriteLine(e.ToString());
      }
    }

    void ButtonStopOnClick(object obj, EventArgs ea)
    {
      Close();
    }

    private void button1_Click(object sender, EventArgs e)
    {
      // how do i send data on button click?
    }

    void AcceptConn(IAsyncResult iar)
    {
      try
      {
        Socket oldserver = (Socket)iar.AsyncState;
        Socket client = oldserver.EndAccept(iar);
        SetStatusText("Connected to: " + client.RemoteEndPoint.ToString());
        string stringData = "Welcome to my server";
        byte[] message1 = Encoding.ASCII.GetBytes(stringData);
        client.BeginSend(message1, 0, message1.Length, SocketFlags.None, new AsyncCallback(SendData), client);
      }
      catch (SocketException e)
      {
        Console.WriteLine(e.ToString());
      }

    }

    void SendData(IAsyncResult iar)
    {
      try
      {
        Socket client = (Socket)iar.AsyncState;
        int sent = client.EndSend(iar);
        client.BeginReceive(data, 0, size, SocketFlags.None, new AsyncCallback(ReceiveData), client);
      }
      catch (SocketException e)
      {
        Console.WriteLine(e.ToString());
      }
    }

    void ReceiveData(IAsyncResult iar)
    {
      try
      {
        Socket client = (Socket)iar.AsyncState;
        int recv = client.EndReceive(iar);
        if (recv == 0)
        {
          client.Close();
          SetStatusText("Waiting for client...");
          server.BeginAccept(new AsyncCallback(AcceptConn), server);
          return;
        }
        string receivedData = Encoding.ASCII.GetString(data, 0, recv);
        SetMessageText(receivedData);
        byte[] message2 = Encoding.ASCII.GetBytes(receivedData);
        client.BeginSend(message2, 0, message2.Length, SocketFlags.None, new AsyncCallback(SendData), client);
      }
      catch (SocketException e)
      {
        Console.WriteLine(e.ToString());
      }
    }

The code you showed is for the PC side? Async sockets are not supported under MF.

  1. Generally, I do not use the async for sending. I use the blocking method Send(). Unless the internal buffers are full, the Send() method returns right away. Async send is used in more advanced high volume solutions. I recommend you use Send() until you recognize a situation where you need async.

  2. In your AcceptConn callback you are not saving the client socket reference. At an unexpected time, your socket could be closed by garbage collection. Save it as “private Socket client;”.

  3. In the AcceptConn callback, do a client.Send() followed by a BeginReceive().

  4. get rid of SendData method.

  5. In your ReceiveData method, after you have handled received data, issue another BeginReceive().

  6. To send on a button click, just do a Send().

Again, the above only applies to the PC side .NET.

For the Cobra side, you will need a thread that does a blocking socket read. A different architecture than shown.

Mike, thanks for the enlightening answer. And yes, the code was for the PC side.

Are there other options to create non-blocking bidirectional communications over ethernet between a PC and a Cobra?

On the PC side there are other methods of doing async .NET sockets, but they are for high volume servers. You would not want to use them for PC ↔ FEZ.

MF does not have any non-blocking calls or events for sockets. I usually use a thread with an outstanding Receive(). If you are concerned with too many sockets and threads, you could use one thread and the Poll() method.

On the FEZ side, why would a seperate thread with a blocking read be a problem?

If you have a blocking read active on a socket, you can still use the socket to send to the remote end in another thread.

Mike,

I followed your advice and got it working.
On the pc side i have a threaded listening socket and a function that connects and send the message to the other side (Cobra) using plain blocking sockets.

The class I build on the pc side can be used also on the Cobra.

Thanks for the pointers.

Greetings