Skip navigation

Arduino

3 Posts authored by: oneleggedredcow Top Member

Introduction

Earlier, I saw the post about the BrewPi and I got excited.  I mean really excited.  After reading up on it, I decided that I wanted to give it a shot and build one.  However, there was at least one thing that I wanted to change.  I didn’t want to crack open the fridge and start messing with the circuits inside.  That’s when I came across a blog post describing how to hack a wireless power switch and control it with an arduino.  This sounded much simpler and safer.  Also, I added a light sensor to the netduino in order to detect whether or not the power was on.

Hacking the Remote Control

To modify the remote control, I followed the steps listed here.  Here’s the circuit to build:

IMG_0655_web_anno.jpg

Here are the connections to the remote control:

IMG_0644_web_anno.jpg

Additional details can be found in the original blog post

Writing the Code

The code is fairly straight forward.  We are going to write an event handler so that when the button on the netduino is pressed, our code gets called.  To do this, we declare the button to be an InterruptPort:

 

InterruptPort button = new InterruptPort(Pins.ONBOARD_SW1, false, Port.ResistorMode.Disabled, Port.InterruptMode.InterruptEdgeBoth);

 

Then we add an event handler to the OnInterrupt event:

 

button.OnInterrupt += new NativeEventHandler(button_OnInterrupt);

 

To start out, we are just going to toggle the state of the onboard LED:

 

private static bool isLedOn = false;

private static OutputPort led = new OutputPort(Pins.ONBOARD_LED, isLedOn);

private static OutputPort d0 = new OutputPort(Pins.GPIO_PIN_D0, false);

 

static void button_OnInterrupt(uint port, uint data, DateTime time)

{

  if (data == 0)

  {

    isLedOn = !isLedOn;

    led.Write(isLedOn);

  }

}

 

Now, if we push the button, the LED should turn on, and if we push the button again, the LED should turn off.

 

Our ultimate goal is to turn on/off the remote power supply, so inside the if statement, we will add the following code:

 

d0.Write(true);

Thread.Sleep(100);

d0.Write(false);

 

What this does is that it turns on the d0 (digital 0) output port, sleeps for 100 milliseconds (0.1 seconds), and then turns off the d0 output port.  This is equivalent to pushing the button on the remote control, holding it down for a fraction of a second and then letting it go.

Adding a Sensor to Detect When the Power Is On

If you read a couple of follow up posts to the blog that this is based on, the author ended up ditching the power switches used in this blog for different model so that they could be more certain whether or not the power was on.  Another option is to add a light sensor and detect whether or not the light on the front of the power switch is on.  This short video shows the light turning on when the switch is turned off:

 

 

So, by placing a sensor next to the light, we can determine whether or not the light sensor is on.  The circuit for the light sensor is a really easy one, and looks like this:

 

LightCircuit.png

 

All that is left is some code to read the sensor and write out the value to the screen:

 

AnalogInput a0 = new AnalogInput(Pins.GPIO_PIN_A0);

 

while (true)

{

  int val = a0.Read();

  Debug.Print(val.ToString());

  Thread.Sleep(100);

}

 

By doing this, we can then tell whether or not the light is on, which in turn lets us know whether or not the power is on.

 

Here’s a video demonstration of the project:

 

 

Conclusion

In this project, I hacked into a power switch remote controller in order to control it with a netduino, and implemented a light sensor to make sure that the command that I sent to the power switch was correctly received and executed.  Hopefully, I’ll be able to build upon this and hook it up to a fridge!

 

See Also

Netduino Tic Tac Toe

Introduction

While working on a netduino web server, I really liked the idea of doing some PHP-like preprocessing on the websites before they were returned for the client.  Basically, the netduino would make a fantastic low power web server, and if I wasn’t expecting much traffic – I was thinking something for my own personal use – then it wouldn’t have to scale to a large number of users.

 

So, I wanted to add a mini compiler to it so that I could interpret some basic commands and create a small web page on the fly.  Luckily, I decided to start small and implement tic tac toe.  The basic idea was that the user would play against the server, so it would have to be capable of accepting the user’s choice of which square that they wanted and then make a move.  The server would also have to determine who won or if the game was a tie.

Creating the Board

First off, I needed to create a game board.  Here’s what the board looks like:

Along with the HTML code that creates the board:

 

<html>

<head>

                <title>Tic Tac Toe</title>

                <style type="text/css">

                                div.row {

                                                height: 100px;

                                                width: 306px;

                                                margin-left: auto;

                                                margin-right: auto;

                                }

                                div.col {

                                                float: left;

                                                height: 100px;

                                                width: 100px;

                                                font-size: 85px;

                                                text-align: center;

                                }

 

                                div.center_row {

                                                height: 100px;

                                                width: 306px;

                                                margin-left: auto;

                                                margin-right: auto;

                                                border-top: 3px solid black;

                                                border-bottom: 3px solid black;

                                }

                                div.center_col {

                                                float: left;

                                                height: 100px;

                                                width: 100px;

                                                font-size: 85px;

                                                text-align: center;

                                                border-left: 3px solid black;

                                                border-right: 3px solid black;

                                }

 

                                div.center {

                                                margin-top: 40 px;

                                                text-align: center;

                                }

 

                                button.empty {

                                                height: 85px;

                                                width: 85px;

                                                margin-top: 7 px;

                                }

 

                                button.another {

                                                font-size: 50 px;

                                }

                </style>

</head>

<body>

                <p style="font-size: 100px; text-align: center;">

                                <span style="color: red;">Tic</span>

                                <span style="color: blue;"> Tac </span>

                                <span style="color: black;">Toe</span>

                </p>

                <div class="row" style="margin-top: -50px;">

                                <div class="col">

                                                <?sme $b00 ?>

                                </div>

                                <div class="center_col">

                                                <?sme $b01 ?>

                                </div>

                                <div class="col">

                                                <?sme $b02 ?>

                                </div>

                </div>

                <div class="center_row">

                                <div class="col">

                                                <?sme $b10 ?>

                                </div>

                                <div class="center_col">

                                                <?sme $b11 ?>

                                </div>

                                <div class="col">

                                                <?sme $b12 ?>

                                </div>

                </div>

                <div class="row">

                                <div class="col">

                                                <?sme $b20 ?>

                                </div>

                                <div class="center_col">

                                                <?sme $b21 ?>

                                </div>

                                <div class="col">

                                                <?sme $b22 ?>

                                </div>

                </div>

 

                <div class="center">

                                <?sme $another ?>

                </div>

</body>

 

So, I used the same style as my previous post, where the custom code is enclosed with <?sme … ?> tags.  Also, like last time, I just have a couple of “variables” that are going to get replaced with HTML code.  The variables specify the board position, so the upper left hand square is $b00 and the middle square is $b11.   The $another variable is a place holder to ask the user if they want to play another game.

 

So, this is not really close to a mini compiler at all.  Luckily, I started small with just variables like last time.   I was lucky because the latency on server responses was pretty slow. So slow, that I would claim that it is unusable.  If a variable substitution doesn’t work, then there is no way that interpreting the code and then executing is going to have a reasonable response time.  Therefore, I think that the complier idea is a bust, but I was still able to make a website to play tic tac toe against.

On the server side, it stores the state of the board and every time someone comes to the website they are presented with a blank board like the one above.  This is done by replacing all of the board variables ($b00, etc.), with the following HTML code:

 

<button type=button class=\"empty\" onClick=\"location.href='tictactoe.sme?id=" + _id + "&loc=" + (3*row + col + 1) + "'\" />

 

Where id is a random number assigned to every game (so that the server can track the board state) and the loc is the board position, where the top left is 1 and the top right is 3. When the user clicks the button, they are redirected to a URL that looks something like this (I chose the upper right hand corner):

 

http://localhost:1554/tictactoe.sme?id=1128789054&loc=3

 

When the server receives a URL like this, it looks up the board associated with that id and selects the square for the user specified by the loc parameter.  It then picks a move for itself and returns the board to the user.

Server Opponent AI

I implemented two algorithms for the computer.  The first one I found from this website:

 

http://ethangunderson.com/blog/minimax-algorithm-in-c/

 

It is basically an optimal tic tac toe algorithm, so it will either win or tie.  (It will never lose.)  After a couple of times playing against it, I really wasn’t having any fun, so I decided to implement another strategy for the web server.   This time it would randomly pick a square.  This also didn’t turn out to be much fun to play against, but it was a little bit better because this time I could win!

So here it is in action. The first video is one of me playing against the optimal opponent and the web server is running on my desktop PC:

 

And the second video is of me playing against the random opponent and the web server is running on the netduino:

Conclusion

As you can see from the second video, the lag between me choosing a square and the web server responding is pretty long.  Too long really.  Therefore, I abandoned my original plan of creating a mini compiler and just stuck with my variable substitution code.  If you have any questions about the implementation the code is attached or you can post a question/comment about the project to the comments section. I always like to hear feedback about my projects.

See Also

Netduino Web Server

Netduino Controlled Power Outlet

oneleggedredcow

Netduino File Server

Posted by oneleggedredcow Top Member Sep 4, 2012

Goal:

To write a program to transfer files to/from the netduino SD card to a PC.

Background:

After moving the SD card back and forth from my PC to the netduino, I ran into a problem where the PC claimed that the SD card was locked, but the netduino had no problem writing/reading to it.  This issue, along with the fact that transferring the SD card between the netduino and a PC is a pain, prompted me to write a small program that can transfer files over the network.

Introduction:

The program consists of two parts: a server that runs on the netduino and a client that runs on the PC. The server listens for commands sent over the network and responds to the requests that it receives.  The server is capable of executing the following commands:

 

List – lists the files that are on the netduino SD card

Get – transfers a file from the netduino to the PC

Put – transfers a file from the PC to the netduino

Delete – deletes a file from the netduino SD card

Implementation:

First, in order to be a good citizen, I spawn off a new thread for the file server.  This is because socket calls can take a large amount of time and are blocking, so this leaves it open for something else to be going on.  (This program doesn’t take advantage of that, but future ones might…)  This is the same trick that was used in the netduino web server.

 

When a client makes a request, the first thing that is sent over is the type of request that they wish to perform, one of the four options listed above.  After that a string is sent.  The string is either a directory, in the case of list, or a file name in all of the other cases.  In order to send that, first the length of the string is sent, then followed by the string, so that the server knows when to stop.  The depending on the request that is made, the data is either returned – in the case of list or get, the server waits for more data – in the case of put, or the server executes the command – in the case of delete.

 

The actual transferring of data isn’t that exciting.  In a similar fashion to the way that the file name is sent over, first the size of the transfer is sent followed by the actual data.  The only tricky part was that since the netduino doesn’t have that much memory, I tried to keep the transfer sizes small.  Unfortunately, by doing this, I ran into an issue where the socket buffer is full and I’m trying to make a send call and I get a socket error #10055 (WSAENOBUFS).  The workaround to the problem is to issue a sleep after sending a small amount of data and giving the netduino a chance to clear out its buffer.  This seemed to work very well, but it slows down the transfer of the file.  I also tried sending larger chunks of data, but I had much better luck with adding a sleep.

 

The client code is a mirror of the server code, so there isn’t really anything new in there.  If you have any questions, check out the attached code or ask a question in the comments.

Summary:

In this post, I described a simple netduino file server that can be used to quickly and seamlessly transfer data between the netduino and a PC over Ethernet.  This means that you don’t have to remove the SD card every time you want to transfer data back and forth.  This also opens up the possibility to use the netduino a network file store to transfer files back and forth between home and work.  However, if you are going to do that, you should probably put some kind of authentication so that a third party can’t steal or delete your data.  (Potential future project!)

See Also:

Netduino Web Server

Netduino Tic Tac Toe

Filter Blog

By date: By tag: