Working with the Nextion HMI Display

Lately I’ve been playing around with the Nextion HMI 2.8″ display from Itead studios. If you can get past the chinglish in their documentation it can be pretty fun and interesting little display. It provides RS232 at TTL for communication between a MCU and the display. There are some quirks, as we will see.

The editor software for the display only runs on windows, and is very limited and somewhat confusing. Writing to the display can be done in two ways, direct RS232 to the display (which according to some forum posts, can be rather tedious process) or upload to the display via µSD card.

Since I have a mac I use Windows 10 virtual machine for editing the display layout and then upload to the display via µSD card. Annoyingly enough, mac for some reason always creates ._NameOfFile.tft when uploading to the µSD card, apparently it’s something for spotligh for search indexing. This drives the display mad, and it took quite a while for me to realise what was going on when the display complained about multiple .tft files on the card. So, I went through terminal, navigated to the root folder of the card and just deleted that file… You can see where I’m going with this, it’s pretty time consuming when you are constantly updating the layout. So, I wrote a short script to handle this stuff for me. It’s pretty savage but pretty neat.

#!/bin/bash/

/bin/df -h | grep K-30

/bin/cp /Users/username/path/to/HMIfile.tft /Volumes/YourSDCard

/bin/rm /Volumes/YourSDCard/._HMIfile.tft

/usr/sbin/diskutil unmount /Volumes/YourSDCard/

echo ‘Success’

Could use some improvement, but it works and saves time which can be used for something more important, like petting my cats.

The HMI display spits out hex code for push actions, which makes it pretty easy to read whatever is coming from it, for example if you want  a button to let’s say, turn on a LED on an Arduino. But timing is critical, and especially if you are not using interrupts on the AVR so don’t let your loop do too much apart from just waiting for serial data coming from the display. Since I’m playing a lot with Arduino Nanos, I want to use software serial for the HMI, and use the primary serial for debugging and uploading sketches. Here’s my code snippet of how I read the data from the display:

 // We add the library software serial

#include <SoftwareSerial.h>

#define SERIAL_BAUD_RATE

#define LED_PIN 13 // Our LED is on pin 13 (default on Arduino nano, uno etc)

SoftwareSerial HMISerial(10,11); // connects to the serial of the HMI, TX, RX.

bool HMIcommand = false;

byte HMIbuffer[10]; // buffer for bytes coming from HMI.

int HMIbytes = 0;

int HMIendcount = 0;

//We wait for a specific hex string from the display to turn the LED on or off

byte switch_led_state[7] = {0x65,0x01,0x01,0x01,0xFF,0xFF,0xFF};

byte led_state =0; // Zero is off, so that’s how we start.

void setup()

{

Serial.begin(SERIAL_BAUD_RATE);

HMISerial.begin(SERIAL_BAUD_RATE);

}

void loop()

{

if (HMISerial.available())

{

byte inbyte;

inbyte = HMISerial.read();

//Serial.println(inbyte); print inbyte for debug purposes

if (inbyte == 0xFF)

{

HMIendcount++;

if (HMIendcount == 3)

{

HMIcommand = true;

HMIendcount = 0;

}

}

else

{

HMIendcount = 0;

}

HMIbuffer[HMIbytes] = inbyte;

HMIbytes++;

//Serial.println(“Loop ran again”); for debugging

}

// Process HMI data when they have been completely received and print them to the Serial interface

if(HMIcommand==true)

{

// For debugging purposes, shows what HMIbytes includes and the hex string from the HMI

/*

Serial.println(HMIbytes);

for (int x = 0; x < HMIbytes; x++)

{

Serial.print(HMIbuffer[x],HEX); // For debugging purposes

if(HMIbuffer[x] == 0x65)

Serial.print(” “);

}

Serial.println(“”);

*/

// compare arrays and do stuff – Here we check for the right hex value that changes the state of the LED.

int allMatch = 1;

for (int i = 0; i < HMIbytes; i++)

{

if (HMIbuffer[i] != switch_led_state[i])

{

allMatch = 0;

break; //If there’s a mismatch we just break out of the loop.

}

}

if(allMatch == 1)

{

Serial.println(“\nSwitch LED state\n”);

if(led_state  < 1)

{

digitalWrite(LED_PIN,HIGH);

led_state = 1; // We set the led_state to 1 so the program is aware that the LED is turned on.

}

else

digitalWrite(LED_PIN,LOW);

led_state = 0; // We set it to 0 so we know it’s off.

}

So that program should turn on and off the LED depending on the LED’s state and whether we are pushing the button or not. Let us break that down a bit to understand what we are doing. Most of these things should be pretty much explain them selves, so let us look at the interesting bits.

The HMI display ends all it’s hex strings with 0xFF 0xFF 0xFF. So whenever we see those three sisters, we know that the display has stopped sending us something and whatever comes next is a new event.

bool HMIcommand = false;

byte HMIbuffer[10]; // buffer for bytes coming from HMI.

int HMIbytes = 0;

int HMIendcount = 0;

HMIcommand is basically used to let us know whether we’ve received those three 0xFFs or not. HMIbuffer contains the string from the display and HMIbytes would be each byte that the display sends. Note that 0xFF is one byte, or 1111 1111 in binary, or 255 in decimal, so it fits nicely in our byte array of 7 bytes. Ie. each row in that array is a byte in size.

//We wait for a specific hex string from the display to turn the LED on or off

byte switch_led_state[7] = {0x65,0x01,0x01,0x01,0xFF,0xFF,0xFF};

byte led_state =0; // Zero is off, so that’s how we start.

The string of hex values we’re looking for is 0x65,0x01,0x01,0x01,0xFF,0xFF,0xFF. 0x65 is a touch event. First 0x01 is the page it’s happening at on the HMI. Second 0x01 is the ID of the button that is being pressed. The third is a touch event, we’ll not dig into that now. And then come the three 0xFF.

void setup()

{

Serial.begin(SERIAL_BAUD_RATE);

HMISerial.begin(SERIAL_BAUD_RATE);

}

We start both of the Serial ports, former for debugging, latter for the HMI serial port. Note that the display runs at 9600baud by default.

void loop()

{

if (HMISerial.available())

{

byte inbyte;

inbyte = HMISerial.read();

//Serial.println(inbyte); print inbyte for debug purposes

if (inbyte == 0xFF)

{

HMIendcount++;

if (HMIendcount == 3)

{

HMIcommand = true;

HMIendcount = 0;

}

}

else

{

HMIendcount = 0;

}

        HMIbuffer[HMIbytes] = inbyte;

HMIbytes++;

//Serial.println(“Loop ran again”); for debugging

}

If the serial is available, we read what’s coming from it, and put that into the array HMIbuffer.

    // Process HMI data when they have been completely received and print them to the Serial interface

if(HMIcommand==true)

{

Here comes the HMIcommand == true, if the first loop filled up it’s buffer, defined by the three 0xFFs, we look into what data the array holds.

// For debugging purposes, shows what HMIbytes includes and the hex string from the HMI

/*

Serial.println(HMIbytes);

for (int x = 0; x < HMIbytes; x++)

{

Serial.print(HMIbuffer[x],HEX); // For debugging purposes

if(HMIbuffer[x] == 0x65)

Serial.print(” “);

}

Serial.println(“”);

*/

The debugging function prints out the received hex string.

        // compare arrays and do stuff – Here we check for the right hex value that changes the state of the LED.

int allMatch = 1;

for (int i = 0; i < HMIbytes; i++)

{

if (HMIbuffer[i] != switch_led_state[i])

{

allMatch = 0;

break; //If there’s a mismatch we just break out of the loop.

}

        }

Here we look at the HMIbuffer array and compare it to our own predefined array that contains only one matching hex string, which in turn changes the state of the LED.

        if(allMatch == 1)

If it matches, we print a message to the debug serial consol, and check the state of the LED.

        {

Serial.println(“\nSwitch LED state\n”);

if(led_state  < 1)

If the led_state variable is less than zero, we assume it’s 1 or higher, and turn on the LED.

            {

digitalWrite(LED_PIN,HIGH);

led_state = 1; // We set the led_state to 1 so the program is aware that the LED is turned on.

}

Otherwise, it’s not turned off and we turn it off.

            else

digitalWrite(LED_PIN,LOW);

led_state = 0; // We set it to 0 so we know it’s off.

}

 

So that’s it! A bit of code to play against the Nextion HMI display to get you started. Of course there are libraries from Nextion that makes it easier, but my experience with them has been rather bad, so I ended up with just playing with the raw hex values rather than the libraries.

 

Posted in Electronics, Mechatronics | Leave a comment

If you install this, then you are cognetively disabled

Let us have a quick discussion about MacKeeper. Some people seem to think it’s a good idea to install it. (Yes, I’m looking at you people who watch porn).

It’s not. It falsely advertises to make your Mac more secure. It doesn’t. What it does instead is that it installs malware. Malware that will fuck up your computer.

If you have MacKeeper on your computer, remove it using this guide here

To remove the last remaining folders that the how to doesn’t state, open up terminal, (command + spacebar and then type in Terminal)

rm -rf “The path suggested in the article”

That should be it.

Now come at me MacKeeper, I shall give you no quarter!

Posted in Almennt | Leave a comment

Heimsókn til Púerto Ríkó

Í maí fór ég til Púerto Ríkó til að aðstoða nemendur við háskólann í Púertó Ríkó Rio Piedras við að leysa vandamál varðandi myndavél og upptökur. Nú er ég á leiðinni aftur út, en mig vantar aðstoð ykkar. Þetta verkefni er algjörlega unnið á sjálfboðaliðastarfi og við höfum í raun engar fastar tekjur fyrir utan dagvinnu sem við vinnum utan verkefnisins sem við notum í að keyra verkefnið áfram. Því leita ég til ykkar. Ef þið getið séð af smá aurum fyrir verkefnið, ekki fyrir okkur, heldur fyrir geimrannsóknir og þróun!
Hér er kynnningarbréfið mitt:

Góðan daginn. 

Samúel heiti ég og er að vinna samstarfsverkefni með listamanninum Heins Kim og kvikmyndagerðamanninum Eric Adamsons.

Við rekum samtök (Nonprofit organization) sem nefnist Bifröst Corporation og erum að vinna verkefni sem kallast Bifröst The Aurora Project. Þetta verkefni byrjaði sem hugmynd um hvernig hægt væri að ná myndum innan úr norðurljósabeltinu, en hefur nú undið upp á sig og við erum að vinna að heimildarmynd um ferlið frá því að hugmyndin fæddist þar til við náum takmarki okkar, að efla samstarf milli háskóla víðsvegar um heim sem og efla hönnun og tækniþekkingu.

 

Þetta er langtímaverkefni þar sem verið er að sameina marga háskóla víðsvegar um heiminn til samstarfs ásamt því að efla alþjóðleg tengsl íslenskra háskóla. Við erum nú þegar að vinna að því að koma á samstarfi milli Háskólans Í Reykjavík og háskólans Í Púertó Ríkó, og er stefnan að HR sjái um hönnun og smíði hluta af tækjabúnaði eldflaugaskots næsta árs.

 

Við höfum nú þegar talsverðar reynslu og unnið ýmislegt, meðal annars styrkt eldflaugaskotið Mjölnir sem Háskólinn í Reykjavík gerði í maí á síðastliðnu ári. Á hverju ári höfum við þokast nær takmarki okkar, og nú er það svo að við erum að vinna verkefni með háskólanum í Púerto Ríkó þar sem ætlunin er að senda Sony A7S hágæðamyndavél upp í allt að 200km hæð í eldflaug sem NASA leggur til í tengslum við sérstakt verkefni fyrir háskóla í Bandaríkjunum núna í Ágúst.

Þetta kostar það að ég þarf að vera úti í Púerto ríkó meirihlutann af sumrinu við að aðstoða nemendur þar við smíði og samsetningu á tækjabúnaði sem við erum að nota í flugið.

 

Ég er því að leita að styrkjum til að styrkja mína dvöl þar úti nú í sumar.

Kostnaðaráætlun hljómar uppá 1200þús kr þar sem kaupa þarf meiri búnað til notkunar við flugið sem og flugmiðar, uppihald, gisting og fleira. Verði eitthvað fé umfram eftir sumarið verður það notað í áframhaldandi vinnu með Háskólanum í Reykjavík. Ég er að reyna að finna styrktarleiðir til að létta undir þeim kostnaði. Ef þið hafið áhuga á að styðja við bakið á verkefninu munum við meta það mikils, hversu há upphæðin sem þið getið styrkt mig kann að vera.

 

Það sem þið fáið út úr þessu:

Nafn fyrirtækisins kemur auðvitað við sögu í því sem gæti mótað framtíðar geimferðaráætlun Íslands, bæði fyrir og eftir ferðina mun ég bjóða upp á fyrirlestra í þeim fyrirtækjum sem styðja við mig þar sem ég mun kynna verkefnið ítarlega.

 

Kynningarmyndband má sjá á vefsíðunni okkar: http://www.bifrostaurora.org/videotrailer/

Einnig eru þar myndir af fyrri verkefnum okkar.

 

Virðingarfyllst,

Samúel Þór Hjaltalín Guðjónsson.

Þið sem viljið leggja mér liðveislu, getið lagt inn á reikning 0186-26-528 kt 280587-3119 tilvísun: Bifröst

Posted in Almennt | Leave a comment

It’s all so hard.

No one told us it was going to be easy, as we entered this world, against our will.
We cried, we wept, we grew, we gained experience and we matured amongst other things we felt.
Later we realized that life wasn’t bed of roses, but there never is a rose without thorns.
Life isn’t easy, it’s quite hard, but we mustn’t give up hope, and we must continue to grow, to experience, to mature. That’s something we’ll never stop doing.

A very sage person once said:
“We set sail on this new sea because there is new knowledge to be gained, and new rights to be won, and they must be won and used for the progress of all people. For space science, like nuclear science and all technology, has no conscience of its own. Whether it will become a force for good or ill depends on man, and only if the United States occupies a position of pre-eminence can we help decide whether this new ocean will be a sea of peace or a new terrifying theater of war. I do not say that we should or will go unprotected against the hostile misuse of space any more than we go unprotected against the hostile use of land or sea, but I do say that space can be explored and mastered without feeding the fires of war, without repeating the mistakes that man has made in extending his writ around this globe of ours.

There is no strife, no prejudice, no national conflict in outer space as yet. Its hazards are hostile to us all. Its conquest deserves the best of all mankind, and its opportunity for peaceful cooperation may never come again. But why, some say, the Moon? Why choose this as our goal? And they may well ask, why climb the highest mountain? Why, 35 years ago, fly the Atlantic? …

We choose to go to the Moon! … We choose to go to the Moon in this decade and do the other things, not because they are easy, but because they are hard; because that goal will serve to organize and measure the best of our energies and skills, because that challenge is one that we are willing to accept, one we are unwilling to postpone, and one we intend to win …”

This speech is of course the famous “Moon” speech of John F. Kennedy.
Imagine that we had just flew the Atlantic 35 years earlier at that point, but mankind went to the Moon. As with all obstacles in life, they can be passed, and we must not give up.

There’s always hope, even if we sometimes can’t see it, and even though the darkness looks overwhelming…

Posted in Almennt | Leave a comment