ESP32 cansat next: Difference between revisions
(8 intermediate revisions by the same user not shown) | |||
Line 17: | Line 17: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
To have more JSON data structure use | |||
<syntaxhighlight lang="C"> | |||
snprintf(report, sizeof(report), "{\"time\": %d, " | |||
"\"ax\": %4.2f, \"ay\": %4.2f, \"az\": %4.2f, \"temp\": %4.2f, \"p\": %4.2f, " | |||
"\"lat\": %8.7f, \"long\": %8.7f, \"alt\": %4.2f, \"speed\": %4.2f, " | |||
"\"so2\": %4.2f },", | |||
treset, | |||
ax, ay, az, t, p, | |||
lat, lng, alt, speed, | |||
so2 ); | |||
</syntaxhighlight> | |||
but there is a small problem with array of JSON. | |||
== Radio == | == Radio == | ||
To send strings, use <code>sendData( str );</code> command. | |||
== SD Card == | == SD Card == | ||
First, open the file | |||
<code>const String filepath = "/filename.csv";</code> | |||
and then append the data and linebreak | |||
<syntaxhighlight lang="C"> | |||
appendFile(filepath, data); | |||
appendFile(filepath, "\n"); | |||
</syntaxhighlight> | |||
== Simple code == | |||
A simple code to read data and send that via radio and store to SD card. | |||
=== Declare === | |||
First declare the variables: | |||
<syntaxhighlight lang="C"> | |||
#include <TinyGPS++.h> | |||
#include "CanSatNeXT.h" | |||
#define GPS_BAUDRATE 9600 // The default baudrate of NEO-6M is 9600 | |||
TinyGPSPlus gps; // the TinyGPS++ object | |||
const String filepath = "/filename.csv"; | |||
long tradio = 0; | |||
long treset = 0; //Time from reset | |||
float ax; | |||
float ay; | |||
float az; | |||
float t; | |||
float p; | |||
float lat = 0; | |||
float lng = 0; | |||
float alt = 0; | |||
float speed = 0; | |||
float so2; // Connect to pin 33 | |||
</syntaxhighlight> | |||
=== Initialize === | |||
Start the serial monitor, and software serial. Find the GPS module and start CanSatInitialization. Note that the radio needs a number in parenthesis. | |||
<syntaxhighlight lang="C"> | |||
void setup() { | |||
Serial.begin(9600); | |||
Serial2.begin(GPS_BAUDRATE, SERIAL_8N1, 16, 17); | |||
Serial.println(F("ESP32 - GPS module")); | |||
Serial.println(F("Lat, Lon, Alt, speed [m/s], Datetime")); | |||
// Start all CanSatNeXT on-board systems. | |||
CanSatInit(); | |||
} | |||
</syntaxhighlight> | |||
== Read data == | |||
Using the loop() method, read the data. Update the GPS only if there is new data. | |||
<syntaxhighlight lang="C"> | |||
void loop() { | |||
//Get the data | |||
treset = millis(); | |||
ax = readAccelX(); | |||
ay = readAccelY(); | |||
az = readAccelZ(); | |||
t = readTemperature(); | |||
p = readPressure(); | |||
so2 = analogRead(33); // Connect to pin 33 | |||
if (Serial2.available() > 0) { | |||
if (gps.encode(Serial2.read())) { | |||
if (gps.location.isValid()) { | |||
lat = gps.location.lat(); | |||
lng = gps.location.lng(); | |||
alt = gps.altitude.meters(); | |||
} | |||
if (gps.speed.isValid()) { | |||
speed = gps.speed.mps(); | |||
} | |||
} | |||
} | |||
</syntaxhighlight> | |||
=== Print the data === | |||
Again, in the loop() method include the printing methods. | |||
<syntaxhighlight lang="C"> | |||
//Printing | |||
char report[128]; | |||
memset(report, 0, sizeof(report)); | |||
snprintf(report, sizeof(report), "%d, " | |||
"%4.2f, %4.2f, %4.2f, %4.2f, %4.2f, " | |||
"%8.7f, %8.7f, %4.2f, %4.2f, " | |||
"%4.2f", | |||
treset, | |||
ax, ay, az, t, p, | |||
lat, lng, alt, speed, | |||
so2 ); | |||
// Print the data to Serial | |||
Serial.println( report ); | |||
// Save the data to SD card | |||
appendFile(filepath, report); | |||
appendFile(filepath, "\n"); | |||
// Transmit the data via radio | |||
if (treset - tradio > 5000){ | |||
tradio = millis(); | |||
//sendData( report ); | |||
Serial.println( "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" ); | |||
} | |||
} | |||
</syntaxhighlight> |
Latest revision as of 15:30, 4 April 2025
Introduction
Some interesting coding stuff
snprintf
printf, print
Each ASCII character takes 1 byte. You can store exactly 128 ASCII characters in the above char array.
char report[128];
memset(report, 0, sizeof(report));
snprintf(report, sizeof(report), "%4.2f, %4.2f, %4.2f, %4.2f, %4.2f, %4.2f",
ax, ay, az, gx, gy, gz);
Serial.println(report);
To have more JSON data structure use
snprintf(report, sizeof(report), "{\"time\": %d, "
"\"ax\": %4.2f, \"ay\": %4.2f, \"az\": %4.2f, \"temp\": %4.2f, \"p\": %4.2f, "
"\"lat\": %8.7f, \"long\": %8.7f, \"alt\": %4.2f, \"speed\": %4.2f, "
"\"so2\": %4.2f },",
treset,
ax, ay, az, t, p,
lat, lng, alt, speed,
so2 );
but there is a small problem with array of JSON.
Radio
To send strings, use sendData( str );
command.
SD Card
First, open the file
const String filepath = "/filename.csv";
and then append the data and linebreak
appendFile(filepath, data);
appendFile(filepath, "\n");
Simple code
A simple code to read data and send that via radio and store to SD card.
Declare
First declare the variables:
#include <TinyGPS++.h>
#include "CanSatNeXT.h"
#define GPS_BAUDRATE 9600 // The default baudrate of NEO-6M is 9600
TinyGPSPlus gps; // the TinyGPS++ object
const String filepath = "/filename.csv";
long tradio = 0;
long treset = 0; //Time from reset
float ax;
float ay;
float az;
float t;
float p;
float lat = 0;
float lng = 0;
float alt = 0;
float speed = 0;
float so2; // Connect to pin 33
Initialize
Start the serial monitor, and software serial. Find the GPS module and start CanSatInitialization. Note that the radio needs a number in parenthesis.
void setup() {
Serial.begin(9600);
Serial2.begin(GPS_BAUDRATE, SERIAL_8N1, 16, 17);
Serial.println(F("ESP32 - GPS module"));
Serial.println(F("Lat, Lon, Alt, speed [m/s], Datetime"));
// Start all CanSatNeXT on-board systems.
CanSatInit();
}
Read data
Using the loop() method, read the data. Update the GPS only if there is new data.
void loop() {
//Get the data
treset = millis();
ax = readAccelX();
ay = readAccelY();
az = readAccelZ();
t = readTemperature();
p = readPressure();
so2 = analogRead(33); // Connect to pin 33
if (Serial2.available() > 0) {
if (gps.encode(Serial2.read())) {
if (gps.location.isValid()) {
lat = gps.location.lat();
lng = gps.location.lng();
alt = gps.altitude.meters();
}
if (gps.speed.isValid()) {
speed = gps.speed.mps();
}
}
}
Print the data
Again, in the loop() method include the printing methods.
//Printing
char report[128];
memset(report, 0, sizeof(report));
snprintf(report, sizeof(report), "%d, "
"%4.2f, %4.2f, %4.2f, %4.2f, %4.2f, "
"%8.7f, %8.7f, %4.2f, %4.2f, "
"%4.2f",
treset,
ax, ay, az, t, p,
lat, lng, alt, speed,
so2 );
// Print the data to Serial
Serial.println( report );
// Save the data to SD card
appendFile(filepath, report);
appendFile(filepath, "\n");
// Transmit the data via radio
if (treset - tradio > 5000){
tradio = millis();
//sendData( report );
Serial.println( "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" );
}
}