[go: up one dir, main page]

0% found this document useful (0 votes)
12 views2 pages

Video

video how to use django
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views2 pages

Video

video how to use django
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <WiFi.

h>
#include <HTTPClient.h>
#include <Audio.h>
#include <U8g2lib.h>

// Define I2S pins for MAX98357A


#define I2S_DOUT 19
#define I2S_BCLK 18
#define I2S_LRC 5

// Wi-Fi credentials and server URL


const char* ssid = "Galaxy A12 F9F2";
const char* password = "jdpq9672";
const char* serverUrl = "http://YOUR_NGROK_URL/ask"; // Replace with your ngrok URL

// U8g2 OLED display setup (I2C)


U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/U8X8_PIN_NONE, /*
clock=*/21, /* data=*/22);

Audio audio;
int led_blue = 15;
int led_green = 2;
int led_red = 4;

void setup() {
Serial.begin(115200);

// Initialize LEDs
pinMode(led_blue, OUTPUT);
pinMode(led_green, OUTPUT);
pinMode(led_red, OUTPUT);

// Initialize U8g2 for OLED


u8g2.begin();
u8g2.clearBuffer(); // Clear the display buffer

// Initialize WiFi connection


WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("\nConnected to WiFi!");

// Initialize audio for MAX98357A


audio.setPinout(I2S_BCLK, I2S_LRC, I2S_DOUT);
audio.setVolume(100);
}

void loop() {
audio.loop();

// Check for Serial input


if (Serial.available()) {
String question = Serial.readStringUntil('\n');
askQuestion(question);
}
}
void askQuestion(String question) {
WiFiClient client;
HTTPClient http;

if (http.begin(client, serverUrl)) { // Begin connection to the server URL


http.addHeader("Content-Type", "application/json");

// Construct JSON payload with the question


String jsonPayload = "{\"question\": \"" + question + "\"}";
int httpResponseCode = http.POST(jsonPayload);

// Check for valid response


if (httpResponseCode == 200) {
String response = http.getString();
String answer = parseJson(response, "answer");

Serial.println("Response: " + answer);


displayText(answer); // Display on OLED
audio.connecttospeech(answer.c_str(), "en"); // Play audio
} else {
Serial.print("Error: ");
Serial.println(httpResponseCode);
}
http.end(); // Close HTTP connection
} else {
Serial.println("Failed to connect to server.");
}
}

// Helper function to display text on OLED


void displayText(String text) {
u8g2.clearBuffer();
u8g2.setFont(u8g2_font_ncenB08_tr);
u8g2.drawStr(0, 10, text.c_str());
u8g2.sendBuffer();
}

// Helper function to parse JSON response


String parseJson(String json, String key) {
int startIndex = json.indexOf(key) + key.length() + 3;
int endIndex = json.indexOf("\"", startIndex);
return json.substring(startIndex, endIndex);
}

You might also like