Vesc to nextion display

Hi Everyone, I've been trying for several weeks now, and have been to so many pages looking for a solution but i am well and truly stuck and cant find anyone that has done this.

Ive built a very fast brushless razor electric scooter and have designed some dials to display the voltage and amps and so forth. aand maybe nav in the future.

I'm using an uno and have got the nextion display to work on softwareserial on basic sketchs with a pot just to make sure software serial was working.

I am using solid geeks libaries for the vesc and have had that working on the serial monitor but as soon as i add the nextion stuff it stops working and says faied to get data in the serial monitor.

Thanks in advanced

#include <SoftwareSerial.h>
// Master RX, TX, connect to Nextion TX, RX

SoftwareSerial HMISerial(10,11);



#include "Nextion.h"
#include <Nexnumber.h>
#include <buffer.h>
#include <crc.h>
#include <datatypes.h>
#include <VescUart.h>



#include <VescUart.h>

/** Initiate VescUart class */
VescUart UART;




void setup() {

  /** Setup Serial port to display data */
 HMISerial.begin(9600);

  /** Setup UART port (Serial1 on Atmega32u4) */
 Serial.begin(115200);
  
  while (!Serial) {;}

  /** Define which ports to use as UART */
  UART.setSerialPort(&Serial);
}

void loop() {
  
  /** Call the function getVescValues() to acquire data from VESC */
  if ( UART.getVescValues() ) {

   
   HMISerial.print("n0.val=");
   HMISerial.print(UART.data.inpVoltage);
   HMISerial.write(0xff);
   HMISerial.write(0xff);
   HMISerial.write(0xff);   
    

  }
  else
  {
    Serial.println("Failed to get data!");
  }

  delay(50);
}

Sorry about the uploads dont know how to change it. Before someone tears me a new 1 :slight_smile:

Hello scoosh355,
Welcome to the forum.

Sorry about the uploads don't know how to change it. Before someone tears me a new 1 :slight_smile:

There are lots of people here who would be happy to try and help you, including me. However, when someone can't follow simple instructions for using the forum it does rather put people off helping. So, read the instructions that nice Mr. Gammon went to so much trouble to write then try again. Maybe me or someone will be able to help you then. I hope you don't feel torn.

nothing that wont heal, sorted found the modify button

Sorted found the modify button

Right, good, that's much better :slight_smile:

Unfortunately I can't help as I don't know about the Nextion libraries, I don't use them. There are people on here that do and maybe someone will come along and help. Alternatively, try using the methods I set out in 'Using Nextion displays with Arduino', which don't use libraries but do require you to understand the methods I have devised. There is also a link from that tutorial to an improved version of the libraries written by Ray Livingston, which I strongly recommend you try as there are a lot of bugs in the original libraries.

Thanks i am going to try your method now.

I got it working with your method, cant thank you enough. I read through loads of post today and thanks praise and kudos definately needs to be given to you.

I'm gonna go off and design some more pages and buttons for my project and learn some more.

Thanks again I wouldve been totally stuck without the work you put in.

:slight_smile: Thank you :slight_smile:
Glad you are having some success! :slight_smile:

#include <SoftwareSerial.h>

SoftwareSerial HMISerial(10, 11);

#include <VescUart.h>

/** Initiate VescUart class */
VescUart UART;

void setup() {

 HMISerial.begin(9600);

  /** Setup UART port (Serial1 on Atmega32u4) */
  Serial.begin(9600);
  
while (!HMISerial) {;}

  /** Define which ports to use as UART */
  UART.setSerialPort(&Serial);
}

void loop() {
  
  /** Call the function getVescValues() to acquire data from VESC */
  if ( UART.getVescValues() ) {
    
    
    //This displays the voltage 
    HMISerial.print(F("t0.txt=\""));
    HMISerial.print(UART.data.inpVoltage);
    HMISerial.print(F("\""));
    HMISerial.write(0xff);
    HMISerial.write(0xff);
    HMISerial.write(0xff);

    //This displays the Avg cell voltage 
    HMISerial.print(F("t2.txt=\""));
    HMISerial.print(UART.data.inpVoltage/14);
    HMISerial.print(F("\""));
    HMISerial.write(0xff);
    HMISerial.write(0xff);
    HMISerial.write(0xff);

    //This displays the battery amps 
    HMISerial.print(F("t1.txt=\""));
    HMISerial.print(UART.data.avgInputCurrent);
    HMISerial.print(F("\""));
    HMISerial.write(0xff);
    HMISerial.write(0xff);
    HMISerial.write(0xff);

     //This displays the motor amps 
    HMISerial.print(F("t3.txt=\""));
    HMISerial.print(UART.data.avgMotorCurrent);
    HMISerial.print(F("\""));
    HMISerial.write(0xff);
    HMISerial.write(0xff);
    HMISerial.write(0xff);

    //This displays the watts 
    HMISerial.print(F("t4.txt=\""));
    HMISerial.print(UART.data.watt_hours);
    HMISerial.print(F("\""));
    HMISerial.write(0xff);
    HMISerial.write(0xff);
    HMISerial.write(0xff);

     //This displays the distance 
    HMISerial.print(F("t5.txt=\""));
    HMISerial.print(UART.data.tachometerAbs);
    HMISerial.print(F("\""));
    HMISerial.write(0xff);
    HMISerial.write(0xff);
    HMISerial.write(0xff);


    

  }
  
  
}

Hi Perry, I saw a video on youtube where a guy creates a dial but to eliminate the flickering he created several pictures of the dial with the needle going up in small increments, 208 pictures to be exact.

I have created some dials with a progression bar going around the dials, saved the frames into pictures and want the progression bar to go up relative to the amps and volts.

I think i need to create an if statement like.

if(Volts.val=0)
VolltsDial.pic=2

Can it be done with you method?

![](http:// nextion pic )

if(Volts.val=0)

Common mistake, I think you mean:

if(Volts.val==0)

Can it be done with you method?

Yes, however it is a feature of the Nextion language, not particular to my methods. I think, however, that you might have a problem with 208 images, I don't think there is enough memory on a Nextion for that many. The new 'intelligent' displays might be OK, but I haven't bought one and the lovely people at ITEAD have not sent me a free one....

Look at reply #3 of 'Using Nextion displays with Arduino' Download 2019-06-08 Arduino Nextion demo cropped images 43V1.zip
(Note to people reading this in the future, I occasionally update the examples, so, by the time you read this the file might have been changed for an updated one)
Read the text 'Using background images'. Study the example and understand it.

For each of your dial images create a background image, which must be the exact size of your display, so if your display is 480 * 272 pixels then each image must be 480 * 272 pixels. The only difference between each image is the dial, and each dial must be in exactly the same place on each image.

Upload the images to the display.

Over the position of the dial images create an object, probably a picture in your case (other things work, in the example it is buttons). Set the background of the object to picc (cropped image).

Assuming, for example, that your object is a picture with objname of p0.
If you send:

p0.picc=0

It will display picture 0

If you send:

p0.picc=1

It will display picture 1

etc.
(Don't forget the 0xff 0xff 0xff)
(This is slightly different to the example, but the principal is the same)

You might want to try this with 3 or 4 images before doing the whole thing!

Let me know how you get on.

#include <SoftwareSerial.h>

SoftwareSerial HMISerial(10, 11);

#include <VescUart.h>

/** Initiate VescUart class */
VescUart UART;

void setup() {

 HMISerial.begin(9600);

  /** Setup UART port (Serial1 on Atmega32u4) */
  Serial.begin(9600);
  
while (!HMISerial) {;}

  /** Define which ports to use as UART */
  UART.setSerialPort(&Serial);
}

void loop() {
  
  /** Call the function getVescValues() to acquire data from VESC */
  if ( UART.getVescValues() ) {
    
    
    //This displays the voltage 
    
    HMISerial.print(F("t0.txt=\""));
    HMISerial.print(UART.data.inpVoltage);
    HMISerial.print(F("\""));
    HMISerial.write(0xff);
    HMISerial.write(0xff);
    HMISerial.write(0xff);

    HMISerial.print(F("t1.txt=\""));
    HMISerial.print(UART.data.inpVoltage/14);
    HMISerial.print(F("\""));
    HMISerial.write(0xff);
    HMISerial.write(0xff);
    HMISerial.write(0xff);

    
    //This displays the battery amps 
    HMISerial.print(F("t2.txt=\""));
    HMISerial.print(UART.data.avgInputCurrent);
    HMISerial.print(F("\""));
    HMISerial.write(0xff);
    HMISerial.write(0xff);
    HMISerial.write(0xff);
    
    

     //This displays the motor amps 
    HMISerial.print(F("t3.txt=\""));
    HMISerial.print(UART.data.avgMotorCurrent);
    HMISerial.print(F("\""));
    HMISerial.write(0xff);
    HMISerial.write(0xff);
    HMISerial.write(0xff);

  }

  
  /** Call the function getVescValues() to acquire data from VESC */
  if ( UART.getVescValues() ) {
    

   if((UART.data.inpVoltage==50.00>47.50));
   HMISerial.print(F("p0.pic=7"));
  }
   else{
   HMISerial.print(F("p0.pic=0"));
   HMISerial.write(0xff);
   HMISerial.write(0xff);
   HMISerial.write(0xff);

  }




  }

So..... lol. My head is hurting, cant seem to get it to work. It does nothing with the else statement and if i remove the else statement it displays the pic no matter what value i set if((UART.data.inpVoltage==50.00>47.50)); to.

#include <VescUart.h>

Unfortunately I have never seen this before today, I don't know what it does or what to expect from it, so can't help with it unless you explain it.

   if((UART.data.inpVoltage==50.00>47.50));
   HMISerial.print(F("p0.pic=7"));

I think you missed a few things from that, such as some { } and some 0xffs. Look at the previous if, that one is correct, this one is never going to do what you want.

If you are not sure if the correct data is being send to the Nextion replace

HMISerial.print

With

Serial.print

So the data gets sent to the serial monitor, then you can see what is being sent.

#ifndef _VESCUART_h
#define _VESCUART_h

#include <Arduino.h>
#include "datatypes.h"
#include "buffer.h"
#include "crc.h"

class VescUart
{
	/** Struct to store the telemetry data returned by the VESC */
	struct dataPackage {
		float avgMotorCurrent;
		float avgInputCurrent;
		float dutyCycleNow;
		long rpm;
		float inpVoltage;
		float ampHours;
		float ampHoursCharged;
		long tachometer;
		long tachometerAbs;
	};

	/** Struct to hold the nunchuck values to send over UART */
	struct nunchuckPackage {
		int	valueX;
		int	valueY;
		bool upperButton; // valUpperButton
		bool lowerButton; // valLowerButton
	};


	public:
		/**
		 * @brief      Class constructor
		 */
		VescUart(void);

		/** Variabel to hold measurements returned from VESC */
		dataPackage data; 

		/** Variabel to hold nunchuck values */
		nunchuckPackage nunchuck; 

		/**
		 * @brief      Set the serial port for uart communication
		 * @param      port  - Reference to Serial port (pointer) 
		 */
		void setSerialPort(HardwareSerial* port);

		/**
		 * @brief      Set the serial port for debugging
		 * @param      port  - Reference to Serial port (pointer) 
		 */
		void setDebugPort(Stream* port);

		/**
		 * @brief      Sends a command to VESC and stores the returned data
		 *
		 * @return     True if successfull otherwise false
		 */
		bool getVescValues(void);

		/**
		 * @brief      Sends values for joystick and buttons to the nunchuck app
		 */
		void setNunchuckValues(void);

		/**
		 * @brief      Set the current to drive the motor
		 * @param      current  - The current to apply
		 */
		void setCurrent(float current);

		/**
		 * @brief      Set the current to brake the motor
		 * @param      brakeCurrent  - The current to apply
		 */
		void setBrakeCurrent(float brakeCurrent);

		/**
		 * @brief      Set the rpm of the motor
		 * @param      rpm  - The desired RPM (actually eRPM = RPM * poles)
		 */
		void setRPM(float rpm);

		/**
		 * @brief      Set the duty of the motor
		 * @param      duty  - The desired duty (0.0-1.0)
		 */
		void setDuty(float duty);

		/**
		 * @brief      Help Function to print struct dataPackage over Serial for Debug
		 */
		void printVescValues(void);

	private: 

		/** Variabel to hold the reference to the Serial object to use for UART */
		HardwareSerial* serialPort = NULL;

		/** Variabel to hold the reference to the Serial object to use for debugging. 
		  * Uses the class Stream instead of HarwareSerial */
		Stream* debugPort = NULL;

		/**
		 * @brief      Packs the payload and sends it over Serial
		 *
		 * @param      payload  - The payload as a unit8_t Array with length of int lenPayload
		 * @param      lenPay   - Length of payload
		 * @return     The number of bytes send
		 */
		int packSendPayload(uint8_t * payload, int lenPay);

		/**
		 * @brief      Receives the message over Serial
		 *
		 * @param      payloadReceived  - The received payload as a unit8_t Array
		 * @return     The number of bytes receeived within the payload
		 */
		int receiveUartMessage(uint8_t * payloadReceived);

		/**
		 * @brief      Verifies the message (CRC-16) and extracts the payload
		 *
		 * @param      message  - The received UART message
		 * @param      lenMes   - The lenght of the message
		 * @param      payload  - The final payload ready to extract data from
		 * @return     True if the process was a success
		 */
		bool unpackPayload(uint8_t * message, int lenMes, uint8_t * payload);

		/**
		 * @brief      Extracts the data from the received payload
		 *
		 * @param      message  - The payload to extract data from
		 * @return     True if the process was a success
		 */
		bool processReadPacket(uint8_t * message);

		/**
		 * @brief      Help Function to print uint8_t array over Serial for Debug
		 *
		 * @param      data  - Data array to print
		 * @param      len   - Lenght of the array to print
		 */
		void serialPrint(uint8_t * data, int len);

};

#endif
#include <SoftwareSerial.h>

SoftwareSerial HMISerial(10, 11);

#include <VescUart.h>

/** Initiate VescUart class */
VescUart UART;



void setup() {

 HMISerial.begin(9600);

  /** Setup UART port (Serial1 on Atmega32u4) */
  Serial.begin(9600);
  
while (!HMISerial) {;}

  /** Define which ports to use as UART */
  UART.setSerialPort(&Serial);
}

  
void loop() {
  
  /** Call the function getVescValues() to acquire data from VESC */
  if ( UART.getVescValues() ) {
    
    
    //This displays the voltage 
    Serial.println("Voltage");
    Serial.println(UART.data.inpVoltage);
    HMISerial.print(F("t0.txt=\""));
    HMISerial.print(UART.data.inpVoltage);
    HMISerial.print(F("\""));
    HMISerial.write(0xff);
    HMISerial.write(0xff);
    HMISerial.write(0xff);

    //This displays the average cells voltage
    
    Serial.println("Avg Cell Voltage");
    Serial.println(UART.data.inpVoltage/14);
    HMISerial.print(F("t1.txt=\""));
    HMISerial.print(UART.data.inpVoltage/14);
    HMISerial.print(F("\""));
    HMISerial.write(0xff);
    HMISerial.write(0xff);
    HMISerial.write(0xff);

    
    //This displays the battery amps 
    
    Serial.println("Battery Amps");
    Serial.println(UART.data.avgInputCurrent);
    HMISerial.print(F("t2.txt=\""));
    HMISerial.print(UART.data.avgInputCurrent);
    HMISerial.print(F("\""));
    HMISerial.write(0xff);
    HMISerial.write(0xff);
    HMISerial.write(0xff);
    
    

     //This displays the motor amps 

    Serial.println("Motor Current");
    Serial.println(UART.data.avgMotorCurrent);
    HMISerial.print(F("t3.txt=\""));
    HMISerial.print(UART.data.avgMotorCurrent);
    HMISerial.print(F("\""));
    HMISerial.write(0xff);
    HMISerial.write(0xff);
    HMISerial.write(0xff);

  }

  
  /** Call the function getVescValues() to acquire data from VESC */
  if ( UART.getVescValues() ) {
    
      {
       if((UART.data.inpVoltage==60.00>57.50));
       Serial.print("p0.pic=7");
       HMISerial.print(F("p0.pic=7"));
       HMISerial.write(0xff);
       HMISerial.write(0xff);
       HMISerial.write(0xff);
  
   }
      }  
      else
       {
       Serial.print("p0.pic=0");
       HMISerial.print(F("p0.pic=0"));
       HMISerial.write(0xff);
       HMISerial.write(0xff);
       HMISerial.write(0xff);
    
      }




  }

Sorry that's tiredness setting in, i did have 1 that compiled. I couldn't explain the vesc h file.

All the values in my text boxes are correct and i've now put them in the serial monitor to check and its good.
my bat is only charged to 49v so i changed it to ==60>57.50 to see if the else statement would work and it display pic 0 but it still displays pic 7 like the if statement wasn't there. what am i missing

It still displays pic 7 like the if statement wasn't there.

Well it would do, entirely to be expected.

That's tiredness setting in.

The mistake you have made is the kind I sometimes make when tired, then when I come back after some sleep I kick myself for not seeing the problem. I have actually already pointed out the problem, please read again my previous post, the answer is in there.

I copied and pasted the if statement and changed it but it still doesn't work. I cant see the error I tried all kind of combinations.
This is my first arduino project and starting to feel like i bitten of more than i can chew.

If UART.data.inpVoltage is a float do i have to create another float to compare it to?

I copied and pasted the if statement and changed it but it still doesn't work. I cant see the error I tried all kind of combinations.

You realise that I am deliberately not just giving you the answer because I want you to think about it don't you?

You have 3 if statements in your code, 2 are correct, one is not. The one you are having problems with is the one that does not follow the correct syntax, actually, that's not quite true, it does follow correct C syntax (which is why the compiler is not giving you an error), but the way you have written it won't work the way you expect, which is what you are experiencing.

This one is correct:

  if ( UART.getVescValues() ) {
    
    
    //This displays the voltage 
    Serial.println("Voltage");
    Serial.println(UART.data.inpVoltage);
    HMISerial.print(F("t0.txt=\""));
    HMISerial.print(UART.data.inpVoltage);
    HMISerial.print(F("\""));
    HMISerial.write(0xff);
    HMISerial.write(0xff);
    HMISerial.write(0xff);

    //This displays the average cells voltage
    
    Serial.println("Avg Cell Voltage");
    Serial.println(UART.data.inpVoltage/14);
    HMISerial.print(F("t1.txt=\""));
    HMISerial.print(UART.data.inpVoltage/14);
    HMISerial.print(F("\""));
    HMISerial.write(0xff);
    HMISerial.write(0xff);
    HMISerial.write(0xff);

    
    //This displays the battery amps 
    
    Serial.println("Battery Amps");
    Serial.println(UART.data.avgInputCurrent);
    HMISerial.print(F("t2.txt=\""));
    HMISerial.print(UART.data.avgInputCurrent);
    HMISerial.print(F("\""));
    HMISerial.write(0xff);
    HMISerial.write(0xff);
    HMISerial.write(0xff);
    //This displays the motor amps 
Serial.println("Motor Current");
    Serial.println(UART.data.avgMotorCurrent);
    HMISerial.print(F("t3.txt=\""));
    HMISerial.print(UART.data.avgMotorCurrent);
    HMISerial.print(F("\""));
    HMISerial.write(0xff);
    HMISerial.write(0xff);
    HMISerial.write(0xff);

  }
1 Like

Quote

I copied and pasted the if statement and changed it but it still doesn't work. I cant see the error I tried all kind of combinations.

You realise that I am deliberately not just giving you the answer because I want you to think about it don't you?

You have 3 if statements in your code, the first 2 are correct, the last one is not. The one you are having problems with is the one that does not follow the correct syntax, actually, that's not quite true, it does follow correct C syntax (which is why the compiler is not giving you an error), but the way you have written it won't work the way you expect, which is what you are experiencing.

I suggest on your next post to me, if you've not spotted the error, you show me how you think an if statement should be written and compare it to the one you are having problems with. I feel I should point out that I have given you the answer in post #11.

yh, i know i gotta learn and no way better and appreciate your time, back to youtube for some more top tech boy.

This might help:
If statement tutorial

#include <buffer.h>
#include <crc.h>
#include <datatypes.h>
#include <VescUart.h>

#include <SoftwareSerial.h>

SoftwareSerial HMISerial(10, 11);

#include <VescUart.h>

/** Initiate VescUart class */
VescUart UART;



void setup() {

 HMISerial.begin(9600);

  /** Setup UART port (Serial1 on Atmega32u4) */
  Serial.begin(9600);
  
while (!HMISerial) {;}

  /** Define which ports to use as UART */
  UART.setSerialPort(&Serial);
}

  
void loop() {




  
  /** Call the function getVescValues() to acquire data from VESC */
  if ( UART.getVescValues() ) {
    
   
    
    //This displays the voltage 
    if ( UART.data.inpVoltage < 59.00 ) {
      Serial.println(" successful ");
      HMISerial.print(F("p1.pic=29"));
      HMISerial.write(0xff);
      HMISerial.write(0xff);
      HMISerial.write(0xff);
      }else{
        Serial.println(" Failed ");
        HMISerial.print(F("p1.pic=0"));
        HMISerial.write(0xff);
        HMISerial.write(0xff);
        HMISerial.write(0xff);
      }

   
    Serial.println("Voltage");
    Serial.println(UART.data.inpVoltage);
    HMISerial.print(F("t0.txt=\""));
    HMISerial.print(UART.data.inpVoltage);
    HMISerial.print(F("\""));
    HMISerial.write(0xff);
    HMISerial.write(0xff);
    HMISerial.write(0xff);

      


   
    //This displays the average cells voltage
    
    Serial.println("Avg Cell Voltage");
    Serial.println(UART.data.inpVoltage/14);
    HMISerial.print(F("t1.txt=\""));
    HMISerial.print(UART.data.inpVoltage/14);
    HMISerial.print(F("\""));
    HMISerial.write(0xff);
    HMISerial.write(0xff);
    HMISerial.write(0xff);

    
    //This displays the battery amps 

    if ( UART.data.avgInputCurrent < 1.00 ) {
          Serial.println(" successful ");
          HMISerial.print(F("p1.pic=3"));
          HMISerial.write(0xff);
          HMISerial.write(0xff);
          HMISerial.write(0xff);
          }else{
            Serial.println(" Failed ");
            HMISerial.print(F("p1.pic=22"));
            HMISerial.write(0xff);
            HMISerial.write(0xff);
            HMISerial.write(0xff);
          }
    
    Serial.println("Battery Amps");
    Serial.println(UART.data.avgInputCurrent);
    HMISerial.print(F("t2.txt=\""));
    HMISerial.print(UART.data.avgInputCurrent);
    HMISerial.print(F("\""));
    HMISerial.write(0xff);
    HMISerial.write(0xff);
    HMISerial.write(0xff);
    
    

     //This displays the motor amps 

    Serial.println("Motor Current");
    Serial.println(UART.data.avgMotorCurrent);
    HMISerial.print(F("t3.txt=\""));
    HMISerial.print(UART.data.avgMotorCurrent);
    HMISerial.print(F("\""));
    HMISerial.write(0xff);
    HMISerial.write(0xff);
    HMISerial.write(0xff);

  
    
   }

  }

I got it to work on the amps side but the volts side wont work right.
I added a successful and failed line to the if and else statements so i could see what was going on in the serial monitor and did a test to see if it would change if the statement was false. It did what it was supposed to do on the amps side and the volts side but when the statement was true on the volt side nothing happens it doesn't display the pic it supposed to and the last pic is still shown, however it says successful in the serial monitor.

1 Like