Termin:Elektronik-Stammtisch47/LiliSketch

Aus Attraktor Wiki

Wechseln zu: Navigation, Suche

/***************************************************
 \----------------------------/
 | .____    .__.____    .__   |
 | |    |   |__|    |   |__|  |
 | |    |   |  |    |   |  |  |
 | |    |___|  |    |___|  |  |
 | |_______ \__|_______ \__|  |
 |         \/          \/     |
 /----------------------------\
  
   LiLi --- Lianes Lichtanlage
   lili@cobii.de  --- Stand 28.03.2016   
                                                                                                      • /

/****************************************************/

  1. include <ESP8266WiFi.h>
  2. include <MQTTClient.h>


/************************* Global Stuff *********************************/

  1. define VERSION "0.0.5"

/************************* WiFi Access Point *********************************/

//#define WLAN_SSID "MeineSSID" //#define WLAN_PASS "xxxxxxcccc"

  1. define WLAN_SSID "Attraktor"
  2. define WLAN_PASS "blablabla"

/************************* MQTT Setup *********************************/

  1. define IO_SERVER "cobiispi"
  2. define IO_SERVERPORT 1883 // use 8883 for SSL
  3. define IO_USERNAME ""
  4. define IO_KEY ""
  5. define MQTT_TOPPIC_SUP "/lilis/incoming_"
  6. define DEVICE_ID "lili_2"

/* ########################################################### */ // Pinout settings

  1. define PINS_LED 13 //(digital pin)
  2. define PINS_LEDPOS 12 //(digital pin)
  3. define PINS_LEDNEG 15 //(digital pin)
  4. define PINS_FLASH 16 //(digital pin) Eigentlich 16 zum Testen 12
  5. define PINS_MOTION 4 //(digital pin)
  6. define PINS_SENS_BRIGHTNESS A0 //(analog pin)

// Variables used on interrupt mode volatile boolean cancelFlag = false; // Flag used to abort interrupt mode

// Variables uint32_t nightLightVal = 10; uint32_t lichtSens = 0; long MQTT_UPDATE_TIME = 10000; boolean call = false; // Anrufstatus an oder aus boolean nightlight = false; // Nachlicht an oder aus boolean motion = false; // Wert des Motionsensors boolean motionAlt = false; // Alter Wert des Motionsensors unsigned long lastMillis = 0;

/************ ******************/

// Create an ESP8266 WiFiClient class to connect to the MQTT server. WiFiClient net; MQTTClient client; void connect(); // <- predefine connect() for setup()

// Store the MQTT server, username, and password in flash memory. const char MQTT_SERVER[] PROGMEM = IO_SERVER; const char MQTT_USERNAME[] PROGMEM = IO_USERNAME; const char MQTT_PASSWORD[] PROGMEM = IO_KEY;




/*************************** Sketch Code ************************************/

// Bug workaround for Arduino 1.6.6, it seems to need a function declaration // for some reason (only affects ESP8266, likely an arduino-builder bug). //void MQTT_connect();

//Postiv feedback blink void positiv_blink () {

 int i = 0; 
 while(i<2){
 digitalWrite(PINS_LED,HIGH);
 delay(80);
 digitalWrite(PINS_LED,LOW);
 delay(80);
 i++;
 }
 delay(180);
 digitalWrite(PINS_FLASH,LOW);
 delay(2000);

}


//incomming call signal void incomming_call_blink () {

 digitalWrite(PINS_FLASH,HIGH);
 delay(180);
 digitalWrite(PINS_FLASH,LOW);
 delay(180);
 digitalWrite(PINS_FLASH,HIGH);
 delay(180);
 digitalWrite(PINS_FLASH,LOW);
 delay(2000);

}

//incomming door signal void doorbell_blink () {

 int i = 0; 
 while(i<2){
 digitalWrite(PINS_FLASH,HIGH);
 delay(300);
 digitalWrite(PINS_FLASH,LOW);
 delay(300);
 i++;
 }

}

//connect funktion void connect() {

 Serial.print("checking wifi...");
 while (WiFi.status() != WL_CONNECTED) {
   digitalWrite(PINS_LEDNEG,HIGH);
   delay(250);
   Serial.print(".");
   digitalWrite(PINS_LEDNEG,LOW);
   delay(250);
 }
 positiv_blink();
 Serial.print("\nconnecting...");
 while (!client.connect("arduino", "try", "try")) {
   delay(250);
   Serial.print(".");
   digitalWrite(PINS_LEDNEG,LOW);
   delay(250);
 }
 Serial.println("\nconnected!");
 positiv_blink();
 client.subscribe(MQTT_TOPPIC_SUP DEVICE_ID);
 // client.unsubscribe("/example");

}


// MQTT empfangen, prüfen und "parsen" void messageReceived(String topic, String payload, char * bytes, unsigned int length) {

 Serial.print("incoming: ");
 Serial.print(topic);
 Serial.print(" - ");
 Serial.print(payload);
 Serial.println();
 int payloadInt = payload.toInt() ;
 switch (payloadInt){
      case 0:
        call = false;
        Serial.println("case - 0");        
        break; 
      case 1:
        call = true;
        Serial.println("case - 1");
        break;
      case 2:
        call = true;
        Serial.println("case - 2");
        digitalWrite(PINS_LEDPOS,LOW);
        break;
      case 3:
        call = true;
        Serial.println("case - 3");
        digitalWrite(PINS_LEDPOS,HIGH);
        break;         
      case 4:
        call = true;
        Serial.println("case - 4");
        digitalWrite(PINS_LEDNEG,LOW);
        break;
      case 5:
        call = true;
        Serial.println("case - 5");
        digitalWrite(PINS_LEDNEG,HIGH);
        break;          
      case 20:
        nightlight = false;
        Serial.println("case - 20");
        break;
      case 21:
        nightLightVal = 25;
        nightlight = true;
        Serial.println("case - 21");
        break;
      case 22:
        nightLightVal = 51;
        nightlight = true;
        Serial.println("case - 22");
        break;
      case 23:
        nightLightVal = 77;
        nightlight = true;
        Serial.println("case - 23");
        break;
      case 24:
        nightLightVal = 102;
        nightlight = true;
        Serial.println("case - 24");
        break;
      case 25:
        nightLightVal = 128;
        nightlight = true;
        Serial.println("case - 25");
        break;
      case 26:
        nightLightVal = 179;
        nightlight = true;
        Serial.println("case - 26");
      case 27:
        nightLightVal = 204;
        nightlight = true;
        Serial.println("case - 27");
      case 28:
        nightLightVal = 230;
        nightlight = true;
        Serial.println("case - 28");
      case 29:
        nightLightVal = 255;
        nightlight = true;
        Serial.println("case - 29");
        break;         
      }  
}


//Licht Sensor senden void licht_sensor_send() {

     lichtSens = analogRead(PINS_SENS_BRIGHTNESS);
     if(millis() - lastMillis > MQTT_UPDATE_TIME) {
     lastMillis = millis();
     client.publish(("feeds/helligkeiten/licht_"DEVICE_ID), String(lichtSens));
     Serial.print("feeds/helligkeiten/licht_"DEVICE_ID);
     Serial.print(" - ");
     Serial.print(lichtSens);
     Serial.println();
   }
 }

//Motion Sensor senden

void motion_sensor_send (){
 motion = digitalRead(PINS_MOTION);
   if (motion != motionAlt) {
     switch (motion){
      case true:
         digitalWrite(PINS_LED,HIGH);
         client.publish("feeds/bewegungssensoren/bewegung_"DEVICE_ID, String(motion));
         Serial.print("feeds/bewegungssensoren/bewegung_"DEVICE_ID);
         Serial.print(" - ");
         Serial.print(motion);
         Serial.println();        
         motionAlt = motion;
         break;
      case false:
         digitalWrite(PINS_LED,LOW);
         client.publish("feeds/bewegungssensoren/bewegung_"DEVICE_ID, String(motion));
         Serial.print("feeds/bewegungssensoren/bewegung_"DEVICE_ID);
         Serial.print(" - ");
         Serial.print(motion);
         Serial.println();
         motionAlt = motion;
         break;   
      }
   }

}


// Ruft die client.loop() der MQTTClient.h Lib auf und checkt ob Verbindung besteht. // Dadurch werden MQTT Pakete empfangen und gesendet void mqtt_updater() {

 client.loop();
   delay(10);                       // <- fixes some issues with WiFi stability
   if(!client.connected()) {
     connect();
   }

}


/* ###### SETUP ################################### */ void setup() { //Pinmode inputs pinMode(PINS_MOTION, INPUT); pinMode(PINS_SENS_BRIGHTNESS, INPUT);

//Pinmode output pinMode(PINS_LED, OUTPUT); pinMode(PINS_LEDNEG, OUTPUT); pinMode(PINS_FLASH, OUTPUT); pinMode(PINS_LEDPOS, OUTPUT);


// Serial starten #######################################

 Serial.begin(115200);
 delay(10); 
 Serial.println(); Serial.println();
 Serial.println(F("MQTT LichtAnlage -Version "VERSION));

// Say Hello #########################################

 delay(3000);
 positiv_blink();
 delay(3000);
 Serial.println("check Call Blink");
 incomming_call_blink ();
 

// Wifi Starten #########################################

 Serial.println(); Serial.println();
 Serial.print("Connecting to ");
 Serial.println(WLAN_SSID);
 WiFi.begin(WLAN_SSID, WLAN_PASS);
 

// MQTT Starten #########################################

 client.begin(IO_SERVER, net);   
 connect();

}


/* ###### LOOP LOOOOOOOPP ################################### */

void loop() {


licht_sensor_send() ; motion_sensor_send() ; mqtt_updater() ;

 if (call==true) 
   incomming_call_blink ();
 if(nightlight==true)
   analogWrite(PINS_FLASH,nightLightVal);
 if(nightlight==false)
   analogWrite(PINS_FLASH,0);

}

Diese Seite wurde zuletzt am 8. Juni 2016 um 15:38 Uhr geändert. Diese Seite wurde bisher 1.107 mal abgerufen.