Advertisement
microrobotics

SparkFun DataLogger IoT - 9DoF

Jun 5th, 2023
1,289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. This board contains an ESP32 module, an integrated WiFi transceiver, USB connectivity, LiPo battery charger, and a variety of sensors. This includes an accelerometer, a gyroscope, and a magnetometer. This board also supports I2C communication, allowing for easy expansion and the addition of more sensors if needed.
  3.  
  4. Since this board uses the ESP32, it can be programmed using the Arduino IDE, provided you've installed the ESP32 Arduino core.
  5.  
  6. This is a simple example and should be expanded to handle errors and edge cases appropriately for your specific use case. Be sure to replace "your_SSID" and "your_PASSWORD" with your Wi-Fi network's SSID and password, and "http://example.com/post-endpoint" with your server's unique URL endpoint.
  7.  
  8. Remember that the power draw on WiFi can be high, ensure your power supply can provide adequate power for both the board and the WiFi transmission. If you're powering the board from a battery, consider using a sleep function to increase the battery life.
  9.  
  10. Below is a sample sketch which uses the ESP32's built-in Wi-Fi capabilities to connect to a local Wi-Fi network and sends IMU data to a server. This sketch uses the HTTPClient library to send a POST request to a server with the sensor data. It uses the Wire library for I2C communication with the sensors.
  11. */
  12.  
  13. #include <Wire.h>
  14. #include <WiFi.h>
  15. #include <HTTPClient.h>
  16. #include "SparkFunLSM9DS1.h"
  17.  
  18. // Replace with your network credentials
  19. const char* ssid = "your_SSID";
  20. const char* password = "your_PASSWORD";
  21.  
  22. // Replace with your unique URL endpoint
  23. const char* serverName = "http://example.com/post-endpoint";
  24.  
  25. LSM9DS1 imu;
  26.  
  27. void setup() {
  28.   Serial.begin(115200);
  29.  
  30.   WiFi.begin(ssid, password);
  31.   while (WiFi.status() != WL_CONNECTED) {
  32.     delay(1000);
  33.     Serial.println("Connecting to WiFi...");
  34.   }
  35.   Serial.println("Connected to WiFi");
  36.  
  37.   if (!imu.begin()) {
  38.     Serial.println("Failed to initialize IMU!");
  39.     while (1);
  40.   }
  41. }
  42.  
  43. void loop() {
  44.   if (imu.gyroAvailable()) {
  45.     imu.readGyro();
  46.   }
  47.   if (imu.accelAvailable()) {
  48.     imu.readAccel();
  49.   }
  50.   if (imu.magAvailable()) {
  51.     imu.readMag();
  52.   }
  53.  
  54.   if(WiFi.status()== WL_CONNECTED){
  55.     HTTPClient http;
  56.    
  57.     http.begin(serverName);
  58.     http.addHeader("Content-Type", "application/x-www-form-urlencoded");
  59.    
  60.     String httpRequestData = "gx=" + String(imu.gx) + "&gy=" + String(imu.gy) + "&gz=" + String(imu.gz)
  61.                            + "&ax=" + String(imu.ax) + "&ay=" + String(imu.ay) + "&az=" + String(imu.az)
  62.                            + "&mx=" + String(imu.mx) + "&my=" + String(imu.my) + "&mz=" + String(imu.mz);
  63.     int httpResponseCode = http.POST(httpRequestData);
  64.    
  65.     if (httpResponseCode>0) {
  66.       String response = http.getString();
  67.       Serial.println(httpResponseCode);
  68.       Serial.println(response);
  69.     } else {
  70.       Serial.print("Error on sending POST: ");
  71.       Serial.println(httpResponseCode);
  72.     }
  73.     http.end();
  74.   }
  75.   else {
  76.     Serial.println("WiFi Disconnected");
  77.   }
  78.   delay(10000);  //Send a request every 10 seconds
  79. }
  80.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement