Saturday, May 14, 2016

ESP8266 and SHT1x Web Interface

Here's a *duino-less (*pi-less) simple project using ESP11 (ESP8266) Wifi Module and SHT10 humidity-temperature sensor.


local DAT = 4
local CLK = 3

function DL()
  gpio.write(DAT, gpio.LOW)
  gpio.mode(DAT, gpio.OUTPUT)

function DH()
  gpio.mode(DAT, gpio.INPUT)
  gpio.write(DAT, gpio.HIGH)

function CL()
  gpio.write(CLK, gpio.LOW)

function CH()
  gpio.write(CLK, gpio.HIGH)

function DR()
  gpio.mode(DAT, gpio.INPUT)

function W8()
  for i = 1, 100 do
    if DR() == gpio.LOW then
      return true
  return false

function RB()
  local val = 0
  for i = 0, 7 do
    val = val*2 + DR()
  return val

function shtread(cmd)
  DH() CH() DL() CL() CH() DH() CL()
  for i = 0, 7 do
    if, 2^(7-i))==0 then
    CH() CL()
  CH() CL()
  if not W8() then
    return nil
  local val = RB()
  DH() DL() CH() CL() DH()
  val = val*256 + RB()
  DH() CH() CL()
  return val

page=[[<head><meta charset="utf-8"/>
<link href="" rel="icon"/>
<body><h1>ESP8266 and SHT1x</h1><h2 id="T">T??</h2><h2 id="H">H??</h2>
 function update(){
  var xrq = new XMLHttpRequest();
   if (xrq.readyState==4 && xrq.status==200){
    var th=JSON.parse(xrq.responseText);
    var t0=th["t"], h0=th["h"];
    var t=(t0*0.01)-39.7;
    var hl=(0.0367*h0)+(-0.0000015955*h0*h0)-2.0468;
    var h=(t-25.0)*(0.01+(0.00008*h0))+hl;
    document.getElementById("T").innerHTML="Temperature: "+t.toFixed(2)+"&deg;C";
    document.getElementById("H").innerHTML="Humidity: "+h.toFixed(2)+"%";}
 }setInterval(update, 1000);

  conn:on("receive", function(client,request)
    local buf = "";
    local _, _, mtd, path, _ = string.find(request, "([A-Z]+) (.+)?(.+) HTTP");
    if(mtd == nil)then
      _, _, mtd, path = string.find(request, "([A-Z]+) (.+) HTTP");
    if path == "/" then
      buf =
    elseif path == "/get/sht" then
      t = shtread(3)
      h = shtread(5)
      if (t ~= nil and h ~= nil) then
        buf = buf.."{\"t\":"..t..", \"h\":"..h.."}"

ESP8266 Web Server Tutorial
Github esp8266 sht10
e-Gizmo ESP11 kit
ESP8266 wiki
NodeMCU firmware 
NodeMCU http server

Monday, May 9, 2016

Android-Arduino Communication via USB OTG

USB On-the-go capability in Android devices has now become more available in the market. And why wouldn’t it be? This feature, nicknamed OTG, enables you to use your flash drives, keyboards, mice and even printers with just your phone! What’s more interesting is it can also enable you to communicate and control your microprocessors using your Android device without the need for additional modules – just a cable. In this article, we will see how this communication can become possible. To demonstrate, we will control the behavior of an LED and send messages to another very popular item in the electronics world – the Arduino.

The shorter end of the stick is Arduino’s side. In your Arduino, simply upload this code:

int ledPin = 13;

void setup(){  
  pinMode(ledPin, OUTPUT);  

void loop(){
  if (Serial.available()){
    String c = Serial.readString();
    if (c.equals("TONLED")) digitalWrite(ledPin, HIGH); 
    else if (c.equals("TOFFLED")) digitalWrite(ledPin, LOW);
    else Serial.print(c);

In the above sketch, we are simply waiting for the data arriving at our serial line and performing actions based on the data received. For instance, turning on the LED ledPin requires a TONLED message from our Android device. You’ve probably noticed that there are no special libraries or methods in our Arduino sketch.  That’s a great thing because it tells us that the system is not exclusive to Arduino and will work with any microcontroller that supports serial communication.

Let’s now move on to Android’s side. The first step is to create an Android project and add the necessary components. In the project we created, we added extra components for user convenience. For learning and testing purposes, only the following are necessary:
  • Text Field – used to get input data by the user, which will be sent to and echoed by the Arduino
  • Toggle Button – used to control the behavior of the LED
  • Start Button – used to open the serial port
  • Send Button – used to send messages to Arduino
  • Text View – used to display logs
To simplify the setup and processes, we will use the UsbSerial library by felHR85. There are a lot of libraries you can choose from. In case you have other preferences, feel free to modify and adapt to your preferred library.

In the build.gradle of your project, add jitpack. Jitpack is a very awesome tool that enables us to get a Git project into our build.

allprojects {
    repositories {
        maven { url "" }

Now, add the dependency to your module’s build.gradle.

compile 'com.github.felHR85:UsbSerial:4.3'

Moving on to our main activity, there are some variables that we wish to declare globally for convenience.

private static final String ACTION_USB_PERMISSION = "";

UsbDevice device;
UsbDeviceConnection connection;
UsbManager usbManager;
UsbSerialDevice serialPort;
PendingIntent pendingIntent;

The next items that we will present here will not be discussed thoroughly, but you can refer to Android's official documentation for details.

Before trying to start the communication, you must seek permission from the user. To do this, create a broadcast receiver. This receiver listens for the intent that gets broadcasted when you call requestPermission(). Only when granted can we proceed to opening the connection and setting parameters for the Serial communication.

private final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(ACTION_USB_PERMISSION)) {
            boolean granted = intent.getExtras().getBoolean(UsbManager.EXTRA_PERMISSION_GRANTED);
            if (granted) {
                connection = usbManager.openDevice(device);
                serialPort = UsbSerialDevice.createUsbSerialDevice(device, connection);
                if (serialPort != null) {
                    if ( {
                    } else {
                        Log.d("SERIAL", "PORT NOT OPEN");
                } else {
                    Log.d("SERIAL", "PORT IS NULL");
            } else {
                Log.d("SERIAL", "PERMISSION NOT GRANTED");
        } else if (intent.getAction().equals(UsbManager.ACTION_USB_DEVICE_ATTACHED)) {
        } else if (intent.getAction().equals(UsbManager.ACTION_USB_DEVICE_DETACHED)) {
            //can add something to close the connection

On your onCreate method, declare the intent and register your broadcast receiver to start and stop the serial connection.

pendingIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
registerReceiver(broadcastReceiver, filter);

In our application, we created a start button to start the connection when pressed. In the method that corresponds to the onClick action of our button, we add the following:

public void onClickStart(View view) {

    if (!isSerialStarted) {
        usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);

        HashMap<String, UsbDevice> usbDevices = usbManager.getDeviceList();
        if (!usbDevices.isEmpty()) {
            boolean keep = true;
            for (Map.Entry<String, UsbDevice> entry : usbDevices.entrySet()) {
                device = entry.getValue();
                int deviceVID = device.getVendorId();

                if (deviceVID == 1027 || deviceVID == 9025) { //Arduino Vendor ID
                    usbManager.requestPermission(device, pendingIntent); 
                    keep = false;
                } else {
                    connection = null;
                    device = null;
                if (!keep)

The code above searches for vendor IDs 1027 or 9025 – the vendor ID’s associated to FTDI or Arduino. The vendor ID equal to 9025 is the more popular and more common value based on other articles in the internet, but mine has an ID of 1027. The easiest way to know is to just print the vendor IDs detected by the Android device.  If the vendor ID matches the expected ID for our device, we will call the requestPermission() method. With this, the intent will be broadcasted and picked up by our receiver, starting and opening the connection.

Once communication is opened, we can start sending and receiving data. To receive from Arduino, simply add the codes below. Note that we are appending the data received to the text view.

private UsbSerialInterface.UsbReadCallback mCallback = new UsbSerialInterface.UsbReadCallback() {
    //Defining a Callback which triggers whenever data is read.
    public void onReceivedData(byte[] arg0) {
        String data = null;
        try {
            data = new String(arg0, "UTF-8");
            tvAppend(displayView, data);
        } catch (UnsupportedEncodingException e) {

private void tvAppend(final TextView tv, final CharSequence text) {
    runOnUiThread(new Runnable() {
        @Override public void run() {
            if (text != null) {

Sending data is easier. We only need to get user input from the text field, and send it to the connected device.

public void onClickSend(View view) {
    String textInput = inputView.getText().toString();

To control the LED in Arduino, simply add the code below. You are free to change TONLED and TOFFLED to whatever names you want. Just don’t forget to adjust the Arduino code as well.

public void onClickToggle(View view) {
    if (isLedON == false) {
        isLedON = true;
        tvAppend(displayView, "\nLED TURNED ON\n");
    } else {
        isLedON = false;
        tvAppend(displayView, "\nLED TURNED OFF\n");

You can close the connection using:


We are almost done. In your manifest file, add the following so that your application will be notified of an attached USB device. 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""

    <uses-feature android:name="" />

                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
                <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
                android:resource="@xml/device_filter" />
                <action android:name="android.hardware.usb.action.USB_DEVICE_DETACHED" />

Create an xml folder inside the res folder and add device_filter.xml

<?xml version="1.0" encoding="utf-8"?>

And… were done! Additional tips, we can add a checker to confirm that the serial connection is already open. This saves us from crashes due to attempts to send serial data while the connection is still closed. We can also add clear buttons, or place the text view inside a scroll view then automatically scroll to end of page using: 

mScrollView.smoothScrollTo(0, displayView.getBottom());

That’s it. If you want to extend your phone’s sensors, or if you want to add storage, wireless and messaging capability, camera and orientation sensors in your microprocessor project with just one device, USB On-the-Go, may be your way to go.

A demo application, SimpleArDroid by YMSoftLabs can be downloaded from Google Play. Here's a video of how the system works.

UsbSerial: A serial port driver library for Android v3.0
Communicate with Your Arduino Through Android

Saturday, February 13, 2016

Saturday, January 9, 2016

Intel Edison: LED Blinker

Here's a "hello world" test application for Intel® Edison Compute Module. As what the title says, it's just a simple LED blinker task executed by a fast processor.


Below is the tiny module powered by Intel Atom dual-core processor at 500 MHz, with 1 GB DDR3 RAM, 4 GB eMMC Flash, Bluetooth 4.0 and Wi-Fi. It runs Yocto Linux (Linux 3.10.17-poky i686).

Some Intel Edison breakout boards that are shield-like (can be stacked on each other) are also already available on SparkFun. Most useful one is the base block which is used for connecting the Edison module to the host PC via USB. This block has two mini USB ports. The first port is an OTG type and is primarily used for flashing firmware/image of the Edison. The other one is an FTDI-based USB-to-Serial which is used for connecting to a host's serial terminal/console.

Initial configuration requires the Edison module to be connected to a serial terminal, like putty (sample boot log). After configuring the WiFi and the root's password, it is now possible to access the module remotely using ssh connection, just like in the demo video above.

The code below is a simple C++ code for blinking an LED connected at GP14. The code depends on mraa low level library to access the GPIO hardware.
/* intel edison led blink - yus 20150109 */

#include <mraa.hpp>
#include <iostream>

int main()
    mraa::Gpio *gpio = new mraa::Gpio( 36 /*=GP14!*/ );
    if (gpio && gpio->dir(mraa::DIR_OUT)==mraa::SUCCESS)
        std::cout<<"Blinking GP14. Press [CTRL+C] to stop...\n";
        while (1) {
    return -1;

The provided Yocto Linux installed in the Edison already includes a GNU toolchain (GCC 4.9). So the demo code  can be compiled/build inside the Edison itself.

Alternatively, a BASH script can be also used to do a LED blink task.

# select led pin


if [ ! -d "$gpio" ]; then
    echo $pin > /sys/class/gpio/export

echo out > $gpio/direction
echo Blinking GP$pin. Press [CTRL+C] to stop...
while :
    echo 1 > $gpio/value
    sleep 1
    echo 0 > $gpio/value
    sleep 1

Also, for those who are comfortable developing with Arduino IDE, the Arduino IDE also supports Intel Edison. However, you need to check the correct pin mappings if you're going to use boards other than Arduino's Edison kit or Edison breakout board.

Edison Getting Started
Edison GPIO block
Flashing Firmware
mraa low level library

Sunday, June 14, 2015