Forum: Mikrocontroller und Digitale Elektronik Arduino IDE Slider ESP


von Michael S (Gast)


Lesenswert?

Hallo zusammen,

ich versuche gerade eine RGBW Steuerung per ESP mit der Arduino IDE.
Ich bekomme den Wert ausgelesen "NewRvalue".
Ich habe das etwas zusammengestückelt aus Internetbeispielen.

Was ich noch möchte ist:

1.diesen Wert in den VALUE Wert des Sliders einzufügen damit dieser 
nicht immer in die Mitte zurück springt. Ich habe schon viel versucht 
leider ohne Erfolg

2. Wie kann ich diesen Wert im EEPROM speichern damit er nach dem 
Nuestart noch zur verfügung steht?

Danke :-)
1
#include <ESP8266WiFi.h>
2
#include "FS.h"
3
4
//////////////////////
5
// WiFi Definitions //
6
//////////////////////
7
const char WiFiAPPSK[] = "sparkfun";
8
9
/////////////////////
10
// Pin Definitions //
11
/////////////////////
12
const int LED_PIN = 5; // Thing's onboard, green LED
13
const int ANALOG_PIN = A0; // The only analog pin on the Thing
14
const int DIGITAL_PIN = 12; // Digital pin to be read
15
String NewRvalue;
16
17
WiFiServer server(80);
18
19
void setup() 
20
{
21
  initHardware();
22
  setupWiFi();
23
  server.begin();
24
}
25
26
void loop() 
27
{
28
  // Check if a client has connected
29
  WiFiClient client = server.available();
30
  if (!client) {
31
    return;
32
  }
33
34
  // Read the first line of the request
35
  String req = client.readStringUntil('\r');
36
  Serial.println(req);
37
  //neu2
38
  int IndexBeginRValue = req.indexOf('=') + 1;
39
  int IndexEndRValue = req.indexOf('&');
40
  //Serial.println(IndexBeginRValue);
41
  //Serial.println(IndexEndRValue);
42
  if (req.startsWith("R0", 6)) {
43
    String NewRvalue = req.substring(IndexBeginRValue,IndexEndRValue);
44
    Serial.println(NewRvalue);
45
  } 
46
47
  String TestString = "<input type=\"range\" name=\"R0\" min=\"0\" max=\"255\" value=\""+NewRvalue+"\" style=\"width:95%\" />";
48
  Serial.println(TestString);
49
50
  client.flush();
51
  
52
  //neu2
53
  /*
54
  client.flush();
55
56
  // Match the request
57
  int val = -1; // We'll use 'val' to keep track of both the
58
                // request type (read/set) and value if set.
59
  if (req.indexOf("/led/0") != -1)
60
    val = 0; // Will write LED low
61
  else if (req.indexOf("/led/1") != -1)
62
    val = 1; // Will write LED high
63
  else if (req.indexOf("/read") != -1)
64
    val = -2; // Will print pin reads
65
  // Otherwise request will be invalid. We'll say as much in HTML
66
67
  // Set GPIO5 according to the request
68
  if (val >= 0)
69
    digitalWrite(LED_PIN, val);
70
71
  client.flush();
72
  */
73
  
74
75
  // Prepare the response. Start with the common header:
76
  String s = "HTTP/1.1 200 OK\r\n";
77
  s += "Content-Type: text/html\r\n\r\n";
78
  s += "<!DOCTYPE HTML>\r\n<html>\r\n";
79
  //neu
80
  s += "<form  method=\"get\">";
81
  s += "ESP RGBW Test";  
82
  //neu
83
  /*
84
  // If we're setting the LED, print out a message saying we did
85
  if (val >= 0)
86
  {
87
    s += "LED is now ";
88
    s += (val)?"on":"off";
89
  }
90
  */
91
 // else //if (val == -2)
92
  //{ // If we're reading pins, print out those values:
93
    s += "Analog Pin = ";
94
    s += String(analogRead(ANALOG_PIN));
95
    s += "<br>"; // Go to the next line.
96
    s += "Digital Pin 12 = ";
97
    s += String(digitalRead(DIGITAL_PIN));
98
    s += "<input type=\"range\" name=\"R0\" min=\"0\" max=\"255\" value=\""+NewRvalue+"\" style=\"width:95%\" />";
99
    s += "<br><br>"; // Go to the next line.
100
    s += "<input type=\"range\" name=\"G0\" min=\"0\" max=\"255\" value=\"128\" style=\"width:95%\" />";
101
    s += "<br><br>"; // Go to the next line.
102
    s += "<input type=\"range\" name=\"G0\" min=\"0\" max=\"255\" value=\"128\" style=\"width:95%\" />";
103
    s += "<br><br>"; // Go to the next line.
104
    s += "<input type=\"range\" name=\"W0\" min=\"0\" max=\"255\" value=\"128\" style=\"width:95%\" />";
105
    s += "<br><br>"; // Go to the next line.
106
    s += "<p> <input type=\"submit\"> </p>";
107
    s += "</form>";
108
    
109
 // }
110
  
111
  /*
112
  else
113
  {
114
    s += "Invalid Request.<br> Try /led/1, /led/0, or /read.";
115
  }
116
  */
117
  s += "</html>\n";
118
119
  // Send the response to the client
120
  client.print(s);
121
  delay(1);
122
  //Serial.println("Client disonnected");
123
124
  // The client will actually be disconnected 
125
  // when the function returns and 'client' object is detroyed
126
}
127
128
void setupWiFi()
129
{
130
  WiFi.mode(WIFI_AP);
131
132
  // Do a little work to get a unique-ish name. Append the
133
  // last two bytes of the MAC (HEX'd) to "Thing-":
134
  uint8_t mac[WL_MAC_ADDR_LENGTH];
135
  WiFi.softAPmacAddress(mac);
136
  String macID = String(mac[WL_MAC_ADDR_LENGTH - 2], HEX) +
137
                 String(mac[WL_MAC_ADDR_LENGTH - 1], HEX);
138
  macID.toUpperCase();
139
  String AP_NameString = "ESP_RGBW_TEST";
140
141
  char AP_NameChar[AP_NameString.length() + 1];
142
  memset(AP_NameChar, 0, AP_NameString.length() + 1);
143
144
  for (int i=0; i<AP_NameString.length(); i++)
145
    AP_NameChar[i] = AP_NameString.charAt(i);
146
147
  WiFi.softAP(AP_NameChar, WiFiAPPSK);
148
}
149
150
void initHardware()
151
{
152
  Serial.begin(115200);
153
  pinMode(DIGITAL_PIN, INPUT_PULLUP);
154
  pinMode(LED_PIN, OUTPUT);
155
  digitalWrite(LED_PIN, LOW);
156
  // Don't need to set ANALOG_PIN as input, 
157
  // that's all it can be.
158
}

von Michael S (Gast)


Lesenswert?

So das mit dem Wert im Slider übernehmen klappt jetzt.
Anbei mal der Code.

Leider klappt das mit dem speichern im EEPROM nicht.

Hätte da jemand einen Vorschlag bzw. könnte das mal implementieren?

Es reicht mir wenn der Wert von R sagen wir an Adresse 0 geschrieben 
wird und G an 1 / B an 2 und W an 3

Ich habe es nach diesen Beispielen Versucht leider ohne Erfolg
https://github.com/esp8266/Arduino/blob/master/libraries/EEPROM/examples/eeprom_write/eeprom_write.ino
https://github.com/esp8266/Arduino/blob/master/libraries/EEPROM/examples/eeprom_read/eeprom_read.ino

Danke
1
#include <ESP8266WiFi.h>
2
#include "EEPROM.h"
3
4
//////////////////////
5
// WiFi Definitions //
6
//////////////////////
7
const char WiFiAPPSK[] = "sparkfun";
8
9
/////////////////////
10
// Pin Definitions //
11
/////////////////////
12
const int LED_PIN = 5; // Thing's onboard, green LED
13
const int ANALOG_PIN = A0; // The only analog pin on the Thing
14
const int DIGITAL_PIN = 12; // Digital pin to be read
15
String NewRvalue;
16
17
WiFiServer server(80);
18
19
void setup() 
20
{
21
  initHardware();
22
  setupWiFi();
23
  server.begin();
24
}
25
26
void loop() 
27
{
28
  // Check if a client has connected
29
  WiFiClient client = server.available();
30
  if (!client) {
31
    return;
32
  }
33
34
  // Read the first line of the request
35
  String req = client.readStringUntil('\r');
36
  Serial.println(req);
37
  //neu2
38
  int IndexBeginRValue = req.indexOf('=') + 1;
39
  int IndexEndRValue = req.indexOf('&');
40
  //Serial.println(IndexBeginRValue);
41
  //Serial.println(IndexEndRValue);
42
  if (req.startsWith("R0", 6)) {
43
    NewRvalue = req.substring(IndexBeginRValue,IndexEndRValue);
44
    Serial.println(NewRvalue);
45
  } 
46
47
  String TestString = "<input type=\"range\" name=\"R0\" min=\"0\" max=\"255\" value=\""+NewRvalue+"\" style=\"width:95%\" />";
48
  Serial.println(TestString);
49
50
  client.flush();
51
  
52
  //neu2
53
  /*
54
  client.flush();
55
56
  // Match the request
57
  int val = -1; // We'll use 'val' to keep track of both the
58
                // request type (read/set) and value if set.
59
  if (req.indexOf("/led/0") != -1)
60
    val = 0; // Will write LED low
61
  else if (req.indexOf("/led/1") != -1)
62
    val = 1; // Will write LED high
63
  else if (req.indexOf("/read") != -1)
64
    val = -2; // Will print pin reads
65
  // Otherwise request will be invalid. We'll say as much in HTML
66
67
  // Set GPIO5 according to the request
68
  if (val >= 0)
69
    digitalWrite(LED_PIN, val);
70
71
  client.flush();
72
  */
73
  
74
75
  // Prepare the response. Start with the common header:
76
  String s = "HTTP/1.1 200 OK\r\n";
77
  s += "Content-Type: text/html\r\n\r\n";
78
  s += "<!DOCTYPE HTML>\r\n<html>\r\n";
79
  //neu
80
  s += "<form  method=\"get\">";
81
  s += "Headlight Control";  
82
  //neu
83
  /*
84
  // If we're setting the LED, print out a message saying we did
85
  if (val >= 0)
86
  {
87
    s += "LED is now ";
88
    s += (val)?"on":"off";
89
  }
90
  */
91
 // else //if (val == -2)
92
  //{ // If we're reading pins, print out those values:
93
    s += "Analog Pin = ";
94
    s += String(analogRead(ANALOG_PIN));
95
    s += "<br>"; // Go to the next line.
96
    s += "Digital Pin 12 = ";
97
    s += String(digitalRead(DIGITAL_PIN));
98
    s += "<input type=\"range\" name=\"R0\" min=\"0\" max=\"255\" value=\""+NewRvalue+"\" style=\"width:95%\" />";
99
    s += "<br><br>"; // Go to the next line.
100
    s += "<input type=\"range\" name=\"G0\" min=\"0\" max=\"255\" value=\"128\" style=\"width:95%\" />";
101
    s += "<br><br>"; // Go to the next line.
102
    s += "<input type=\"range\" name=\"G0\" min=\"0\" max=\"255\" value=\"128\" style=\"width:95%\" />";
103
    s += "<br><br>"; // Go to the next line.
104
    s += "<input type=\"range\" name=\"W0\" min=\"0\" max=\"255\" value=\"128\" style=\"width:95%\" />";
105
    s += "<br><br>"; // Go to the next line.
106
    s += "<p> <input type=\"submit\"> </p>";
107
    s += "</form>";
108
    
109
 // }
110
  
111
  /*
112
  else
113
  {
114
    s += "Invalid Request.<br> Try /led/1, /led/0, or /read.";
115
  }
116
  */
117
  s += "</html>\n";
118
119
  // Send the response to the client
120
  client.print(s);
121
  delay(1);
122
  //Serial.println("Client disonnected");
123
124
  // The client will actually be disconnected 
125
  // when the function returns and 'client' object is detroyed
126
}
127
128
void setupWiFi()
129
{
130
  WiFi.mode(WIFI_AP);
131
132
  // Do a little work to get a unique-ish name. Append the
133
  // last two bytes of the MAC (HEX'd) to "Thing-":
134
  uint8_t mac[WL_MAC_ADDR_LENGTH];
135
  WiFi.softAPmacAddress(mac);
136
  String macID = String(mac[WL_MAC_ADDR_LENGTH - 2], HEX) +
137
                 String(mac[WL_MAC_ADDR_LENGTH - 1], HEX);
138
  macID.toUpperCase();
139
  String AP_NameString = "ESP_RGBW_TEST";
140
141
  char AP_NameChar[AP_NameString.length() + 1];
142
  memset(AP_NameChar, 0, AP_NameString.length() + 1);
143
144
  for (int i=0; i<AP_NameString.length(); i++)
145
    AP_NameChar[i] = AP_NameString.charAt(i);
146
147
  WiFi.softAP(AP_NameChar, WiFiAPPSK);
148
}
149
150
void initHardware()
151
{
152
  Serial.begin(115200);
153
  pinMode(DIGITAL_PIN, INPUT_PULLUP);
154
  pinMode(LED_PIN, OUTPUT);
155
  digitalWrite(LED_PIN, LOW);
156
  // Don't need to set ANALOG_PIN as input, 
157
  // that's all it can be.
158
}

von Michael U. (amiga)


Lesenswert?

Hallo,

habe mir nicht alles angeschaut, aber ich sehe keine EEPROM Aufrufe...
Weder
EEPROM.begin(512); noch  EEPROM.write(addr, val); und kein 
EEPROM.commit();

Eigentlich ist das Handling doch unproblematisch und das Beispiel 
verständlich?

Gruß aus Berlin
Michael

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.