// BLOG Eletrogate // ESP32 Frequency Meter // ESP32 DevKit 38 pins + LCD // https://blog.eletrogate.com/esp32-frequencimetro-de-precisao // Rui Viana and Gustavo Murta august/2020 #include "stdio.h" // Library STDIO #include "driver/ledc.h" // Library ESP32 LEDC #include "driver/pcnt.h" // Library ESP32 PCNT #define LCD_OFF // LCD_ON, if want use LCD 4 bits interface #define LCD_I2C_OFF // LCD_I2C_ON, if want use I2C LCD (PCF8574) #define JFS #define TEST #define WIFI #ifdef LCD_I2C_ON // If I2C LCD enabled #define I2C_SDA 21 // LCD I2C SDA - GPIO_21 #define I2C_SCL 22 // LCD I2C SCL - GPIO_22 #include // I2C Libray #include // Library LCD with PCF8574 LiquidCrystal_PCF8574 lcd(0x3F); // Instance LCD I2C - adress x3F #endif // LCD I2C #ifdef LCD_ON // LCD with 4 bits interface enabled #include // Library LCD LiquidCrystal lcd(4, 16, 17, 5, 18, 19); // Instance - ports RS,ENA,D4,D5,D6,D7 #endif // LCD #ifdef JFS #include "SSD1306.h" // alias for `#include "SSD1306Wire.h" // Board Handling // jfs Wemos lolin32 // jfs Heltec WiFi kit 32 (weisses Board) // #define HELTEC // Initialize the OLED display using Wire library #ifdef HELTEC SSD1306 display(0x3c, 4, 15); #else SSD1306 display(0x3c, 5, 4); #endif #endif #ifdef WIFI #include const int mxSize=4; String ssids[mxSize] ={"GAST","pipanet","FRITZ!Box Gastzugang","WLAN-DE8245"}; String ssidp[mxSize] = {"passatvr6","passatvr6","praxis123","4955065570896956"}; boolean conok =false; void netfound(int i){ display.clear(); display.setColor(BLACK); display.fillRect(0, 0, 128, 10); display.setColor(WHITE); display.drawString(0,0,String(i)); display.drawString(20,0,"networks found"); display.display(); } boolean init_wifi(){ boolean ok = false; WiFi.mode(WIFI_STA); WiFi.disconnect(); delay(100); int n = WiFi.scanNetworks(); Serial.println("scan done"); if (n == 0) { Serial.println("no networks found"); netfound(0); } else { Serial.print(n); Serial.println(" networks found"); netfound(n); for (int i = 0; i < n; ++i) { for (int p=0;p= radix) s = ultos_recursive(val / radix, s, radix, pos + 1); c = val % radix; c += (c < 10 ? '0' : 'a' - 10); *s++ = c; if (pos % 3 == 0) *s++ = ','; return s; } //---------------------------------------------------------------------------------------- char *ltos(long val, char *s, int radix) // Format an long (32 bits) into a string { if (radix < 2 || radix > 36) { s[0] = 0; } else { char *p = s; if (radix == 10 && val < 0) { val = -val; *p++ = '-'; } p = ultos_recursive(val, p, radix, 0) - 1; *p = 0; } return s; } #ifdef JFS uint32_t start = 100000; void test(){ if (start < 100){ start = 100000; } else { osc_freq = start; init_osc_freq (); start -= 100; } } #endif //--------------------------------------------------------------------------------- void loop() { if (flag == true) // If count has ended { flag = false; // change flag to disable print frequency = (pulses + (multPulses * overflow)) / 2 ; // Calculation of frequency #ifdef JFS factor = 1000000/sample_time; frequenz = frequency*factor; // correct frequency if sample_time is smaller then 1sec Serial.println(frequency); #else printf("Frequency : %s", (ltos(frequency, buf, 10))); // Print frequency with commas printf(" Hz \n"); // Print unity Hz #endif #if defined LCD_ON || defined LCD_I2C_ON // If using LCD or I2C LCD lcd.setCursor(2, 1); // Set cursor position - column and row lcd.print((ltos(frequency, buf, 10))); // LCD print frequency lcd.print(" Hz "); // LCD print unity Hz #endif #ifdef JFS display.clear(); #ifdef WIFI display.drawString(0,0,"IP : "+WiFi.localIP().toString()); #endif display.drawString(0, 10,"sample time ms "); display.drawString(80,10,String(sample_time/1000)); display.drawString(0, 20,"Pulses SVP"); display.drawString(60, 20, (ltos(frequency, buf, 10))); display.drawString(0, 30,"Freq"); display.drawString(60, 30, (ltos(frequenz, buf, 10))); display.drawString(0, 40,"Oscila 16"); display.drawString(60, 40, (ltos(save_osc, buf, 10))); display.display(); #endif multPulses = 0; // Clear overflow counter // Put your function here, if you want #ifdef JFS #ifdef TEST test(); #endif delay (5); // Delay 100 ms 100 #else delay(100); #endif // Put your function here, if you want pcnt_counter_clear(PCNT_COUNT_UNIT); // Clear Pulse Counter esp_timer_start_once(timer_handle, sample_time); // Initialize High resolution timer (1 sec) gpio_set_level(OUTPUT_CONTROL_GPIO, 1); // Set enable PCNT count } String inputString = ""; // clear temporary string osc_freq = 0; // Clear oscillator frequency while (Serial.available()) { char inChar = (char)Serial.read(); // Reads a byte on the console inputString += inChar; // Add char to string if (inChar == '\n') // If new line (enter) { Serial.println(inputString); #ifdef JFS if (inputString.startsWith("s")){ inputString.remove(0,1); Serial.println(inputString); sample_time = inputString.toInt(); Serial.println(sample_time); } if (inputString.startsWith("o")){ inputString.remove(0,1); Serial.println(inputString); osc_freq = inputString.toInt(); Serial.println(sample_time); } #else osc_freq = inputString.toInt(); #endif // Converts String into integer value inputString = ""; // Clear string } } if (osc_freq != 0) // If some value inputted to oscillator frequency { init_osc_freq (); // reconfigure ledc function - oscillator } }