Thursday, July 17, 2014

Note to future self

Today is a regular day of July 2014 with the exception that I am writing a few lines for my future self.
These days the technology is moving very fast. Everyone has or want what is called a smart phone. The market is dominated by Apple iPhone and Google Android. In a not so close third place there is Microsoft with the windows phone.
Apart of phones home electronics is getting everyday more strong.
Just wanted to tell myself that no matter how cool technology may be there are other things more important. As am engineer we need to understand that at the other end of any software or hardware there is a person.  Is on those humans that we need to think.
When you read this,  Samsung may not have any galaxy phone but there will be people and your family will be at your side.
Keep what is worth, there is no undo checkout for missing life moments. Today I am a happy person and for sure in the future will still be.
The crime is worst every day like the economy.  We are holding up for better days to come. Always happy and together with the family.

Tuesday, July 15, 2014

Remote Alert Server is public

Just as mentioned on a previous blog post I opened the Remote Alert server code. The repository is at my github. It is a Flask app ready to be used on heroku.
Heroku may look complicated for a windows developer but if you give it a chance and try other hosting turns out that heroku is not that hard.
I like it because the code is pretty clean and without much modifications.
The code is at:
https://github.com/soynerdito/RemoteAlert_Server

Latter on the android code will be published as well and hope to inform in a post about it.
Fork it, copy, sell, modify. Do what ever you like with the code. No need to even mention where you got it. Do as you like.

Saturday, May 24, 2014

How to send notifications from Raspberry Pi to your Android Phone (Free!)


Ever wanted to send notifications from your hardware project to your phone but didn't wanted to code it all up. Well I wanted to do the same so I created a simple app to do just that. 
A few simple steps and you will be pushing on custom messages into your phone. There is no registration required and the app has the least permissions to get the job done. There is no user information saved on the server, just the id you see on the screen and the Google registered id of the device so a message can be sent to it.

How to? 
1. Install RemoteAlert app from the android market. The app will show a big orange number on the main activity. 
2. All needed to do is to do a HTTP post with that number and you should get a message on the notification of the phone. Message may take up to a minute to arrive. There is nothing I can do to send it faster, it is just the way it is.
3. Refer to the samples bellow on how to send the HTTP Post to make it work.
 RemoteAlert




Remember to replace the Id with the id from your own device!

Python sample code:
import json
import urllib2

class RemoteAlert:
    def send(self, device_id, message ):
        data = { 'message' : message }
        url = 'http://remote-alert.herokuapp.com/post/' + device_id
        req = urllib2.Request( url )
        req.add_header('Content-Type', 'application/json')
        urllib2.urlopen(req, json.dumps(data))
        return 'OK'

ra = RemoteAlert()
dev_id = 'a32b831c-7623-4c43-99cf-b614ff54e902'

print ra.send(dev_id, 'Hellow Push Message World')


Push message sent from the shell using curl
curl -X POST -H "Content-Type: application/json" http://remote-alert.herokuapp.com/post/a32b831c-7623-4c43-99cf-b614ff54e902 -d '{"message":"Hellow Push Message World"}'

The above curl sample you can send a notification to your phone, just replace the long id with the one from your phone and write in your message.


These two samples sends exactly the same message. There is so much that can be done to improve this and get a better solution but a friend said to me once "Forget about it, ship it" and keeping the spirit better to have something than nothing. Improvements will be latter added depending in my needs and if the community request it.

Future plans:
1. Formal documentaion
2. Sample codes for different platforms and languages
3. Make it all open source for references. The android app is pretty much the same code from the Google push notification sample and the hosting is on heroku using a free account. I do not know how long this will work if I get too many users and reach the hosting limit. However I feel pretty confident this will hold it's ground and the plan is to keep it free so anyone can have push notifications from their respective projects.


Update:
Server Source Code Posted at:
https://github.com/soynerdito/RemoteAlert_Server

Tuesday, October 1, 2013

Today I got a Beaglebone Black

Just a few hours ago it finally arrived. My long wait for a Beaglebone Black is finally over. This is a BB that actually has some future. 
There is something about a small and slow Linux computer that got me hook. It is too soon to actually doing something useful with it, so there is not much to say. The only news for now is that I just realized how used I am to Debian. After a few years with Ubuntu and now Mint you get used to a lot of the "helpers" Ubuntu provides, there is so much Debian does for you. The Beaglebone uses Angstrom as the default Linux distro and it is quite different. A few years ago (about 10+) I tried Gentoo and told myself there is now way I go back to that and now I am very close to it once again. It will be interesting using a different distro. The small beagle has very limited resources and I want the power on my apps not in the OS, so Angstrom will stay.
Hope to get back with a new post with more progress.
Right now I will be upgrading the factory firmware and setting up the wireless.

Update:
Few days with the Beaglebone now. For the wifi tried successfully with debian from the sdcard however not from the eMMC with the Angstrom. On Angstrom I manage to get the usb wifi recognized but not associated with my network. Still not giving up, tried with two different usb dongles. Will continue banging my head with the wifi until success is achieved.

Update:
Finally make it work the wifi on the beaglebone black on Argnstrom. Used the tutorial from adafruit http://learn.adafruit.com/beaglebone/wifi with a modification for my WPA2 network. Just put the Security on the wifi.config as wpa like shown bellow.

[service_home]
Type = wifi
Name = ssid
Security = wpa
Passphrase = my_password


Friday, July 19, 2013

Object seriliazitation with Type attributes in the nodes.

Here is a problem that I had while consuming a service in C#.

What I needed to do:
I got a service from a vendor. This service is an XML post to a PHP page (btw, I hate these).  This vendor provided me with a sample XML that would be the package sent. I need to populate every node with my data and make the call. I would much rather have an object to do this, so I created one. I serialized my object and prepared for the posing of it. When comparing my result with the provided XML I was missing attributes on my object so I started looking on how to add them.

My vendor provided XML was similar to this:

<?xml version="1.0" encoding="utf-16"?>
<user>
  <FirstName Type="string">Soy</FirstName>
  <LastName Type="string">Nerdito</LastName>
  <IsPaying Type="boolean">True</IsPaying>
</user>

My Serialized user object looked like this:

<?xml version="1.0" encoding="utf-16"?>
<user>
  <FirstName>Soy</FirstName>
  <LastName >Nerdito</LastName>
  <IsPaying>True</IsPaying>
</user>

This looks simple enough to add the type to the node on my object so when serialized they are made identical. Duh! could not find a good solution for this. Most posts online were talking about doing a replace on the serialized string of each node to add the type. This requires me to add code to that section every time I create a new object property. Well I'm lazy and did not wanted to do that, so something else was needed.

The Solutions:
Manipulate the XML attributes that I had on my object to allow the serializer to do its job. Well, that was not an easy find either. So here is what I did.
.NET allows you to do this, add attributes to your nodes, but your objects must be constructed a certain way. So let the fun begin.

The Code:
First you need to create an object that will be the type for each one of your properties. Why? because natives will not cut it when the serializer gets a hold of them. So I decided to create a generic object that will accommodate anything I throw at it and it will take the type of the value all by itself (lazy, remember!).

Namespaces:
 You need these.
using System.Xml.Serialization;
using System.Xml;

Each of my nodes will be of this type:


    public class SingleNode<T>
    {
        public SingleNode(){}

        public SingleNode(T val)
        {
            value = val.ToString();
            //This is like so to remove the System. definition from the Type
            Type = val.GetType().ToString().Replace("System.""").ToLower();
        }

        [XmlText]
        public string value;

        [XmlAttribute("Type")]
        public string Type;
    }

 The XMLText and XMLAttribute define what the serializer will do when working with this. My constructor is generic so I should be able to pass any object type, I want to do ENUMS also but have not tried yet.

Now that you have the base object lets create User:


    [XmlRoot("user")]
    public class user
    {
        [XmlElement("FirstName")]
        public SingleNode FName = null;

        [XmlElement("LastName")]
        public SingleNode LName = null;

        [XmlElement("IsPaying")]
        public SingleNode IsPaying = null;
    }


The XMLRoot defines the name for the root node once is serialized.  The XMLElement attributes defines the node name after is serialized. Note that this is not the property name during design time and run time. You must reference the object in your code as user.FName, user.LName and so on. Each of the properties are defaulted to NULL but you can create the object instance there is you so pleased and assign a default value.

To create an instance of this is quite simple:


            user test = new user();
            test.FName = new SingleNode<string>("Soy");
            test.LName = new SingleNode<string>("Nerdito");
            test.IsPaying = new SingleNode<bool>(true);


You can see the instantiation assigns the object type because the constructor is generic.

The serializer:
If you do not have it, here is how to serialize it.
            XmlSerializerNamespaces xns = new XmlSerializerNamespaces();
            xns.Add(string.Emptystring.Empty); 
            
            //xml format settings
            XmlWriterSettings xmlSettings = new XmlWriterSettings();
            xmlSettings.Indent = true;
            xmlSettings.NewLineOnAttributes = false;
            xmlSettings.NamespaceHandling = NamespaceHandling.OmitDuplicates; 
            XmlSerializer mySerializer = new  XmlSerializer(typeof(user));
            StringWriter ms = new StringWriter();
            XmlWriter xw = XmlWriter.Create(msxmlSettings);
 
            mySerializer.Serialize(xwtestxns); 
            string serializedString = ms.ToString();
            ms.Close();

Conclusion:
This is it, simple enough to create the objects when you need to add a particular attribute. The attribute does not have to be the data type, you can change this to be anything you need it to be. .NET can add a DataType but if you service does not "play" with that, here you have an alternative. 

Would there be other ways of doing this, maybe but I could not find it. Please let me know if you have any improvements.

Why with all the new ways of creating web services people still write services that work with a post and do not have a WSDL or at least provide XSDs is beyond me, but hey not everyone can be a Nerdito.