#include <WiFi.h>
#include <ESPAsyncWebServer.h>
// https://techtutorialsx.com/2022/01/17/esp32-chat-application-part-1/
const char* ssid = "Wokwi-GUEST";
const char* password = "";
AsyncWebServer server(80);
AsyncWebSocket ws("/chat");
void onWsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len){
if(type == WS_EVT_CONNECT){
Serial.println("Websocket client connection received");
} else if(type == WS_EVT_DISCONNECT){
Serial.println("Client disconnected");
} else if(type == WS_EVT_DATA){
ws.textAll(data, len);
Serial.print("Data received: ");
for(int i=0; i < len; i++) {
Serial.print((char) data[i]);
}
Serial.println();
}
}
void setup(){
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi..");
}
Serial.println(WiFi.localIP());
ws.onEvent(onWsEvent);
server.addHandler(&ws);
server.begin();
}
void loop(){}
/*
#include <Arduino.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <LiquidCrystal_I2C.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <ArduinoJson.h>
#include <HTTPClient.h>
#include "complement.h"
LiquidCrystal_I2C LCD = LiquidCrystal_I2C(0x27, 16, 2);
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;
// OLED Display
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// Important!
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD ""
#define WIFI_CHANNEL 6
String serverName = "https://speech-development-tools.glitch.me/webhook";
void setup()
{
Serial.begin(9600);
// set LED pin as output
pinMode(ledPower, OUTPUT); // set the LED pin low
//Serial.println("Could not find a valid BME280 sensor, check wiring!");
//Serial.println("Impossible de trouver un capteur BME280 valide, vérifiez le câblage !");
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
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS))
{
Serial.println(F("SSD1306 allocation failed"));
for (;;); // Don't proceed, loop forever
}
display.display();
// Clear the buffer
display.clearDisplay();
display.setTextSize(2); // Draw 2X-scale text
display.setTextColor(SSD1306_WHITE);
display.setCursor(10, 5);
display.println("Dino Game");
display.display();
// initialize the LED pin as an output
// à droite
pinMode(out23, OUTPUT);
pinMode(out19, OUTPUT);
pinMode(out18, OUTPUT);
pinMode(out15, OUTPUT);
pinMode(out05, OUTPUT);
pinMode(out04, OUTPUT);
//pinMode(out02, OUTPUT);
// à gauche
pinMode(out32, OUTPUT);
pinMode(in34, INPUT_PULLUP);
pinMode(in35, INPUT_PULLUP);
pinMode(in25, INPUT_PULLUP);
pinMode(in26, INPUT_PULLUP);
pinMode(in27, INPUT_PULLUP);
pinMode(in14, OUTPUT);
pinMode(in12, OUTPUT);
pinMode(in13, OUTPUT);
LCD.init();
LCD.backlight();
LCD.setCursor(0, 0);
LCD.print("Connecting to ");
LCD.setCursor(0, 1);
LCD.print("WiFi ");
Serial.println("Connecting to SERIAL..");
// 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 ...");
delay(1000);
}
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
int ledPins[] = {23, 19, 18, 5, 4, 15, 14, 32};
void loop() {
// Allumer chaque LED l'une après l'autre
for (int i = 0; i < 8; i++) {
digitalWrite(ledPins[i], HIGH); // Allumer la LED
// G
int pir_value = digitalRead(in27);
//Serial.print(pir_value);
digitalWrite(in12, pir_value);
int value = digitalRead(in26);
Serial.print(value);
digitalWrite(in13, HIGH); // G buzzer
//digitalWrite(in14, HIGH); // G
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(12, 20);
display.print("Score:");
display.display();
delay(200); // Attendre une seconde
//display.setTextColor(BLACK);
display.setCursor(12, 20);
display.print(" ");
display.display();
digitalWrite(ledPins[i], LOW); // Éteindre la LED
digitalWrite(in12, LOW); // G
digitalWrite(in13, LOW); // G
//digitalWrite(in14, LOW); // G
delay(200);
}
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>();
root["temperature"] = "temp";
root["humidity"] = "humid";
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);
String payload = http.getString();
Serial.println(payload);
}
else {
Serial.print("Error code: "); Serial.println(httpResponseCode);
}
http.end();
}
else {
Serial.println("WiFi Disconnected");
}
}
*/