Getting an Analog value for Audio signal using ESP8266 and an Electret Microphone

52 views Asked by At

Code :

#include <ESP8266WiFi.h>
#include <WiFiClient.h>

#define SAMPLE_RATE_HZ 16000
#define PACKET_INTERVAL_MS 50
#define SAMPLE_DELAY_US (1000000 / SAMPLE_RATE_HZ) // Delay in microseconds between samples

const char* ssid = "TP-Link_39E0";
const char* password = "8983761246";
const char* host = "192.168.1.118";
const int port = 1234;

WiFiClient client;
uint32_t lastPacketTime = 0; // Declare lastPacketTime at the global scope

void setup() {
  Serial.begin(115200);
  pinMode(A0, INPUT);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1);
    Serial.println("Connecting to WiFi...");
  }
  Serial.println("Connected to WiFi");
}

void loop() {
  if (!client.connected()) {
    if (!connectToServer()) {
      return;
    }
  }

  int16_t sample;
  uint32_t lastSampleTime = micros(); // Track the time of the last sample

  for (;;) {
    // Check if it's time to send a sample
    if (micros() - lastSampleTime >= SAMPLE_DELAY_US) {
      sample = analogRead(A0);
      client.write((uint8_t*)&sample, sizeof(int16_t));
      lastSampleTime += SAMPLE_DELAY_US; // Update last sample time
    }

    // Check if it's time to send a packet
    if (millis() - lastPacketTime >= PACKET_INTERVAL_MS) {
      lastPacketTime = millis(); // Update last packet time
      break; // Exit the loop to send the packet
    }
  }
}

bool connectToServer() {
  if (!client.connect(host, port)) {
    Serial.println("Connection failed");
    return false;
  }
  Serial.println("Connected to server");
  return true;
}

Error in Arduino IDE:
. Variables and constants in RAM (global, static), used 28316 / 80192 bytes (35%)
║   SEGMENT  BYTES    DESCRIPTION
╠══ DATA     1508     initialized variables
╠══ RODATA   1040     constants       
╚══ BSS      25768    zeroed variables
. Instruction RAM (IRAM_ATTR, ICACHE_RAM_ATTR), used 59823 / 65536 bytes (91%)
║   SEGMENT  BYTES    DESCRIPTION
╠══ ICACHE   32768    reserved space for flash instruction cache
╚══ IRAM     27055    code in IRAM    
. Code in flash (default, ICACHE_FLASH_ATTR), used 245812 / 1048576 bytes (23%)
║   SEGMENT  BYTES    DESCRIPTION
╚══ IROM     245812   code in flash

The '+ve' terminal of the Microphone is connected to the 'AO' pin & '3v3' pin of the ESP8266 (Analog Output and 3.3v output) AND the '-ve' terminal of the Microphone is connected to the 'GND' pin of the ESP8266

Have already tried:

  1. upgrading the drivers,
  2. checking the data cable being used,
  3. Optimizing the code for variables(to the best that i can do).
0

There are 0 answers