Thursday, April 17, 2014

[Arduino] GPS Tracker, Problem

Last week, I assembled a few different arduino modules together, namely the Arduino GPS module, GPS antenna, Arduino UNO board, and a 16X2 LCD screen together to form the initial proof of concept of my little project named, GPS Tracker. It can be found here.

The idea behind the GPS tracker is simple, the GPS module will keep on looking for satellite and attempt to lock on as soon as possible. And the device will unscramble NMEA GPS code from the Arduino GPS module, and I will then make sense of those data and feed on to a Arduino GSM module and send data over to my little server backend that will store the data. I am almost 50% complete with the backend services required to store any communication from the GPS tracker and also a good frontend that makes up of a Google Maps which will then plot the data into a more sensible presentation onto the map.

However, I ran into a small little problem. Which is quite simply, I believe this might be the limitation of Arduino board as a whole, while it's designed to be simple in architecture, it's not going to have parallel processing. What I mean here, is that while I am processing the data from the GPS module. It's almost impossible to use the same Arduino board to send any HTTP signal over GSM. As those process will either hang the GPS signal processing or risk having the HTTP signal lost somewhere and have constant timeout.

But I am not giving up so soon, I found out that, I may actually be able to put 2 Arduino to work together! And after brief Google search, below are some of the searches that seem promising.

Hacknmod.com
Marcoramilli.blogspot.com



So, stay tune while I am waiting for my next Arduino board to arrive and I shall tinker with it!

[Arduino] Auto Fish Feeding Video

Last few weeks, I managed to put together an Auto Fish Feeding machine using Arduino as the base controller. You may find it here. And this week, I am going to show you guys in video how it feeds the fish!




I tuned the amount of feeds to go through the hole by trial and error, and found out that if I set the duration to 6 hours between each feeding, the fishes will be more than happy! As evident to that, it's that the fish population actually grows from previous 8 to now 15!

Monday, April 14, 2014

[Dropbox] Finally, I got the Pro Account 100GB

The DropBox poison!

I had upgraded my Dropbox account to a Pro account due to my visitors' generous support using me as their referral for Dropbox account, in which quite a number of them are Pro users as well. Giving me 1GB free! Sweet! And this only has one effect, which is leading me to use Dropbox and rely on the service even more and thus until one day, Dropbox prompt me that I have an additional 4.5GB worth of photos to upload! I was left with no choice but to upgrade my service to Pro 100GB at an affordable price of roughly RM325/year, which is USD $99/year. 

If you want me to be your referral, I would be greatly pleased! Here you go! 

My Referral Account! db.tt


The upside to this, is that I basically don't have to carry any of my hard disk around and yet, still can keep my daily files accessible. Besides that, I don't have to worry about disk breakdown. So far, Dropbox has not fail me yet on this. Hopefully in the future where I am still their customer, they won't fail me. Please, my files are important to me. Don't fail me.

And thanks peeps! Sweet 29.88GB freebiess!



Monday, April 7, 2014

[Arduino] GPS Coordinate Live Tracking!

About a month ago, I have this idea. To build a simple vehicular tracking device that is able to give accurate GPS information when driving about the town over the cloud. And so, I ordered a couple of Arduino shields and parts from myduino.com. The delivery was swift and quick, usually if you order and paid fully by today morning, you should get it shipped to your doorstep the second day afternoon. Assuming they have the stocks ready.

To get started, I ordered the following parts.
1. Arduino GPS Shield v1.1
2. GPS Active Antenna



3. Arduino UNO


4. Arduino LCD Shield



Then I went on to search for its demo and example. I spent a good 2 weeks scouring the internet looking for clues on who has this shield and already got it working. I was excited to know that there is  a link in myduino.com that links to the people who knew what they were doing. And I bought the GPS Shield without even researching further. But when I got the shield, I try to go to iteadstudio.com link. It brings me to their homepage. The link is dead, outdated. I have to search the site to get to the content. But when the content shows up, it's not running on an Arduino! It is using C language. When I thought all hope was lost, I pressed on and found forums that explains things and also am delighted to know that many people are also having the same headache with me while using the same GPS shield.

There were a couple of codes posted freely on one of the forum that works for others but not for me. Attached, is the code that works for others, but not me.

GPS Data Logger
#include 
const int chipSelect = 10;
void setup()
{
  Serial.begin(9600);
  
  pinMode(10, OUTPUT);
  if (!SD.begin(chipSelect)) {
   
    return;
  }
 
}

void loop()
{
  // make a string for assembling the data to log:
  char index = 0;
  char temp = 0;
  String dataString = "";
  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  /*
    while(Serial.available())
    {
      File dataFile = SD.open("datalog.txt", FILE_WRITE);
      if(dataFile)
      {
        temp = Serial.read();
        dataString += String(temp);
        dataFile.print(dataString);
        dataString = "";
        dataFile.close();
      }
    }
  */  
File dataFile = SD.open("datalog.txt", FILE_WRITE);
 if(dataFile)
 {
   while(Serial.available())
   {
     temp = Serial.read();
     dataString += String(temp);
     index++;
     if(index>200)
       break;
   }
   dataFile.print(dataString);
   dataFile.close();
 }
} 



And another example that didn't work for me whatsoever.

#include 

#include 

/* This sample code demonstrates the normal use of a TinyGPS object.
   It requires the use of SoftwareSerial, and assumes that you have a
   4800-baud serial GPS device hooked up on pins 3(rx) and 4(tx).
*/

TinyGPS gps;
SoftwareSerial nss(1, 0);//tx and rx pins in my arduino GPS shield

static void gpsdump(TinyGPS &gps);
static bool feedgps();
static void print_float(float val, float invalid, int len, int prec);
static void print_int(unsigned long val, unsigned long invalid, int len);
static void print_date(TinyGPS &gps);
static void print_str(const char *str, int len);

void setup()
{
  Serial.begin(115200);
  nss.begin(38400);//baud rate for the GPS shield
  
  Serial.print("Testing TinyGPS library v. "); Serial.println(TinyGPS::library_version());
  Serial.println("by Mikal Hart");
  Serial.println();
  Serial.print("Sizeof(gpsobject) = "); Serial.println(sizeof(TinyGPS));
  Serial.println();
  Serial.println("Sats HDOP Latitude Longitude Fix  Date       Time       Date Alt     Course Speed Card  Distance Course Card  Chars Sentences Checksum");
  Serial.println("          (deg)    (deg)     Age                        Age  (m)     --- from GPS ----  ---- to London  ----  RX    RX        Fail");
  Serial.println("--------------------------------------------------------------------------------------------------------------------------------------");
}

void loop()
{
  bool newdata = false;
  unsigned long start = millis();
  
  // Every second we print an update
  while (millis() - start < 1000)
  {
    if (feedgps())
      newdata = true;
  }
  
  gpsdump(gps);
}

static void gpsdump(TinyGPS &gps)
{
  float flat, flon;
  unsigned long age, date, time, chars = 0;
  unsigned short sentences = 0, failed = 0;
  static const float LONDON_LAT = 51.508131, LONDON_LON = -0.128002;
  
  print_int(gps.satellites(), TinyGPS::GPS_INVALID_SATELLITES, 5);
  print_int(gps.hdop(), TinyGPS::GPS_INVALID_HDOP, 5);
  gps.f_get_position(&flat, &flon, &age);
  print_float(flat, TinyGPS::GPS_INVALID_F_ANGLE, 9, 5);
  print_float(flon, TinyGPS::GPS_INVALID_F_ANGLE, 10, 5);
  print_int(age, TinyGPS::GPS_INVALID_AGE, 5);

  print_date(gps);

  print_float(gps.f_altitude(), TinyGPS::GPS_INVALID_F_ALTITUDE, 8, 2);
  print_float(gps.f_course(), TinyGPS::GPS_INVALID_F_ANGLE, 7, 2);
  print_float(gps.f_speed_kmph(), TinyGPS::GPS_INVALID_F_SPEED, 6, 2);
  print_str(gps.f_course() == TinyGPS::GPS_INVALID_F_ANGLE ? "*** " : TinyGPS::cardinal(gps.f_course()), 6);
  print_int(flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0UL : (unsigned long)TinyGPS::distance_between(flat, flon, LONDON_LAT, LONDON_LON) / 1000, 0xFFFFFFFF, 9);
  print_float(flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : TinyGPS::course_to(flat, flon, 51.508131, -0.128002), TinyGPS::GPS_INVALID_F_ANGLE, 7, 2);
  print_str(flat == TinyGPS::GPS_INVALID_F_ANGLE ? "*** " : TinyGPS::cardinal(TinyGPS::course_to(flat, flon, LONDON_LAT, LONDON_LON)), 6);

  gps.stats(&chars, &sentences, &failed);
  print_int(chars, 0xFFFFFFFF, 6);
  print_int(sentences, 0xFFFFFFFF, 10);
  print_int(failed, 0xFFFFFFFF, 9);
  Serial.println();
}

static void print_int(unsigned long val, unsigned long invalid, int len)
{
  char sz[32];
  if (val == invalid)
    strcpy(sz, "*******");
  else
    sprintf(sz, "%ld", val);
  sz[len] = 0;
  for (int i=strlen(sz); i 0) 
    sz[len-1] = ' ';
  Serial.print(sz);
  feedgps();
}

static void print_float(float val, float invalid, int len, int prec)
{
  char sz[32];
  if (val == invalid)
  {
    strcpy(sz, "*******");
    sz[len] = 0;
        if (len > 0) 
          sz[len-1] = ' ';
    for (int i=7; i= 1000 ? 4 : vi >= 100 ? 3 : vi >= 10 ? 2 : 1;
    for (int i=flen; i


And all I got was this

Sats HDOP Latitude Longitude Fix  Date       Time       Date Alt     Course Speed Card  Distance Course Card  Chars Sentences Checksum          (deg)    (deg)     Age                        Age  (m)     --- from GPS ----  ---- to London  ----  RX    RX        Fail--------------------------------------------------------------------------------------------------------------------------------------**** **** *******  *******   **** *******    *******    **** ******* ****** ***** ***   0        0.00   ***   173   0         0        **** **** *******  *******   **** *******    *******    **** ******* ****** ***** ***   0        0.00   ***   310   0         0etc...

Which is not very helpful, and I thought my GPS shield and antenna is not working at all! Though, there is data steaming over. It's still not helpful at all.

Until I read the specification document and realized that RX and TX pin is really 0 and 1 and because I keep reading the comments saying those are hardware serial and you cannot read it with SoftwareSerial. But I went ahead and tried it, I begin to see more data steaming through, that's when I confirmed my suspicion as there was no data coming through with RX and TX set to 3 and 4. Either those people have no idea what they are talking about or they are just trolling with noobie like myself.

So, the following are the code that works for me, and hopefully for you as well.
-Deleted, please email me to ask for the piece of code-

What this set of code does, it's that it will continously listen to TX and RX 1 in Software Serial with baud rate of 38400. And will outpout NMEA codes, http://aprs.gids.nl/nmea/ will give you certain understanding of GPS data works and all its jargon!

While I was having trouble with my improved piece of code, I began to see more data and I am thinking, may be it's because of the type of data the satelite around my area is transmitting is not the same as those people who developed the initial codebase. So there is no confirmed lock on satelite and may be those information is not available when your GPS module is having a hard time trying to lock on to multiple satelite. That's when I modified my code to scour whatever information that is available to me. Namely, the current detected latitude and longitude and try to parse them to make sense of the data. Before long, I was able to accurate get my current location in latitude and longitude!

Which this piece of code plays a vital role in describing those data into usable information.
  String latitude = getValue(stringOne, ',',3);
  String degreeLatitude = latitude.substring(0,2);
  String minuteLatitude = latitude.substring(2);
  
  char floatbuf[32]; // make this at least big enough for the whole string
  minuteLatitude.toCharArray(floatbuf, sizeof(floatbuf));
  float latitudeminuteInDecimal = atof(floatbuf)/60;
  
  char buffer[32];
  String latitudeMinuteOuput = dtostrf(latitudeminuteInDecimal,10,10,buffer);
  
  lcd.setCursor(0,0);
  
  if(degreeLatitude != "" && minuteLatitude != "")
  {
    lastKnowLat = "Lat:"+degreeLatitude+latitudeMinuteOuput.substring(1) +"          ";
    lcd.print(lastKnowLat);
  }else{
    lcd.print(lastKnowLat);
  } 
   
  String longitube = getValue(stringOne, ',',5);
  String degreelongitube = longitube.substring(0,3);
  String minutelongitube = longitube.substring(3); 
  
  char floatbuf2[32]; // make this at least big enough for the whole string
  minutelongitube.toCharArray(floatbuf2, sizeof(floatbuf2));
  float longitudeminuteInDecimal = atof(floatbuf2)/60;
  
  char buffer2[32];
  String longitubeMinuteOuput = dtostrf(longitudeminuteInDecimal,10,10,buffer2);//floatToString(floatbuf, longitudeminuteInDecimal, 10);
  
  lcd.setCursor(0,1); 
  if(degreelongitube != "" && minutelongitube != "")
  {
    lastknowLon = "Lon:"+degreelongitube+longitubeMinuteOuput.substring(1) +"          ";
     lcd.print(lastknowLon);
  }else{
    lcd.print(lastknowLon);
  }


String getValue(String data, char separator, int index)
{
  int found = 0;
  int strIndex[] = {0, -1};
  int maxIndex = data.length()-1;

  for(int i=0; i<=maxIndex && found<=index; i++){
    if(data.charAt(i)==separator || i==maxIndex){
        found++;
        strIndex[0] = strIndex[1]+1;
        strIndex[1] = (i == maxIndex) ? i+1 : i;
    }
  }

  return found>index ? data.substring(strIndex[0], strIndex[1]) : "";
}

And I was pleased with what I have, even though the data may not be extremely accurate, but it's fairly good enough for me! 

It's very accurate and updates are fairly quick once it has satellite lock on!

While waiting for any information from GPS

Lock on! Oh Yeah!

Time to take it out for a ride!


So, there will be a phase 2, which is when I tie the module with a GSM shield and code my own server backend to record those data and my first prototype is finished!

Wednesday, March 26, 2014

[Arduino] Auto Fish Feeding Project with Servo

Last year, near Christmas, I took a long one week annual leave to chill out with my grandma and my in-laws back in Taiping. While I was away from home, my fish aquarium was left with no one to take care of. But before I switch off the last appliance in the house, I poured quite a big of the feeds into the tank and hope for the best.

But one week later, almost all of the fishes died because I made the following mistake.
1. Feed too much
2. Switched off the aquarium light.

The feeds turned bad and caused the water to cloud, and soon, the plants in the tank all died off because there was no light. Further causing the water to cloud and choked the fishes to death. There were however, two little fish baby survived the whole incident.

From that moment onwards, I was thinking. I must build a auto fish feeding machine. So, I started sketching. To put things in perspective, it was a lot harder to think in 3D space when you need to think how mechanical part should come together to perform what you want. And in my case, an auto fish feeding machinery. It could be like those found in McDonald french fries section where the salt portion is measured and poured into a special section inside the salt shaker every time the staff turn it upside down to let the salt out. But, I couldn't wrap my mind around how it works exactly. To me, if I could build sometime that shakes out salt like this. I would be glad.


While my final design for the first prototype is very simple, I think it does the job quite well. Because I believe simplicity sometimes bring better outcome than complex design. As with most of my work, I chose simplicity! Though I have to admit I tried using ethernet shield to make connection with internet and listen to certain action from a web service to feed the fish. But I figure, if I have to manual feed the fish using the internet, I might as well go down the stairs and feed the fish myself there. So, the key here is automatic, fully automatic. May be in the future, when I decide to upgrade the feeding machine to have interval setting buttons. I will leave the design as simple as possible.



The idea behind this simple fish fishing machine, is that it has to be automatic. Means it will feed on a fixed interval. In my case, it's feeding the fish every hour. For the first few days, I was practically sitting by the side of the tank and wait for every feeding hour to see whether the flow of the feeds are consistent as I just melt some rectangular holes at the edge of the container. Since the servo is not 100% smooth when it operates, which works in my favor, it tend to shake ever so subtle that the motion of servo turning will shakes the feeds in the container a little while it turn. Allowing more feeds to go through the little rectangular holes. This is rather important as you don't want the holes to be big enough for the feeds to flow out endlessly when the holes are facing downwards. The feeds itself consists of different shapes and sizes, which helps to contain itself inside the container. The last thing you want, is to have the feeds leaked out like a sand clock.

While I encounter some hiccup during the process of developing some very simple code with adaptation from the sample given by the good folks from Arduino. I figure, I might as well share it with people who cares about their fish!



#include <Servo.h> 
 
Servo myservo;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created 
 
int pos = 0;    // variable to store the servo position 

const long oneSecond = 1000;  // a second is a thousand milliseconds
const long oneMinute = oneSecond * 60;
const long fiveMinute = 1000 * 300;
const long oneHour   = oneMinute * 60;
const long oneDay    = oneHour * 24;

void setup() 
{  
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
   pinMode(ledPin, OUTPUT);  
   Serial.begin(9600);   
} 
 
 
void loop() 
{ 
    Serial.println("Start Delay");
    delay(oneMinute);
    for(int i=0; i<60;i++){
      Serial.print("Delay for minute");
       Serial.println(i);
       delay(oneMinute);
    }  
    
    spin();
} 

void spin(){
  Serial.println("Feeding time!");
  
  for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
  for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees 
  {                                
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
}
As you may notice, instead of using the full one hour wait, I use a for loop to wait one minute for 60 times, cause I was running into trouble where I believe the logic was congested somewhere when the number of delay is too big. So, to be safe, I used a very small number to delay. It works!

Sunday, February 9, 2014

AngularJS - Executed 0 of 0 ERROR


GDG Kuala Lumpur is around the corner!
Am absolutely ecstatic about it, may be can meet some new folks there. (Update post GDG Kuala Lumpur: It was boring, please, if you want to advocate new framework, at least have someone who already does something with it showing us how it's done and not show us the video from angularjs.com. It would be pointless to travel all the way there just to find out it's a projection from the website. If next upcoming GDG is like that, then I recommend you guys who wants to take home something, not to attend at all)

So, I heard about AngularJS and its promise, and went ahead to setup the environment on my pc. While the process isn't idiot proof, may be just to keep out idiots from trying too hard. It has some novice error. At least for novice like me. 5 minutes into setting up, I got error when using Karma with the following output Executed 0 of 0 ERROR. However, the fix is simple. Hope it helps you from pulling your hair a little.

Error when running AngularJS Karma sample
Might see a warning of Cannot find plugin "karma-junit-reporter"
Install it to solve the problem

Executed 0 of 0 ERROR
To fix it, the following line should be included in karma.conf.js
exclude: ['app/lib/angular/angular-scenario.js']
reference for the solution is found here
Starting Karma Server (http://karma-runner.github.io)
-------------------------------------------------------------------

./scripts/test.sh: line 9: ./scripts/../node_modules/karma/bin/karma: No such file or directory


My Version

#!/bin/bash

BASE_DIR=`dirname $0`

echo ""

echo "Starting Karma Server (http://karma-runner.github.io)"

echo "-------------------------------------------------------------------"

 
/usr/local/bin/karma start $BASE_DIR/../config/karma.conf.js $*

Run with "sudo ./scripts/test.sh"