====== Programme ====== Le programme est le dernier mis à jour avec l'afficheur OLED.\\ Il est encore en phase de test, mais on peu y trouver quasi toutes les fonctions.\\ Reste a agencer ça. ===== Synoptique ===== Initiation Boucle principale Boucle tant que et temps limite Test de connexion MQTT Si réception message MQTT Affichage Exécution de l'ordre ( mise à jour des seuils, Commande volet, ) Récupération de l'heure / min Affichage Récupération des données interne (temp, humidité) / 5min Affichage Récupération des commandes du BP (interruption) Affichage/exécution en fonction BP 1 fois - Affiche Temp, humidité, lumière externe BP 2 fois - Affiche pression BP 3 fois - désactive/réactive volets Envoi info au serveur MQTT / 10min Fin boucle ===== Code ===== /* Projet de sonde luminosité, température et humidité. Ajout de commande des volets roulants pour une gestion de la température du logement. Basé sur l'exemple Basic ESP8266 MQTT Il cherche une connexion sur un serveur MQTT puis : - Il affiche les luminosités/humidités, lunière, pression baro intener (et externe). - Il envoie tout cela en MQTT - La température interne, (externe) - L'humidité interne, (externe) - La lumiére - La pression barométrique et les autres parmêtres du BMP180 En fonction des paramètres lumiére, température, il pourra monter ou descendre le volets roulants. Affichage normal : (heure option) Tempéarute, Humidité Si BP = 1s -> (peut étre 1 pression) Affiche Temp int/ext, Lum int/ext, hum int/ext Si BP = 3S -> Affiche pression, (prévisions météos options) Si BP = 10s -> désactivation volets FUTUR : On pourra définir les paramètres via une instruction MQTT Fonction accessoire : It will reconnect to the server if the connection is lost using a blocking reconnect function. See the 'mqtt_reconnect_nonblocking' example for how to achieve the same result without blocking the main loop. Exemples : MQTT: https://github.com/aderusha/IoTWM-ESP8266/blob/master/04_MQTT/MQTTdemo/MQTTdemo.ino Witty: https://blog.the-jedi.co.uk/2016/01/02/wifi-witty-esp12f-board/ Module tricapteur: http://arduinolearning.com/code/htu21d-bmp180-bh1750fvi-sensor-example.php */ #include #include #include // Include the correct display library // For a connection via I2C using Wire include #include // Only needed for Arduino 1.6.5 and earlier #include "SSD1306.h" // alias for `#include "SSD1306Wire.h"` // Include custom images #include "WeatherStationImages.h" #include "WeatherStationFonts.h" // DHT 11 sensor #define DHTPIN 2 #define DHTTYPE DHT11 // Utilisation d’une photo-résistance // Et ports pour cmd volet const int port = A0; // LDR #define haut 12 #define arret 13 #define bas 15 #define lbp 14 // Update these with values suitable for your network. // Buffer pour convertir en chaine de l'adresse IP de l'appareil char buffer[20]; const char* ssid1 = "FREEBOX_xxx_EXT"; const char* ssid2 = "FREEBOX_xxx_P2"; const char* password = "xxxx"; const char* mqtt_server = "192.168.0.x"; // Création objet WiFiClient espClient; PubSubClient client(espClient); // DHT sensor DHT dht(DHTPIN, DHTTYPE, 15); // Variables int valeur = 0; float vin = 0; char msg[50]; int value = 0; unsigned long readTime; //Buffer qui permet de décoder les messages MQTT reçus char message_buff[100]; long lastMsg = 0; //Horodatage du dernier message publié sur MQTT long lastRecu = 0; bool debug = false; //Affiche sur la console si True bool mess = false; // true si message reçu String sujet = ""; String mesg = ""; // Initialize the OLED display using Wire library SSD1306 display(0x3c, 4, 5); //======================================== void setup_wifi() { int cpt = 0; boolean ssid = true; delay(10); int ss = 1; display.setTextAlignment(TEXT_ALIGN_LEFT); display.setFont(ArialMT_Plain_10); display.drawString(0, 0, "Connexion au WiFi"); // We start by connecting to a WiFi network while (WiFi.status() != WL_CONNECTED) { if (ssid) { WiFi.begin(ssid1, password); } else { ss = 2; WiFi.begin(ssid2, password); } if (debug) { Serial.println(); Serial.print("Connecting to "); if (ss == 1) { Serial.println(ssid1); } else { Serial.println(ssid2); } } int counter = 0; while ((WiFi.status() != WL_CONNECTED) && (cpt <= 20)) { delay(500); Serial.print("."); display.clear(); display.setTextAlignment(TEXT_ALIGN_LEFT); display.drawString(24, 0, "Connecting to WiFi"); display.drawXbm(46, 30, 8, 8, counter % 3 == 0 ? activeSymbole : inactiveSymbole); display.drawXbm(60, 30, 8, 8, counter % 3 == 1 ? activeSymbole : inactiveSymbole); display.drawXbm(74, 30, 8, 8, counter % 3 == 2 ? activeSymbole : inactiveSymbole); display.display(); counter++; cpt=cpt+1; // Serial.print("."); } if (cpt >= 20) { if (ssid) { ssid=false; } else { ssid=true; } } } // Connexion au serveur MQTT display.clear(); display.setTextAlignment(TEXT_ALIGN_LEFT); display.setFont(ArialMT_Plain_10); display.drawString(32, 0, "WiFi connecté"); display.drawString(32, 20, "IP address"); // On récupère et on prépare le buffer contenant l'adresse IP attibué à l'ESP-01 IPAddress ip = WiFi.localIP(); String ipStr = String(ip[0]) + '.' + String(ip[1]) + '.' + String(ip[2]) + '.' + String(ip[3]); ipStr.toCharArray(buffer, 20); display.drawString(30, 30, String(ipStr)); display.display(); delay(2000); display.clear(); display.setTextAlignment(TEXT_ALIGN_LEFT); display.setFont(ArialMT_Plain_10); display.drawString(25, 0, "Connecting to MQTT"); display.drawString(32, 20, mqtt_server); display.display(); delay(2000); if ( debug ) { Serial.println(""); Serial.println("WiFi connected"); Serial.print("IP address: "); Serial.println(WiFi.localIP()); Serial.print("Connecting to "); Serial.println(mqtt_server); } /* Serial.print(" as "); Serial.println(clientName); */ } //======================================== // Déclenche les actions à la réception d'un message // D'après http://m2mio.tumblr.com/post/30048662088/a-simple-example-arduino-mqtt-m2mio void callback(char* topic, byte* payload, unsigned int length) { // pinMode(lbp,OUTPUT); int i = 0; if ( debug ) { Serial.println("Message recu => topic: " + String(topic)); Serial.print(" | longueur: " + String(length,DEC)); } sujet = String(topic); // create character buffer with ending null terminator (string) for(i=0; i 5000) { lastMsg = now; ++value; snprintf (msg, 50, "hello world #%ld", value); //Serial.print("Publish message: "); //Serial.println(msg); client.publish("cuisine", msg); // Lit l’entrée analogique A0 valeur = analogRead(port); //Serial.print("valeur = "); //Serial.println(valeur); // convertit l’entrée en volt vin = (valeur * 3.3) / 1024.0; //Serial.print("volt = "); //Serial.println(vin); display.clear(); display.setTextAlignment(TEXT_ALIGN_LEFT); display.setFont(ArialMT_Plain_10); display.drawString(14, 0, "Luminosité interne"); display.setFont(ArialMT_Plain_16); display.drawString(25, 20, "Niv=" + String(valeur)); display.drawString(18, 38, "Volts=" + String(vin)); display.display(); delay(1500); snprintf (msg, 50, "Luminosité %ld", valeur); client.publish("cuisine", msg); while (now - lastMsg > 10000) { Serial.print("compteur: "+ (now-lastMsg)); } //Serial.println("Lecture du capteur"); sensorRead(); delay(1500); } } /* void DHT11() { // Grab the current state of the sensor int humidity_data = (int)dht.readHumidity(); int temperature_data = (int)dht.readTemperature(); // Publish data if (! temperature.publish(temperature_data)) Serial.println(F("Failed to publish temperature")); else Serial.println(F("Temperature published!")); if (! humidity.publish(humidity_data)) Serial.println(F("Failed to publish humidity")); else Serial.println(F("Humidity published!")); // Setup feeds for temperature & humidity const char TEMPERATURE_FEED[] PROGMEM = AIO_USERNAME "/feeds/temperature"; Adafruit_MQTT_Publish temperature = Adafruit_MQTT_Publish(&mqtt, TEMPERATURE_FEED); const char HUMIDITY_FEED[] PROGMEM = AIO_USERNAME "/feeds/humidity"; Adafruit_MQTT_Publish humidity = Adafruit_MQTT_Publish(&mqtt, HUMIDITY_FEED); } */