Skip to main content Link Search Menu Expand Document (external link)

Interface & App

Table of contents
  1. Interface & App
    1. Group Assignment for Output Devices
    2. Web Interface with ESP32-CAM
    3. PySerial Interface with D21E18A
      1. Fixing the Morse Code Decoder Issue in ToF-based Range Meter
      2. Code Snippets

Group Assignment for Output Devices

compare as many tool options as possible.

shown in Week 11 of EECS Group Assignments.

Web Interface with ESP32-CAM

As shown in Week 10 | Networking & Comm, I made a demo of a web interface with ESP32-CAM showing the input device ToF sensor that I made in Week 8 | Input Devices and the built-in Hall magnetic sensor.

More implementation details can be found in Week 10 | Networking & Comm along with code snippets.

PySerial Interface with D21E18A

Inspired by Nadieh’s HTML5 Canvas visualization, I decided to make a similar visualization using PySerial to read the ToF sensor (VL53L0X) and visualize its value from a web interface. The whole hardware is built on the output device ( \(128\times 64\) OLED display) of Week 9 | Output Devices and the input device (ToF sensor VL53L0X) of Week 8 | Input Devices.

Fixing the Morse Code Decoder Issue in ToF-based Range Meter

I was facing the Morse Code Decoder not working issue after merging it with the ToF-based Range Meter. After talking with Anthony, he recommended me to output a bunch of intermediate values for Morse Code Decoder and compare them with the expected values (from the stand-alone counterpart of the Morse code decoder). Two issues were found:

  • Time accumulation issue: the display occupied ~50 ms instead of 1 ms.
  • Line buffer issue: the line buffer was not created and thus not displayed consistently on the OLED display.

The following code snippets have addressed these two issues in L#26-27 and L#50-53, respectively.

Code Snippets

pyserial_tof_range_meter_morsecode.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
#include <SPI.h>
#include <Wire.h>
#include <VL53L0X.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

#define OLED_RESET     -1   // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C // 0x3C for 128x64 OLED https://www.amazon.com/PEMENOL-Display-0-96inch-Raspberry-Microcontroller/dp/B07F3KY8NF
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire1, OLED_RESET); // Wire1 for second I2C channel in D21E18A

VL53L0X sensorX; // ToF sensor
const int pin_shutdown = 15; // Pin of ToF sensor SHUTDOWN

const int pin_led = 18; // Pin of the LED
const int pin_btn = 14; // Pin of the button

int ledState = HIGH;         
int buttonState = HIGH;             
int lastButtonState = HIGH;  

// const long debounceDelay =  50; // debounce delay time in ms [originally 50 ms; increased to improve robustness to noise]
// const long pauseInterval = 200;  // pause interval [ms]
const long debounceDelay = 1; // debounce delay time in ms [originally 50 ms; increased to improve robustness to noise]
const long pauseInterval = 4;  // pause interval in ms [ms]
long signalLength =   0;  // length of the signal
long pauseTime    =   0;  // pause time

const String dot  = "·";
const String dash = "—";
String morse = "";
String codeBuffer = "";

bool newChar = true;
bool newLine = true;

void setup() {
  Serial.begin(9600);

  init_button_led();   // button and LED for Morse Code Decoder initialization
  init_tof_sensor();   // ToF sensor initialization
  init_oled_display(); // OLED display initialization  
}

void loop() {
  int range = read_tof_sensor();
  String code = read_morse_code();
  if (newLine)
    codeBuffer = "";
  else 
    codeBuffer = codeBuffer + code;

  display_range_morse_code(range, codeBuffer);
}

void init_button_led() { // button and LED for Morse Code Decoder initialization
  pinMode(pin_btn, INPUT_PULLUP); // use internal pull-up resistor [Neil & Anthony]
  pinMode(pin_led, OUTPUT);

  // while(digitalRead(pin_btn));
  Serial.println("Range Meter and Morse Code Decoder:");
}

void init_tof_sensor() { // ToF sensor initialization
  Wire.begin();

  pinMode(pin_shutdown, OUTPUT);
  digitalWrite(pin_shutdown, HIGH);

  sensorX.setTimeout(500);
  if (!sensorX.init())
  {
    Serial.println("Failed to detect and initialize sensorX!");
    while (1) {}
  }
}

void init_oled_display() { // OLED display initialization
  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }

  // Show initial display buffer contents on the screen --
  // the library initializes this with an Adafruit splash screen.
  display.display();
  delay(2000); // Pause for 2 seconds

  // Clear the buffer
  display.clearDisplay();

  // Draw a single pixel in white
  // display.drawPixel(10, 10, SSD1306_WHITE);

  // testdrawchar();      // Draw characters of the default font
  // testdrawstyles();    // Draw 'stylized' characters  
}

int read_tof_sensor() {
  int rangeX = sensorX.readRangeSingleMillimeters();
  if (sensorX.timeoutOccurred()) { Serial.print(" SensorX TIMEOUT"); }
  Serial.println(rangeX);
  return rangeX;
}

String read_morse_code() {
  String code = "";
  buttonState = digitalRead(pin_btn);
  
  if (!buttonState && !lastButtonState) { // button pressing status - gathering signal
    ++signalLength; 
  }
  else if(buttonState && !lastButtonState){ // button released - send signal (dot or dash)
    if (signalLength>=debounceDelay && signalLength<2*pauseInterval) {
      morse = morse + dot;
    } 
    else if (signalLength>=2*pauseInterval) {
      morse = morse + dash;
    }
    signalLength = 0; 
    digitalWrite(pin_led, LOW); 
  }
  else if (!buttonState && lastButtonState) { // button just pressed - reset signal
    pauseTime = 0;
    digitalWrite(pin_led, HIGH);  
    newChar = false;
    newLine = false;
  }
  else if (buttonState && lastButtonState) {  
    ++pauseTime;
    if (( pauseTime>=4*pauseInterval ) && (!newChar)) { 
      code = decoder(morse);
      newChar = true;
      morse = "";
    }
    if ((pauseTime>=20*pauseInterval) && (!newLine)) {
      code = "\n";
      newLine = true;
    }
  }
  lastButtonState = buttonState;
  delay(1); 

  return code; 
}

String decoder(String message)   {   
  String code = "";                        
  if (message == "·—")
    code = "A";
  else if (message == "—···")  
    code = "B";
  else if (message == "—·—·")  
    code = "C";
  else if (message == "—··")  
    code = "D";
  else if (message == "·")  
    code = "E";
  else if (message == "··—·")  
    code = "F";
  else if (message == "——·")  
    code = "G";
  else if (message == "····")  
    code = "H";
  else if (message == "··")  
    code = "I";
  else if (message == "·———")  
    code = "J";
  else if (message == "—·—")  
    code = "K";
  else if (message == "·—··")  
    code = "L";
  else if (message == "——")  
    code = "M";
  else if (message == "—·")  
    code = "N";
  else if (message == "———")  
    code = "O";
  else if (message == "·——·")  
    code = "P";
  else if (message == "——·—")  
    code = "Q";
  else if (message == "·—·")  
    code = "R";
  else if (message == "···")  
    code = "S";
  else if (message == "—")  
    code = "T";
  else if (message == "··—")  
    code = "U";
  else if (message == "···—")  
    code = "V";
  else if (message == "·——")  
    code = "W";
  else if (message == "—··—")  
    code = "X";
  else if (message == "—·——")  
    code = "Y";
  else if (message == "——··")  
    code = "Z";
  else if (message == "·————")  
    code = "1";
  else if (message == "··———")  
    code = "2";
  else if (message == "···——")  
    code = "3";
  else if (message == "····—")  
    code = "4";
  else if (message == "·····")  
    code = "5";
  else if (message == "—····")
    code = "6";
  else if (message == "——···")  
    code = "7";
  else if (message == "———··")  
    code = "8";
  else if (message == "————·")  
    code = "9";
  else if (message == "—————")  
    code = "0";
  else 
    code = message;
    
  message = ""; 
  return code;
}

void testdrawchar(void) {
  display.clearDisplay();

  display.setTextSize(1);      // Normal 1:1 pixel scale
  display.setTextColor(SSD1306_WHITE); // Draw white text
  display.setCursor(0, 0);     // Start at top-left corner
  display.cp437(true);         // Use full 256 char 'Code Page 437' font

  // Not all the characters will fit on the display. This is normal.
  // Library will draw what it can and the rest will be clipped.
  for(int16_t i=0; i<256; i++) {
    if(i == '\n') display.write(' ');
    else          display.write(i);
  }

  display.display();
  delay(2000);
}

void testdrawstyles(void) {
  display.clearDisplay();

  display.setTextSize(1);             // Normal 1:1 pixel scale
  display.setTextColor(SSD1306_WHITE);        // Draw white text
  display.setCursor(0,0);             // Start at top-left corner
  display.println(F("Hello, world!"));

  display.setTextColor(SSD1306_BLACK, SSD1306_WHITE); // Draw 'inverse' text
  display.println(3.141592);

  display.setTextSize(2);             // Draw 2X-scale text
  display.setTextColor(SSD1306_WHITE);
  display.print(F("0x")); display.println(0xDEADBEEF, HEX);

  display.display();
  delay(2000);
}

void display_range_morse_code(int range, String code) {
  display.clearDisplay();

  display.setTextSize(2);             // Normal 1:1 pixel scale
  display.setTextColor(SSD1306_WHITE);        // Draw white text
  display.setCursor(0,0);             // Start at top-left corner
  display.print(F("d = "));  
  display.print(range);
  display.println(F("mm"));  
  display.println(code);

  display.display();
  // delay(10);  
}