#include <Wire.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <DHT.h>
#include <WiFi.h>
#include <Blynk.h>
#include <TinyGPSPlus.h>
#include <SoftwareSerialTX.h>
#include <ThingSpeak.h>
#include <WiFiClient.h>
#include <esp8266-google-tts.h>
// Define Blynk settings
#define BLYNK_TEMPLATE_ID "TMPL3FR9dY-_A"
#define BLYNK_TEMPLATE_NAME "SMART GLASSES FOR VISUALLY BLIND"
#define BLYNK_AUTH_TOKEN "W0UQHwSq9Dhcz1WnUfMhgtRtjp16gwi1"
// Ultrasonic Sensor Pins
#define TRIG_PIN1 14
#define ECHO_PIN1 13
#define TRIG_PIN2 45
#define ECHO_PIN2 48
// IR Sensor Pins
#define IR_PIN1 15
#define IR_PIN2 42
#define IR_PIN3 35
// DHT22 Sensor Settings
#define DHTPIN 4
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
// Buzzer Pin
#define BUZZER_PIN 12
// Temperature Sensor Pins
#define ONE_WIRE_BUS 18
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
// GPS Settings
TinyGPSPlus gps;
SoftwareSerial ss(16, 17); // RX, TX
// WiFi and Blynk settings
char ssid[] = "Hash";
char pass[] = "contrasena";
// ThingSpeak settings
unsigned long myChannelNumber = 2628555;
const char * myWriteAPIKey = "TSBK165SONTOXYR1Y";
WiFiClient client;
// Google TTS Settings
WiFiClient ttsClient;
const char* ttsBaseURL = "http://translate.google.com/translate_tts?ie=UTF-8&client=tw-ob&q=";
void setup() {
Serial.begin(115200);
ss.begin(9600);
// Initialize sensors and modules
pinMode(TRIG_PIN1, OUTPUT);
pinMode(ECHO_PIN1, INPUT);
pinMode(TRIG_PIN2, OUTPUT);
pinMode(ECHO_PIN2, INPUT);
pinMode(IR_PIN1, INPUT);
pinMode(IR_PIN2, INPUT);
pinMode(IR_PIN3, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
dht.begin();
sensors.begin();
// Initialize WiFi, Blynk, and ThingSpeak
WiFi.begin(ssid, pass);
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
ThingSpeak.begin(client);
// Connect to Google TTS
connectToTTS();
}
void loop() {
Blynk.run();
checkUltrasonicSensors();
checkIRSensors();
checkTemperature();
checkGPS();
delay(2000); // delay between checks
}
void checkUltrasonicSensors() {
long duration1, distance1, duration2, distance2;
// Ultrasonic 1
digitalWrite(TRIG_PIN1, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN1, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN1, LOW);
duration1 = pulseIn(ECHO_PIN1, HIGH);
distance1 = (duration1 * 0.034) / 2;
// Ultrasonic 2
digitalWrite(TRIG_PIN2, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN2, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN2, LOW);
duration2 = pulseIn(ECHO_PIN2, HIGH);
distance2 = (duration2 * 0.034) / 2;
// Object detection
if (distance1 < 50 || distance2 < 50) {
tone(BUZZER_PIN, 1000, 200);
speak("Obstacle ahead");
}
// Stairs detection (up or down)
if (distance1 > distance2 + 10) {
speak("Stairs down detected");
} else if (distance2 > distance1 + 10) {
speak("Stairs up detected");
}
}
void checkIRSensors() {
int ir1 = digitalRead(IR_PIN1);
int ir2 = digitalRead(IR_PIN2);
int ir3 = digitalRead(IR_PIN3);
// Hole or gap detection
if ((ir1 == LOW && ir2 == LOW) || (ir2 == LOW && ir3 == LOW) || (ir1 == LOW && ir3 == LOW)) {
tone(BUZZER_PIN, 1500, 200);
speak("Gap detected");
}
}
void checkTemperature() {
sensors.requestTemperatures();
float temperature = sensors.getTempCByIndex(0);
float tempDHT = dht.readTemperature();
if (temperature > 30 || tempDHT > 30) {
tone(BUZZER_PIN, 1000, 200);
speak("Hot object detected");
}
}
void checkGPS() {
while (ss.available() > 0) {
gps.encode(ss.read());
if (gps.location.isUpdated()) {
float latitude = gps.location.lat();
float longitude = gps.location.lng();
// Send location to ThingSpeak
ThingSpeak.setField(1, latitude);
ThingSpeak.setField(2, longitude);
ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
// Alert via TTS
speak("Location updated");
}
}
}
void speak(String text) {
String url = ttsBaseURL + urlencode(text);
ttsClient.connect("translate.google.com", 80);
ttsClient.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: translate.google.com\r\n" +
"Connection: close\r\n\r\n");
delay(1000);
while (ttsClient.available()) {
Serial.write(ttsClient.read());
}
ttsClient.stop();
}
String urlencode(String str) {
String encoded = "";
for (size_t i = 0; i < str.length(); i++) {
char c = str[i];
if (c == ' ') {
encoded += '+';
} else if (isalnum(c)) {
encoded += c;
} else {
encoded += '%';
encoded += String(c >> 4, HEX);
encoded += String(c & 0x0F, HEX);
}
}
return encoded;
}
void connectToTTS() {
// Function to initialize connection with Google TTS service
// This is a placeholder function if specific initialization is required
}