2_Connecting to WiFi
Создал функцию connectingToWiFi()
// Создал функцию connectingToWiFi()
#include "Arduino.h"
#include "ESP8266WiFi.h"
#include "AudioOutputI2SNoDAC.h"
#include "AudioFileSourceHTTPStream.h"
#include "AudioFileSourceBuffer.h"
#include "AudioGeneratorMP3.h"
#define SSID "AndroidAP" // имя точки
#define PASSWORD "kzdg7437" // пароль
#define url "http://dorognoe.hostingradio.ru:8000/dorognoe1945.mp3"
String url_tmp;
AudioGeneratorMP3 *mp3;
AudioFileSourceHTTPStream *file;
AudioFileSourceBuffer *buff;
AudioOutputI2SNoDAC *out;
void play() {
file = new AudioFileSourceHTTPStream(url_tmp.c_str());
buff = new AudioFileSourceBuffer(file, 2048);
mp3 = new AudioGeneratorMP3();
out = new AudioOutputI2SNoDAC();
mp3->begin(buff, out);
Serial.println("play");
Serial.printf("Play audio Heap: %d b\n", ESP.getFreeHeap());
}
void StopPlaying() {
if (mp3) {
mp3->stop();
delete mp3;
mp3 = NULL;
}
if (buff) {
buff->close();
delete buff;
buff = NULL;
}
if (file) {
file->close();
delete file;
file = NULL;
}
Serial.println("stop");
Serial.printf(" Heap: %d b\n", ESP.getFreeHeap());
}
void connectingToWiFi() {
Serial.println("Connecting to WiFi");
WiFi.disconnect();
WiFi.softAPdisconnect(true);
WiFi.mode(WIFI_STA);
WiFi.begin(SSID, PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(1000);
}
Serial.println();
Serial.print("Connected ");
Serial.println(SSID);
}
void setup() {
Serial.begin(115200);
delay(1000);
Serial.println();
connectingToWiFi();
Serial.printf("Start Heap: %d b\n", ESP.getFreeHeap());
Serial.printf("Init audio out Heap: %d b\n", ESP.getFreeHeap());
url_tmp = url;
play();
}
void loop() {
if (mp3->isRunning()) {
if (!mp3->loop()) mp3->stop();
} else {
StopPlaying();
play();
}
}