#include <Arduino.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <ArduinoJson.h>
#include <HTTPClient.h>
#include "OneButton.h" //we need the OneButton library
//#include "complement.h"
LiquidCrystal_I2C LCD = LiquidCrystal_I2C(0x27, 20, 4);
int ledState = LOW; // current state of the output pin
int buttonState; // current reading from the input pin
int lastButtonState = LOW; // previous reading from the input pin
bool motionDetected = false; // flag variable to send motion alert message
bool clearMotionAlert = true; // clear last motion alert message from web page
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
int ledPower = 2;
double dustDensity;
int sensorValue;
double voltage;
// Important!
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD ""
#define WIFI_CHANNEL 6
// Cannot GET /webhook
String serverName = "https://speech-development-tools.glitch.me/...";
void setup()
{
Serial.begin(9600);
// set LED pin as output
pinMode(ledPower, OUTPUT); // set the LED pin low
ledcSetup(0, 5000, 8); // Configurer le canal 0 pour une fréquence de 5000 Hz et une résolution de 8 bits
ledcAttachPin(14, 0); // Attacher la broche LED au canal 0
//initLeds();
LCD.init();
LCD.backlight();
LCD.setCursor(0, 0);
LCD.print("Connecting to WiFi");
Serial.println("Connecting to SERIAL..");
LCD.setCursor(0, 1);
// We start by connecting to a WiFi network
WiFi.begin(WIFI_SSID, WIFI_PASSWORD, WIFI_CHANNEL);
Serial.print("Wait for WiFi... ");
while (WiFi.status() != WL_CONNECTED) {
Serial.println("Connecting to WiFi ...");
LCD.print(".");
delay(1000);
}
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
LCD.setCursor(0, 1);
LCD.print("WiFi connected ");
LCD.setCursor(0, 2);
LCD.print("IP address: ");
LCD.setCursor(5, 3);
LCD.print(WiFi.localIP());
delay(2000);
tone(13, 262, 250); // Plays 262Hz tone for 0.250 seconds
}
const int LINES_PER_MESSAGE = 2;
const int CHARACTERS_PER_LINE = 16;
const int TYPEWRITER_DELAY = 100;
const int LCD_COLUMNS = 20; // Nombre de colonnes de l'écran LCD
const int LCD_ROWS = 4; // Nombre de lignes de l'écran LCD
const int DELAY_BETWEEN_CHARS = 100; // Délai entre l'affichage de chaque caractère en millisecondes
String message = "Comment puis-je t'aider ?";
int speed = 100; // Vitesse initiale
void printMessageCharacterByCharacter(String text, int delayBetweenChars) {
LCD.clear();
for (int i = 0; i < text.length(); i++) {
LCD.setCursor(i % LCD_COLUMNS, i / LCD_COLUMNS);
LCD.print(text[i]);
delay(delayBetweenChars);
}
}
void loop() {
//testLeds();
printMessageCharacterByCharacter(message, speed);
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
// Your Domain name with URL path or IP address with path
http.begin(serverName);
http.addHeader("Content-Type", "application/json");
DynamicJsonDocument doc(1024);
String jsonstr;
JsonObject root = doc.to<JsonObject>();
/*{
"input": "apprentissageBasique", "langue": "fr"
}*/
root["input"] = "apprentissageBasique";
root["langue"] = "fr";
serializeJson(doc, jsonstr);
String httpRequestData = jsonstr;
Serial.println(httpRequestData);
int httpResponseCode = http.POST(httpRequestData);
if (httpResponseCode > 0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
Serial.println("payload: ");
String payload = http.getString();
Serial.println(payload);
Serial.println("------------------------------\n");
DynamicJsonDocument doc(2048);
DeserializationError error = deserializeJson(doc, payload);
// Test if parsing succeeds.
if (error) {
Serial.print("deserializeJson() failed: ");
Serial.println(error.c_str());
//return "<error>";
}
String message = doc["message"].as<String>();
// Utiliser la fonction substr()
String message60Chars1 = message.substring(0, 60);
printMessageCharacterByCharacter(message60Chars1, speed);
// Afficher la valeur de la clé "message"
Serial.print("Message : ");
Serial.println(message);
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
http.end();
}
else {
Serial.println("WiFi Disconnected");
}
delay(3000);
}
/*
//printMessageCharacterByCharacter(payload["message"], speed);
//const char* jsonString = "{\"message\":\"Permettez à l'assistant de mémoriser certaines préférences... certaine musique ou a une routine quotidienne spécifique, il peut s'adapter en conséquence.\"}";
// Taille du document JSON en fonction de la taille de votre chaîne JSON
const size_t bufferSize = JSON_OBJECT_SIZE(1) + JSON_STRING_SIZE(250);
// Créer un objet JSON
DynamicJsonDocument jsonDoc(bufferSize);
// Parser la chaîne JSON
DeserializationError error = deserializeJson(jsonDoc, jsonString);
// Vérifier les erreurs de parsing
if (error) {
Serial.print("Erreur de parsing : ");
Serial.println(error.c_str());
return;
}
*/
/*
// Create DynamicJsonDocument based on the size of the response.
DynamicJsonDocument jsonDoc(response.length());
deserializeJson(jsonDoc, response);
String outputText = jsonDoc["choices"][0]["message"]["content"];
Received webhook data: {"temperature":"temp","humidity":"humid"}
{"temperature":"temp","humidity":"humid"}
HTTP Response code: 200
payload:
{"message":"Webhook data received successfully"}
*/