my Arduino code is following please check it ,if there is any mistake let me know,
#include "Arduino.h"
#include <HardwareSerial.h>
//*************************************************** Beginning UART Communications
#define SerialMon Serial // For Serial Monitor
HardwareSerial SerialAT(0); // For SIM800 Module
HardwareSerial Serial_MP3(1); // For MP3 Module
//*************************************************** SIM800 Config
#define TINY_GSM_MODEM_SIM800
#define TINY_GSM_USE_HEX
#define TINY_GSM_RX_BUFFER 730
// Necesarry Libraries
#include <TinyGsmClient.h> // https://github.com/vshymanskyy/TinyGSM (0.11.5)
#include <PubSubClient.h> // https://pubsubclient.knolleary.net (2.8.0)
#include "DFRobotDFPlayerMini.h" // https://github.com/DFRobot/DFRobotDFPlayerMini (1.0.5)
DFRobotDFPlayerMini myDFPlayer;
void printDetail(uint8_t type, int value);
//setting all required global variables.
char* lower = "";
char* response = "";
String res = "";
String Prev_payload = "";
String final_amount = "";
uint32_t lastReconnectAttempt = 0;
// LED Indicators
const int Blue_Light = 8;
const int Green_Light = 9;
const int Red_Light = 10;
// Previous Amount Button
const int Prev_Button = 3;
const int delivery = 2;
//*************************************************** GPRS Credentials
const char apn[] = "airtelgprs.com"; // Your APN
const char gprsUser[] = ""; // User
const char gprsPass[] = ""; // Password
const char simPIN[] = ""; // SIM card PIN code, if any
//*************************************************** Adafruit MQTT Credentials
const char* broker = "io.adafruit.com";
const char* amount_topic = "iyansind/feeds/alert";
const char* mqtt_username = "iyansind";
const char* mqtt_password = "aio_SWui84sAjZSqkkEVQrfFTCp3od6N";
TinyGsm modem(SerialAT);
TinyGsmClient clientMQTT(modem, 0);
PubSubClient mqtt(clientMQTT);
// MQTT Callback Fuction
void mqttCallback(char* topic, byte* payload, unsigned int len)
{
SerialMon.print("Message arrived [");
SerialMon.print("ping");
SerialMon.print("]: ");
SerialMon.write(payload, len);
String final_payment;
String topicStr = topic;
payload[len] = 0;
String recv_payload = String((char*)payload);
Serial.println("mqtt_callback - message arrived - topic [" + topicStr + "] payload [" + recv_payload + "]");
// Converting Data recevied in Actual Amount Value ( For Eg. Converting 100 to 1.00 )
Serial.print("final_payment = ");
Serial.print(final_payment);
}
void turnOffNetlight()
{
SerialMon.println("Turning off SIM800 Red LED...");
modem.sendAT("+CNETLIGHT=0");
}
void setup()
{
delay(1000);
Serial_MP3.begin(9600, SERIAL_8N1, 4, 1); // For MP3 Module
Serial.begin(115200); // For Serial Monitor
SerialAT.begin(115200, SERIAL_8N1, 44, 43); // For SIM800 Module
pinMode(delivery, OUTPUT);
pinMode(Blue_Light, OUTPUT);
pinMode(Green_Light, OUTPUT);
pinMode(Red_Light, OUTPUT);
pinMode(Prev_Button, INPUT_PULLUP);
digitalWrite(Blue_Light, LOW);
digitalWrite(Green_Light, LOW);
digitalWrite(Red_Light, LOW);
delay(500);
// Initialising MP3 module
if (!myDFPlayer.begin(Serial_MP3, /*isACK = */ true, /*doReset = */ true)) { //Use serial to communicate with mp3.
Serial.println(F("Unable to begin:"));
Serial.println(F("1.Please recheck the connection!"));
Serial.println(F("2.Please insert the SD card!"));
while (true)
{
delay(0); // Code to compatible with ESP8266 watch dog.
}
}
Serial.println(F("DFPlayer Mini online."));
delay(500);
myDFPlayer.volume(10);
myDFPlayer.play(1);
digitalWrite(Red_Light, HIGH);
delay(50);
digitalWrite(Red_Light, LOW);
delay(200);
digitalWrite(Red_Light, HIGH);
delay(50);
digitalWrite(Red_Light, LOW);
delay(200);
SerialMon.println("Initializing modem...");
// Restarting Modem (Generally it takes some time)
modem.restart();
// Turn off network status lights to reduce current consumption
SerialMon.print("Waiting for network...");
if (!modem.waitForNetwork()) {
SerialMon.println(" fail");
myDFPlayer.play(7);
delay(10000);
return;
}
SerialMon.println(" success");
if (modem.isNetworkConnected())
{
SerialMon.println("Network connected");
myDFPlayer.play(2);
}
digitalWrite(Blue_Light, HIGH);
delay(50);
digitalWrite(Blue_Light, LOW);
delay(200);
digitalWrite(Blue_Light, HIGH);
delay(50);
digitalWrite(Blue_Light, LOW);
// GPRS connection parameters are usually set after network registration
SerialMon.print(F("Connecting to "));
SerialMon.print(apn);
if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
SerialMon.println(" fail");
myDFPlayer.play(7);
delay(10000);
return;
}
SerialMon.println(" success");
if (modem.isGprsConnected()) {
SerialMon.println("GPRS connected");
}
// MQTT Broker setup
mqtt.setServer(broker, 1883);
mqtt.setCallback(mqttCallback);
mqtt.setKeepAlive(60);
const uint32_t t = millis();
if (!modem.isNetworkConnected()) {
SerialMon.println("Network disconnected");
if (!modem.waitForNetwork(180000L, true)) {
SerialMon.println(" fail");
delay(10000);
return;
}
if (modem.isNetworkConnected()) {
SerialMon.println("Network re-connected");
}
}
if (!mqtt.connected())
{
SerialMon.println("=== MQTT CONNECTED ===");
// Reconnect every 10 seconds
uint32_t t = millis();
if (t - lastReconnectAttempt > 10000L) {
lastReconnectAttempt = t;
if (mqttConnect()) {
lastReconnectAttempt = 0;
}
}
delay(100);
return;
}
}
uint32_t lastNetworkCheck;
uint32_t lastMqttCheck;
void loop()
{
mqtt.loop();
button_Check();
}
void voice_commands(String amount)
{
Serial.println("paymant capture");
myDFPlayer.play(4); // Amount Received Voice note
digitalWrite(Red_Light, HIGH);
digitalWrite(Blue_Light, HIGH);
digitalWrite(Green_Light, HIGH);
delay(200);
digitalWrite(Red_Light, LOW);
digitalWrite(Blue_Light, LOW);
digitalWrite(Green_Light, LOW);
delay(50);
digitalWrite(Red_Light, HIGH);
digitalWrite(Blue_Light, HIGH);
digitalWrite(Green_Light, HIGH);
delay(200);
digitalWrite(Red_Light, LOW);
digitalWrite(Blue_Light, LOW);
digitalWrite(Green_Light, LOW);
delay(50);
delay(300);
int amount_int = amount.toInt();
if (amount_int <= 20) {
myDFPlayer.playMp3Folder(amount_int); //play specific mp3 in SD:/MP3/amount_int.mp3; File Name(0~65535)
delay(700);
Serial.println(amount_int);
myDFPlayer.playMp3Folder(20); //play specific mp3 in SD:/MP3/100.mp3; File Name(0~65535)
delay(700);
}
digitalWrite(delivery, HIGH);
delay(500);
digitalWrite(delivery, LOW);
delay(500);
// //end command placed at 61000.
// myDFPlayer.playMp3Folder(61000); //play specific mp3 in SD:/MP3/amount_int.mp3; File Name(0~65535)
// delay(2000);
}
void printDetail(uint8_t type, int value) {
switch (type) {
case TimeOut:
Serial.println(F("Time Out!"));
break;
case WrongStack:
Serial.println(F("Stack Wrong!"));
break;
case DFPlayerCardInserted:
Serial.println(F("Card Inserted!"));
break;
case DFPlayerCardRemoved:
Serial.println(F("Card Removed!"));
break;
case DFPlayerCardOnline:
Serial.println(F("Card Online!"));
break;
case DFPlayerUSBInserted:
Serial.println("USB Inserted!");
break;
case DFPlayerUSBRemoved:
Serial.println("USB Removed!");
break;
case DFPlayerPlayFinished:
Serial.print(F("Number:"));
Serial.print(value);
Serial.println(F(" Play Finished!"));
break;
case DFPlayerError:
Serial.print(F("DFPlayerError:"));
switch (value) {
case Busy:
Serial.println(F("Card not found"));
break;
case Sleeping:
Serial.println(F("Sleeping"));
break;
case SerialWrongStack:
Serial.println(F("Get Wrong Stack"));
break;
case CheckSumNotMatch:
Serial.println(F("Check Sum Not Match"));
break;
case FileIndexOut:
Serial.println(F("File Index Out of Bound"));
break;
case FileMismatch:
Serial.println(F("Cannot Find File"));
break;
case Advertise:
Serial.println(F("In Advertise"));
break;
default:
break;
}
break;
default:
break;
}
}
bool mqttConnect()
{
SerialMon.print("Connecting to ");
SerialMon.print(broker);
// Connect to MQTT Broker
//bool status = mqtt.connect("GsmClientTest");
// Or, if you want to authenticate MQTT:
bool status = mqtt.connect("abcxyz", mqtt_username , mqtt_password);
if (status == false) {
digitalWrite(Green_Light, HIGH);
myDFPlayer.play(7);
delay(2000);
SerialMon.println(" fail");
return false;
}
SerialMon.println(" OK");
myDFPlayer.play(3);
digitalWrite(Green_Light, HIGH);
delay(50);
digitalWrite(Green_Light, LOW);
delay(200);
digitalWrite(Green_Light, HIGH);
delay(50);
digitalWrite(Green_Light, LOW);
delay(200);
digitalWrite(Green_Light, LOW);
mqtt.subscribe(amount_topic);
return mqtt.connected();
}
void button_Check()
{
int test = digitalRead(Prev_Button);
if (test == LOW) {
delay(100);
voice_commands(Prev_payload);
}
}