Thursday, January 16, 2014

Raspberry Pi - Controlling 433 MHz remote outlets

In my previous post here, I tried to use a cheap 433 MHz transmitter to replicate the 433 MHz remote functionality but landed up hacking the remote to send the codes instead. The main issue I was facing there was that the code was not being recognized by the RF outlets.

I was finally able to resolve that issue so I just need one GPIO pin to control all my RF remote outlets now!

Troubleshooting my original issue:

I am using this receiver and transmitter - 433MHz Tx-Rx pair (set of 2)








I was using the RCSwitch library ported over to the Raspberry Pi from here
http://ninjablocks.com/blogs/how-to/7506204-adding-433-to-your-raspberry-pi

As mentioned in my previous post, I first tried using the Audacity route but I could not decode the data correctly.

Since I had an Arduino lying around as well, I downloaded the RCSwitch library for the Arduino and tried the advanced receiver demo.

The output for one switch of my remote looked like this:
Protocol: 1
Decimal: 1398209
binary: 000101010101010111000001
PulseLength: 207

I noticed that the pulse length was 207 which is different from the default length in the code. I quickly changed the pulse length in the send demo sketch on the Arduino and was able to get the RF outlet to switch on!

So I went back to my Raspberry Pi and modified the send.cpp code to look like this:
int main(int argc, char *argv[]) {
    /*
     output PIN is hardcoded for testing purposes
     see https://projects.drogon.net/raspberry-pi/wiringpi/pins/
     for pin mapping of the raspberry pi GPIO connector
     */
    int PIN = 0;
    unsigned long code = atoi(argv[1]);

    if (wiringPiSetup () == -1) return 1;
        printf("sending code[%d]\n", code);
        RCSwitch mySwitch = RCSwitch();
        mySwitch.enableTransmit(PIN);
        mySwitch.setPulseLength(207);
        mySwitch.send(code,24);
        return 0;
}

Note: I have removed the header files for a cleaner code look below. Please include them in your actual code if you want to use the code below.



I have the transmitter data pin connected to the Pi pin 11 (GPIO 17). I re-ran make in the dir and sent the previously decoded code:
sudo ./send 1398209

And it works! Good riddance to the multiple GPIO pins to replicate the remote functionality +  the remote is free for use independently now.


Monday, January 6, 2014

Arduino Yun - Introduction + Controlling GPIO via a web server

My Arduino Yun arrived over the weekend and I was surprised by how small it was compared to my Pi or Galileo. It is roughly the size of a credit card (see below).



Some of the things I really like about this:

  • Built in Wifi, micro SD card reader and  Linux OS (Linino)
  • Small form factor - ideal for applications that I am interested in like robotics and home automation
  • Easy Wifi setup in any new location
  • Ability to program sketches over Wifi and use the console terminal wirelessly to see events happening in the sketches.

This is a great guide to get started: http://arduino.cc/en/Guide/ArduinoYun. I used it to setup WiFi and allow open REST API access.

My first goal was to set up a USB webcam and install motion on my Yun. I achieved this by using the package management to install the necessary driver and packages
opkg install kmod-video-uvc motion

I plugged in my webcam via a USB hub and I was happy to see that it showed up after using dmesg and lsusb.

I started up motion and was able to see that it streams to a Firefox browser seamlessly. Goal #1 accomplished.

Next, I wanted to use a web browser to control GPIO to enable my next project which is a web controlled robot. So I wrote a simple sketch that uses browser buttons to turn the on board LED on and off. The sketch can be downloaded  here.

To get this to work I had to do one step that was not clearly documented anywhere. I had to manually create a soft link in my /www/ folder for sd:
ln -s /mnt/sda1 /www/sd

Another key thing to note here, is that your sketch folder needs to have a www sub folder with the index.html and zepto.min.js file in it. When your sketch is uploaded via WiFi, the contents of the www folder and automatically copied to /www/sd/<sketchname> folder

The REST calls to /arduino are made from the index.html file. To undestand what the REST API is, I would encourage you to read up on it in the getting started URL mentioned above.

If you view the source of the html, you will see this:


<script type="text/javascript">

    function ledon()
   {
       $('#content').load('/arduino/ledon');
   }
   function ledoff()
  {
      $('#content').load('/arduino/ledoff');
  }

</script>


It basically passes the ledon/ledoff commands to the Arduino once the buttons are pressed. The sketch simply looks for this and manipulates the GPIO:


if (command == "ledon") {
       digitalWrite(13, HIGH);
 }

else if (command == "ledoff") {
       digitalWrite(13, LOW);
}

After you have uploaded the sketch over to your Yun, you can use any browser to go to http://your_yun_name.local/sd/webled

You should see these buttons which will allow you to control the on board LED.


Hope this helps others who are trying to attempt something similar.