Op basis van o.a.:
https://www.instructables...el-Wireless-Light-Dimmer/
https://circuits4you.com/...266-timer-ticker-example/
probeer ik een licht dimmer te maken op NodeMCU. Een en ander gaat goed, behalve de timer die om de x microseconde moet triggeren.
Onderstaande code werkt en loopt niet vast, pas ik de waarde van de trigger naar het gewenste getal aan (28 --> +/- 100 microseconde, zie instructables link) dan crasht de code met een Exception "IllegalInstructionCause"
https://arduino-esp8266.r...est/exception_causes.html
Op Github thread wordt gesuggereerd "core_esp8266_timer.c" aan te passen met toevoeging van
https://github.com/esp8266/Arduino/issues/2083
Ook dit werkt niet.
TL;DR waarom crasht het programa als ik de juiste timing instel, d.w.z. 28 ipv 2800?
https://www.instructables...el-Wireless-Light-Dimmer/
https://circuits4you.com/...266-timer-ticker-example/
probeer ik een licht dimmer te maken op NodeMCU. Een en ander gaat goed, behalve de timer die om de x microseconde moet triggeren.
Onderstaande code werkt en loopt niet vast, pas ik de waarde van de trigger naar het gewenste getal aan (28 --> +/- 100 microseconde, zie instructables link) dan crasht de code met een Exception "IllegalInstructionCause"
https://arduino-esp8266.r...est/exception_causes.html
Op Github thread wordt gesuggereerd "core_esp8266_timer.c" aan te passen met toevoeging van
code:
voor de functie die vanuit de code worden aangeroepen.1
| ICACHE_RAM_ATTR |
https://github.com/esp8266/Arduino/issues/2083
Ook dit werkt niet.
TL;DR waarom crasht het programa als ik de juiste timing instel, d.w.z. 28 ipv 2800?
C:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
| #include <Arduino.h> #include <ESP8266WiFi.h> #include <ESP8266WiFiMulti.h> #include <WebSocketsServer.h> #include <ESP8266WebServer.h> #include <ESP8266mDNS.h> #include <Hash.h> #include <Ticker.h> #include "hw_timer.h" #include "FS.h" #define GATE 15 #define SYNC 13 #define PIN_LED 16 #define STEP_TIME 100 //STEP_TIME: 1/(2*frequency*NumLvls)* 10^6 ESP8266WiFiMulti WiFiMulti; ESP8266WebServer server (80); WebSocketsServer webSocket = WebSocketsServer(81); const char* ssid = "NEWTON"; // Your ssid const char* password = "t6A5EB57AK"; // Your Password String html_home; uint8_t status; uint8_t speed; volatile int i=0; volatile boolean zero_cross=0; void ICACHE_RAM_ATTR dim_check() { digitalWrite(PIN_LED, !digitalRead(PIN_LED)); //Serial.println(millis()); if(zero_cross == true) { if(status == 1){ if(i>=speed + 4) { digitalWrite(GATE, LOW); // turn on light i=0; // reset time step counter zero_cross = false; //reset zero cross detection } else { i++; // increment time step counter } } } wdt_reset(); } void zero_cross_detect() { if(status == 1){ digitalWrite(GATE, HIGH); } zero_cross = true; } void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght) { switch(type) { case WStype_DISCONNECTED: Serial.printf("[%u] Disconnected!\n", num); break; case WStype_CONNECTED: { IPAddress ip = webSocket.remoteIP(num); Serial.printf("[%u] Connected from %d.%d.%d.%d url: %s\n", num, ip[0], ip[1], ip[2], ip[3], payload); // send message to client webSocket.sendTXT(num, "Connected"); } break; case WStype_TEXT: Serial.printf("[%u] get Text: %s\n", num, payload); if(payload[0] == '#') { uint32_t cmdMessage = (uint32_t) strtol((const char *) &payload[1], NULL, 16); status = ((cmdMessage >> 8) & 0xFF); speed = ((cmdMessage >> 0) & 0xFF); speed = ((speed)>(95)?(95):(speed)); speed = ((speed)<(5)?(5):(speed)); Serial.printf("Status: %d speed: %d\n", status, speed); if ( status == 0 ) { digitalWrite(GATE, status); } } break; } } void prepareFile(){ Serial.println("Prepare file system"); SPIFFS.begin(); File file = SPIFFS.open("/home.html", "r"); if (!file) { Serial.println("file open failed"); }else{ Serial.println("file open success"); html_home = ""; while (file.available()) { String line = file.readStringUntil('\n'); html_home += line + "\n"; } file.close(); } } void setup() { speed = 50; status = 1; Serial.begin(115200); Serial.println(); for(uint8_t t = 4; t > 0; t--) { Serial.printf("[SETUP] BOOT WAIT %d...\n", t); Serial.flush(); delay(1000); } pinMode(GATE, OUTPUT); pinMode(SYNC, INPUT); pinMode(PIN_LED, OUTPUT); digitalWrite(GATE, 0); prepareFile(); // Connect to WiFi network WiFi.mode(WIFI_STA); 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"); // Start the server server.begin(); Serial.println("Server started"); // Print the IP address Serial.println(WiFi.localIP()); // start webSocket server webSocket.begin(); webSocket.onEvent(webSocketEvent); if(MDNS.begin("esp8266")) { Serial.println("MDNS responder started"); } // handle index server.on("/", []() { // send home.html server.send(200, "text/html", html_home); }); server.begin(); // Add service to MDNS MDNS.addService("http", "tcp", 80); MDNS.addService("ws", "tcp", 81); Serial.printf("Server Start\n"); //timer_init(); attachInterrupt(SYNC, zero_cross_detect, RISING); timer1_disable(); timer1_attachInterrupt(dim_check); timer1_isr_init(); timer1_enable(TIM_DIV256, TIM_EDGE, TIM_LOOP); timer1_write(2800); digitalWrite(PIN_LED, LOW); //Set up ESP watchdog ESP.wdtDisable(); ESP.wdtEnable(WDTO_8S); } void loop() { webSocket.loop(); server.handleClient(); } |