J’ai réalisé une vidéo sur mon projet de télescope à réalité augmentée. Je l’ai présenté en long et en large dans les articles précédents. L’ajout dont je n’ai pas parlé est l’arduino et le BMP085 (un capteur de température et de pression), permettant d’afficher sur l’écran LCD les données de température et d’altitude en temps réel. L’arduino envoie une commande par un port série à la carte Nextion, qui actualise les données affichées à l’écran. Le code arduino, basé sur les exemples de Nextion (je n’arrive plus à retrouver le lien) est le suivant :
#include <SoftwareSerial.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP085.h>
SoftwareSerial mySerial(10, 11); /*Even though you can use the hardware serial port in this case I think it is better to
leave the hardware serial open for debugging purposes*/
Adafruit_BMP085 bmp = Adafruit_BMP085(10085);
float temperature;
void setup() {
Serial.begin(57600); //open the hardware serial port
while (!Serial) { // wait for serial port to connect. Needed for native USB port only
;
}
/* Initialise the sensor */
if(!bmp.begin())
{
/* There was a problem detecting the BMP085 … check your connections */
Serial.print(« Ooops, no BMP085 detected … Check your wiring or I2C ADDR! »);
while(1);
}
Serial.println(« Serial On »); //Print this messages when the serial port is connected
mySerial.begin(9600); // set the data rate for the SoftwareSerial port
}
void loop() {
/* Get a new sensor event */
sensors_event_t event;
bmp.getEvent(&event);
/* Display the results (barometric pressure is measure in hPa) */
if (event.pressure)
{
/* Display atmospheric pressue in hPa */
Serial.print(« Pressure: « );
Serial.print(event.pressure);
Serial.println( » hPa »);
/* Calculating altitude with reasonable accuracy requires pressure *
* sea level pressure for your position at the moment the data is *
* converted, as well as the ambient temperature in degress *
* celcius. If you don’t have these values, a ‘generic’ value of *
* 1013.25 hPa can be used (defined as SENSORS_PRESSURE_SEALEVELHPA *
* in sensors.h), but this isn’t ideal and will give variable *
* results from one day to the next. *
* *
* You can usually find the current SLP value by looking at weather *
* websites or from environmental information centers near any major *
* airport. *
* *
* For example, for Paris, France you can check the current mean *
* pressure and sea level at: http://bit.ly/16Au8ol */
/* First we get the current temperature from the BMP085 */
float temperature;
bmp.getTemperature(&temperature);
Serial.print(« Temperature: « );
Serial.print(temperature);
Serial.println( » C »);
/* Then convert the atmospheric pressure, SLP and temp to altitude */
/* Update this next line with the current SLP for better results */
float seaLevelPressure = SENSORS_PRESSURE_SEALEVELHPA;
Serial.print(« Altitude: « );
float alt = bmp.pressureToAltitude(seaLevelPressure,
event.pressure,
temperature);
Serial.print(alt);
Serial.println( » m »);
Serial.println(« »);
String sendThis = « »; //Declare and initialise the string we will send
delay(300); //Probably unneccessary, but I give the screen some time to respond
sendThis = « n0.val= »; //Build the part of the string that we know
sendThis.concat(int(temperature)); //Add the variable we want to send
writeString(sendThis); /*Use a function to write the message character by character to the Nextion because
mySerial.write(sendThis) gives you an error due to a datatype mismatch*/
delay(300); //Probably unneccessary, but I give the screen some time to respond
sendThis = « n1.val= »; //Build the part of the string that we know
sendThis.concat(int(alt)); //Add the variable we want to send
writeString(sendThis); /*Use a function to write the message character by character to the Nextion because
mySerial.write(sendThis) gives you an error due to a datatype mismatch*/
}
else
{
Serial.println(« Sensor error »);
}
}
//NOTE: A great big thanks to: RamjetX for writing this function. You can find his/her post here: http://forum.arduino.cc/index.php?topic=89143.0. Please go give him/her some Karma!
void writeString(String stringData) { // Used to serially push out a String with Serial.write()
for (int i = 0; i < stringData.length(); i++)
{
mySerial.write(stringData[i]); // Push each char 1 by 1 on each loop pass
}
mySerial.write(0xff); //We need to write the 3 ending bits to the Nextion as well
mySerial.write(0xff); //it will tell the Nextion that this is the end of what we want to send.
mySerial.write(0xff);
}// end writeString function