/**
- SMART SHOPPING TROLLEY
- NODEMCU ESP8266 – RECEIVE BILL FROM ARDUINO & SEND EMAIL
- Library: ESP Mail Client (Mobizt) v3.x.x
*/
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP_Mail_Client.h>
/* ================= WIFI ================= */
#define WIFI_SSID "M12"
#define WIFI_PASSWORD "11111110"
/* ================= SMTP ================= */
#define SMTP_HOST "smtp.gmail.com"
#define SMTP_PORT esp_mail_smtp_port_587
#define AUTHOR_EMAIL "dbatumart2025@gmail.com"
#define AUTHOR_PASSWORD "wpoj mqfd kvoa gmuj" // Gmail App Password
#define RECIPIENT_EMAIL "shravanidavande2@gmail.com"
/* ================= OBJECT ================= */
SMTPSession smtp;
/* ================= GLOBAL ================= */
String billData = "";
bool billStarted = false;
bool billReady = false;
/* ================= CALLBACK ================= */
void smtpCallback(SMTP_Status status);
/* ===================================================== /
/ FORMAT BILL EXACTLY LIKE CUSTOMER EMAIL /
/ ===================================================== */
String formatBillForEmail(String rawBill)
{
String email =
"Hello Customer,\n\n"
"Your shopping bill details are below:\n\n";
String totalAmount = "0";
int start = 0;
while (true)
{
int end = rawBill.indexOf('|', start);
if (end == -1) break;
String item = rawBill.substring(start, end);
start = end + 1;
// -------- TOTAL --------
if (item.startsWith("TOTAL="))
{
totalAmount = item.substring(6);
continue;
}
// -------- SPLIT ITEM --------
int p1 = item.indexOf(':');
int p2 = item.indexOf(':', p1 + 1);
int p3 = item.indexOf(':', p2 + 1);
int p4 = item.indexOf(':', p3 + 1);
if (p4 == -1) continue;
String name = item.substring(0, p1);
String weight = item.substring(p2 + 1, p3);
String rate = item.substring(p3 + 1, p4);
// -------- FORMAT (ONLY REQUIRED FIELDS) --------
email += "Item : " + name + "\n";
email += "Rate : Rs " + rate + "\n";
email += "Weight : " + weight + " g\n";
email += "-------------------------\n";
}
// -------- FINAL TOTAL --------
email += "\nTOTAL AMOUNT : Rs " + totalAmount + "\n\n";
email += "Thank you for shopping with us!\n";
email += "Dbatu SuperMart";
return email;
}
/* ===================================================== /
/ SEND EMAIL FUNCTION /
/ ===================================================== */
void sendBillEmail(String bill)
{
Session_Config config;
config.server.host_name = SMTP_HOST;
config.server.port = SMTP_PORT;
config.login.email = AUTHOR_EMAIL;
config.login.password = AUTHOR_PASSWORD;
config.login.user_domain = F("127.0.0.1");
config.time.ntp_server = F("pool.ntp.org,time.nist.gov");
config.time.gmt_offset = 5.5 * 3600; // IST
config.time.day_light_offset = 0;
SMTP_Message message;
message.sender.name = F("DbatuSuperMart");
message.sender.email = AUTHOR_EMAIL;
message.subject = "SMART SHOPPING BILL";
message.addRecipient(F("Customer"), RECIPIENT_EMAIL);
String emailBody = formatBillForEmail(bill);
message.text.content = emailBody.c_str();
message.text.charSet = F("utf-8");
message.text.transfer_encoding = "7bit";
smtp.debug(1);
smtp.callback(smtpCallback);
if (!smtp.connect(&config))
{
Serial.println("SMTP connection failed!");
return;
}
if (!MailClient.sendMail(&smtp, &message))
{
Serial.println("Email sending failed!");
Serial.println(smtp.errorReason());
}
else
{
Serial.println("Bill Email Sent Successfully!");
}
smtp.sendingResult.clear();
}
/* ================= SETUP ================= */
void setup()
{
Serial.begin(9600);
delay(200);
Serial.println("\nConnecting to WiFi...");
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(300);
}
Serial.println("\nWiFi Connected!");
Serial.println(WiFi.localIP());
MailClient.networkReconnect(true);
}
/* ================= LOOP ================= */
void loop()
{
while (Serial.available())
{
String line = Serial.readStringUntil('\n');
line.trim();
if (line == "BILL_START")
{
billData = "";
billStarted = true;
}
else if (line == "BILL_END")
{
billStarted = false;
billReady = true;
}
else if (billStarted)
{
billData += line + "|";
}
}
if (billReady)
{
sendBillEmail(billData);
billReady = false;
}
}
/* ================= CALLBACK ================= */
void smtpCallback(SMTP_Status status)
{
Serial.println(status.info());
}
/**
*/
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP_Mail_Client.h>
/* ================= WIFI ================= */
#define WIFI_SSID "M12"
#define WIFI_PASSWORD "11111110"
/* ================= SMTP ================= */
#define SMTP_HOST "smtp.gmail.com"
#define SMTP_PORT esp_mail_smtp_port_587
#define AUTHOR_EMAIL "dbatumart2025@gmail.com"
#define AUTHOR_PASSWORD "wpoj mqfd kvoa gmuj" // Gmail App Password
#define RECIPIENT_EMAIL "shravanidavande2@gmail.com"
/* ================= OBJECT ================= */
SMTPSession smtp;
/* ================= GLOBAL ================= */
String billData = "";
bool billStarted = false;
bool billReady = false;
/* ================= CALLBACK ================= */
void smtpCallback(SMTP_Status status);
/* ===================================================== /
/ FORMAT BILL EXACTLY LIKE CUSTOMER EMAIL /
/ ===================================================== */
String formatBillForEmail(String rawBill)
{
String email =
"Hello Customer,\n\n"
"Your shopping bill details are below:\n\n";
String totalAmount = "0";
int start = 0;
while (true)
{
int end = rawBill.indexOf('|', start);
if (end == -1) break;
}
// -------- FINAL TOTAL --------
email += "\nTOTAL AMOUNT : Rs " + totalAmount + "\n\n";
email += "Thank you for shopping with us!\n";
email += "Dbatu SuperMart";
return email;
}
/* ===================================================== /
/ SEND EMAIL FUNCTION /
/ ===================================================== */
void sendBillEmail(String bill)
{
Session_Config config;
config.server.host_name = SMTP_HOST;
config.server.port = SMTP_PORT;
config.login.email = AUTHOR_EMAIL;
config.login.password = AUTHOR_PASSWORD;
config.login.user_domain = F("127.0.0.1");
config.time.ntp_server = F("pool.ntp.org,time.nist.gov");
config.time.gmt_offset = 5.5 * 3600; // IST
config.time.day_light_offset = 0;
SMTP_Message message;
message.sender.name = F("DbatuSuperMart");
message.sender.email = AUTHOR_EMAIL;
message.subject = "SMART SHOPPING BILL";
message.addRecipient(F("Customer"), RECIPIENT_EMAIL);
String emailBody = formatBillForEmail(bill);
message.text.content = emailBody.c_str();
message.text.charSet = F("utf-8");
message.text.transfer_encoding = "7bit";
smtp.debug(1);
smtp.callback(smtpCallback);
if (!smtp.connect(&config))
{
Serial.println("SMTP connection failed!");
return;
}
if (!MailClient.sendMail(&smtp, &message))
{
Serial.println("Email sending failed!");
Serial.println(smtp.errorReason());
}
else
{
Serial.println("Bill Email Sent Successfully!");
}
smtp.sendingResult.clear();
}
/* ================= SETUP ================= */
void setup()
{
Serial.begin(9600);
delay(200);
Serial.println("\nConnecting to WiFi...");
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(300);
}
Serial.println("\nWiFi Connected!");
Serial.println(WiFi.localIP());
MailClient.networkReconnect(true);
}
/* ================= LOOP ================= */
void loop()
{
while (Serial.available())
{
String line = Serial.readStringUntil('\n');
line.trim();
else if (billStarted)
{
billData += line + "|";
}
}
if (billReady)
{
sendBillEmail(billData);
billReady = false;
}
}
/* ================= CALLBACK ================= */
void smtpCallback(SMTP_Status status)
{
Serial.println(status.info());
}