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
Have already tried:
- upgrading the drivers,
- checking the data cable being used,
- Optimizing the code for variables(to the best that i can do).