— 3 min read

Arduino easiness!

An update on the Arduino drivers

Yesterday I posted an update to Kickstarter with the news that the libarary and driver for the NextDev was complete. A number of you asked me for a code example.

 1 #include "NextDev.h"
 3 NextDev disp;
 5 void setup() {
 6   Serial.begin(9600);
 7   disp.begin();
 8   Serial.print("Current page = ");
 9   Serial.println(disp.getPage());
10   Serial.print("Screen brightness = ");
11   Serial.println(disp.getIntVariable("dim"));
12 }
14 void loop() {
16     if (disp.available()) {
17        Event e = disp.getEvent();
18        String v = "b";
19        v.concat(e.getComponent() - 1);
20        v.concat(".txt");
22        Serial.println("Value for " + v + " = " + disp.getStringVariable(v));
23     }
25 }

So, imagine you have designed a UI that looks like this via the Nextion editor:

The Nextion allows you to assign names to each of the components on a page (in fact, the page itself can have a textual name). In this example I named the button components “b1”, “b2”, “b3” and “b4”. I set the text values to be “Relay1” through 4. This is all using the Nextion editor app on my windows desktop. Then I saved the .HMI file and clicked “Compile”. The editor compiled my UI and generated a “.TFT” file. I then uploaded this to my display. It’s not sexy but it shows the “nuts and bolts” behind the example (I’m going to use fancy cropped images in a “real” app). Next, I fired up my Auduino editor on my Mac and wrote the sketch above and flashed it to my NextDev board (connected by a simple USB cable). Finally, I clicked “Serial Monitor” on the “Tools” tab. It showed the following output:

Current page = 0
Screen brightness = 100

I clicked each of the buttons (1-4) on the display and got the following:

Current page = 0
Screen brightness = 100
Value for b0.txt = Relay 1
Value for b1.txt = Relay 2
Value for b2.txt = Relay 3
Value for b3.txt = Relay 4

Of course, I need to trigger some sort of action like send a message over WiFi to turn appliances on/off. I have a “Sonoff” WiFi enabled power switch coming in the mail from iTead. For a whopping $5!

More coming soon! Guy