├── Slide2.JPG ├── MorseSample-15WPM.wav ├── MorseSample-33WPM.wav ├── README.md ├── Licence.txt └── ESP32_Morse_Code_Decoder_02.ino /Slide2.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/G6EJD/ESP32-Morse-Decoder/HEAD/Slide2.JPG -------------------------------------------------------------------------------- /MorseSample-15WPM.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/G6EJD/ESP32-Morse-Decoder/HEAD/MorseSample-15WPM.wav -------------------------------------------------------------------------------- /MorseSample-33WPM.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/G6EJD/ESP32-Morse-Decoder/HEAD/MorseSample-33WPM.wav -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ESP32-Morse-Decoder 2 | Using an ESP32 and OLED with a basic microphone to decode Morse Code live to the display 3 | 4 | You can use the ESP8266, but will need to expriement with the sampling_frequency, try 30000 5 | 6 | If you need to reduce the audio bandwidth of the decoder; currently about 320Hz to cope with very noisy bands/audio, then adjust these lines: 7 | 8 | float sampling_freq = 45000; 9 | 10 | float target_freq   = 558.0; // adjust for your needs see above 11 | 12 | int   n = 128;               // if you change here please change next line also 13 | 14 | n = 128 determines the number of samples, try to get this as high as possible to reduce bandwidth, I went for a compromise with 128. 15 | 16 | Sampling frequency is a function of CPU speed, so largely fixed, but can be veried downwards to about 30000, needs some experimentation with your set-up. 17 | 18 | Target frequency is the received audio frequency, if you prefer a higher beat for Morse tones, then adjust accordingly and remeber to tune your radio to try to match your chosen frequency. 19 | 20 | -------------------------------------------------------------------------------- /Licence.txt: -------------------------------------------------------------------------------- 1 | This software, the ideas and concepts is Copyright (c) David Bird 2014 and beyond. 2 | 3 | All rights to this software are reserved. 4 | 5 | It is prohibited to redistribute or reproduce of any part or all of the software contents in any form other than the following: 6 | 7 | 1. You may print or download to a local hard disk extracts for your personal and non-commercial use only. 8 | 9 | 2. You may copy the content to individual third parties for their personal use, but only if you acknowledge the author David Bird as the source of the material. 10 | 11 | 3. You may not, except with my express written permission, distribute or commercially exploit the content. 12 | 13 | 4. You may not transmit it or store it in any other website or other form of electronic retrieval system for commercial purposes. 14 | 15 | 5. You MUST include all of this copyright and permission notice ('as annotated') and this shall be included in all copies or substantial portions of the software and where the software use is visible to an end-user. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS" FOR PRIVATE USE ONLY, IT IS NOT FOR COMMERCIAL USE IN WHOLE OR PART OR CONCEPT. 18 | 19 | FOR PERSONAL USE IT IS SUPPLIED WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 20 | 21 | IN NO EVENT SHALL THE AUTHOR OR COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /ESP32_Morse_Code_Decoder_02.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Morse Code Decoder using an OLED and basic microphone 3 | 4 | The MIT License (MIT) Copyright (c) 2017 by David Bird. 5 | ### The formulation and calculation method of an IAQ - Internal Air Quality index ### 6 | ### The provision of a general purpose webserver ### 7 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files 8 | (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, 9 | publish, distribute, but not to use it commercially for profit making or to sub-license and/or to sell copies of the Software or to 10 | permit persons to whom the Software is furnished to do so, subject to the following conditions: 11 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 12 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 13 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 14 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 15 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 16 | See more at http://dsbird.org.uk 17 | 18 | CW Decoder by Hjalmar Skovholm Hansen OZ1JHM VER 1.01 19 | Feel free to change, copy or what ever you like but respect 20 | that license is http://www.gnu.org/copyleft/gpl.html 21 | Read more here http://en.wikipedia.org/wiki/Goertzel_algorithm 22 | Adapted for the ESP32/ESP8266 by G6EJD 23 | */ 24 | #include "SH1106.h" // https://github.com/squix78/esp8266-oled-ssd1306 25 | SH1106 display(0x3c, 17,16); // 1.3" OLED display object definition (address, SDA, SCL) Connect OLED SDA , SCL pins to ESP SDA, SCL pins 26 | 27 | float magnitude = 0;; 28 | int magnitudelimit = 100; 29 | int magnitudelimit_low = 100; 30 | int realstate = LOW; 31 | int realstatebefore = LOW; 32 | int filteredstate = LOW; 33 | int filteredstatebefore = LOW; 34 | 35 | // The sampling frequency is 45000 on an 80MHz CPU 36 | // you can set the tuning tone to 496, 558, 744 or 992 37 | // 'n' the number of samples determines bandwidth 38 | 39 | float coeff; 40 | float Q1 = 0; 41 | float Q2 = 0; 42 | float sine; 43 | float cosine; 44 | float sampling_freq = 45000; 45 | float target_freq = 558.0; // adjust for your needs see above 46 | int n = 128; // if you change here please change next line also 47 | int testData[128]; 48 | float bw; 49 | 50 | // Noise Blanker time which shall be computed so this is initial 51 | int nbtime = 6; /// ms noise blanker 52 | 53 | long starttimehigh; 54 | long highduration; 55 | long lasthighduration; 56 | long hightimesavg; 57 | long lowtimesavg; 58 | long startttimelow; 59 | long lowduration; 60 | long laststarttime = 0; 61 | #define num_chars 14 62 | char CodeBuffer[num_chars]; 63 | char DisplayLine[num_chars+1]; 64 | int stop = LOW; 65 | int wpm; 66 | 67 | void setup() { 68 | Wire.begin(17,16); // SDA, SCL 69 | display.init(); 70 | display.setFont(ArialMT_Plain_10); 71 | display.flipScreenVertically(); // Adjust to suit or remove 72 | //////////////////////////////////// The basic goertzel calculation ////////////////////////////////////// 73 | target_freq = 496.0; // tune your radio this this beat frequency 74 | // target_freq=558.0; 75 | // target_freq=744.0; 76 | // target_freq=992.0; 77 | bw = sampling_freq / n; 78 | int k; 79 | float omega; 80 | k = (int) (0.5 + ((n * target_freq) / sampling_freq)); 81 | omega = (2.0 * PI * k) / n; 82 | sine = sin(omega); 83 | cosine = cos(omega); 84 | coeff = 2.0 * cosine; 85 | Serial.begin(115200); 86 | for (int i = 0; i <= num_chars; i++) DisplayLine[i] = ' '; 87 | } 88 | 89 | void loop() { 90 | for (byte index = 0; index < n; index++) {testData[index] = analogRead(A0);} 91 | for (byte index = 0; index < n; index++) { 92 | float Q0; 93 | Q0 = coeff * Q1 - Q2 + (float) testData[index]; 94 | Q2 = Q1; 95 | Q1 = Q0; 96 | } 97 | float magnitudeSquared = (Q1 * Q1) + (Q2 * Q2) - Q1 * Q2 * coeff; // we do only need the real part // 98 | magnitude = sqrt(magnitudeSquared); 99 | Q2 = 0; 100 | Q1 = 0; 101 | 102 | if (magnitude > magnitudelimit_low) { magnitudelimit = (magnitudelimit + ((magnitude - magnitudelimit) / 6)); } /// moving average filter 103 | if (magnitudelimit < magnitudelimit_low) magnitudelimit = magnitudelimit_low; 104 | 105 | // Now check the magnitude // 106 | if (magnitude > magnitudelimit * 0.6) // just to have some space up 107 | realstate = HIGH; 108 | else 109 | realstate = LOW; 110 | 111 | // Clean up the state with a noise blanker // 112 | 113 | if (realstate != realstatebefore) {laststarttime = millis();} 114 | if ((millis() - laststarttime) > nbtime) { 115 | if (realstate != filteredstate) { 116 | filteredstate = realstate; 117 | } 118 | } 119 | 120 | if (filteredstate != filteredstatebefore) { 121 | if (filteredstate == HIGH) { 122 | starttimehigh = millis(); 123 | lowduration = (millis() - startttimelow); 124 | } 125 | 126 | if (filteredstate == LOW) { 127 | startttimelow = millis(); 128 | highduration = (millis() - starttimehigh); 129 | if (highduration < (2 * hightimesavg) || hightimesavg == 0) { 130 | hightimesavg = (highduration + hightimesavg + hightimesavg) / 3; // now we know avg dit time ( rolling 3 avg) 131 | } 132 | if (highduration > (5 * hightimesavg) ) { 133 | hightimesavg = highduration + hightimesavg; // if speed decrease fast .. 134 | } 135 | } 136 | } 137 | 138 | // Now check the baud rate based on dit or dah duration either 1, 3 or 7 pauses 139 | if (filteredstate != filteredstatebefore) { 140 | stop = LOW; 141 | if (filteredstate == LOW) { // we did end on a HIGH 142 | if (highduration < (hightimesavg * 2) && highduration > (hightimesavg * 0.6)) { /// 0.6 filter out false dits 143 | strcat(CodeBuffer,"."); 144 | //Serial.print("."); 145 | } 146 | if (highduration > (hightimesavg * 2) && highduration < (hightimesavg * 6)) { 147 | strcat(CodeBuffer,"-"); 148 | //Serial.print("-"); 149 | wpm = (wpm + (1200 / ((highduration) / 3))) / 2; //// the most precise we can do ;o) 150 | } 151 | } 152 | 153 | if (filteredstate == HIGH) { //// we did end a LOW 154 | float lacktime = 1; 155 | if (wpm > 25)lacktime = 1.0; /// when high speeds we have to have a little more pause before new letter or new word 156 | if (wpm > 30)lacktime = 1.2; 157 | if (wpm > 35)lacktime = 1.5; 158 | if (lowduration > (hightimesavg * (2 * lacktime)) && lowduration < hightimesavg * (5 * lacktime)) { // letter space 159 | CodeToChar(); 160 | CodeBuffer[0] = '\0'; 161 | //AddCharacter('/'); 162 | //Serial.print("/"); 163 | } 164 | if (lowduration >= hightimesavg * (5 * lacktime)) { // word space 165 | CodeToChar(); 166 | CodeBuffer[0] = '\0'; 167 | AddCharacter(' '); 168 | Serial.print(" "); 169 | } 170 | } 171 | } 172 | if ((millis() - startttimelow) > (highduration * 6) && stop == LOW) { 173 | CodeToChar(); 174 | CodeBuffer[0] = '\0'; 175 | stop = HIGH; 176 | } 177 | // the end of main loop clean up// 178 | realstatebefore = realstate; 179 | lasthighduration = highduration; 180 | filteredstatebefore = filteredstate; 181 | display.drawString(0, 0, "WPM = "+String(wpm)); 182 | display.drawString(64, 0,"BW = "+String(bw,0)+"Hz"); 183 | display.setFont(ArialMT_Plain_16); 184 | display.drawString(0, 26, DisplayLine); 185 | display.display(); 186 | display.setFont(ArialMT_Plain_10); 187 | display.clear(); 188 | } 189 | 190 | void CodeToChar() { // translate cw code to ascii character// 191 | char decode_char = '{'; 192 | if (strcmp(CodeBuffer,".-") == 0) decode_char = char('a'); 193 | if (strcmp(CodeBuffer,"-...") == 0) decode_char = char('b'); 194 | if (strcmp(CodeBuffer,"-.-.") == 0) decode_char = char('c'); 195 | if (strcmp(CodeBuffer,"-..") == 0) decode_char = char('d'); 196 | if (strcmp(CodeBuffer,".") == 0) decode_char = char('e'); 197 | if (strcmp(CodeBuffer,"..-.") == 0) decode_char = char('f'); 198 | if (strcmp(CodeBuffer,"--.") == 0) decode_char = char('g'); 199 | if (strcmp(CodeBuffer,"....") == 0) decode_char = char('h'); 200 | if (strcmp(CodeBuffer,"..") == 0) decode_char = char('i'); 201 | if (strcmp(CodeBuffer,".---") == 0) decode_char = char('j'); 202 | if (strcmp(CodeBuffer,"-.-") == 0) decode_char = char('k'); 203 | if (strcmp(CodeBuffer,".-..") == 0) decode_char = char('l'); 204 | if (strcmp(CodeBuffer,"--") == 0) decode_char = char('m'); 205 | if (strcmp(CodeBuffer,"-.") == 0) decode_char = char('n'); 206 | if (strcmp(CodeBuffer,"---") == 0) decode_char = char('o'); 207 | if (strcmp(CodeBuffer,".--.") == 0) decode_char = char('p'); 208 | if (strcmp(CodeBuffer,"--.-") == 0) decode_char = char('q'); 209 | if (strcmp(CodeBuffer,".-.") == 0) decode_char = char('r'); 210 | if (strcmp(CodeBuffer,"...") == 0) decode_char = char('s'); 211 | if (strcmp(CodeBuffer,"-") == 0) decode_char = char('t'); 212 | if (strcmp(CodeBuffer,"..-") == 0) decode_char = char('u'); 213 | if (strcmp(CodeBuffer,"...-") == 0) decode_char = char('v'); 214 | if (strcmp(CodeBuffer,".--") == 0) decode_char = char('w'); 215 | if (strcmp(CodeBuffer,"-..-") == 0) decode_char = char('x'); 216 | if (strcmp(CodeBuffer,"-.--") == 0) decode_char = char('y'); 217 | if (strcmp(CodeBuffer,"--..") == 0) decode_char = char('z'); 218 | 219 | if (strcmp(CodeBuffer,".----") == 0) decode_char = char('1'); 220 | if (strcmp(CodeBuffer,"..---") == 0) decode_char = char('2'); 221 | if (strcmp(CodeBuffer,"...--") == 0) decode_char = char('3'); 222 | if (strcmp(CodeBuffer,"....-") == 0) decode_char = char('4'); 223 | if (strcmp(CodeBuffer,".....") == 0) decode_char = char('5'); 224 | if (strcmp(CodeBuffer,"-....") == 0) decode_char = char('6'); 225 | if (strcmp(CodeBuffer,"--...") == 0) decode_char = char('7'); 226 | if (strcmp(CodeBuffer,"---..") == 0) decode_char = char('8'); 227 | if (strcmp(CodeBuffer,"----.") == 0) decode_char = char('9'); 228 | if (strcmp(CodeBuffer,"-----") == 0) decode_char = char('0'); 229 | 230 | if (strcmp(CodeBuffer,"..--..") == 0) decode_char = char('?'); 231 | if (strcmp(CodeBuffer,".-.-.-") == 0) decode_char = char('.'); 232 | if (strcmp(CodeBuffer,"--..--") == 0) decode_char = char(','); 233 | if (strcmp(CodeBuffer,"-.-.--") == 0) decode_char = char('!'); 234 | if (strcmp(CodeBuffer,".--.-.") == 0) decode_char = char('@'); 235 | if (strcmp(CodeBuffer,"---...") == 0) decode_char = char(':'); 236 | if (strcmp(CodeBuffer,"-....-") == 0) decode_char = char('-'); 237 | if (strcmp(CodeBuffer,"-..-.") == 0) decode_char = char('/'); 238 | 239 | if (strcmp(CodeBuffer,"-.--.") == 0) decode_char = char('('); 240 | if (strcmp(CodeBuffer,"-.--.-") == 0) decode_char = char(')'); 241 | if (strcmp(CodeBuffer,".-...") == 0) decode_char = char('_'); 242 | if (strcmp(CodeBuffer,"...-..-") == 0) decode_char = char('$'); 243 | if (strcmp(CodeBuffer,"...-.-") == 0) decode_char = char('>'); 244 | if (strcmp(CodeBuffer,".-.-.") == 0) decode_char = char('<'); 245 | if (strcmp(CodeBuffer,"...-.") == 0) decode_char = char('~'); 246 | if (strcmp(CodeBuffer,".-.-") == 0) decode_char = char('a'); // a umlaut 247 | if (strcmp(CodeBuffer,"---.") == 0) decode_char = char('o'); // o accent 248 | if (strcmp(CodeBuffer,".--.-") == 0) decode_char = char('a'); // a accent 249 | if (decode_char != '{') { 250 | AddCharacter(decode_char); 251 | Serial.print(decode_char); 252 | } 253 | } 254 | 255 | void AddCharacter(char newchar){ 256 | for (int i = 0; i < num_chars; i++) DisplayLine[i] = DisplayLine[i+1]; 257 | DisplayLine[num_chars] = newchar; 258 | } 259 | 260 | --------------------------------------------------------------------------------