-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ino
301 lines (273 loc) · 9.12 KB
/
main.ino
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
// Constants
#include <Arduino.h>
#include <WiFi.h>
#include <WebSocketsServer.h>
#include <EEPROM.h>
#define EEPROM_SIZE 1
#define R1 10000
#define R2 20000
const char* ssid = "-";
const char* password = "-"
String messagebanana;
// Globals
WebSocketsServer webSocket = WebSocketsServer(80);
int speed = 0; // Global variable to store speed value
int RPM = 0; // Global variable to store RPM value
int Hz = 0; // Global variable to store Hz value
volatile int counter_rpm = 0;
const int pwmPin = 32; // 16 corresponds to GPIO16
const int freq = 25000;
const int ledChannel = 0;
const int resolution = 8;
const int tacho_pin = 25;
const int button_pin = 14;
const int enable_pin = 19;
///const int current_pin = 34;
///float currentA = 0;
byte enablePinState = LOW;
#include <ESP32Servo.h>
ESP32PWM pwm;
int counter = 50;
#include <ESP32Encoder.h> // https://github.com/madhephaestus/ESP32Encoder.git
ESP32Encoder encoder;
#define CLK 4 // CLK ENCODER
#define DT 15 // DT ENCODER
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
void IRAM_ATTR rpm_fan() {
counter_rpm++;
}
void setup() {
// Start Serial port
Serial.begin(115200);
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
Serial.println(F("SSD1306 allocation failed"));
}
EEPROM.begin(EEPROM_SIZE);
// Connect to access point
Serial.println("Connecting");
WiFi.begin(ssid, password);
while ( WiFi.status() != WL_CONNECTED ) {
delay(500);
Serial.print(".");
}
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE,BLACK);
display.setCursor(0, 0);
// Display static text
// Print our IP address
Serial.println("Connected!");
Serial.print("My IP address: ");
Serial.println(WiFi.localIP());
display.println("ip: " + WiFi.localIP().toString());
display.display();
// Start WebSocket server and assign callback
webSocket.begin();
webSocket.onEvent(onWebSocketEvent);
// Setup interrupts for encoder and fan tachometer
pinMode(button_pin, INPUT_PULLUP);
pinMode(tacho_pin, INPUT);
pinMode(enable_pin, OUTPUT);
enablePinState = EEPROM.read(0);
digitalWrite(enable_pin, enablePinState); // Set the LED state
encoder.attachHalfQuad ( DT, CLK );
encoder.setCount ( 0 );
ESP32PWM::allocateTimer(3);
pwm.attachPin(pwmPin, freq, resolution);
pwm.write(counter);
//disableCore0WDT();
xTaskCreatePinnedToCore(
Task1code, /* Task function. */
"Task1", /* Name of task. */
10000, /* Stack size of task */
NULL, /* Parameter of the task */
1, /* Priority of the task */
NULL, /* Task handle to keep track of created task */
0 /* Pin task to core 0 */
);
delay(100);
xTaskCreatePinnedToCore(
Task2code, /* Task function. */
"Task2", /* Name of task. */
10000, /* Stack size of task */
NULL, /* Parameter of the task */
1, /* Priority of the task */
NULL, /* Task handle to keep track of created task */
1 /* Pin task to core 1 */
);
delay(100);
}
String response;
void onWebSocketEvent(uint8_t num,
WStype_t type,
uint8_t * payload,
size_t length) {
// Figure out the type of WebSocket event
switch(type) {
// Client has disconnected
case WStype_DISCONNECTED:
Serial.printf("[%u] Disconnected!\n", num);
break;
// New client has connected
case WStype_CONNECTED:
{
IPAddress ip = webSocket.remoteIP(num);
Serial.printf("[%u] Connection from ", num);
Serial.println(ip.toString());
}
break;
// Echo text message back to client
case WStype_TEXT:
Serial.printf("[%u] Text: %s\n", num, payload);
messagebanana = (const char*)payload;
// Check if received message is "nya~"
if (messagebanana.equals("nya~")) {
webSocket.sendTXT(num, "meow");
}
else if (messagebanana.equals("off")) {
webSocket.sendTXT(num, "turning off");
speed = counter;
encoder.setCount(0); // Set encoder count to 0
counter = 0;
}
else if (messagebanana.equals("on")) {
response = "turning on, speed = " + String(speed);
encoder.setCount(speed * 2); // Set encoder count to 0
counter = speed;
webSocket.sendTXT(num, response);
}
else if (messagebanana.equals("!enablepinstate")) {
response = "enablePinState = " + String(enablePinState);
enablePinState = !enablePinState;
EEPROM.write(0, enablePinState);
EEPROM.commit();
webSocket.sendTXT(num, response);
}
else if (messagebanana.startsWith("set speed")) {
// Extract speed value from the message
int spaceIndex = messagebanana.indexOf(' ');
if (spaceIndex != -1) {
String speedString = messagebanana.substring(spaceIndex + 7);
Serial.print("Speed string: ");
Serial.println(speedString);
Serial.print("Length of speed string: ");
Serial.println(speedString.length());
// Convert speed string to an integer
speed = speedString.toInt();
if(speed > 100) speed = 100,counter = 100,encoder.setCount(200);
else if (speed < 0) speed = 0,counter = 0,encoder.setCount(0);
else counter = speed,encoder.setCount(speed * 2);
Serial.print("Set speed to: ");
Serial.println(speed);
// Send a message back to the client indicating the current speed
webSocket.sendTXT(num, "Speed set to: " + String(speed));
}
}
else if (messagebanana.equals("info") || messagebanana.equals("status")) {
response = "speed=" + String(speed) + ",RPM=" + String(RPM) + ",Hz=" + String(Hz) + ",counter=" + String(counter);
webSocket.sendTXT(num, response);
}
else {
// Otherwise, echo the received message back to the client
webSocket.sendTXT(num, payload);
}
break;
// For everything else: do nothing
case WStype_BIN:
case WStype_ERROR:
case WStype_FRAGMENT_TEXT_START:
case WStype_FRAGMENT_BIN_START:
case WStype_FRAGMENT:
case WStype_FRAGMENT_FIN:
default:
break;
}
}
void updateCount() {
long new_count = encoder.getCount() / 2; // Get the new count value
// Check if new_count is less than 0
if (new_count < 0) {
new_count = 0; // Set new_count to 0
encoder.setCount(0); // Set encoder count to 0
}
// Check if new_count is greater than 100
if (new_count > 100) {
new_count = 100; // Set new_count to 100
encoder.setCount(200); // Set encoder count to 100
}
counter = new_count; // Update the global count variable with the new value
}
void button_press(){
if (counter == 0) {
encoder.setCount(speed * 2);
counter = speed;
}
else {
speed = counter;
encoder.setCount(0);
counter = 0;
}
}
void Task1code( void * pvParameters ) {
Serial.print("Task1 running on core ");
Serial.println(xPortGetCoreID());
for (;;) {
if (counter == 0){
digitalWrite(enable_pin,!enablePinState);
}
else digitalWrite(enable_pin,enablePinState);
vTaskDelay(1000 / portTICK_PERIOD_MS);
if(button_pin == LOW){
button_press();
delay(100);
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}}
void Task2code( void * pvParameters ) {
Serial.print("Task2 running on core ");
Serial.println(xPortGetCoreID());
for (;;) {
counter_rpm = 0;
attachInterrupt(digitalPinToInterrupt(tacho_pin), rpm_fan, CHANGE);
vTaskDelay(1000 / portTICK_PERIOD_MS);
detachInterrupt(digitalPinToInterrupt(tacho_pin));
Hz = counter_rpm / 2;
RPM = Hz * 60 / 2;
///float adc = analogRead(current_pin);
///float adc_voltage = adc * (3.3 / 4096.0);
///float currentA_voltage = (adc_voltage * (R1+R2)/R2);
///currentA = (currentA_voltage - 2.5) / 0.100;
display.setCursor(0, 26); // Set cursor to the next line
display.println("HZ: " + String(Hz) + " ");
Serial.print("Hz: ");
Serial.println(Hz);
display.setCursor(0, 16); // Set cursor to the next line
display.println("RPM: " + String(RPM) + " ");
Serial.print("RPM: ");
Serial.println(RPM);
display.setCursor(0, 36); // Set cursor to the next line
updateCount();
Serial.print("Position: ");
display.println("S: " + String(counter) + " ");
Serial.println(counter);
///Serial.print("Current Value: ");
///Serial.println(currentA);
///display.setCursor(0, 46); // Set cursor to the next line
///display.println("A: " + String(currentA) + " ");
pwm.write(map(counter, 0, 100, 0, 255));
//long newPosition = encoder.getCount() / 2;
// display.println("S: " + String(newPosition) + " ");
//Serial.println(newPosition);
//pwm.write(newPosition);
display.display();
}
}
void loop() {
webSocket.loop();
vTaskDelay(500 / portTICK_PERIOD_MS);
}