Nino Der Kinderen / Mbed 2 deprecated InetTest

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