Jonathan Caes / InetTest-8d87bc453349

Dependencies:   DS1307 TextLCD mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 //library's
00002 #include "mbed.h"
00003 #include "TextLCD.h"
00004 #include "ds1307.h"
00005 #include "EthernetNetIf.h"
00006 //#include "HTTPClient.h"
00007 //#include "NTPClient.h"
00008 #include "HTTPServer.h"
00009 #include "RPCFunction.h"
00010 //Pin initialisatie
00011 AnalogIn binnen(p19);//pin 19:Analog In
00012 AnalogIn buiten(p20);//pin 20:Analog In
00013 TextLCD lcd(p15, p16, p21, p22, p23, p24); //rs, e, d4-d7
00014 InterruptIn knopUP(p6);//pin6:Interrupt
00015 InterruptIn knopDOWN(p5);//pin5:Interrupt
00016 InterruptIn knopINSTEL(p7);//pin7:Interrupt
00017 InterruptIn knopBACK(p8);//pin8:Interrupt
00018 DS1307 my1307(p9, p10);//pin9:sda I2C
00019 DigitalOut relais(p30);//pin10:scl I2C
00020 DigitalOut RGBblauw(p29);//pin29:digital out
00021 DigitalOut RGBgroen(p28);//pin28:digital out
00022 DigitalOut RGBrood(p27);//pin27:digital out
00023 PwmOut Servo(p25);//p25:pwm out
00024 Serial pc(USBTX,USBRX);//serial port:USB
00025 /* HTTP Instellingen */
00026 EthernetNetIf eth(                                                                  //We gaan de functie van ethernet opendoen om hierin de gewenste adressen in te kunnen gaan voegen.
00027     IpAddr(192,168,1,2),                                                            //Ip adres toedienen waarop de MBED verbonden word.
00028     IpAddr(255,255,255,0),                                                          //Subnetmasker voor de MBED.
00029     IpAddr(192,168,1,1),                                                            //Het IP adres waarop we gaan schrijven.
00030     IpAddr(192,168,1,1));                                                           //Het IP adres waarvan we gaan lezen.
00031     HTTPServer svr;                                                                 // Opstarten van de HTTP server met de bovenliggende eigenschappen.
00032 /* END HTTP */
00033 //variabelen
00034 float i = 0;//waarde om binnentempertauur in te steken
00035 float j = 0;//waarde om buitentemperatuur in te steken
00036 int reftemp = 20;//referentietemperatuur
00037 int load=0;//load program
00038 int tellertijd =0;//uit tijdsuitlezing te geraken
00039 int c=0;
00040 //variabelen RTC
00041  int sec;
00042  int minu;
00043  int hours;
00044  int day;
00045  int date;
00046  int month;
00047  int year;
00048  //RTC read routine
00049  void test_rw(int test) {
00050 } 
00051 //interruptroutine: referentietemp instellen
00052 void instellen()
00053 {
00054     knopDOWN.rise(NULL);
00055     knopUP.rise(NULL);//interrupt disable
00056     wait(0.5);
00057     while(knopINSTEL == 0)
00058     {
00059         lcd.locate(0,0);
00060         lcd.printf(" Instellen Temp");
00061         lcd.locate(0,1);
00062         lcd.printf("   Temp = %iC", reftemp);//Printen ingestelde referentietemp
00063         if(knopUP == 1)//knopUP bediend --> reftemp +1
00064         {
00065             reftemp++;
00066             wait(0.4);
00067         }
00068         if(knopDOWN == 1)//knopDOWN bediend --> reftemp -1
00069         {
00070             reftemp--;
00071             wait(0.4);
00072         }
00073         if (reftemp == 33)//hysteresis reftemp
00074         {
00075             reftemp = 15;
00076         }
00077         if (reftemp == 14)//hysteresis reftemp
00078         {
00079             reftemp = 32;
00080         }
00081     }
00082 }
00083 //interruptroutine: tijdsweergave
00084 void back()
00085 {
00086     wait(0.3);
00087     tellertijd=0;//dit is een hulp, zodat de tijdsweergave na een bepaalde teruggaat naar het basisscherm
00088     lcd.cls();//LCD clead
00089     knopBACK.rise(NULL);
00090     knopUP.rise(NULL);//disable interrupt
00091     while(tellertijd < 50)//doorolopen wanneer knopBack NIET is ingedrukt,bij bediening uit routine
00092     {
00093         test_rw(my1307.gettime( &sec, &minu, &hours, &day, &date, &month, &year));//uitlezen tijd RTC
00094         //Dag uitschrijven op lcd
00095         if(day == 1)
00096         {lcd.locate(3,1);
00097         lcd.printf("Ma");}
00098         if(day == 2)
00099         {lcd.locate(3,1);
00100         lcd.printf("Di");}
00101         if(day == 3)
00102         {lcd.locate(3,1);
00103         lcd.printf("Wo");}
00104         if(day == 4)
00105         {lcd.locate(3,1);
00106         lcd.printf("Do");}
00107         if(day == 5)
00108         {lcd.locate(3,1);
00109         lcd.printf("Vr");}
00110         if(day == 6)
00111         {lcd.locate(3,1);
00112         lcd.printf("Za");}
00113         if(day == 7)
00114         {lcd.locate(3,1);
00115         lcd.printf("Zo");}
00116         test_rw(my1307.gettime( &sec, &minu, &hours, &day, &date, &month, &year));//uitlezen tijd RTC
00117         //Uur min sec //Dag datum maand jaar
00118                 if(sec >= 9 && minu >= 9 && hours >= 9)
00119                 {
00120                     lcd.locate(4,0);
00121                     lcd.printf("%d:%d:%d",hours,minu,sec);
00122                     lcd.locate(6,1);
00123                     lcd.printf("%d/%d/%d",date,month,year);
00124                 }
00125                 if(sec <= 9 && minu >= 9 && hours >= 9)
00126                 {
00127                     lcd.locate(4,0);
00128                     lcd.printf("%d:%d:0%d",hours,minu,sec);
00129                     lcd.locate(6,1);
00130                     lcd.printf("%d/%d/%d",date,month,year);
00131                 }
00132                 if(sec >= 9 && minu <= 9 && hours >= 9)
00133                 {
00134                     lcd.locate(4,0);
00135                     lcd.printf("%d:0%d:%d",hours,minu,sec);
00136                     lcd.locate(6,1);
00137                     lcd.printf("%d/%d/%d",date,month,year);
00138                 }
00139                 if(sec >= 9 && minu >= 9 && hours <= 9)
00140                 {
00141                     lcd.locate(4,0);
00142                     lcd.printf("0%d:%d:%d",hours,minu,sec);
00143                     lcd.locate(6,1);
00144                     lcd.printf("%d/%d/%d",date,month,year);
00145                 }
00146                 if(sec <= 9 && minu <= 9 && hours >= 9)
00147                 {
00148                     lcd.locate(4,0);
00149                     lcd.printf("%d:0%d:0%d",hours,minu,sec);
00150                     lcd.locate(6,1);
00151                     lcd.printf("%d/%d/%d",date,month,year);
00152                 }
00153                 if(sec <= 9 && minu >= 9 && hours <= 9)
00154                 {
00155                     lcd.locate(4,0);
00156                     lcd.printf("0%d:%d:0%d",hours,minu,sec);
00157                     lcd.locate(6,1);
00158                     lcd.printf("%d/%d/%d",date,month,year);
00159                 }
00160                 if(sec >= 9 && minu <= 9 && hours <= 9)
00161                 {
00162                     lcd.locate(4,0);
00163                     lcd.printf("0%d:0%d:%d",hours,minu,sec);
00164                     lcd.locate(6,1);
00165                     lcd.printf("%d/%d/%d",date,month,year);
00166                 }
00167                 if(sec <= 9 && minu <= 9 && hours <= 9)
00168                 {
00169                     lcd.locate(4,0);
00170                     lcd.printf("0%d:0%d:0%d",hours,minu,sec);
00171                     lcd.locate(6,1);
00172                     lcd.printf("%d/%d/%d",date,month,year);
00173                 }
00174                 wait(0.2);
00175                 tellertijd++;
00176     }
00177 }
00178 //interruptroutine: start programma
00179 void start()
00180 {
00181     knopINSTEL.rise(NULL);
00182     knopBACK.rise(NULL);
00183     knopDOWN.rise(NULL);
00184     knopUP.rise(NULL);//disable interrupt
00185     lcd.cls();//LCD clear
00186     /*OPSTART ROUTINE START*/
00187     lcd.locate(0,0);
00188     lcd.printf("SYSTEM START");
00189     lcd.locate(0,1);
00190     lcd.printf("LOAD PROGRAM");
00191     wait(2);
00192     lcd.cls();
00193     lcd.locate(0,0);
00194     lcd.printf("SYSTEM START");
00195     lcd.locate(0,1);
00196     lcd.printf("LOAD: %d",load);
00197     for(int l=0;l<=40;l++)
00198     {
00199         load = l;
00200         lcd.locate(0,1);
00201         lcd.printf("LOAD: %d",load);
00202         wait(0.1);
00203     }
00204     wait(1);
00205     for(int l=40;l<=70;l++)
00206     {
00207         load = l;
00208         lcd.locate(0,1);
00209         lcd.printf("LOAD: %d",load);
00210         wait(0.2);
00211     }
00212     wait(1);
00213     for(int l=70;l<=100;l++)
00214     {
00215         load = l;
00216         lcd.locate(0,1);
00217         lcd.printf("LOAD: %d",load);
00218         wait(0.2);
00219     }
00220     wait(1);
00221     lcd.cls();
00222     lcd.locate(0,0);
00223     lcd.printf("SYSTEM START");
00224     lcd.locate(0,1);
00225     lcd.printf("PROGRAM START");
00226     wait(3);
00227     /*OPSTARTROUTINE END*/
00228 }
00229 //interruptroutine: programma stoppen
00230 void afsluiten()
00231 {
00232     knopUP.rise(&start);//interrupt koppelen met routine
00233     knopINSTEL.rise(NULL);
00234     knopBACK.rise(NULL);
00235     knopDOWN.rise(NULL);//disabel interrupt
00236     lcd.cls();//lcd clear
00237     /*AFSLUITROUTINE START*/
00238     lcd.locate(0,0);
00239     lcd.printf("SYSTEM SHUTDOWN");
00240     wait(2);
00241     relais = 0;//relais uit
00242     RGBrood = 0;
00243     RGBgroen = 0;
00244     RGBblauw = 0; //RGB uit
00245     while(knopUP == 0)
00246     {
00247         lcd.cls();
00248         lcd.locate(0,0);
00249         lcd.printf("SYSTEM OFF");
00250         wait(0.2);
00251     }
00252     /*AFSLUITROUTINE END*/
00253 }
00254 /*MAIN PROGRAM*/
00255 int main()
00256 {
00257           
00258     wait(1);
00259     while(1) 
00260     {   EthernetErr ethErr;
00261         int count = 0;
00262         do {
00263          printf("Setting up %d...\n\r", ++count);
00264             ethErr = eth.setup();
00265             if (ethErr) printf("Timeout\n\r", ethErr);
00266          } while (ethErr != ETH_OK);
00267 
00268         printf("Connected OK\n\r");
00269         const char* hwAddr = eth.getHwAddr();
00270 
00271          IpAddr ethIp = eth.getIp();
00272             printf("IP address : %d.%d.%d.%d\n\r", ethIp[0], ethIp[1], ethIp[2], ethIp[3]);
00273             
00274             LocalFileSystem fs("webfs");
00275             
00276             svr.addHandler<SimpleHandler>("/hello");
00277             svr.addHandler<FSHandler>("");
00278             
00279     svr.bind(80);
00280     printf("Server listening\n\r");
00281         knopINSTEL.rise(&instellen);
00282         knopBACK.rise(&back);
00283         knopDOWN.rise(&afsluiten);//interrupts koppelen met routines(bovenaan gedeclareerd)
00284         i=0;
00285         j=0;//variabele temperatuurwaardes 0 zetten
00286         for(int a=0;a<5;a++)
00287         {
00288             for(int t=0;t<100;t++) 
00289             {
00290                 i=i+binnen.read(); //uitlezen temp 
00291             }
00292             for(int k=0;k<100;k++) 
00293             {
00294                 j=j+buiten.read(); //uitlezen temp 
00295             }
00296             wait(0.1);
00297         }
00298         i = (i*3.3)/5;
00299         j = (j*3.3)/5;//berekening juiste temperatuur: Waarde*3.3V/5(for lus van 5)
00300         lcd.cls();//lcd clear
00301         lcd.locate(0,0);
00302         lcd.printf("Binnen:%4.1fC", i);
00303         lcd.locate(0,1);
00304         lcd.printf("Buiten:%4.1fC", j);//LCD print temperaturen
00305         wait(1);
00306         pc.printf("\n\n\rTemperatuur binnen: %4.1f C",i);
00307         pc.printf("\n\n\rTemperatuur buiten: %4.1f C",j);
00308         pc.printf("\n\n\r------------------------------------------");//PC print temperaturen
00309         wait(0.5);
00310         if(i > (reftemp + 2) && i <= (reftemp +4))//Temperatuur te HOOG: reftemp >2 en reftemp <=4
00311         {
00312             wait(2);
00313             relais = 1;//relais aan
00314             RGBrood = 1;
00315             RGBgroen = 1;
00316             RGBblauw = 0;//rgb rood + groen = oranje
00317             wait(1);
00318             Servo.period_ms(20);//servo pwm periode
00319             Servo = (130 * 0.000511 + 0.026); // rechts-half servo
00320         }
00321         else if(i > (reftemp + 3.5))//Temperatuur te HOOG: reftemp > 3.5 (hysteresis ingebouwd)
00322         {
00323             wait(2);
00324             relais = 1;//relais aan
00325             RGBrood = 1;
00326             RGBgroen = 0;
00327             RGBblauw = 0;//rgb rood
00328             wait(1);
00329             Servo.period_ms(20);//servo pwm periode
00330             Servo = (160 * 0.000511 + 0.026); // rechts servo
00331         }
00332         else if(i < (reftemp - 2) && i >= (reftemp -4))//Temperatuur te LAAG: reftemp>2 & reftemp <4
00333         {
00334             wait(2);
00335             relais = 0;//relais uit
00336             RGBrood = 1;
00337             RGBgroen = 0;
00338             RGBblauw = 1;//rgb rood + blauw= paars
00339             wait(1);
00340             Servo.period_ms(20);//servo pwm periode
00341             Servo = (55 * 0.000511 + 0.026); // links-half servo
00342         }
00343         else if(i < (reftemp - 3.5))//Temperatuur te LAAG: reftemp>4
00344         {
00345             wait(2);
00346             relais = 0;//relais uit
00347             RGBrood = 0;
00348             RGBgroen = 0;
00349             RGBblauw = 1;//rgb blauw
00350             wait(1);
00351             Servo.period_ms(20);//servo pwm period
00352             Servo = (5 * 0.000511 + 0.026); // links servo
00353         }
00354         else//Temperatuur OKE: reftemp binnen hysteresis +2 & -2
00355         { 
00356             wait(1);
00357             relais = 0;
00358             RGBrood = 0;
00359             RGBgroen = 1;
00360             RGBblauw = 0;//rgb groen
00361             wait(1);
00362             Servo.period_ms(20);//servo pwm period
00363             Servo = (90 * 0.000511 + 0.026); // 0-positie servo
00364         }
00365         wait(1);
00366         printf("Doorlopen\r\n");
00367         FILE *fp = fopen("/webfs/mydata.htm","w");
00368         fprintf(fp,"<HTML>\r\n");                                               //HTML opstarten
00369         fprintf(fp,"<HEAD>\r\n");                                               //Head van het bestand open zetten.
00370         fprintf(fp,"<script type=\"text/javascript\">window.setTimeout(function(){ document.location.reload(true); }, 3000);</script>");//Script laden  
00371         fprintf(fp,"<link rel=\"stylesheet\" href=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css\">");//Stijl van het document laden
00372         fprintf(fp,"<style>\r\n");//De style toepassen die is opgevraagd          
00373         fprintf(fp,"tbody {background:#FF8866;}");//Achtergrond veranderen van kleur
00374         fprintf(fp,"</style>\r\n");//De aangemaakt stijl terug sluiten
00375         fprintf(fp,"</HEAD>\r\n");//Sluiten van de HEAD
00376         fprintf(fp,"<BODY>\r\n");//Sluiten van de BODY
00377         fprintf(fp,"<div class=\"container\">\r\n");      
00378         fprintf(fp,"<div class=\"panel panel-success\">");
00379         fprintf(fp,"<table class=\"table\">");//Aanmaken van tabel
00380         fprintf(fp,"<thead><tr><th>binnen</th><th>buiten</th></tr></thead>"); 
00381         fprintf(fp,"<tbody><tr><td>%.1f &deg;C</td>",i);
00382         fprintf(fp,"<td>%.1f &deg;C</td></tr></tbody></table></div>",j);
00383         fprintf(fp,"</div>");
00384         printf("Variabelen printen\r\n");
00385         fprintf(fp,"</BODY>\r\n");
00386         fprintf(fp,"</HTML>\r\n");
00387         printf(" %f + %f",i,j);
00388         printf("Sessie stoppen \r\n"); 
00389         fclose(fp);
00390         wait(1);
00391     }
00392 }