I'm trying to stream voice to the MQTT broker. I'm using esp8266(NodeMCU1.0) and it's ADC to sampling the audio signal at 4KHz and 8-bit PCM audio format. I used Pubsubclient.h library to publish audio packets to the broker but on the other side when I receive packets and play them I have the interrupted voice and I have a delay between playing packets as long as my buffer size. please help me to have continues voice.
I have 2 main problems:
1) delay between playing packets as long as my buffer size
example of what i received: ;D ;D ;D .....|||||..........|||||||...........|||||||||............||||||||
2) Quality of voice: I receive a noisy voice.
Please help me! Thanks.
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
const char* ssid = "Knight";
const char* password = "****";
const char* mqtt_server = "104.21.218.224";
WiFiClient espClient;
PubSubClient client(espClient);
unsigned long lastMsg = 0;
#define MSG_BUFFER_SIZE (4096)
int i;
byte voice[MSG_BUFFER_SIZE];
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
randomSeed(micros());
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Create a random client ID
String clientId = "ashkan";
// Attempt to connect
if (client.connect(clientId.c_str())) {
Serial.println("connected");
// Once connected, publish an announcement...
if (!client.publish("doorphone/connected", "true")) {
Serial.println("publish failed, either connection lost, or message too large");
}
else {
Serial.println("publish succeeded");
}
// ... and resubscribe
// client.subscribe("doorphone/open");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup() {
pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
for (i = 0; i < MSG_BUFFER_SIZE; i++) {
voice[i] = analogRead(A0);
// client.write(voice[i]);
delayMicroseconds(125);
}
client.publish("doorphone/connected", voice, MSG_BUFFER_SIZE);
}
Related
Here what I try to do I have quickapp in HC3 that have some buttons I want to call from ESP8266 with it's URL All buttons API is working fine in browser
the project consist of {ESP8266, IR Receiver} that is it
I want to make conditions for the buttons that IR receive I will get it's code from serial monitor
and every button it's condition come true is calling Certain button in fibaro quickapp through API
How can I make API call under every (condition) OR (case) from arduino
I am trying to call button in quick app, if IR receive any code from AC remote it will call the API of this button
the arduino code is this
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <Arduino.h>
#include <IRremoteESP8266.h>
#include <IRrecv.h>
#include <IRutils.h>
#define ssid "Name"
#define password "Passsword"
const char * servername = "http://User:Password#IPAdress/api/plugins/callUIEvent?deviceID=1639&elementName=Cool22&eventType=onReleased";
int MyData;
IRrecv irrecv(14);
decode_results results;
void setup()
{
MyData = 0;
WiFi.mode(WIFI_STA);
WiFi.begin(ssid,password);
Serial.begin(9600);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("connected to ");
Serial.println(ssid);
Serial.print("IP Adress: ");
Serial.println(WiFi.localIP());
irrecv.enableIRIn(); // Start the receiver
}
/*while (!Serial){ // Wait for the serial connection to be establised.
delay(50);
Serial.println();
Serial.print("IRrecv is now running and waiting for IR message on Pin ");
Serial.println(14);
}*/
void loop()
{
HTTPClient http;
WiFiClient client;
if (irrecv.decode(&results)) {
Serial.println("You Receive This:");
MyData = (results.value);
Serial.println(MyData);
if (MyData == 11714416 ){
http.begin(client, servername);
http.addHeader("Content-Type", "application/json");
int httpcode = http.POST(servername);
String payload =(http.getString());
Serial.println(payload);
Serial.print("HTTP Response code: ");
Serial.println(httpcode);
// Disconnect
http.end();
}
else {
Serial.println("No HTTP");
}
irrecv.resume();
}
}
the output tell me
You Receive This:
11714416
HTTP Response code: -1
and sometimes it give me Exception(28)
I need to ring the buzzer within the range of 1 meter only, my esp8266 is connected to a buzzer but the buzzer will ring on and off within the range of 1 meter using two esp8266, or sometimes it does not ring at all. The project is social distancing, what else should I do? Here is the code I am using:
#include <ESP8266WiFi.h>
const char* APssid = "Social Distancing"; //acess point of the device
const char* APpassword = "qwertyuiop";
const int RSSI_MAX = -37;//maximum strength of signal in dBm
const int RSSI_MIN =-100;//minimum strength of signal in dBm
void setup()
{
WiFi.mode(WIFI_OFF);
WiFi.disconnect();
delay(50); //this part turns off the wifi and resets it if it was already on
Serial.begin(115200);
pinMode(14,OUTPUT);
Serial.println();
WiFi.mode(WIFI_AP_STA); //configuring the board in hybrid mode
Serial.print("Configuring access point...");
WiFi.softAP(APssid, APpassword);
Serial.println(WiFi.softAPIP());
}
void loop()
{
Serial.println("Wifi is scanning");
int n = WiFi.scanNetworks();
Serial.println("Wifi scan ended");
if (n == 0)
{
Serial.println("no networks found");
}
else
{
Serial.print(n);
Serial.println(" networks found");
for (int i = 0; i < n; ++i)
{
//Print SSID and RSSI for each network found
Serial.print(i + 1);
Serial.print(") ");
Serial.print(WiFi.SSID(i));//SSID
Serial.print(WiFi.RSSI(i));//Signal strength in dBm
Serial.print("dBm (");
if(WiFi.SSID(i) == "Social Distancing")
{
if(WiFi.RSSI(i) > -37)//THIS -37 (RSSI) is the threshold value, this value is set
according to the distance of 1m
{
digitalWrite(14,HIGH);//(Generic esp8266 : (14,HIGH) , NodeMCU : (D5,HIGH) )
Serial.println("Social Distancing");
break;
}
}
else
{
digitalWrite(14,LOW);
}
}
delay(50);
}
Serial.println("");
delay(50);
WiFi.scanDelete();
}
Hi Lisa I have modified your code and I have added a 64x48 OLED display connected to the I2C connections, to visualize the results and it works beautifully. Moreover I have used the built in led (see: LED_BUILTIN) instead of the buzzer.
You have had a great idea. Now it works great and you can interrogate any Access point.
Please find attached the code:
//OLED
#include <Arduino.h>
#include <U8g2lib.h>
#include <U8x8lib.h> //this one in use
#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif
U8X8_SSD1306_64X48_ER_HW_I2C u8x8(/* reset=*/ U8X8_PIN_NONE); // EastRising 0.66" OLED breakout board, Uno: A4=SDA, A5=SCL, 5V powered
// End of constructor list
#define U8LOG_WIDTH 16
#define U8LOG_HEIGHT 8
uint8_t u8log_buffer[U8LOG_WIDTH*U8LOG_HEIGHT];
U8X8LOG u8x8log;
//OLED end
#include <ESP8266WiFi.h>
const char* APssid = "My_SSID"; //acess point of the device
const char* APpassword = "My_Password";
//const char* myconstcharstarstring = "------------------------------";
const int RSSI_MAX = -37;//maximum strength of signal in dBm
const int RSSI_MIN =-100;//minimum strength of signal in dBm
void setup()
{
//OLED
u8x8.begin();
u8x8.setFont(u8x8_font_chroma48medium8_r);
u8x8log.begin(u8x8, U8LOG_WIDTH, U8LOG_HEIGHT, u8log_buffer);
u8x8log.setRedrawMode(1); // 0: Update screen with newline, 1: Update screen for every char
//OLED end
WiFi.mode(WIFI_OFF);
WiFi.disconnect();
delay(50); //this part turns off the wifi and resets it if it was already on
Serial.begin(115200);
pinMode(LED_BUILTIN,OUTPUT);
Serial.println();
WiFi.mode(WIFI_AP_STA); //configuring the board in hybrid mode
Serial.print("Configuring access point...");
WiFi.softAP(APssid, APpassword);
Serial.println(WiFi.softAPIP());
}
void loop()
{
digitalWrite(LED_BUILTIN,LOW);
Serial.println("Wifi is scanning");
int n = WiFi.scanNetworks();
Serial.println("Wifi scan ended");
if (n == 0)
{
Serial.println("no networks found");
}
else
{
Serial.print(n);
Serial.println(" networks found");
for (int i = 0; i < n; ++i)
{
//Print SSID and RSSI for each network found
Serial.print(i + 1);
Serial.print(") ");
Serial.print(WiFi.SSID(i));//SSID
Serial.print(WiFi.RSSI(i));//Signal strength in dBm
Serial.print("dBm (");
if(WiFi.RSSI(i) > -37)//THIS -37 (RSSI) is the threshold value, this value is set according to the distance of 1m
{
digitalWrite(LED_BUILTIN,HIGH);//(Generic esp8266 : (14,HIGH) , NodeMCU : (D5,HIGH) )
Serial.println("Social Distancing");
delay(500);
}
//OLED
u8x8log.print(WiFi.SSID(i));
u8x8log.print("\n");
u8x8log.print(WiFi.RSSI(i));
u8x8log.print("\n");
u8x8log.print("-------------");
u8x8log.print("\n");
delay(2000);
}
Serial.println("");
delay(50);
WiFi.scanDelete();
}
}
Hope this answers All the best
Hi Lisa please add digitalWrite(14,HIGH); at the beginning of the loop, it will work:
void loop()
{
digitalWrite(14,HIGH);
/... rest of your code...
I don't know how you have wired your buzzer, try forcing your ouput pin HIGH or LOW depending on your circuit.
Hope this helps All the best
I'm trying to get data from a txt file through my website to turn the LED on or off, the connection to the website works and the data from a txt file is printed on the serial as the code shows, but on the loop condition when I read a txt file with '1' it should light on the LED, but that does not work
can you please help me
this is my code
#include <SPI.h>
#include <WiFi.h>
char ssid[] = "HomeBroadband"; // your network SSID (name)
char pass[] = "h12345678"; // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0; // your network key Index number (needed only for WEP)
int status = WL_IDLE_STATUS;
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(74,125,232,128); // numeric IP for Google (no DNS)
char server[] = "daffostore.com"; // name address for Google (using DNS)
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
WiFiClient client;
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
// don't continue:
while (true);
}
String fv = WiFi.firmwareVersion();
if (fv != "1.1.0") {
Serial.println("Please upgrade the firmware");
}
// attempt to connect to Wifi network:
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
Serial.println("Connected to wifi");
printWifiStatus();
Serial.println("\nStarting connection to server...");
// if you get a connection, report back via serial:
if (client.connect(server, 80) == 1) {
Serial.println("connected to server");
// Make a HTTP request:
client.println("GET /ard/ledstatus.txt HTTP/1.1");
client.println("Host: daffostore.com");
client.println("Connection: keep-alive");
//client.println("Content-Length: 1845");
client.println("Keep-Alive: timeout=10, max=20");
client.println();
}
}
void loop() {
// if there are incoming bytes available
// from the server, read them and print them:
while (client.available()) {
char c = client.read();
Serial.write(c);
if(c=='1'){
digitalWrite(13, HIGH);
}
else{
digitalWrite(13, LOW);
}
}
// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting from server.");
client.stop();
// do nothing forevermore:
while (true);
}
}
void printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}
I am trying to upload data from arduino to a local server using Arduino wifi shied.But the code never reaches the line "connected".I am using WAMP server.The IP address ping of the arduino wifi shield is okay.The wifi shield connects to the network.The code is below:
#include <TinkerKit.h>
#include <WiFi.h>
#include <SPI.h>
char ssid[] = "Connectify-moloi"; // your network SSID (name)
char pass[] = "1234567890"; // your network password
int status = WL_IDLE_STATUS;
//WiFiServer server(80);
long previousMillis = 0;
unsigned long currentMillis = 0;
long interval = 250000; // READING INTERVAL
int sensor;
int analog_val;
String data;
String Hall;
String Temp;
WiFiClient client;
IPAddress server(192,168,164,101);
void setup() {
Serial.begin(9600);
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
// you're connected now, so print out the status:
printWifiStatus();
Hall = "50";
Temp = "50";
data = "";
}
void loop(){
currentMillis = millis();
if(currentMillis - previousMillis > interval) { // READ ONLY ONCE PER INTERVAL
previousMillis = currentMillis;
Hall ="50"; //String(analog_val);
Temp ="50"; //String(sensor);
}
data = "temp1=" + Hall + "&hum1=" + Temp;
client.flush();
if (client.connect(server,80)) { // REPLACE WITH YOUR SERVER ADDRESS
Serial.println("Connected");
client.println("POST project/add.php HTTP/1.1");
client.println("Host: 192.168.164.101"); // SERVER ADDRESS HERE TOO
client.println("Content-Type: application/x-www-form-urlencoded");
client.print("Content-Length: ");
client.println(data.length());
client.println();
client.print(data);
}
if (client.connected()) {
client.stop(); // DISCONNECT FROM THE SERVER
client.flush();
}
delay(7000); // WAIT FIVE MINUTES BEFORE SENDING AGAIN
}
void printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}
Instead of a POST command, try using a GET request.
Change POST project/add.php to GET /project/add.php?
Using Arduino + Sparkfun SM5100B + Cellular Shield.
I need the following code to loop trough a set of mobile numbers with about a minute delay for each. If the Arduino receives a positive/'yes' message I need this to break the loop and end it. If it receives a 'no' or a negative message I need it to continue its loop.
Right now, I have it successfully compiling, but while it delays correctly in the loop and prints the correct messages in the serial monitor, it actually only sends a test message to the first number along with the AT commands to send to the next number (in the text message received by the first number) in this code I have included the receive message code yet, as I would like to get the loop working first before I try to break it.
//THis code is sending both messages to first number
#include <GSM.h>
#define PINNUMBER ""
const int buttonPin = 4;
const int ledPin1 = 13;
const int ledPin2 = 12;
int buttonState = 0;
// initialize the library instance
GSM gsmAccess; // include a 'true' parameter for debug enabled
GSM_SMS sms;
// char array of the telephone number to send SMS
const char* number1 = "xxxxxxxxxxxxx"; // enter any two mobile numbers here in international format
const char* number2 = "xxxxxxxxxxxxx";
// char array of the message
char txtMsg[200]="test";
// connection state
boolean notConnected = true;
void setup()
{
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(buttonPin, INPUT);
// initialize serial communications
Serial.begin(9600);
Serial.println("SMS Messages Sender");
// Start GSM shield
// If your SIM has PIN, pass it as a parameter of begin() in quotes
while(notConnected)
{
if(gsmAccess.begin(PINNUMBER)==GSM_READY)
notConnected = false;
else
{
Serial.println("Not connected");
delay(1000);
}
}
Serial.println("GSM initialized");
}
void loop()
{
buttonState = digitalRead(buttonPin);
{
sendSMS();
}
}
void sendSMS()
{
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, LOW);
if(buttonState == HIGH) {
buttonState = 1;
};
if (buttonState == 1) {
digitalWrite(ledPin2, HIGH);
delay(1000);
Serial.print("Message to mobile number: ");
Serial.println(number1);
// sms text
Serial.println("SENDING");
Serial.println();
Serial.println("Message:");
Serial.println(txtMsg);
// send the message
sms.beginSMS(number1);
sms.print(txtMsg);
Serial.println("\nFirst Message Sent\n");
digitalWrite(ledPin2, LOW);
delay (60000);
digitalWrite(ledPin2, HIGH);
delay(1000);
Serial.print("Message to mobile number: ");
Serial.println(number2);
// sms text
Serial.println("SENDING");
Serial.println();
Serial.println("Message:");
Serial.println(txtMsg);
// send the message
sms.beginSMS(number2);
sms.print(txtMsg);
sms.endSMS();
Serial.println("\nSecond Message Sent\n");
digitalWrite(ledPin2, LOW);
}
}
Change GSM_sms;
to
GSM_smsone;
GSM_smstwo;
use
smsone.beginSMS(number1);
smsone.print(txtMsg);
smsone.endSMS();
smstwo.beginSMS(number2);
smstwo.print(txtMsg);
smstwo.endSMS();
for sending the sms.
#include <GSM.h>
#define PINNUMBER ""
const int buttonPin = 4;
const int ledPin1 = 13;
const int ledPin2 = 12;
int buttonState = 0;
// initialize the library instance
GSM gsmAccess; // include a 'true' parameter for debug enabled
GSM_SMS smsone;
GSM_SMS smstwo;
// char array of the telephone number to send SMS
const char* number1 = "xxxxxxxxxxxxx"; // enter any two mobile numbers here in international format
const char* number2 = "xxxxxxxxxxxxx";
// char array of the message
char txtMsg[200] = "test";
// connection state
boolean notConnected = true;
void setup()
{
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(buttonPin, INPUT);
// initialize serial communications
Serial.begin(9600);
Serial.println("SMS Messages Sender");
// Start GSM shield
// If your SIM has PIN, pass it as a parameter of begin() in quotes
while (notConnected)
{
if (gsmAccess.begin(PINNUMBER) == GSM_READY)
notConnected = false;
else
{
Serial.println("Not connected");
delay(1000);
}
}
Serial.println("GSM initialized");
}
void loop(){
buttonState = digitalRead(buttonPin);
{
sendSMS();
}
}
void sendSMS()
{
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, LOW);
if (buttonState == HIGH) {
buttonState = 1;
};
if (buttonState == 1) {
digitalWrite(ledPin2, HIGH);
delay(1000);
Serial.print("Message to mobile number: ");
Serial.println(number1);
// sms text
Serial.println("SENDING");
Serial.println();
Serial.println("Message:");
Serial.println(txtMsg);
// send the first message
smsone.beginSMS(number1);
smsone.print(txtMsg);
smsone.endSMS();
Serial.println("\nFirst Message Sent\n");
digitalWrite(ledPin2, LOW);
delay (60000);
digitalWrite(ledPin2, HIGH);
delay(1000);
Serial.print("Message to mobile number: ");
Serial.println(number2);
// sms text
Serial.println("SENDING");
Serial.println();
Serial.println("Message:");
Serial.println(txtMsg);
// send the second message
smstwo.beginSMS(number2);
smstwo.print(txtMsg);
smstwo.endSMS();
Serial.println("\nSecond Message Sent\n");
digitalWrite(ledPin2, LOW);
}
}