Monday, January 23, 2017

Arduino IDE alternative (ATOM - PlatformIO and Visual Studio - Visual micro)

Recently I have been searching for a good Arduino IDE. My main purpose was to find one with good intellisense so that I could code more efficiently. I tried two. Visual Studio w/ visual micro and Atom w/ PlatformIO. I am going to talk more about my experiences with them both.

Visual Studio w/ visual micro (Free with limitations)
Download Visual studio community version
Download Visual Micro or by Visual studio > Extensions and Updates


The good is that if you are familiar with Visual Studio, you will like this very much. Intellisense and code search is easy. There is auto formatting as well. Supports Arduino OTA (Python required).

The bad is that if you use version control, the project settings files changes frequently even when just opening the project (Doing nothing) sometimes. Also after free trial duration, the library manager is not enabled.

What you can do is to make use of the default Arduino IDE, change sketchbook directory to where visual micro is and install the libraries through Arduino IDE. The changes will be picked up by visual micro. However, I find it too troublesome so finally I tried ATOM w/ PlatformIO.

ATOM w/ PlatformIO (Free)
ATOM alone is an editor like sublime. However you can install PlatformIO like an extension to ATOM to support Arduino. According to PlatformIO website, it says that it is the next-generation integrated development environment for IoT. And it seems like there is a lot of potential which made me want to try it out.
Download PlatformIO
or
Download ATOM and Open Atom Package Manager and install platformio-ide

The good is that Atom project files is simple and straight forward. It does not change much and can have self contained library packages per project. This is done through platformio.ini file. A simple example of adding MySensors library is done as follows in the image. When compiling, the project will auto download the library to build. So library codes do not need to be checked into the repo. Unlike Visual Micro the project setting files does not change automatically and therefore does not confuse the commits.

The bad is that code files are recommended to be saved with cpp extension. This means that if you want to use back the old Arduino IDE, you may need to change back to ino or pde extension. The IDE is a little new to the market so it might still have existing bugs. (At this point of writing there is the following bug in the image, but the issue has been raised).

Overall, I will be sticking with ATOM for the time being. Cheers!

Tuesday, January 17, 2017

Auto discover Mqtt server using arduino mdns

So I am using MyControllerDevice to connect to MyController, however the default way is to input the ip address of the mqtt server when setting up the arduino. I would like a way to auto discover the mqtt server and register itself to the mqtt server. I searched and found mdns could work. These are the steps i did to get it working:

1) Set up avahi-daemon.
sudo apt-get install avahi-daemon

2) Create an avahi configuration file /etc/avahi/services/mosquitto.service
<?xml version="1.0" standalone='no'?>
<!DOCTYPE service-group SYSTEM "avahi-service.dtd">
<service-group>
 <name replace-wildcards="yes">Mosquitto MQTT server on %h</name>
  <service>
   <type>_mcmqtt._tcp</type>
   <port>1883</port>
   <txt-record>info=A MQTT PubSub service! mqtt.org</txt-record>
  </service>
</service-group>

3) Use this piece of code to get the ipaddress of the mqtt server
char hostString[16] = { 0 };
sprintf(hostString, "ESP_%06X", ESP.getChipId());
Serial.print("Hostname: ");
Serial.println(hostString);
WiFi.hostname(hostString);
if (!MDNS.begin(hostString)) {
    Serial.println("Error setting up MDNS responder!");
}
Serial.println("MC: Sending mDNS query");
int n = MDNS.queryService("mcmqtt", "tcp"); // Send out query for esp tcp services
Serial.println("MC: mDNS query done");
if (n == 0) {
   Serial.println("MC: no services found");
}
else {
    Serial.print(n);
    Serial.println(" service(s) found");
    for (int i = 0; i < n; ++i) {
    DNS.port(i));
    MDNS.IP(i).toString().toCharArray(_mqttServer, 51);
    _mqttPort = MDNS.port(i);
}
_mqttServer is the ip address of the mqtt server.
_mqttPort is the port number of the mqtt server.

4) set the mqtt server and use it.
mqttClient.setServer(_mqttServer, _mqttPort);

5) That's all. =)

Friday, January 6, 2017

Docker Tips


Remove all untagged images (Source)

docker rmi $(docker images | grep "^<none>" | awk "{print $3}")

MyController Web server for DIY home automation (Docker installation)

Recently I discovered an open source simple home automation web server. My requirement was to look for one which supports MySensors API. I found MyController and it works very well.

The best part about MyController user interface is that it can detect sensors automatically without any coding. In addition, it also allows user to modify the dashboard through the website.

Web Server
http://www.mycontroller.org/

Arduino Library
https://www.mysensors.org/

With these two software we can basically build a home automation solution.

*Bonus Installation
I created a guide for running MyController in docker. Feel free to take a look. The reason for me to use docker to run MyController is
  • Docker helps me to handle porting MyController to a new rpi. ie works on a clean rpi without any java installed or other dependencies.
  • Docker helps me to handle auto startup on boot.
  • All other docker advantages.
https://github.com/wengjjpaul/dockers-rpi-mycontroller

How: to install docker on Raspberry pi

Tested on RPI 1 model B

Read the full article or follow my steps

# sudo apt-get update
# sudo apt-get upgrade
# curl -sSL https://get.docker.com | sh

* Once installed, follow the instructions to add user to docker group. (This is to allow user to run docker)
* These steps can be applied to any linux OS not just raspberry pi. Tested on Ubuntu as well.

Changing Raspberry PI Timezone in docker container

In docker, we are not able to use interactive mode (UI) to select time zone. We need to use a non interactive way of editing time zone.

To set time zone in docker container use the following:

# echo "Asia/Singapore" > /etc/timezone
# dpkg-reconfigure -f noninteractive tzdata