Project Embedded C

Dependencies:   DS1307 EthernetInterface Servo TextLCD WebSocketClient mbed-rtos mbed

Revision:
0:f8f8433058d6
Child:
1:00c0039f4309
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Dec 18 10:35:34 2014 +0000
@@ -0,0 +1,476 @@
+#include "mbed.h"
+#include "TextLCD.h"
+#include "Servo.h"
+#include "ds1307.h"
+#include <time.h>  
+#include "EthernetInterface.h"
+#include "Websocket.h"
+
+
+DigitalOut myled(LED1);
+AnalogIn LM35_1(p15);
+AnalogIn LM35_2(p16);
+AnalogIn LM35_3(p17);
+AnalogIn LM35_4(p18);
+AnalogIn Potmeter(p19); 
+Servo myservo(p24);
+TextLCD lcd(p12,p11,p8,p7,p6,p5);
+DigitalOut Relais(p13);
+PwmOut b (p21);
+PwmOut g (p22);
+PwmOut r (p23);
+DigitalIn SW_1(p30);
+DigitalIn SW_2(p29);
+DigitalIn SW_3(p28);
+DigitalIn SW_4(p27);
+DS1307 my1307(p9,p10);
+
+float duty_red,duty_green;
+float LMTemp_1,LMTemp_2,LMTemp_3,LMTemp_4;
+int menu = 0;
+float instelliving = 20;
+float instelkeuken = 20;
+float instelslaap = 20;
+int sec,min,uur,dag,datum,maand,jaar;
+int startsec = 0;
+int startmin = 0;
+int startuur = 7; 
+int stopsec = 0;
+int stopmin = 0;
+int stopuur = 23;
+
+int timerwijzigen = 0;
+int flikertimer = 0;
+int wijzigmenustart = 0;
+int wijzigmenustop = 0;
+float stooklijn = 0;
+int menubuffer = 0;
+int showtimer = 0;
+bool showstooklijn = false;
+float hysteresis = 1.5;
+bool hysteresisbuffer[3];
+float position = 0;
+bool aan = true;
+//------------------------------------------------------------------- 
+void onoff(int status){
+    if (status >= 1){
+        Relais = 1;
+            duty_red=duty_red+0.0005;
+            duty_green=duty_green-0.0005;
+            if (duty_red >= 1) (duty_red = 1);
+            if (duty_green <= 0) (duty_green = 0);
+            r.write(duty_red);
+            g.write(duty_green);   
+        }
+    else {
+        Relais = 0;            
+            duty_red=duty_red-0.0005;
+            duty_green=duty_green+0.0005;
+            if (duty_red <= 0) (duty_red = 0);
+            if (duty_green >=1) (duty_green = 1);
+            r.write(duty_red);
+            g.write(duty_green);
+         }
+ }
+ //-----------------------------------------------------------------
+ float LM35 (int sensor){
+     float waardes[100] = {0};
+     float gemiddelde = 0;
+     float temperatuur = 0;
+     
+     for(int i=0;i<100;i++){
+         if (sensor == 1) (waardes[i] = LM35_1.read());
+         if (sensor == 2) (waardes[i] = LM35_2.read());
+         if (sensor == 3) (waardes[i] = LM35_3.read());
+         if (sensor == 4) (waardes[i] = LM35_4.read());
+         wait(0.00001);
+         }
+    
+    for(int i=0;i<100;i++){
+        gemiddelde = gemiddelde + waardes[i];
+        }
+    
+    temperatuur = ((gemiddelde /100) * 368.5503686)- 6; //368
+    return temperatuur;
+} 
+//------------------------------------------------------------------
+float stooklijnfunctie (float huidige){
+   
+     float analoog = Potmeter * 3;
+    if (huidige - analoog > 0.1 | analoog - huidige > 0.1){
+        if(showstooklijn == false) (menubuffer = menu);
+        showstooklijn = true;
+        menu = 8;
+    }
+    else (analoog = huidige);
+    
+    if (showstooklijn == true & menu != 8)(showstooklijn = false);
+    if (showstooklijn == true & menu == 8){
+     showtimer = showtimer + 1;
+     if (showtimer > 3000) {
+        menu = menubuffer;
+        showstooklijn = false;
+        showtimer = 0;
+        }
+    }
+    
+   
+    return analoog;
+}
+//------------------------------------------------------------------
+const char* dagen (int dag){
+    const char* dagbenaming ="";
+    if (dag == 1) (dagbenaming = "Ma");
+    else if (dag == 2) (dagbenaming = "Di");
+    else if (dag == 3) (dagbenaming = "Wo");
+    else if (dag == 4) (dagbenaming = "Do");
+    else if (dag == 5) (dagbenaming = "Vr");
+    else if (dag == 6) (dagbenaming = "Za");
+    else if (dag == 7) (dagbenaming = "Zo");    
+    
+return dagbenaming;
+    }
+
+ //-----------------------------------------------------------------
+ int aantalwarmtevraag (){
+     int aantal =0; 
+     if (LMTemp_1 < (instelliving - (hysteresis /2)) & hysteresisbuffer[0] == false) (hysteresisbuffer[0] = true);
+     if (LMTemp_1 < (instelliving - (hysteresis /2)) | hysteresisbuffer[0] == true ) (aantal = aantal + 1);
+     if (LMTemp_1 > (instelliving + (hysteresis /2))) (hysteresisbuffer[0] = false);
+     
+     if (LMTemp_2 < (instelkeuken - (hysteresis /2)) & hysteresisbuffer[1] == false) (hysteresisbuffer[1] = true);
+     if (LMTemp_2 < (instelkeuken - (hysteresis /2)) | hysteresisbuffer[1] == true ) (aantal = aantal + 1);
+     if (LMTemp_2 > (instelkeuken + (hysteresis /2))) (hysteresisbuffer[1] = false);
+     
+     if (LMTemp_3 < (instelslaap - (hysteresis /2)) & hysteresisbuffer[2] == false) (hysteresisbuffer[2] = true);
+     if (LMTemp_3 < (instelslaap - (hysteresis /2)) | hysteresisbuffer[2] == true ) (aantal = aantal + 1);
+     if (LMTemp_3 > (instelslaap + (hysteresis /2))) (hysteresisbuffer[2] = false);
+    
+    if (uur == startuur & min == startmin & sec == startsec) (aan = true);
+    if (uur == stopuur & min == stopmin & sec == stopsec) (aan = false);
+    if (aan == false) (aantal = 0);
+    return aantal;
+     }
+ //-----------------------------------------------------------------
+ void servoposition (int aantalwarmtevraag, float stooklijn){
+     float graden = -105 + (aantalwarmtevraag * 40);
+      graden = graden + (stooklijn * 25);
+      if (graden >= 90) (graden = 90);
+      if (aantalwarmtevraag == 0) (graden = -105);
+      if (stooklijn <= 0.09) (graden = -105);
+      myservo.position(graden);
+    
+     }
+ //*****************************************************************
+ //*******************************Main******************************
+ //*****************************************************************
+int main() {
+
+lcd.cls();
+lcd.locate(0,0);
+lcd.printf("Welkom");                                                   //Geef een welkomsboodschap weer op het LCD wanneer.
+lcd.locate(0,1);
+lcd.printf("Initialiseren");    
+    
+    
+myservo.calibrate(0.0009, 105);                                         //Geef het middenpunt van de servo aan (servo is niet 100% gecentreerd daarom 105°)
+//---------------Websocket--------------------------
+char json_str[1000];                                                    //Maak een buffer aan voor de te verzenden karakters
+ 
+    EthernetInterface eth;                                              //Declareer de ethernet interface
+    eth.init();                                                         //Use DHCP ip
+    eth.connect();                                                      //Maak conectie met het gekregen ip adress
+    
+    
+    Websocket ws("ws://sockets.mbed.org:443/ws/ProjectEmbeddedC/wo");   //Server mbed
+    ws.connect();                                                       //Connectie maken met de server
+    wait(2.0);                                                          //Wacht even totodat de mbed conectie heeft gemaakt met de server
+//--------------------------------------------------
+
+int timer = 0; 
+int timerweb = 0;
+  
+    while(1) {
+          
+timer = timer + 1;
+if (timer >= 250){
+timer = 0;
+ timerweb = timerweb + 1;
+ if (timerweb >= 20){
+ timerweb = 0;
+    
+//-----------------------------------------------------------------
+sprintf(json_str,"------------------------------------------------------------------- \r\n ");
+ws.send(json_str);
+sprintf(json_str, "Starttijd: %.2D:%.2D:%.2D \r\n        Stoptijd: %.2D:%.2D:%.2D \r\n",startuur,startmin,startsec,stopuur,stopmin,stopsec);
+ws.send(json_str);
+sprintf(json_str, "Hysteresis: %.1f °C \r\n",hysteresis);
+ws.send(json_str);
+sprintf(json_str, "Huidige stooklijn: %.1f °C/uur \r\n",stooklijn);
+ws.send(json_str);
+sprintf(json_str, "Huidige buiten temperatuur: %.1f °C \r\n ",LMTemp_4);
+ws.send(json_str);
+sprintf(json_str, "Huidige temperuur slaapkamer: %.1f °C \r\n        Gewenst %.1f °C \r\n ",LMTemp_3,instelslaap);
+ws.send(json_str);
+sprintf(json_str, "Huidige temperuur keuken: %.1f °C \r\n        Gewenst %.1f °C \r\n ",LMTemp_2,instelkeuken);
+ws.send(json_str);
+sprintf(json_str, "Huidige temperuur living: %.1f °C \r\n        Gewenst %.1f °C \r\n ",LMTemp_1,instelliving);
+ws.send(json_str);
+sprintf(json_str, "%s %.2D-%.2D-%.2D %.2D:%.2D:%.2D \r\n ",dagen(dag),datum,maand,jaar,uur,min,sec);
+ws.send(json_str);
+
+        myled = 1;
+}
+//-----------------------------------------------------------------
+    
+    
+    
+    
+    my1307.gettime( &sec, &min, &uur, &dag, &datum, &maand, &jaar);
+    
+   
+       LMTemp_1 = LM35(1);
+       LMTemp_2 = LM35(2);
+       LMTemp_3 = LM35(3);
+       LMTemp_4 = LM35(4);
+        
+        if (SW_1 == true){
+            menu = menu + 1;
+            if (menu > 11) (menu = 0);
+        }
+            
+        if (SW_2 == true){
+            menu =menu - 1;
+            if (menu < 0) (menu = 11);
+        }
+        
+       if (menu == 0){
+       lcd.cls();
+       lcd.locate(4,0);
+       lcd.printf("%.2D:%.2D:%.2D",uur,min,sec);
+       lcd.locate(3,1);
+       lcd.printf("%s %.2D-%.2D-%.2D",dagen(dag),datum,maand,jaar);
+       
+       }
+       
+       
+       else if (menu == 1){
+       lcd.cls();
+       lcd.locate(0,0);
+       lcd.printf("Temperatuur");
+       lcd.locate(0,1);
+       lcd.printf("Living: %.1f C",LMTemp_1);
+       }
+       
+       else if (menu == 2){
+       lcd.cls();
+       lcd.locate(0,0);
+       lcd.printf("Temperatuur");
+       lcd.locate(0,1);
+       lcd.printf("Keuken: %.1f C",LMTemp_2);
+       }
+       
+        else if (menu == 3){
+       lcd.cls();
+       lcd.locate(0,0);
+       lcd.printf("Temperatuur");
+       lcd.locate(0,1);
+       lcd.printf("Slaap: %.1f C",LMTemp_3);
+       }
+       
+       else if (menu == 4){
+       lcd.cls();
+       lcd.locate(0,0);
+       lcd.printf("Temperatuur");
+       lcd.locate(0,1);
+       lcd.printf("Buiten: %.1f C",LMTemp_4);
+       }
+       
+       else if (menu == 5){
+       lcd.cls();
+       lcd.locate(0,0);
+       lcd.printf("Gewenste temp");
+       lcd.locate(0,1);
+       lcd.printf("Living: %.1f C",instelliving);
+            
+            if (SW_3 == true & instelliving < 30) (instelliving = instelliving + 0.1);
+            if (SW_4 == true & instelliving > 0) (instelliving = instelliving - 0.1);
+       }
+       
+       else if (menu == 6){
+       lcd.cls();
+       lcd.locate(0,0);
+       lcd.printf("Gewenste temp");
+       lcd.locate(0,1);
+       lcd.printf("Keuken: %.1f C",instelkeuken);
+            
+            if (SW_3 == true & instelkeuken < 30) (instelkeuken = instelkeuken + 0.1);
+            if (SW_4 == true & instelkeuken > 0) (instelkeuken = instelkeuken - 0.1);
+       }
+       
+       else if (menu == 7){
+       lcd.cls();
+       lcd.locate(0,0);
+       lcd.printf("Gewenste temp");
+       lcd.locate(0,1);
+       lcd.printf("Slaap: %.1f C",instelslaap);
+            
+            if (SW_3 == true & instelslaap < 30) (instelslaap = instelslaap + 0.1);
+            if (SW_4 == true & instelslaap > 0) (instelslaap = instelslaap - 0.1);
+       }
+       
+       else if (menu == 8){
+       lcd.cls();
+       lcd.locate(0,0);
+       lcd.printf("Stooklijn");
+       lcd.locate(0,1);
+       lcd.printf("%.1f C/uur",stooklijn);
+       wijzigmenustart = 0;
+       }
+       
+       else if (menu == 9){
+       lcd.cls();
+       lcd.locate(0,0);
+       lcd.printf("Hysteresis");
+       lcd.locate(0,1);
+       lcd.printf("%.1f C",hysteresis);
+       wijzigmenustart = 0;
+       
+       if (SW_3 == true & hysteresis < 5) (hysteresis = hysteresis + 0.1);
+       if (SW_4 == true & hysteresis > 0) (hysteresis = hysteresis - 0.1);
+       }
+//----------------Menu 10--------------------------------     
+       else if (menu == 10){
+           lcd.cls();
+           lcd.locate(0,0);
+           lcd.printf("Starttijd");
+               if (wijzigmenustart == 0 | flikertimer >= 3){
+               lcd.locate(0,1);
+               lcd.printf("%.2D:%.2D:%.2D",startuur,startmin,startsec);
+               }
+            timerwijzigen = timerwijzigen + 1;
+                if (!(SW_3 == true && SW_4 == true & 1)) (timerwijzigen = 0);
+            if (timerwijzigen == 3)(wijzigmenustart = wijzigmenustart + 1);
+            if (wijzigmenustart > 3) (wijzigmenustart = 0);
+         
+            if (wijzigmenustart == 1){
+            flikertimer = flikertimer + 1;
+                if (flikertimer <= 4) {
+                lcd.locate(0,0);
+                lcd.printf("Starttijd");
+                lcd.locate(0,1);
+                lcd.printf("  :%.2D:%.2D",startmin,startsec);//,startmin,startsec); 
+                }
+                if (flikertimer > 5) (flikertimer = 0);             
+            if (SW_3 == true) (startuur = startuur + 1);
+            if (SW_4 == true) (startuur = startuur - 1);
+            if (startuur > 23) (startuur = 0);
+            if (startuur <= 0) (startuur = 23);
+            }
+            
+            if (wijzigmenustart == 2){
+            flikertimer = flikertimer + 1;
+                if (flikertimer <= 4) {
+                lcd.locate(0,0);
+                lcd.printf("Starttijd");
+                lcd.locate(0,1);
+                lcd.printf("%.2D:  :%.2D",startuur,startsec);//,startmin,startsec); 
+                }
+                if (flikertimer > 5) (flikertimer = 0);            
+            if (SW_3 == true) (startmin = startmin + 1);
+            if (SW_4 == true) (startmin = startmin - 1);
+            if (startmin > 59) (startmin = 0);
+            if (startmin <= 0) (startmin = 59);
+            }                        
+          if (wijzigmenustart == 3){
+            flikertimer = flikertimer + 1;
+                if (flikertimer <= 4) {
+                lcd.locate(0,0);
+                lcd.printf("Starttijd");
+                lcd.locate(0,1);
+                lcd.printf("%.2D:%.2D:  ",startuur,startmin);//,startmin,startsec); 
+                }
+                if (flikertimer > 5) (flikertimer = 0);           
+            if (SW_3 == true) (startsec = startsec + 1);
+            if (SW_4 == true) (startsec = startsec - 1);
+            if (startsec > 59) (startsec = 0);
+            if (startsec <= 0) (startsec = 59);
+            }
+            wijzigmenustop = 0;
+    }
+//---------------------------- Menu 11---------------------------------       
+       else if (menu == 11){
+           lcd.cls();
+           lcd.locate(0,0);
+           lcd.printf("Stoptijd");
+               if (wijzigmenustop == 0 | flikertimer >= 3){
+               lcd.locate(0,1);
+               lcd.printf("%.2D:%.2D:%.2D",stopuur,stopmin,stopsec);
+               }
+            timerwijzigen = timerwijzigen + 1;
+                if (!(SW_3 == true && SW_4 == true & 1)) (timerwijzigen = 0);
+            if (timerwijzigen == 3)(wijzigmenustop = wijzigmenustop + 1);
+            if (wijzigmenustop > 3) (wijzigmenustop = 0);
+         
+            if (wijzigmenustop == 1){
+            flikertimer = flikertimer + 1;
+                if (flikertimer <= 4) {
+                lcd.locate(0,0);
+                lcd.printf("Stoptijd");
+                lcd.locate(0,1);
+                lcd.printf("  :%.2D:%.2D",stopmin,stopsec); 
+                }
+                if (flikertimer > 5) (flikertimer = 0);             
+            if (SW_3 == true) (stopuur = stopuur + 1);
+            if (SW_4 == true) (stopuur = stopuur - 1);
+            if (stopuur > 23) (stopuur = 0);
+            if (startuur <= 0) (startuur = 23);
+            }
+            
+            if (wijzigmenustop == 2){
+            flikertimer = flikertimer + 1;
+                if (flikertimer <= 4) {
+                lcd.locate(0,0);
+                lcd.printf("Stoptijd");
+                lcd.locate(0,1);
+                lcd.printf("%.2D:  :%.2D",stopuur,stopsec);
+                }
+                if (flikertimer > 5) (flikertimer = 0);            
+            if (SW_3 == true) (stopmin = stopmin + 1);
+            if (SW_4 == true) (stopmin = stopmin - 1);
+            if (stopmin > 59) (stopmin = 0);
+            if (startmin <= 0) (startmin = 59);
+            }                        
+          if (wijzigmenustop == 3){
+            flikertimer = flikertimer + 1;
+                if (flikertimer <= 4) {
+                lcd.locate(0,0);
+                lcd.printf("Stoptijd");
+                lcd.locate(0,1);
+                lcd.printf("%.2D:%.2D:  ",stopuur,stopmin);
+                }
+                if (flikertimer > 5) (flikertimer = 0);           
+            if (SW_3 == true) (stopsec = stopsec + 1);
+            if (SW_4 == true) (stopsec = stopsec - 1);
+            if (stopsec > 59) (stopsec = 0);
+            if (startsec <= 0) (startsec = 59);
+            }
+            wijzigmenustart = 0;
+    }
+    
+       
+       
+       } 
+
+
+      stooklijn = stooklijnfunctie(stooklijn);
+    // int test = SW_4;
+     onoff(aantalwarmtevraag());
+     servoposition(aantalwarmtevraag(),stooklijn);
+     
+        
+     wait(0.001);
+     
+    }
+}
+