/* Libraries pour la ESP8266 */
//#include <WiFiClient.h>
//#include <ESP8266WiFi.h>
/* Libraries pour la ESP32 */
#include <HTTPClient.h>
#include <WiFi.h>
#include <C:\Users\Alexi\Desktop\smartwatchProject\ESPHue.h>
/* Libraries pour le serveur d'heure NTP */
#include <WiFiUdp.h>
#include <NTPClient.h>
/* Libraries pour l'afficheur OLED */
//#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_Sensor.h>
#include <DHT.h> // Sensor Temperature and Humidity
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#define BLYNK_PRINT Serial
#define DHTPIN 19
#define DHTTYPE DHT11
#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 NUMFLAKES 10 // Number of snowflakes in the animation example
#define LOGO_HEIGHT 16
#define LOGO_WIDTH 16
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "95cdc8b7584b4881b2162c380ccd00a5";
// Configuration du WiFi
const char* ssid = "Polytech_IOT";
const char* password = "#Polytech";
boolean onOffState = true; //to store actual on/off state of lights as reported by Hue bridge
/* test de la dure d'une mthode
static unsigned dureeConnexionW=0;
static unsigned debutConnexionW=millis();
*/
const String endpoint = "http://api.openweathermap.org/data/2.5/weather?q=paris,fr&units=metric&APPID=";
const String key = "8a7706d75f2e53397288f63eecc106cb";
int button1 = 5; // afficher sur l'cran la date et l'tat des Leds
int button2 = 15; // Ampoule 1
int button3 = 2; // Ampoule 3
/****** variables de controle ******/
int etat1 = 0;
int stocke_etat1 = LOW;
int etat2 = 0; // variable pour la lecture du pushbutton
int stocke_etat2 = LOW; // variable our stocker les valeurs du pushbutton
int etat3 = 0; // variable pour la lecture du pushbutton
int stocke_etat3 = LOW; // variable our stocker les valeurs du pushbutton
String etatAfficher_1 = ""; // variable global stocke la tat de la LAMPE
String etatAfficher_2 = "";
/** Configuration du serveur NTP France **/
const char* serveurNTP = "0.fr.pool.ntp.org"; // Serveur NTP France
const int fusoHoraire = 7200; // Fuso horaire en second (+2 h = 7200 sec -> heure d't)
/***********************/
//const int taxaDeAtualizacao = 1800000; // Taux d'actualisation du serveur NTP en milliseconds
WiFiClient client;
WiFiUDP ntpUDP; // Declaration du Protocole UDP
NTPClient timeClient(ntpUDP, serveurNTP, fusoHoraire, 60000);
// Variables to save date and time
String formattedDate;
String dayStamp;
String timeStamp;
DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer; // frquence d'envoi de la donnes du capteur
WidgetLED led1(V1); // Liaison entre la led blynk et la patte de la carte
WidgetLED led2(V2);
ESPHue myHue = ESPHue(client, "odWQKGHevUnlmmEhdCdk17aEWwe-dcxyzNwFqAUr", "192.168.1.101", 80);
void envoieDonnees(){
float humidite = dht.readHumidity();
float temperature = dht.readTemperature();
if (isnan(temperature) || isnan(humidite)) {
Serial.println("Erreur de lecture !");
return;
}
Blynk.virtualWrite(V4, temperature);
Blynk.virtualWrite(V5, humidite);
}
void setup() {
// debutConnexionW=millis(); // on vient de lancer la connexion wifi
dht.begin();
Blynk.begin(auth, ssid, password);
timer.setInterval(1000L, envoieDonnees);
pinMode(button1, INPUT_PULLUP);
pinMode(button2, INPUT_PULLUP);
pinMode(button3, INPUT_PULLUP);
connexionWifi();
Serial.begin(9600);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
// if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3D)) { // Address 0x3D for 128x64
// Serial.println(F("SSD1306 allocation failed"));
// for(;;); // Don't proceed, loop forever
// }
afficheLogo();
}
void afficheLogo(void){
//SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3D)) { // Address 0x3D for 128x64
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
display.display();
delay(2000); // Pause for 2 seconds
display.clearDisplay();
// Draw a single pixel in white
display.drawPixel(10, 10, WHITE);
display.display();
delay(2000);
}
void loop() {
Blynk.run();
timer.run();
// lis l'tat du pushbutton : allumer (HIGH) ou teindre (LOW)
etat1 = digitalRead(button1);
etat2 = digitalRead(button2);
etat3 = digitalRead(button3);
//Serial.println(alex);
/********************************** vrification pour le bouton 1 *******************************************/
if(etat2 == HIGH){
Serial.println("button2 Pressed");
// inverse la valeur de la variable stocke_etat3
stocke_etat2= !stocke_etat2;
// Delay de 500ms pour viter qu'il y ait plusieurs changement
//delay(500);
}
if (stocke_etat2 == HIGH) // If Light 3 is on
{
Serial.println("Hue Light 1 is Currently On, Turning Light Off");
/******* Changement de couleur alatoirement *******/
/*int rndNum = random(267, 65534);
myHue.setGroup(0, myHue.ON, 255, 255, rndNum); // Set group 0 to random color (group 0 is all lights by default)*/
/**** ***/
myHue.setLightPower(1, myHue.ON);
etatAfficher_1 = "ON";
led2.on();
}
else // Light 1 is off
{
Serial.println("Hue Light 1 is Currently Off, Turning Light On");
myHue.setLightPower(1, myHue.OFF);
etatAfficher_1 = "OFF";
led2.off();
}
/******************************************* fin bouron 1 ********************************************************/
/*********************************** vrification pour le bouton 3 ***********************************************/
if(etat3 == HIGH){
Serial.println("Button2 Pressed");
// inverse la valeur de la variable stocke_etat3
stocke_etat3= !stocke_etat3;
// Delay de 500ms pour viter qu'il y ait plusieurs changement
//delay(500);
}
if (stocke_etat3 == HIGH) // If Light 3 is on
{
Serial.println("Hue Light 3 is Currently On, Turning Light Off");
/******* Changement de couleur alatoirement *******/
int rndNum = random(267, 65534);
myHue.setGroup(0, myHue.ON, 255, 255, rndNum); // Set group 0 to random color (group 0 is all lights by default)*/
myHue.setLightPower(3, myHue.ON);
etatAfficher_2 = "ON";
led1.on();
}
else // Light 3 is off
{
Serial.println("Hue Light 3 is Currently Off, Turning Light On");
myHue.setLightPower(3, myHue.OFF);
etatAfficher_2 = "OFF";
led1.off();
}
/****************************************** fin bouton 3 **************************************************************/
timeClient.update();
Serial.println(timeClient.getFormattedTime());
String horario = timeClient.getFormattedTime();
formattedDate = timeClient.getFormattedDate();
// Extract date
int splitT = formattedDate.indexOf("T");
dayStamp = formattedDate.substring(0, splitT);
// Extract time
timeStamp = formattedDate.substring(splitT+1, formattedDate.length()-1);
// String weather = getWeather();
// Serial.println(weather);
// String temp = weather.substring(144,149);
testscrolltext(dayStamp, timeStamp, etatAfficher_1, etatAfficher_2,"13.54");
delay(500);
// dureeConnexionW +=millis()-debutConnexionW; // on est connecter
// Serial.println(dureeConnexionW);
}
void testdrawchar(void) {
display.clearDisplay();
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(WHITE); // Draw white text
display.setCursor(0, 0); // Start at top-left corner
display.cp437(true); // Use full 256 char 'Code Page 437' font
// Not all the characters will fit on the display. This is normal.
// Library will draw what it can and the rest will be clipped.
for(int16_t i=0; i<256; i++) {
if(i == '\n') display.write(' ');
else display.write(i);
}
display.display();
delay(2000);
}
void testscrolltext(String date, String heure, String afficheEtat_1, String afficheEtat_2, String temp) {
display.clearDisplay();
display.setTextSize(1); // Draw 2X-scale text
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.println("Date : " + date);
display.println("Heure : " + heure);
display.setCursor(0, 20);
display.println("Lampe 1 : " + afficheEtat_1);
//display.setCursor(0, 20);
display.println("Lampe 2 : " + afficheEtat_2);
display.println("Temperature : " + temp);
display.display(); // Show initial text
delay(100);
}
/*
* Permet de configurer la connexion Wifi
* de l'ESP32 au rseau local
*/
void connexionWifi(void){
Serial.begin(115200);
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
// Initialisation du client d'acquisition du temp !
timeClient.begin();
}
String getWeather(){
String payload;
if ((WiFi.status() == WL_CONNECTED)) { //Check the current connection status
HTTPClient http;
http.begin(endpoint + key); //Specify the URL
int httpCode = http.GET(); //Make the request
if (httpCode > 0) { //Check for the returning code
payload = http.getString();
}
else {
// Serial.println("Error on HTTP request");
payload = "Error on HTTP request";
}
http.end(); //Free the resources
}
delay(1000);
return payload;
}
Comments