PrettyPrint JavaScript

sabato 24 ottobre 2015

Setting I2C on ESP8266MOD with ESP 07/12 Breakout

The ESP8266 module allows us to create a portable, energy-saver, arduino compliant project for IoT with low cost and without an Arduino boards.

For the technical features of this module read this.

In order to simplify working on ESP8266 with a breadboard I reccomend to connect it with the ESP 07/12 Breakout Board.

So, after Setup ArduinoIDE to work on ESP8266 and load my script, I try to connect it with an i2c sensor: the HTU21D. But Wire library seems not working.

I googled a lot and finally I found the solution.

The ESP8266 seems to needs the GPIO02 as SDA and GPIO14 as SCL for the I2C protocol! So, take a look to the ESP 07/12 Breakout pinout in order to rightly connect ours I2C module on board.

Now, we can load a simple script testing I2C:
#include <Wire.h>

void setup() {
  int sda=2;// qui SDC del sensore
  int sdc=14;// qui SDA del sensore

  Wire.begin(sda,sdc);
  Serial.begin(115200);

}

void loop() {
  i2cscan();
  delay(1000);

}

int i2cscan(){
  //scanning i2c
  byte error, address;
  int nDevices;

  Serial.println("Scanning...");

  nDevices = 0;
  for(address = 1; address < 127; address++ ) 
  {
    // The i2c_scanner uses the return value of
    // the Write.endTransmisstion to see if
    // a device did acknowledge to the address.
    Wire.beginTransmission(address);
    error = Wire.endTransmission();

    if (error == 0)
    {
      Serial.print("I2C device found at address 0x");
      if (address<16) 
        Serial.print("0");
      Serial.print(address,HEX);
      Serial.println("  !");

      nDevices++;
    }
    else if (error==4) 
    {
      Serial.print("Unknow error at address 0x");
      if (address<16) 
        Serial.print("0");
      Serial.println(address,HEX);
    }    
  }
  if (nDevices == 0)
    Serial.println("No I2C devices found\n");
  else
    Serial.println("done\n");
}
If it's all OK you should be able to see on Serial monitor: I2C device found at address 0x.... N.B. Another information I did not easily found is how setting ESP8266 on programming mode with ESP 07/12 Breakout Board: just hold on the reset button for at least 1 second and release. After that you are able to flash the firmware!

sabato 18 aprile 2015

Show and Manage Notes on your Linux Desktop with Conky

In this article I'll show you how display and manage a simple note list into linux desktop with conky.
Start installing conky on system. On Ubuntu simply:
sudo apt-get install conky
Conky is a simple application that show some custom information on your linux desktop. To run conky open a terminal and run:
conky
Without close terminal, on your desktop you can see some information about your system. Configure conky is not soo difficult, on-line there are many post related and generally all you have to do is edited the conky.config file. How customize your conky is off-topic, we limited to add a simple note list. Now will download the latest release of pyclinote, it allows to manage a list of notes using command line. Download the latest from here Extract the content of zip in a directory of your choice, for example: /home/user/pyclinote-1.0 In this directory create a file named starter.sh, this file will allow you to open the gui even from a key-shortcut
gedit /home/user/pyclinote-1.0/starter.sh
paste the following code, then save:
(cd /home/user/pyclinote-1.0 && python pyclinote.py -g)
Ensure that /home/user/pyclinote-1.0 is the directory contains pyclinote.py! Open a terminal and add execution permission to this file:
chmod +x /home/user/pyclinote-1.0/pyclinote.py
Before execute pyclinote we have to install the tkinter library for python:
sudo apt-get install python-tk
Now we can execute pyclinote:
cd /home/user/pyclinote-1.0
./starter.sh
You can add a note by typing on the text-box, then click add. Your note is now on the listbox. (the number on the left of your note is the note number identification)
The aim of this article is to show note list on your desktop, so edit the conky.conf file. In the latest version of conky this file located on /etc/conky/conky.conf
sudo gedit /etc/conky/conky.conf 
and add the following line on the bottom of the file:
${execi 2 (cd /home/user/pyclinote-1.0 && python pyclinote.py -s)}
However, ensure that /home/user/pyclinote-1.0 is the directory contains pyclinote.py. This line tells conky to execute (cd /home/user/pyclinote-1.0 && python pyclinote.py -s) every 2 seconds.
Your desktop, will show the note typing before!
In order to improve usability we have to run conky on startup and configure a key-shortcut opening the pyclinote GUI.
To run conky on sturtup:
Click the gear icon in the upper right hand corner of the top panel. Select Startup Applications. Click Add. In the resulting dialog box give the name as "Conky" and the command as conky. Click add and close.
To add the shortcut, go to System Settings → Keboard, select Shortcuts tab and add a new custom shortcut. Into message box insert the name of shortcut, ex: Notes and the command:
/home/user/pyclinote-1.0/starter.sh
So enter the key combination you choiche to open the pyclinote GUI.
References:

martedì 3 marzo 2015

Deserialize XMLGregorianCalendar from JSON using flexjson-2.1

In order to deserialize an XMLGregorianCalendar flexjson raise a JSonException with the following message: "There was an exception trying to instantiate an instance of javax.xml.datatype.XMLGregorianCalendar" This occours because the class XMLGregorianCalendar has not a default constructor with no parameter as flexjson is supposed to have. To avoid this you need to implement the flexjson.ObjectFactory interface to instantiate and build a well-defined XMLGregorianCalendar object. Suppose we have a simple class as follows:
public class Foo{
 
   private XMLGregorianCalendar startDate;
   
   private XMLGregorianCalendar endDate;

   public XMLGregorianCalendar getStartDate(){return startDate;}

   public void setStartDate(XMLGregorianCalendar start){this.startDate = start;}
   
   public XMLGregorianCalendar getEndDate(){return endDate;}
   
   public void setEndDate(XMLGregorianCalendar end){this.endDate = end;}


}
Example of ObjectFactory implementation:
public class XMLGregorianCalendarFactory implements ObjectFactory {

 @Override
 public Object instantiate(ObjectBinder context, Object value, Type targetType,
   Class targetClass) {
    
  try {
   XMLGregorianCalendar result = DatatypeFactory.newInstance().newXMLGregorianCalendar();
   JSONDeserializer jd = new JSONDeserializer();
   String input =  new JSONSerializer().deepSerialize(value);
   jd.deserializeInto(input, result);
   return result;
  } catch (Exception e) {
   e.printStackTrace();
  }
  return null;
 }

}
To force flexjson uses our implementation, we need to pass it with the use method, as shown below:
JSONDeserializer jd = new JSONDeserializer();
jd.use(XMLGregorianCalendar.class, new XMLGregorianCalendarFactory());
Foo target = new Foo();
jd.deserializeInto(json, target);
After jd.deserializeInto(json, target) execute, the target object Foo will be correctly instatiate. (If we provide a valid json string!)