Tuesday, September 16, 2014

Intermediate: u-blox Neo-6M GPS Module (Revisited)

Previously, I've posted about this module which you can find here. This time around I'm going to share with you another library that I wrote specifically for this module.

If you've tried my code from the tutorial, you might notice that sometimes it is hard to change the baud rate of the module. Most of the time, the module failed to change the baud rate to 9600. Note that the module runs by default, at 38400 baud rate. I notice that it is easier to change to this baud rate, but the Arduino Uno or Nano that I'm using can't keep up with the speed, thus, loosing some bytes. However, it still managed to give you some of the important information. I manage to get about 63 bytes of data for each reading. This data is actually enough to extract the location data, latitude, longitude, altitude and the UTC. You won't get to verify this data however, of cause, due to the missing checksum, but still, for me it is fine. Unless your project requires you to be extremely cautious, you might want to try out this library.

You may download the library here.

The sketch could end up pretty huge, but can be reduced if you remove all the Serial print statements in the library. I just leave it there so that we can see what happen. The following is the short code to print the available information.


#include <CustomUBlox.h>
#include <SoftwareSerial.h>

CustomUBlox gps(2, 3); // RX, TX, Baudrate, mode, datarate

void setup() {
  Serial.begin(19200);
  Serial.println("Initializing...");

  gps.init();
}

void loop() {
  gps.processData();
  
  if(gps.available() && gps.isFixed()) {
    Serial.print("\nLocal time : ");
    Serial.println(gps.getLocalTime());
    Serial.print("Latitude : ");
    Serial.print(gps.getLatitude(), 4);
    Serial.println(gps.getLatitudeHeading());
    Serial.print("Longitude : ");
    Serial.print(gps.getLongitude(), 4);
    Serial.println(gps.getLongitudeHeading());
    Serial.print("MSL Altitude : ");
    Serial.print(gps.getAltitude(), 4);
    Serial.println("m");
    delay(1000);
  }
}

The functions available in the library are as follows:
Constructor:
CustomUBlox(int rx, int tx, int baudrate = 38400, int mode = 2, int rate = 3)
  • Baud rate can be: 4800, 9600, 19200, 38400 (default), 57600, 115200 or 230400
  • Mode: 0 (Portable), 1 (Stationary), 2 (Pedestrian - Default), 3 (Automotive), 4 (Sea), 5 (<1G), 6 (<2G), 7 (<4G)
  • Data rate: 0 (1Hz), 1 (2Hz), 2 (3.33Hz), 3 (4Hz)
Functions:
void init() : to initialize the GPS module.
void showStatus(bool status) : Show Serial messages (true - default) or hide Serial messages (false)
void processData() : Checks and updates new data from the GPS.
double getLatitude() : Return the latitude.
char getLatitudeHeading() : Return 'N' for North or 'S' for South.
double getLongitude() : Return the longitude.
char getLongitudeHeading() : Return 'W' for West or 'E' for East.
double getAltitude() : Return the altitude.
bool available() : Return true if new data is available. Otherwise, false.
bool isFixed() : Return true if the GPS got a fix.
String getLocalTime(byte offset = 8) : Return UTC time + offset (default=8) in form of HH:MM:SS
byte getHour() : Return hour.
byte getMinute() : Return minute.
byte getSecond() : Return second.

Monday, September 1, 2014

Basic: HC-06 Bluetooth Module


Hi. It has been a while since my last post. I was busy with the Eid preparation, getting my driving license, babysitting, etc. Finally I have some free time to write a short post about a Bluetooth module, HC-06. As always, this module is purchased from GI Electronic which you may view here. It's considerably cheap and easy to use.

Living in advance technology era, I believe all of you have at least heard about Bluetooth technology before. Contrary to some belief, this module is actually very simple to use; programming-wise speaking. The only drawback is that you'll need to attach 2 resistors (1kΩ and 2kΩ) on the module's RX pin. This is because the module is working at 3.3V. We'll see this later. The module will allow you to perform simple serial communication from/to your computer, smartphone, tablet, etc. There's just a lot of things that you can do with it due to its flexibility. In fact, I'm currently working on reviving my brother's RC car using this module. Now, I can control the RC car using my smartphone and my computer. Now, let's get down to business.

Connection

I actually use 2kΩ for R1, but I think 2.2kΩ should also works just fine. Make sure the connection is correct. Triple-check it!


You can actually use the hardware RX (0) and TX (1) pins on Arduino (like diagram above), but I use the pin 2 and 3 instead because pin 0 and 1 will be used for serial monitor. Using pins other than hardware RX and TX pins requires us to use SoftwareSerial library instead. If you are using the hardware RX and TX pin, simply write Serial.begin (baud rate);
How to connect to the device, then? In the following examples, I'm going to connect to the module using my computer and my Android phone. I simply search for the available device and connect to it. The default password is: 1234.
To connect and send/receive messages from/to Android phone, go to Play Store and search for Bluetooth Terminal. There should be a bunch of them. I've tried using "ArduDroid" and "ArduinoRC". Both works just fine.
If you want to change the device's name, password, and others, refer to the product's guideline.

Programming
If you are used to writing programs to display messages in the Serial Monitor, this would be a walk in the park. The coding is almost similar. If you're using the hardware RX and TX pin, the coding should be exactly the same, but you wouldn't be able to use the Serial Monitor.
btmodule.read () will return whatever the Bluetooth module is receiving from the connected device.
btmodule.write ("message") will send the message to the connected device.
Simple, isn't it?

#include <SoftwareSerial.h>

SoftwareSerial btModule(2, 3);

void setup() {
  Serial.begin(19200);
  btModule.begin(9600);
}

void loop() {
  if(btModule.available()) {
    Serial.write(btModule.read());
  }
  
  if(Serial.available()) {
    btModule.write(Serial.read());
  }
}

Using the code above, you may send and receive messages from computer to an Android phone or other bluetooth devices with Arduino as the middleman. What you need to do is, install a bluetooth terminal on your smartphone and connect to the bluetooth module, not the computer's bluetooth device. I use ArduDroid. You may find more information about it here. The LED on the bluetooth module should stop blinking after connected to a device. While the Arduino still connected to the computer, open the Serial Monitor. Type something and press "Enter". The message should be sent to the device connected to it. You can also try to type something and press "Send Data" in the ArduDroid application and you receive a line with symbols, numbers, and finally your message. We'll discuss what those numbers mean later. But now you should understand how easy it is to do it, right?

Alright. Now, let's discuss a little bit about ArduDroid. The message that you receive is in form of *number|number|number|message# when sending message and *number|number|number# when pressing the Digital Write and changing the value for Analog Write. It is actually like this. * marks the beginning of the message. # marks the end of it. | separates the values of message code, pin number and value, respectively. To simplify the usage of it, I wrote a rather simple library which you may download here.

After extracting it to the Arduino's library folder, run the following code:

 
#include <SoftwareSerial.h>
#include <ArduDroid.h>

ArduDroid btModule(6, 7);

void setup() {
  Serial.begin(19200);
  Serial.println("Starting...");
}

void loop() {
  if(btModule.available()) {
    if(btModule.getMessageCode() == 12) {
      Serial.print("Message Received: ");
      Serial.println(btModule.getMessage());
    }
    else {
      Serial.print("Setting value of pin ");
      Serial.print(btModule.getPinNum());
      Serial.print(" to ");
      Serial.println(btModule.getValue());
      btModule.executeCommand();
    }
  }
  
  if(Serial.available()) {
    char message[50] = "";
    int i = 0;
    
    while(Serial.available()) {
      message[i++] = (char) Serial.read();
    }
    
    btModule.sendMessage(message);
    Serial.print("Sent Message: ");
    Serial.println(message);
  }
}

The code above will allow 2-ways communication between your Android phone to the Arduino. I guess the code is quite self-explanatory. Still, if you have any question, do comment below.
I hope this tutorial may help you develop more interesting things and increase you creativity and functionality of your project. Until next time, good bye.