mc-Things

mc-Innovation Showcase => mc-Innovations => Topic started by: kristofferis on June 15, 2016, 03:04:40 am

Title: mcModules goes touch
Post by: kristofferis on June 15, 2016, 03:04:40 am
Hello.
Here is my first project contribution to this forum.

I use several Android tablets mounted on walls in my house to control and verify things and the communication i use there is MQTT so mcModules MQTT support feels very natural for me. Something that i need is very small touch screen and the possibility to use them to send MQTT, one example could be a little 2.4" screen next to the light switch that could control the Sonos (http://www.sonos.com) in that room.
The problem I had is that I need everything to be small so that it easily could be fit inside a case or inside the wall behind the screen.

mcModule is the perfect device for this, it is very small and it has MQTT and Uart serial communication that is all you need to have this up and running.

I Am providing a simple example and also a short video so that you could see it in action.

Code loaded to mcModule
Code: [Select]
Define PinMode Pin1 As DigitalInput //RX
Define PinMode Pin3 As DigitalOutput //TX

Class NextionTest
    Shared serial As Uart
    Shared Event Boot()
        // Enable serial communication
        serial = Uart.Create(9600, Pin.Pin3, Pin.Pin1)
    End Event
   
   
    Shared Event Uart0Receive()
        //Data is received
        Dim chr As Integer = serial.Read
        If chr = 1 Then
            //Button 1 clicked
            //Turn on Green LED
            LedGreen = True         
        ElseIf chr = 2 Then
            //Button 2 clicked
            //Turn off Green LED
            LedGreen = False
        ElseIf chr = 3 Then
            //Button 3 clicked
            //Turn on or off Red LED
            LedRed = Not LedRed
        End If     
    End Event
End Class

Video (https://youtu.be/ykH_takVCIM)

Please ask questions about this and I will do my best to answer them.
Title: Re: mcModules goes touch
Post by: mc-T2 on June 15, 2016, 12:47:18 pm
Very cool Kristofferis! That is awesome and a good showcase of some of the ways that mc-Modules and the mc-Things platform can be used! Please keep us updated as you progress with your projects, we are excited to see more of your use-cases!!
Title: Re: mcModules goes touch
Post by: kristofferis on June 15, 2016, 12:59:12 pm
Thanks T2.
I will alao update the code to show how you could update the display with information that is coming to the mcModule with MQTT and also show how the touchscreen could make something happen on another mcModule with MQTT.

I have to say that's it pretty cool that with only two cables from the screen have communication to any device in the world (MQTT)
Title: Re: mcModules goes touch
Post by: plains203 on June 24, 2016, 04:14:08 am
Pretty interested to know what touch screen module your using here.

Cheers
Title: Re: mcModules goes touch
Post by: kristofferis on June 24, 2016, 04:45:34 am
Hello.

The Touchscreen that I am using in the above example is a Nextion (http://wiki.iteadstudio.com/Nextion_HMI_Solution) and the exact model that i used is NX3224T024
It is a 2.4" screen and as you could see in my code it is very simple to communicate with it.
Title: Re: mcModules goes touch
Post by: EthanSpitz on August 03, 2016, 09:54:41 pm
How did you get the display up and running? It's not clear from the code.
Title: Re: mcModules goes touch
Post by: kristofferis on August 04, 2016, 02:23:59 am
The display is configured with Nextion Editor.
It is a simple tool to drag and drop buttons and things like that.

And then you assign what should happen when the different objects is being pushed.

In this simple example I am just sending 1,2 or 3 from the different buttons on the display.
Title: Re: mcModules goes touch
Post by: Riverrider on August 13, 2016, 12:32:49 pm
Does one need a level shifter to drive the LCD's UART interface from the mcModule?
Title: Re: mcModules goes touch
Post by: kristofferis on August 13, 2016, 12:38:40 pm
The logical level of the display is 3.3V so no need for a level shifter.
Title: Re: mcModules goes touch
Post by: Nick_W on September 09, 2016, 10:56:51 pm
I really like this application. I just received a couple of nexion displays (and made a couple of bezels for them).

The display is crazy hard to figure out how to program though. Thei designer is confusing as all hell.

Are you willing to share your files (say on github) so I can get a starting point for this.

It looks like I have to make a bitmap for everything (buttons etc), then upload the whole lot to the display. I have files for the demo that comes with the display, but it looks like a ton of work to get it going.

Any help to get me started would be appreciated.
Title: Re: mcModules goes touch
Post by: kristofferis on September 10, 2016, 08:55:03 am
Hello.
I will do my best to help you with this.

I am using Nextion Editor Version 0.38
In that program you drag and drop different "functions" like buttons, text and things like that.
And when you are done you compile and that file should be installed on the display.
You could also debug the project without the need of an display, a very nice feature.

I have uploaded one project on GitHub that will get you started https://github.com/Isaksson/Nextion (https://github.com/Isaksson/Nextion)
It's a project for monitoring temperature and battery.

I have included all images and fonts needed for the project.

Try this and let me know how it goes.
Title: Re: mcModules goes touch
Post by: Nick_W on September 10, 2016, 12:37:46 pm
Thanks!

I've downloaded the latest Nextion editor, and loaded your project. I'm having a little trouble communicating with the display (I'll figure that out).

Do you have the mcThings program to share that you use for communicating with the display? I've never used the UART interface before, and it would be helpful.

Thanks again. I'll let you know how it goes.
Title: Re: mcModules goes touch
Post by: kristofferis on September 10, 2016, 01:05:56 pm
That sounds good  :)

Sure, I pushed some code to the same repository (this code only send data to the display it does not receive any data)

I have made some changes in the code that i not have verified but that does not matter, you will see how it is done.
Receiving data is not any problem, just ask if you need anything about that.
Title: Re: mcModules goes touch
Post by: Nick_W on September 10, 2016, 01:26:51 pm
Great!

I'll take a look. Any hints on how to upload to the display from the editor? I've set the model (2.4" enhanced), and I'm connected via an FTDI cable. - display powers up and works OK. I have TX to RX and RX to TX (and tried the other way round). Cable shows up at COM7, which I can select, but no communications when I try to upload (goes through the baud rates, then says "Connection failed Forced interrupt!").

I can try the SD card route next, but the serial connection should be simple. I know my cable works (used it on enough other projects).

Bit puzzling. maybe I'll reboot and see what happens...
Title: Re: mcModules goes touch
Post by: kristofferis on September 10, 2016, 01:30:46 pm
I have only used the sd-card way of moving my files.
So i don't know, but to move the file with sd-card works good enough, I debug in the emulator so no need for the actual device there.
Title: Re: mcModules goes touch
Post by: Nick_W on September 10, 2016, 02:43:10 pm
Ok,

SD card upload worked.

The mcThings script is very helpful, the Nextion class will be very useful.

Haven't got it working yet, but my UART wires are connected to pin 1 and 2, not 1 and 3 like you have. I may have some rewiring to do.

Thanks for all the help. I'll let you know when I have some results.
Title: Re: mcModules goes touch
Post by: kristofferis on September 10, 2016, 02:45:07 pm
You could use any pin you like so no need for you to change, just update the code with your pin.
Title: Re: mcModules goes touch
Post by: Nick_W on September 10, 2016, 02:47:30 pm
Huh, I'm wired to pins 9 and 10, not 1 and 2. I should be able to reconfigure without rewiring.

I'll give this another go tomorrow (daughters birthday - have to pack in for now).
Title: Re: mcModules goes touch
Post by: Nick_W on September 11, 2016, 03:28:13 pm
Update.

After a lot of messing around (isn't it always the way) I finally determined that my "known working" FTDI cable is in fact not transmitting correctly.

I did manage to figure out the Nextion Editor. It's actually a simple concept presented in the most confusing way possible (and with "chinenglish" instructions). So I made myself a button that just transmits a "hello" message, and I can see it on a serial monitor! But sending data the other way - nothing...

So, find my backup FTDI connector, and spend a few hours sorting out Windows driver conflicts, and now it works!

I can actually upload from the editor, debug with the display connected, and upload via a python script.

Now back to the mcThings module.

More problems. It seems the "you can define any pin as anything" theory is not quite so much. Only pins 0 to 8 are defined as pins, the other pins are "allocated" to specific other things. The pins I wired (not sure why I wired it this way) 9 and 10 are allocated to the Green and Red LED. So I have to allocate the UART to RedLed and GreenLed - you can't rename them, or allocate them as inputs/outputs, despite what the documentation says.

It is all a bit baffling, and doesn't work. I may have to rewire, as I suspect the LEDs are defined as outputs, and I can't change them.

So progress in that the display is tested and working (and I understand the editor a bit better now).

Stuck in that the pin allocations on the module are confusing.

Will break out the soldering iron tomorrow. I have scored some rare-as-hens-teeth 0.05" headers, plus a difficult-to-find 0.05" to 0.1" adapter board, so I was going to wire one of these anyway.
Title: Re: mcModules goes touch
Post by: kristofferis on September 12, 2016, 07:00:59 am
Sounds good that you got it to work.

About the pins, when i check the documentation for the mcmodule then it says that Pin 0 - 8 are  General Purpose Input/Output Pins so the documentation seems right?
Title: Re: mcModules goes touch
Post by: Nick_W on September 12, 2016, 07:27:31 am
I guess it depends where you read the documentation. P70 has a table that shows pins 9 and 10 as I/O pins.
Title: Re: mcModules goes touch
Post by: Nick_W on September 12, 2016, 07:59:29 am
Ok I have confused myself again.

I was basing the theory that pins 9 and 10 are the LED's, as the IDE shows the pin object as having pin 9 and 10 connected to the LED. The documentation (mcScript) has a table showing pin 9 and 10 as general I/O and p114 has the pin object defined as this:
Quote
Const Pin0 As Pin = 0
Const Pin1 As Pin = 1
Const Pin2 As Pin = 2
Const Pin3 As Pin = 3
Const Pin4 As Pin = 4
Const Pin5 As Pin = 5
Const Pin6 As Pin = 6
Const Pin7 As Pin = 7
Const Pin8 As Pin = 8
Const Pin9 As Pin = 9
Const Pin10 As Pin = 10
Const LedRed As Pin = 11
Const LedGreen As Pin = 12
Const Button As Pin = 13
Const ReedSwitch As Pin = 14

Which does not match what the IDE says (pin9 and pin10 are not valid pin constants).

Looking at the "product specification", It gives yet another scenario, where the physical pins do not correspond to the software pins. P9 has this table:

Quote
Pin Name Type Function
1 PIN6_D6 I/O General Purpose I/O 6
2 PIN5_D5 I/O General Purpose I/O 5
3 PIN4_D4 I/O General Purpose I/O 4
4 GND G Ground
5 PIN3_D3_A3 I/O General Purpose I/O 3 (Analog Input)
6 PIN2_D2_A2 I/O General Purpose I/O 2 (Analog Input)
7 PIN1_D1_A1 I/O General Purpose I/O 1 (Analog Input)
8 PIN0_D0_A0 I/O General Purpose I/O 0 (Analog Input)
9 PIN7_D7_A7 I/O General Purpose I/O 7 (Analog Input through OpAmp)
10 PIN8_D8_A8 I/O General Purpose I/O 8 (Analog Input through OpAmp)

So pins 9 and 10 are actually pins 7 and 8. How hard would it be to have the physical pins match the pin names? especially as you get to define the pin names???

The table from P70 of the mcScript documentation says:
Quote
The PinName is Pin0 to PinN where N is the number of pins minus 1. The Alias is optional and can be used to give the pin a more descriptive name. The PinMode describes what the pin does. Not all pins support all PinModes. See the following table for the mcMod110:

And lists pin 9 and 10 as not having analog input, just general digital I/O. But as pin 4 is ground, the above description cannot be true (unless "number of pins" does not mean "physical pin number"). So I have no idea what all this means.

I'll just have to experiment and see what documentation turns out to be right (maybe they are all wrong...).