Untitled
unknown
plain_text
a year ago
3.3 kB
6
Indexable
#include <Arduino.h>
#include <WiFi.h>
#include "SparkFun_LSM6DSV16X.h"
#include <Wire.h>
#define WIFI_SSID "LFGW" // Name of the network created by ESP32
#define WIFI_PASSWORD "123456789" // Network password
String x1s, y1s, z1s, x2s, y2s, z2s, adc1s, adc2s;
SparkFun_LSM6DSV16X myLSM;
sfe_lsm_data_t accelData;
sfe_lsm_data_t gyroData;
WiFiServer server(80); // HTTP server for port 80
void setup() {
Serial.begin(115200);
Wire.begin(19, 20); // I2C pins
pinMode(1, INPUT);
pinMode(2, INPUT);
// Initialize the LSM6DSV16X sensor
if (!myLSM.begin()) {
Serial.println("Did not begin, check your wiring and/or I2C address!");
while (1);
}
myLSM.deviceReset();
while (!myLSM.getDeviceReset()) {
delay(1);
}
Serial.println("Board has been Reset.");
Serial.println("Applying settings.");
myLSM.enableBlockDataUpdate();
myLSM.setAccelDataRate(LSM6DSV16X_ODR_AT_7Hz5);
myLSM.setAccelFullScale(LSM6DSV16X_16g);
myLSM.setGyroDataRate(LSM6DSV16X_ODR_AT_15Hz);
myLSM.setGyroFullScale(LSM6DSV16X_2000dps);
myLSM.enableFilterSettling();
myLSM.enableAccelLP2Filter();
myLSM.setAccelLP2Bandwidth(LSM6DSV16X_XL_STRONG);
myLSM.enableGyroLP1Filter();
myLSM.setGyroLP1Bandwidth(LSM6DSV16X_GY_ULTRA_LIGHT);
Serial.println("Ready.");
// Start the ESP32 Wi-Fi network
WiFi.softAP(WIFI_SSID, WIFI_PASSWORD); // Start Wi-Fi network
IPAddress IP = WiFi.softAPIP();
Serial.print("LATEFOSS GATEWAY STARTED. IP address: ");
Serial.println(IP);
// Start the web server
server.begin();
}
void loop() {
// Read sensor data
if (myLSM.checkStatus()) {
myLSM.getAccel(&accelData);
myLSM.getGyro(&gyroData);
x1s = String(accelData.xData);
y1s = String(accelData.yData);
z1s = String(accelData.zData);
x2s = String(gyroData.xData);
y2s = String(gyroData.yData);
z2s = String(gyroData.zData);
adc1s = String(analogRead(1)); // Read from A0 pin
adc2s = String(analogRead(2)); // Read from A1 pin
}
// Check connection and send data
WiFiClient client = server.available();
if (client) {
Serial.println("A new client has been connected!");
String request = "";
// Listen to the client and capture their requests
while (client.connected()) {
if (client.available()) {
char c = client.read();
request += c;
if (c == '\n') {
break;
}
}
}
// Format data as JSON
String data = "{";
data += "\"A0\":\"" + adc1s + "\",";
data += "\"A1\":\"" + adc2s + "\",";
data += "\"accelx\":\"" + x1s + "\",";
data += "\"accely\":\"" + y1s + "\",";
data += "\"accelz\":\"" + z1s + "\",";
data += "\"gyrox\":\"" + x2s + "\",";
data += "\"gyroy\":\"" + y2s + "\",";
data += "\"gyroz\":\"" + z2s + "\",";
// Add timestamp
data += "\"Ts\":" + String(millis()) + "}";
// Send HTTP response
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: application/json");
client.println("Connection: close");
client.println();
client.println(data); // Send JSON data
// Close the connection
client.stop();
Serial.println("The client connection was lost.");
}
// No delay in this loop, data is updated continuously.
}
Editor is loading...
Leave a Comment