Arduino Programm+Physik?

5 Antworten

//Diese funktioniert bei mir am besten

#include <Arduino.h>

#include <WiFi.h>

#include "SPIFFS.h"

#include <AsyncTCP.h>

#include <ESPAsyncWebServer.h>

#include <Arduino_JSON.h>

#include <Adafruit_BMP085.h>

#include <Adafruit_Sensor.h>

#include <Servo.h>

const char* ssid   = "ParachuteDrop";

const char* password = "parachutedrop";

Adafruit_BMP085 sensor;

float altitude = 0.0;

float currentAltitude = 0.0;

float pressure = 0.0;

JSONVar readings;

const float maxElevation = 1.5;

const float seaLevelPressure = 101325.0;

Servo servo;

const int SERVOPIN = 4;

const int servoOpenPosition = 0;

const int servoClosePosition = 90;

const int LEDPIN = 2; //LED_BUILTIN;

bool locked = false;

// Create AsyncWebServer object on port 80

AsyncWebServer server(80);

AsyncWebSocket webSocket("/ws");

// Generally, you should use "unsigned long" for variables that hold time

// The value will quickly become too large for an int to store

unsigned long previousMillis = 0;  // will store last time DHT was updated

const long interval = 1000;  

const char index_html[] PROGMEM = R"rawliteral(

<!DOCTYPE HTML><html>

<head>

 <meta name="viewport" content="width=device-width, initial-scale=1">

 <style>

  html {

   font-family: Arial;

   display: inline-block;

   margin: 0px auto;

   text-align: center;

  }

  h2 { font-size: 3.0rem; }

  p { font-size: 3.0rem; }

  .units { font-size: 1.2rem; }

  .bmp-labels{

   font-size: 1.5rem;

   vertical-align:middle;

   padding-bottom: 15px;

  }

   .button {

  padding: 15px 50px;

  font-size: 24px;

  text-align: center;

  outline: none;

  color: #fff;

  background-color: #0f8b8d;

  border: none;

  border-radius: 5px;

  -webkit-touch-callout: none;

  -webkit-user-select: none;

  -khtml-user-select: none;

  -moz-user-select: none;

  -ms-user-select: none;

  user-select: none;

  -webkit-tap-highlight-color: rgba(0,0,0,0);

  }

  .button:active {

   background-color: #0f8b8d;

   box-shadow: 2 2px #CDCDCD;

   transform: translateY(2px);

  }

  .state {

   font-size: 1.5rem;

   color:#8c8c8c;

   font-weight: bold;

  }

 </style>

</head>

<body>

 <h2>Parachute Drop</h2>

 <p>

  <span class="bmp-labels">Altitude</span> 

  <span id="altitude">%ALTITUDE%</span>

  <sup class="units">m</sup>

 </p>

 <p>

  <span class="bmp-labels">Pressure</span>

  <span id="pressure">%PRESSURE%</span>

  <sup class="units">hPa</sup>

 </p>

 <p class="state">Lock state: <span id="state">%STATE%</span></p>

 <p><button id="button" class="button">Toggle</button></p>

</body>

<script>

 var gateway = `ws://${window.location.hostname}/ws`;

 var websocket;

 window.addEventListener('load', onLoad);

 function getInformations(){

  websocket.send("getReadings");

 }

  

 function initWebSocket() {

  console.log('Trying to open a WebSocket connection...');

  websocket = new WebSocket(gateway);

  websocket.onopen  = onOpen;

  websocket.onclose  = onClose;

  websocket.onmessage = onMessage; // <-- add this line

 }

 function onOpen(event) {

  console.log('Connection opened');

  getInformation();

 }

 function onClose(event) {

  console.log('Connection closed');

  setTimeout(initWebSocket, 2000);

 }

 function onMessage(event) {

  console.log(event.data);

   

  var state;

  var myObj = JSON.parse(event.data);

  var keys = Object.keys(myObj);

  for (var i = 0; i < keys.length; i++){

    var key = keys[i];

    document.getElementById(key).innerHTML = myObj[key];

  }

 }

 function onLoad(event) {

  initWebSocket();

  initButton();

 }

 function initButton() {

  document.getElementById('button').addEventListener('click', toggle);

 }

 function toggle(){

  websocket.send('toggle');

 }

</script>

</html>)rawliteral";

void initWiFi() {

 Serial.print("Setting AP (Access Point)…");

  

 WiFi.softAP(ssid, password);

 IPAddress IP = WiFi.softAPIP();

 Serial.print("AP IP address: ");

 Serial.println(IP);

 Serial.println(WiFi.localIP());

}

void initSPIFFS() {

 if (!SPIFFS.begin(true))

  Serial.println("An error has occurred while mounting SPIFFS");

 else {

  Serial.println("SPIFFS mounted successfully");

// Filename sensor.csv

  File file = SPIFFS.open("/sensor.csv", FILE_WRITE);

  if (!file) {

   Serial.println("There was an error opening the file for writing");

   return;

  }

  if (file.println("Uptime;Pressure;Altidue;Current Altitude;Locked;"))

   Serial.println("File was written");

  else

   Serial.println("File write failed");

  file.close();

 }

}

void AppendSensorDatatoFile(int CurrentMillis, float Pressure, float Altitude, float CurrentAltitude, bool Locked) {

 File file = SPIFFS.open("/sensor.csv", FILE_APPEND);

 if (!file) {

  Serial.println("There was an error opening the file for writing");

  return;

 }  

 file.print(CurrentMillis); file.print(";"); file.print(Pressure); file.print(";"); file.print(Altitude); file.print(";"); file.print(CurrentAltitude); file.print(";"); file.print( Locked ? "LOCKED" : "OPEN" ); file.println(";");

 file.close();

}

void initSensor() {

 if (!sensor.begin(0x76)) {

  Serial.println("Could not find a valid BMP085 sensor, check wiring!");

  while (1);

 }  

}

void setServo(bool Locked){

 digitalWrite(LEDPIN, Locked);

 if ( Locked ) {

  servo.write(servoClosePosition);

 } else {

  servo.write(servoOpenPosition);

 }

}

void initServo() {

 pinMode(SERVOPIN, OUTPUT);

 servo.attach(SERVOPIN);

 setServo(false); delay(1000);

  

 setServo(true); delay(1000);

 setServo(false);

}

void initLED() {

 pinMode(LEDPIN, OUTPUT);

 digitalWrite(LEDPIN, locked);

}

String getInformation(){

 readings["altitude"] = String(altitude);

 readings["pressure"] = String(pressure);

 readings["state"] = locked ? "LOCKED" : "OPEN";

 String jsonString = JSON.stringify(readings);

 return jsonString;

}

void notifyClients() {

 String information = getInformation();

 webSocket.textAll(information);

}

void handleWebSocketMessage(void *arg, uint8_t *data, size_t len) {

 AwsFrameInfo *info = (AwsFrameInfo*)arg;

 if (info->final && info->index == 0 && info->len == len && info->opcode == WS_TEXT) {

  data[len] = 0;

  if (strcmp((char*)data, "toggle") == 0) {

   locked = !locked;

   if ( locked )

    currentAltitude = altitude;

   else

    currentAltitude = 0.0;

   setServo(locked);

  }  

  notifyClients();

 }

}

void handleWebSocketEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type, void *arg, uint8_t *data, size_t len) {

 switch (type) {

  case WS_EVT_DISCONNECT:

   Serial.printf("Disconnected!\n", client->id());

   break;

  case WS_EVT_CONNECT:

   {

    IPAddress ip = client->remoteIP();

    Serial.printf("Connected from %d.%d.%d.%d", client->id(), ip[0], ip[1], ip[2], ip[3]);

   }

   break;

  case WS_EVT_DATA:

   handleWebSocketMessage(arg, data, len);

   break;

  case WS_EVT_PONG:

  case WS_EVT_ERROR:

   break;

 }

}

void initWebSocket() {

 webSocket.onEvent(handleWebSocketEvent);

 server.addHandler(&webSocket);

}

void setup(){

// Első beállítás

 Serial.begin(115200); // Soros kommunikáció beállítása

 initWiFi(); // WiFi beállítás

 initSPIFFS(); // fájlrendszer beállítása

 initSensor();

 initLED();

 initServo();

 initWebSocket();

// Route for root / web page

 server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){

  request->send_P(200, "text/html", index_html);

 });

 server.on("/download", HTTP_GET, [](AsyncWebServerRequest *request){

  request->send(SPIFFS, "/sensor.csv", "text/csv", true);

});

// Start server

 server.begin();

}

 

void loop(){  

 unsigned long currentMillis = millis();

 if (currentMillis - previousMillis >= interval) {

  previousMillis = currentMillis;

  pressure = sensor.readPressure();

  altitude = 44330.0 * (1.0 - pow(pressure / seaLevelPressure, 0.190294957183635));

   

  Serial.print("altidue: "); Serial.println(String(altitude));

  Serial.print("current: "); Serial.println(String(currentAltitude));

  AppendSensorDatatoFile(int(currentMillis/1000.0), pressure, altitude, currentAltitude, locked);

   

  if ( locked && altitude >= currentAltitude + maxElevation )

  {

   currentAltitude = 0.0;

   locked = false;

   setServo(locked);

  }

  notifyClients();

 }

 webSocket.cleanupClients();

}

Das hängt von ein paar Faktoren ab wie beispielsweise wie kompliziert das ganze werden muss.
LG

Das hängt von vielen Faktoren ab. Bsp. Wie hoch muss das sein.