ESP32 Radio - How to improve the sound quality? - arduino

I got this ESP32 Radio source code from this site.
I love it because its very simple radio and works fine on ESP32.
But sometime the sound is very choppy.
I suspect maybe because a lack of proper buffers, but I don't know how to fix it.
The code:
/*
WITHOUT OLED, only ESP32 & VS1053
VS1053 - connections detail
XRST = EN (D3)
MISO = D19
MOSI = D23
SCLK = D18
VCC = 5V / 3.3 V
Gnd = Gnd
*/
#include <VS1053.h> //ESP_VS1053_Library
#include <Preferences.h> //For reading and writing into the ROM memory
Preferences preferences;
unsigned int counter,old_counter,new_counter;
#include "helloMp3.h"
#include <WiFi.h>
#include <HTTPClient.h>
#include <esp_wifi.h>
char ssid[] = "SSID"; // your network SSID (name)
char pass[] = "PASSWORD"; // your network password
char *host[7] = {"184.95.47.178","us4.internet-radio.com","us3.internet-radio.com","us3.internet-radio.com","174.36.206.197","sj64.hnux.com","uk7.internet-radio.com"};
char *path[7] = {"/","/","/live.m3u","/","/","/","/"};
int port[7] = {9328,8266,8371,8297,8000,80,8226};
char *sname[7] = {"Love-Radio-USA","Smooth-Jazz-Florida","Best-Rock-Radio","Country-Radio","Venice-Classic-Radio","Smooth-Jazz-Global","UK-Top-40"};
int change=13;
bool x=true;
int status = WL_IDLE_STATUS;
WiFiClient client;
uint8_t mp3buff[32]; // vs1053 likes 32 bytes at a time
// Wiring of VS1053 board (SPI connected in a standard way)
#define VS1053_CS 32 //32
#define VS1053_DCS 33 //33
#define VS1053_DREQ 35 //15
#define VOLUME 100 // volume level 0-100
VS1053 player(VS1053_CS, VS1053_DCS, VS1053_DREQ);
void setup () {
pinMode(change,INPUT_PULLUP);
// initialize SPI
Serial.begin(115200);
delay(500);
// initialize SPI bus;
SPI.begin();
// initialize VS1053 player
player.begin();
// player.switchToMp3Mode(); // optional, some boards require this
player.setVolume(VOLUME);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
// display.drawString(0,15," Connecting..");
delay(500);
Serial.print(".");
}
Serial.println("WiFi connected");
Serial.print("IP address:");
Serial.println(WiFi.localIP());
preferences.begin("my-app", false);
counter = preferences.getUInt("counter", 0);
old_counter=counter;
Serial.printf("Current counter value1: %u\n", counter);
//if(counter>0) counter=0;
//Serial.printf("Current counter value2: %u\n", counter);
delay(100);
player.playChunk(hello2, sizeof(hello2)); //VS1053 is wake up & running
station_connect(counter);
}
void loop() {
if (client.available() > 0) {
uint8_t bytesread = client.read(mp3buff, 32);
player.playChunk(mp3buff, bytesread);
}
if(digitalRead(change)==0 and x==true){
x=false;
counter = counter+1;
if(counter>6) counter=0;
preferences.putUInt("counter",counter);
new_counter=counter;
Serial.printf("Old_Counter: %u\n",old_counter);
Serial.printf("New_Counter: %u\n",new_counter);
Serial.printf("Set counter to new_value: %u\n", counter);
delay(500);
if(old_counter != new_counter) {
player.softReset();
x=true;
station_connect(new_counter);
preferences.putUInt("counter",new_counter);
}
}
}
void station_connect (int station_no ) {
if (client.connect(host[station_no],port[station_no]) ) {
Serial.println("Connected now");
}
Serial.print(host[station_no]);
Serial.println(path[station_no]);
Serial.println(sname[station_no]);
client.print(String("GET ") + path[station_no] + " HTTP/1.1\r\n" +
"Host: " + host[station_no] + "\r\n" +
"Connection: close\r\n\r\n");
}
How to improve the sound quality? Please help
Thanks for all the helpers! :)

Related

Unable to establish connection to mosquitto broker from Arduino+ESP8266

I am using Arduino Uno and ESP8266 for WIFI connectivity. My Laptop on which mosquitto broker is running and Arduino(through ESP8266) is are both connected to wifi network. But I am unable to establish a connection to mqtt server.
I am using the follwing code.
#define FASTLED_INTERNAL
#include <SoftwareSerial.h>
#include <DHT.h>
#include <WiFiEsp.h>
#include <WiFiEspClient.h>
#include <WiFiEspUdp.h>
#include <PubSubClient.h>
#include <FastLED.h>
#define DHTPIN 2
#define DHTTYPE DHT11
#define ESP_RX 9
#define ESP_TX 8
#define WIFI_AP "Anurag"
#define WIFI_PASSWORD "qwerty12345"
#define NUM_LED 64
#define DATA_PIN 5
#define CLOCK_PIN 13
static const int RXPin = 4, TXPin = 3;
static const uint32_t baudRate = 9600;
static const int rainSensorMin = 0;
static const int rainSensorMax = 1024;
WiFiEspClient espClient;
PubSubClient mqttClient(espClient);
IPAddress broker(192,168,43,235);
SoftwareSerial ESP8266(ESP_RX, ESP_TX);
DHT dht(DHTPIN, DHTTYPE);
int status = WL_IDLE_STATUS;
char *ID = "uno";
char *TOPIC = "weatherStats";
int AT_cmd_time;
boolean AT_cmd_result = false;
CRGB leds[NUM_LED];
void setup() {
//pinMode(13, OUTPUT);
//FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LED);
Serial.begin(9600);
//ss.begin(baudRate);
initWiFi();
dht.begin();
mqttClient.setServer(broker,1883);
Serial.println("GPS, DHT11, and, Raindrop detector started.");
}
void loop() {
status = WiFi.status();
if ( status != WL_CONNECTED) {
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(WIFI_AP);
// Connect to WPA/WPA2 network
status = WiFi.begin(WIFI_AP, WIFI_PASSWORD);
delay(500);
}
Serial.println("Connected to AP");
}
delay(3000);
float h = dht.readHumidity();
float t = dht.readTemperature();
int rainReading = analogRead(A0);
int range = map(rainReading, rainSensorMin, rainSensorMax, 0, 2);
if(!mqttClient.connected()){
reconnect_mqtt();
}
mqttClient.loop();
mqttClient.publish(TOPIC,"one step forward");
//Serial.println("Satellites: " + String(gps.satellites.value()));
if (!isnan(h) || !isnan(t))
Serial.print("Temparture: " + String(t) + "°C | " + " Humidity: " + String(h) + " | ");
else {
Serial.println("Failed to obtain temparature and humidity data");
}
switch (range) {
case 0:
Serial.print("Rain Warning : 1");
break;
case 1:
Serial.print("Rain Warning : 0");
break;
}
Serial.println(" | Lat: 22.716191217468406 Lon: 72.44229199663788");
}
void initWiFi(){
ESP8266.begin(9600);
WiFi.init(&ESP8266);
// check for the presence of the shield
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
// don't continue
while (true) {
}
}
// attempt to connect to WiFi network
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(WIFI_AP);
//Connect to WPA/WPA2 network
status = WiFi.begin(WIFI_AP, WIFI_PASSWORD);
}
}
void reconnect_mqtt(){
while(!mqttClient.connected()){
Serial.println("Attempting MQTT Connection");
if(mqttClient.connect(ID)){
Serial.println("MQTT connection established.");
Serial.print("Publishing to: ");
Serial.println(TOPIC);
}
else{
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}
Runnig this code I keep getting output "try again in 5 seconds". Please help.

Can't get ws2812b SMD LED to light up with NodeMCU using Blynk and FastLED

Wiring Diagram:
Sorry for the lame diagram, I am new to this.
I have a 5V input with 2.2A (checked using multimeter), with my data pin wired inline with a 220Ω resistor.
I'm able to successfully connect to my WiFi network and Blynk's cloud server, but am unable to get the LED to turn on or change color. The LED turned on for a little while when I was looking at code, which I have no idea why, but haven't been able to get it to turn on since.
Currently I am only driving 1 ws2812b LED.
Main.cpp:
#define BLYNK_PRINT Serial
#include <BlynkSimpleEsp8266.h>
#define FASTLED_ESP8266_RAW_PIN_ORDER
#include "FastLED.h"
#define NUM_LEDS1 60
#define LED_TYPE WS2812
#define COLOR_ORDER GRB
CRGB leds1[NUM_LEDS1];
char auth[] = "xxxxx";
char ssid[] = "xxx";
char pass[] = "xxxx";
#define PIN1 D2
int data=255;
int r,g,b;
void setup() {
Serial.begin(9600);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
Blynk.begin(auth, ssid, pass);
Blynk.connect(3333);
while (Blynk.connect() == false) {
// Wait until connected
}
Serial.println("Connected to Blynk server");
FastLED.addLeds<LED_TYPE, PIN1, COLOR_ORDER>(leds1, NUM_LEDS1).setCorrection( TypicalLEDStrip );
}
void static1(int r, int g, int b, int brightness) {
FastLED.setBrightness(brightness);
for (int i = 0; i < NUM_LEDS1; i++) {
leds1[i] = CRGB(r, g, b);
}
FastLED.show();
}
BLYNK_WRITE(V3) {
r = param[0].asInt();
g = param[2].asInt();
b = param[2].asInt();
static1(r, g, b,data);
}
void loop() {
Blynk.run();
}
BLYNK_WRITE(V2) {
data = param.asInt();
static1(r, g, b, data);
}
Solved... The 220 Ohm Resistor was unnecessary because there's a resistor on-board.

ESP12F, cant get a relay to work with data from dht11

i've been trying to build a system that detects in one room of the house the temperature using an esp8266 and a dht11 sensor, this esp sends the data to a webserver while the ESP12F grabs this data and should turn the relay on when a certain value is reached, however i can't get the relay to work, as soon as i upload the code, the led on the module turns but not at its full power, i tried with another code to see if it was just a faulty relay, but it works perfectly with the same connections. The code from the client side of the system is below:
#include "ESP8266WiFi.h"
#include "ESP8266HTTPClient.h"
#include "WiFiClient.h"
#include "ESP8266WiFiMulti.h"
ESP8266WiFiMulti WiFiMulti;
const char* ssid = "*******";
const char* password = "********";
//Your IP address or domain name with URL path
const char* serverNameTemp = "http://192.168.1.188/temperature";
const char* serverNameHumi = "http://192.168.1.188/humidity";
const char* serverNamePres = "http://192.168.1.188/pressure";
#include "Wire.h"
#include "Adafruit_GFX.h"
#include "Adafruit_SSD1306.h"
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
String temperature;
String humidity;
String pressure;
const int relay = 5;
float temp_int = temperature.toFloat();
unsigned long previousMillis = 0;
const long interval = 2000;
void setup() {
Serial.begin(115200);
Serial.println();
pinMode(relay,OUTPUT);
digitalWrite(relay,LOW);
// Address 0x3C for 128x64, you might need to change this value (use an I2C scanner)
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
display.clearDisplay();
display.setTextColor(WHITE);
int int_temp = temperature.toFloat();
WiFi.mode(WIFI_STA);
WiFiMulti.addAP(ssid, password);
while((WiFiMulti.run() == WL_CONNECTED)) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("Connected to WiFi");
}
void loop() {
unsigned long currentMillis = millis();
if(currentMillis - previousMillis >= interval) {
// Check WiFi connection status
if ((WiFiMulti.run() == WL_CONNECTED)) {
int int_temp = temperature.toFloat();
temperature = httpGETRequest(serverNameTemp);
humidity = httpGETRequest(serverNameHumi);
pressure = httpGETRequest(serverNamePres);
if(int_temp < 26){
digitalWrite(relay,LOW);
}
else if (int_temp > 26){
digitalWrite(relay,HIGH);
}
Serial.println("Temperature: " + temperature + " *C - Humidity: " + humidity + " % - Pressure: " + pressure + " hPa");
Serial.println(int_temp);
Serial.println(temperature.toFloat());
display.clearDisplay();
// display temperature
display.setTextSize(2);
display.setCursor(0,0);
display.print("T: ");
display.print(temperature);
display.print(" ");
display.setTextSize(1);
display.cp437(true);
display.write(248);
display.setTextSize(2);
display.print("C");
// display humidity
display.setTextSize(2);
display.setCursor(0, 25);
display.print("H: ");
display.print(humidity);
display.print(" %");
// display pressure
display.setTextSize(2);
display.setCursor(0, 50);
display.print("P:");
display.print(pressure);
display.setTextSize(1);
display.setCursor(110, 56);
display.print("hPa");
display.display();
// save the last HTTP GET Request
previousMillis = currentMillis;
}
else {
Serial.println("WiFi Disconnected");
}
}
}
String httpGETRequest(const char* serverName) {
WiFiClient client;
HTTPClient http;
// Your IP address with path or Domain name with URL path
http.begin(client, serverName);
// Send HTTP POST request
int httpResponseCode = http.GET();
String payload = "--";
if (httpResponseCode>0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
payload = http.getString();
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
// Free resources
http.end();
return payload;
}

Code error in smart irrigation system, using dht11 sensor

I have wrtitten a code for automatically watering plant using esp8266, dht11, moisture sensor but my code has some error, i dont know how to fix it
#include <DHT.h>
#include <ESP8266WiFi.h>
String apiKey = "X5AQ3EGIKMBYW31H"; // Enter your Write API key here
const char* server = "api.thingspeak.com";
const char *ssid = "CircuitLoop"; // Enter your WiFi Name
const char *pass = "circuitdigest101"; // Enter your WiFi Password
#define DHTPIN D3 // GPIO Pin where the dht11 is connected
DHT dht(DHTPIN, DHT11);
WiFiClient client;
const int moisturePin = A0; // moisteure sensor pin
const int motorPin = D0;
unsigned long interval = 10000;
unsigned long previousMillis = 0;
unsigned long interval1 = 1000;
unsigned long previousMillis1 = 0;
float moisturePercentage; //moisture reading
float h; // humidity reading
float t; //temperature reading
void setup()
{
Serial.begin(115200);
delay(10);
pinMode(motorPin, OUTPUT);
digitalWrite(motorPin, LOW); // keep motor off initally
dht.begin();
Serial.println("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print("."); // print ... till not connected
}
Serial.println("");
Serial.println("WiFi connected");
}
void loop()
{
unsigned long currentMillis = millis(); // grab current time
h = dht.readHumidity(); // read humiduty
t = dht.readTemperature(); // read temperature
if (isnan(h) || isnan(t))
{
Serial.println("Failed to read from DHT sensor!");
return;
}
moisturePercentage = ( 100.00 - ( (analogRead(moisturePin) / 1023.00) * 100.00 ) );
if ((unsigned long)(currentMillis - previousMillis1) >= interval1) {
Serial.print("Soil Moisture is = ");
Serial.print(moisturePercentage);
Serial.println("%");
previousMillis1 = millis();
}
if (moisturePercentage < 50) {
digitalWrite(motorPin, HIGH); // tun on motor
}
if (moisturePercentage > 50 && moisturePercentage < 55) {
digitalWrite(motorPin, HIGH); //turn on motor pump
}
if (moisturePercentage > 56) {
digitalWrite(motorPin, LOW); // turn off mottor
}
if ((unsigned long)(currentMillis - previousMillis) >= interval) {
sendThingspeak(); //send data to thing speak
previousMillis = millis();
client.stop();
}
}
void sendThingspeak() {
if (client.connect(server, 80))
{
String postStr = apiKey; // add api key in the postStr string
postStr += "&field1=";
postStr += String(moisturePercentage); // add mositure readin
postStr += "&field2=";
postStr += String(t); // add tempr readin
postStr += "&field3=";
postStr += String(h); // add humidity readin
postStr += "\r\n\r\n";
client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(postStr.length()); //send lenght of the string
client.print("\n\n");
client.print(postStr); // send complete string
Serial.print("Moisture Percentage: ");
Serial.print(moisturePercentage);
Serial.print("%. Temperature: ");
Serial.print(t);
Serial.print(" C, Humidity: ");
Serial.print(h);
Serial.println("%. Sent to Thingspeak.");
}
}
This is the error which i get
Arduino: 1.8.9 (Windows 8.1), Board: "Generic ESP8266 Module, 80 MHz, Flash, Disabled, All SSL ciphers (most compatible), ck, 26 MHz, 40MHz, DOUT (compatible), 512K (no SPIFFS), 2, nonos-sdk 2.2.1 (legacy), v2 Lower Memory, Disabled, None, Only Sketch, 115200"
sketch_oct03a:7:16: error: 'D3' was not declared in this scope
#define DHTPIN D3 // GPIO Pin where the dht11 is connected
^
C:\Users\Shweta\Desktop\Libraries\sketch_oct03a\sketch_oct03a.ino:8:9: note: in expansion of macro 'DHTPIN'
DHT dht(DHTPIN, DHT11);
^
sketch_oct03a:12:22: error: 'D0' was not declared in this scope
const int motorPin = D0;
^
exit status 1
'D3' was not declared in this scope
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
Digital pins are not prefixed with D, unlike their analog counterparts.
#define DHTPIN 3
const int motorPin = 0;

Can't commute a relay with Arduino wifi shield + Xively

I’m trying to turn on and off an LED light bulb connected to a Tinterkit Relay.
I’m using an Arduino UNO r3 connected to internet thanks to an official Arduino Wifi shield.
I’ve done a simple website with two buttons to send a 1 (on) or a 0 (off) to my Xively account.
I’ve written a code to detect the last posted channel value. The code is working fine and I’m able to detect a 1 or a 0 every 3 seconds approximately. The problem is that the relay doesn’t commute.
Please please, I would appreciate a lot your help to solve this problem.
Here is the code:
#include <SPI.h>
#include <WiFi.h>
#include <HttpClient.h>
#include <Xively.h>
#include <TinkerKit.h>
char ssid[] = "XXXXXX";
char pass[] = "XXXXXX";
int state;
int status = WL_IDLE_STATUS;
char myIntStream[]="LED";
char xivelyKey[] = "XXXXXX";
#define FEED_ID XXXXXX
TKMosFet relay(O0);
XivelyDatastream datastreams[] = {
XivelyDatastream(myIntStream, strlen(myIntStream), DATASTREAM_INT),
};
XivelyFeed feed(FEED_ID,datastreams,1);
WiFiClient client;
XivelyClient xivelyclient(client);
void printWifiStatus() {
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}
void setup() {
Serial.begin(9600);
Serial.println("LED light bulb");
Serial.println();
while ( status != WL_CONNECTED) {
Serial.println("Connecting to internet ...");
status = WiFi.begin(ssid, pass);
Serial.println("");
delay(5000);
}
Serial.println("Connected to wifi");
printWifiStatus();
}
void loop() {
Serial.println();
Serial.print("Datastream is:");
Serial.println(datastreams[0]);
Serial.print("LED value = ");
state = datastreams[0].getInt();
Serial.print(state);
if(state == 0){
relay.off();
Serial.println(" => Light is on.");
}
else if(state == 1){
relay.on();
Serial.println(" => Light is off.");
}
xivelyclient.get(feed, xivelyKey);
}

Resources