Forum: Mikrocontroller und Digitale Elektronik Watchdog Reset mit Oled Display und Rotary Encoder HILFE


von Peerless R. (peerless_r)


Lesenswert?

Servuz,

ich baue gerade einen Ersatz-Controller für eine Kühlbox, da der alte 
(ChinaMüll) Controller abgeraucht ist. Ich verwende einen Wemos D1 Mini, 
ein 128x64 SSD1306 OLED Display und einen Rotary Encoder über I2C.

Das ganze soll relativ simpel sein, man schaltet das Gerät an, es zeigt 
die aktuelle Temperatur auf dem Display an. Man drückt den Rotary 
Encoder Button und ist nun im Einstellungs-Modus, durch Drehen stellt 
man die Ziel-Temperatur ein, durch erneutes Drücken des Buttons wird 
diese bestätigt. Den Berechnungsteil mit Widerstand/Temperatur messen 
und Steuern des Kompressors etc hab ich noch ausgelassen geht mir 
erstmal nur um die Bedienelemente. Soweit funktioniert jetzt alles ABER 
wenn ich den Button drücke, dann einen Wert einstelle und dann einfach 
warte und nichts mache, triggert der Watchdog und macht nen Reset. Das 
selbe wenn ich den Button drücke und dann gar keinen Wert einstelle und 
einfach nur warte. So nach 2-3 Sekunden hängt sich da was auf und ich 
hab keine Ahnung wieso. Bin dann doch eher Elektroniker als 
Programmierer und hoffe mir kann jemand weiterhelfen.
Ich verwende die Library ESPRotary und für das Display die Adafruit 
SSD1306/GFX Libraries.

Hier hab ich mal ein kurzes Video von dem Problem zum besseren 
Verständnis:

https://streamable.com/76rj2y

Und hier natürlich mein Code:
1
#include <Wire.h>
2
#include <Adafruit_GFX.h>
3
#include <Adafruit_SSD1306.h>
4
#include "ESPRotary.h"
5
6
#define ROTARY_PIN1 12
7
#define ROTARY_PIN2 14
8
#define CLICKS_PER_STEP 4
9
#define MIN_POS         -20
10
#define MAX_POS         20
11
#define START_POS       0
12
#define INCREMENT       1
13
#define SCREEN_WIDTH 128
14
#define SCREEN_HEIGHT 64
15
#define OLED_RESET -1
16
#define LOGO_WIDTH 128
17
#define LOGO_HEIGHT 64
18
19
ESPRotary r;
20
21
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
22
23
const unsigned char logo_bmp [] PROGMEM = {
24
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
25
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
26
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
27
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
28
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
29
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
30
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
31
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
32
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
33
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
34
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
35
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
36
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
37
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
38
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
39
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
40
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
41
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
42
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
43
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
44
  0x7f, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7c, 0x00, 0x0f, 0xc0, 0x7c, 0x00, 0x00, 0x00, 
45
  0x7f, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7c, 0x00, 0x0f, 0xc0, 0x7c, 0x00, 0x00, 0x00, 
46
  0x7f, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0x00, 0x0f, 0x80, 0xfc, 0x00, 0x00, 0x00, 
47
  0x7f, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0xf8, 0x00, 0x1f, 0x80, 0xf8, 0x00, 0x00, 0x00, 
48
  0x7f, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x01, 0xf8, 0x00, 0x1f, 0x01, 0xf8, 0x00, 0x00, 0x00, 
49
  0x00, 0x0f, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x01, 0xf8, 0x00, 0x00, 0x01, 0xf8, 0x00, 0x00, 0x00, 
50
  0x00, 0x1f, 0xff, 0xff, 0xf8, 0x0f, 0xff, 0x81, 0xf0, 0xfe, 0x3f, 0x1f, 0xff, 0x07, 0xff, 0xe0, 
51
  0x00, 0x1f, 0xff, 0xff, 0xf8, 0x3f, 0xff, 0x83, 0xf1, 0xfc, 0x3e, 0x1f, 0xff, 0x1f, 0xff, 0xc0, 
52
  0x00, 0x1f, 0x07, 0xff, 0xf0, 0x7f, 0xff, 0x83, 0xe3, 0xf8, 0x7e, 0x3f, 0xfe, 0x3f, 0xff, 0xc0, 
53
  0x00, 0x3f, 0x0f, 0xff, 0xf0, 0x7f, 0xff, 0x03, 0xe7, 0xf0, 0x7e, 0x3f, 0xfe, 0x3f, 0xff, 0xc0, 
54
  0x00, 0x3e, 0x0f, 0xff, 0xf0, 0xff, 0xff, 0x07, 0xef, 0xe0, 0x7c, 0x07, 0xe0, 0x3f, 0xff, 0x80, 
55
  0x00, 0x3e, 0x0f, 0x83, 0xe0, 0xf8, 0x3f, 0x07, 0xff, 0xc0, 0xfc, 0x07, 0xc0, 0x7e, 0x1f, 0x80, 
56
  0x00, 0x7e, 0x1f, 0x87, 0xe0, 0xf8, 0x3e, 0x07, 0xff, 0x80, 0xfc, 0x07, 0xc0, 0x7c, 0x1f, 0x80, 
57
  0x00, 0x7c, 0x1f, 0x07, 0xc1, 0xf8, 0x7e, 0x0f, 0xff, 0x00, 0xf8, 0x0f, 0xc0, 0xfc, 0x1f, 0x00, 
58
  0x00, 0xfc, 0x1f, 0x07, 0xc1, 0xf0, 0x7e, 0x0f, 0xbe, 0x01,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x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
70
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
71
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
72
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
73
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
74
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
75
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
76
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
77
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
78
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
79
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
80
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
81
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
82
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
83
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
84
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
85
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
86
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
87
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
88
};
89
90
void setup() {
91
  Serial.begin(9600);
92
  delay(50);
93
94
  pinMode(13, INPUT);
95
96
  r.begin(ROTARY_PIN1, ROTARY_PIN2, CLICKS_PER_STEP, MIN_POS, MAX_POS, START_POS, INCREMENT);
97
  r.setChangedHandler(rotate);
98
99
  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
100
    Serial.println(F("SSD1306 allocation failed"));
101
    for(;;);
102
  }
103
  
104
  startup();
105
  display.setTextSize(4);
106
  display.setTextColor(SSD1306_WHITE);
107
}
108
109
void loop() {
110
  showTemp();
111
  if (digitalRead(13) == LOW) {
112
    setTemp();
113
  }
114
}
115
116
void setTemp(void) {
117
  delay(500);
118
  display.setTextSize(4);
119
  display.setTextColor(SSD1306_WHITE);
120
  display.clearDisplay();
121
  display.fillRect(0, 60, 128, 10, SSD1306_WHITE);
122
  display.display();
123
  do {
124
      r.loop();
125
  } while (digitalRead(13) != LOW);
126
  delay(500);
127
}
128
129
void showTemp(void) {
130
  display.clearDisplay();
131
  display.setCursor(0, 10);
132
  int tempval = analogRead(A0);
133
  long temp = map (tempval, 0, 1023, -20, 20);
134
  display.print(temp);
135
  display.print((char)247);
136
  display.print("C");
137
  display.display();
138
}
139
140
void startup(void) {
141
  display.clearDisplay();
142
  display.drawBitmap(
143
    (display.width()  - LOGO_WIDTH ) / 2,
144
    (display.height() - LOGO_HEIGHT) / 2,
145
    logo_bmp, LOGO_WIDTH, LOGO_HEIGHT, 1);
146
  display.display();
147
  delay(1500);
148
  String str = "Modell 650";
149
  display.clearDisplay();
150
  display.setTextColor(SSD1306_WHITE);
151
  display.setTextSize(2);
152
  display.setCursor(128-(str.length())/2, 10);
153
  display.print(F("Modell 650"));
154
  display.display();
155
  delay(1500);
156
}
157
158
void rotate(ESPRotary& r) {
159
  int pos = r.getPosition();
160
  Serial.println(pos);
161
  display.clearDisplay();
162
  display.fillRect(0, 60, 128, 10, SSD1306_WHITE);
163
  display.setCursor(0, 10);
164
  display.print(pos);
165
  display.print((char)247);
166
  display.print("C");
167
  display.display();
168
}

: Bearbeitet durch User
von Stefan F. (Gast)


Lesenswert?

Espressif empfiehlt maximal 20ms, sonst riskierst du einen Ausfall der 
WLAN Schnittstelle. Deine Warteschleife dauert so lange, dass der 
Watchdog einen Neustart auslöst.

Diesen und weitere gemeine Hinweise findest du auf 
http://stefanfrings.de/esp8266/index.html#fallstricke
1
do 
2
{
3
    r.loop();
4
} 
5
while (digitalRead(13) != LOW);

Dein Programm ist falsch strukturiert. Eigentlich soll es solche 
Warteschleifen nicht geben. Die loop soll bei jedem Durchlauf nur kurz 
etwas machen und dann die Kontrolle zurück an das Betriebssystem 
übergeben (indem sie sich beendet).

Deine langen delay() Aufrufe sind schon schlecht. An dieser Stelle 
rettet dich allerdings das Arduino Framework, weil es innerhalb der 
delay Funktion aktiv Kontrolle an das Betriebssystem abgibt indem es 
yield() aufruft. Das könntest du in deiner Warteschleife auch tun. 
Allerdings treibt man damit den Verbrauch von Stapelspeicher (der eh 
relativ klein ist) in die Höhe und handelt sich ab und zu mal andere 
unerwartete Seiteneffekte ein.

Lies meinen Artikel über Zustandsautomaten, denn da lernst du, wie du 
deine Aufgabe ganz ohne Warteschleifen umsetzen kannst.
http://stefanfrings.de/multithreading_arduino/index.html

von Peerless R. (peerless_r)


Lesenswert?

Hey danke für deine ausführliche Antwort, versteh das Problem jetzt 
auch, da hat wirklich ne Fliege geklemmt. Hab den Code umgeschrieben, 
damit er im main loop bleibt und dort über Variablen checkt ob er sich 
im "Settings Modus" befindet. Jetzt funktioniert alles wunderbar. :)

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.