Code:
#include <Wire.h>
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h>
//Spannung auslesen vorbereiten
ADC_MODE(ADC_VCC);
String Ubatt = "";
//verbinden mit wlan
const char* ssid = "a-b-c";
const char* password = "x-y-z";
// Hier wird der aktuelle Status der schalter festgehalten
String summer_zustand = "off";
String LED_zustand = "off";
//slider power
String sliderValue_power = "400";//0
const char* PARAM_INPUT_POWER = "value";
//slider frequency
String sliderValue_frequency = "2";//0
const char* PARAM_INPUT_FREQUENCY = "value";
//frequency
uint8_t frequency = 0;
// Create AsyncWebServer object on port 80
AsyncWebServer server(80);
//website - slider
const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE HTML>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>anti apnoe web</title>
<style>
html {font-family: Arial; display: inline-block; text-align: center;}
h2 {font-size: 2.3rem;}
p {font-size: 1.9rem;}
body {max-width: 400px; margin:0px auto; padding-bottom: 25px;}
.slider { -webkit-appearance: none; margin: 14px; width: 300px; height: 25px; background: #FFD65C;//360px
outline: none; -webkit-transition: .2s; transition: opacity .2s;}
.slider::-webkit-slider-thumb {-webkit-appearance: none; appearance: none; width: 35px; height: 35px; background: #003249; cursor: pointer;}
.slider::-moz-range-thumb { width: 35px; height: 35px; background: #003249; cursor: pointer; }
</style>
</head>
<body>
<h2>vibration power</h2>
<p><span id="textSliderValue_power">%SLIDERVALUE_POWER%</span></p>
<p><input type="range" onchange="updateSliderPWM_power(this)" id="pwmSlider_power" min="300" max="1023" value="%SLIDERVALUE_POWER%" step="1" class="slider"></p>
<script>
function updateSliderPWM_power(element)
{
var sliderValue_power = document.getElementById("pwmSlider_power").value;
document.getElementById("textSliderValue_power").innerHTML = sliderValue_power;
console.log(sliderValue_power);
var xhr = new XMLHttpRequest();
xhr.open("GET", "/slider?value="+sliderValue_power, true);
xhr.send();
}
</script>
<h2>vibration frequency</h2>
<p><span id="textSliderValue_frequency">%SLIDERVALUE_FREQUENCY%</span></p>
<p><input type="range" onchange="updateSliderPWM_frequency(this)" id="pwmSlider_frequency" min="0" max="10" value="%SLIDERVALUE_FREQUENCY%" step="1" class="slider"></p>
<script>
function updateSliderPWM_frequency(element)
{
var sliderValue_frequency = document.getElementById("pwmSlider_frequency").value;
document.getElementById("textSliderValue_frequency").innerHTML = sliderValue_frequency;
console.log(sliderValue_frequency);
var xhr = new XMLHttpRequest();
xhr.open("GET", "/slider?value="+sliderValue_frequency, true);
xhr.send();
}
</script>
</body>
</html>
)rawliteral";
// Replaces placeholder with button section in your web page
String processor(const String& var)
{
//Serial.println(var);
if (var == "SLIDERVALUE_POWER")
{
return sliderValue_power;
}
return String();
if (var == "SLIDERVALUE_FREQUENCY")
{
return sliderValue_frequency;
}
return String();
}
//lagezaehler
uint8_t rueckenlage = 0;
//LED
#define LED_pin_gruen D1
int LED_state_gruen = LOW;
long previousMillis_gruen = 0;
//summer
#define summer D2
//timer
unsigned long aktuelle_timer_millis, vergangene_timer_millis;
unsigned long halbe_tick, sekunden_tick;
unsigned long interval_timer = 1000;
// MPU6050 Slave Device Address
const uint8_t MPU6050SlaveAddress = 0x68;
double Ax, Ay, Az, T, Gx, Gy, Gz;
// Select SDA and SCL pins for I2C communication
const uint8_t scl = D7;
const uint8_t sda = D6;
// sensitivity scale factor respective to full scale setting provided in datasheet
const uint16_t AccelScaleFactor = 16384;
const uint16_t GyroScaleFactor = 131;
// MPU6050 few configuration register addresses
const uint8_t MPU6050_REGISTER_SMPLRT_DIV = 0x19;
const uint8_t MPU6050_REGISTER_USER_CTRL = 0x6A;
const uint8_t MPU6050_REGISTER_PWR_MGMT_1 = 0x6B;
const uint8_t MPU6050_REGISTER_PWR_MGMT_2 = 0x6C;
const uint8_t MPU6050_REGISTER_CONFIG = 0x1A;
const uint8_t MPU6050_REGISTER_GYRO_CONFIG = 0x1B;
const uint8_t MPU6050_REGISTER_ACCEL_CONFIG = 0x1C;
const uint8_t MPU6050_REGISTER_FIFO_EN = 0x23;
const uint8_t MPU6050_REGISTER_INT_ENABLE = 0x38;
const uint8_t MPU6050_REGISTER_ACCEL_XOUT_H = 0x3B;
const uint8_t MPU6050_REGISTER_SIGNAL_PATH_RESET = 0x68;
int16_t AccelX, AccelY, AccelZ, Temperature, GyroX, GyroY, GyroZ;
void setup()
{
Serial.begin(115200);
Serial.print("code----/home/georg/Arduino/schlaf_apnoe/anti_apnoe_STA_browser_zwei_slider_zugriff_2");
//MPU
Wire.begin(sda, scl);
MPU6050_Init();
//summer
pinMode(D2, OUTPUT);
//LED
pinMode(D1, OUTPUT);
//start-meldung summer und LED
analogWrite(summer, 500);
delay(500);
analogWrite(summer, 0);
digitalWrite(LED_pin_gruen, HIGH);
delay(500);
digitalWrite(LED_pin_gruen, LOW);
// mit wlan verbinden
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// server starten
server.begin();
Serial.println("Server started");
// locale Ip adresse
Serial.print("Use this URL to connect: ");
Serial.print("http://");
Serial.print(WiFi.localIP());
Serial.println("/");
// Route for root / web page
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request)
{
request->send_P(200, "text/html", index_html, processor);
});
// Send a GET request to <ESP_IP>/slider?value=<inputMessage_power>
server.on("/slider", HTTP_GET, [] (AsyncWebServerRequest *request)
{
String inputMessage_power;
// GET input1 value on <ESP_IP>/slider?value=<inputMessage_power>
if (request->hasParam(PARAM_INPUT_POWER))
{
inputMessage_power = request->getParam(PARAM_INPUT_POWER)->value();
sliderValue_power = inputMessage_power;
analogWrite(summer, sliderValue_power.toInt());
}
else
{
inputMessage_power = "No message sent";
}
Serial.println(inputMessage_power);
request->send(200, "text/plain", "OK");
});
// Send a GET request to <ESP_IP>/slider?value=<inputMessage_frequency>
server.on("/slider", HTTP_GET, [] (AsyncWebServerRequest *request)
{
String inputMessage_frequency;
// GET input1 value on <ESP_IP>/slider?value=<inputMessage_frequency>
if (request->hasParam(PARAM_INPUT_FREQUENCY))
{
inputMessage_frequency = request->getParam(PARAM_INPUT_FREQUENCY)->value();
sliderValue_frequency = inputMessage_frequency;
analogWrite(frequency, sliderValue_frequency.toInt());
}
else
{
inputMessage_frequency = "No message sent";
}
Serial.println(inputMessage_frequency);
request->send(200, "text/plain", "OK");
});
}
void loop()
{
//timer();
//read_gyro();
// blinken_10_sec ();
//abfrage_lage();
//abfrage_lage_10_sec ();
//lage_zaehler();
//sekunden_aufgaben();
}
wie gesagt, so weit fast alles ok, die darstellung auf der webseite ist nicht gut, vermute einen HTML problem
Lesezeichen