Counter

Dependencies:   EthernetInterface NTPClient SDFileSystem TextLCD WebSocketClient mbed-rtos mbed Socket lwip-eth lwip-sys lwip FATFileSystem

Committer:
Tuxitheone
Date:
Mon Feb 29 18:59:15 2016 +0000
Revision:
0:ecaf3e593122
TankCounter

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Tuxitheone 0:ecaf3e593122 1 #include "mbed.h"
Tuxitheone 0:ecaf3e593122 2 #include "EthernetInterface.h"
Tuxitheone 0:ecaf3e593122 3 #include "NTPClient.h"
Tuxitheone 0:ecaf3e593122 4 #include "TextLCD.h"
Tuxitheone 0:ecaf3e593122 5 #include "Websocket.h"
Tuxitheone 0:ecaf3e593122 6 #include "Counter.h"
Tuxitheone 0:ecaf3e593122 7 #include "SDFileSystem.h"
Tuxitheone 0:ecaf3e593122 8 #include "uplink.h"
Tuxitheone 0:ecaf3e593122 9
Tuxitheone 0:ecaf3e593122 10 TextLCD lcd(p20, p19, p18, p17, p16, p15,TextLCD::LCD20x4); // rs, e, d4-d7
Tuxitheone 0:ecaf3e593122 11 SDFileSystem sd(p5, p6, p7, p8, "sd"); // the pinout on the mbed Cool Components workshop board
Tuxitheone 0:ecaf3e593122 12
Tuxitheone 0:ecaf3e593122 13 NTPClient ntp;
Tuxitheone 0:ecaf3e593122 14 int offset_DK = 7200;
Tuxitheone 0:ecaf3e593122 15 Counter T1(p22);
Tuxitheone 0:ecaf3e593122 16 AnalogIn tempsensor(p16);
Tuxitheone 0:ecaf3e593122 17
Tuxitheone 0:ecaf3e593122 18
Tuxitheone 0:ecaf3e593122 19 float ReadTempetur(void)
Tuxitheone 0:ecaf3e593122 20 {
Tuxitheone 0:ecaf3e593122 21 float tempreading=(tempsensor.read()*3.3-0.25)/0.028;
Tuxitheone 0:ecaf3e593122 22 return tempreading;
Tuxitheone 0:ecaf3e593122 23 }
Tuxitheone 0:ecaf3e593122 24
Tuxitheone 0:ecaf3e593122 25 float TankContens()
Tuxitheone 0:ecaf3e593122 26 {
Tuxitheone 0:ecaf3e593122 27 //beregner hvor meget der er tilbage
Tuxitheone 0:ecaf3e593122 28 return 1000-T1.Flow();
Tuxitheone 0:ecaf3e593122 29 }
Tuxitheone 0:ecaf3e593122 30
Tuxitheone 0:ecaf3e593122 31 void InitLog(void) {
Tuxitheone 0:ecaf3e593122 32 FILE *fp; //Logfile
Tuxitheone 0:ecaf3e593122 33 fp = fopen("/sd/taptraq/Tank.log", "r");
Tuxitheone 0:ecaf3e593122 34 if (fp == NULL) //For at være sikker på at der er en fil og skrive i
Tuxitheone 0:ecaf3e593122 35 fp = fopen("/sd/taptraq/Tank.log", "w");
Tuxitheone 0:ecaf3e593122 36 fclose(fp);
Tuxitheone 0:ecaf3e593122 37 }
Tuxitheone 0:ecaf3e593122 38
Tuxitheone 0:ecaf3e593122 39 void LogToSd(void)
Tuxitheone 0:ecaf3e593122 40 {
Tuxitheone 0:ecaf3e593122 41 FILE *fp; //Logfile
Tuxitheone 0:ecaf3e593122 42 time_t seconds = time(NULL);
Tuxitheone 0:ecaf3e593122 43 char timestr[32];
Tuxitheone 0:ecaf3e593122 44 seconds = time(NULL);
Tuxitheone 0:ecaf3e593122 45 strftime(timestr, 32, "%Y/%m/%d-%H:%M:%S", localtime(&seconds));
Tuxitheone 0:ecaf3e593122 46 fp = fopen("/sd/taptraq/Tank.log", "a");
Tuxitheone 0:ecaf3e593122 47 fprintf(fp,"%s,%.4f,%.1f\r\n",
Tuxitheone 0:ecaf3e593122 48 timestr,T1.Flow(),ReadTempetur());
Tuxitheone 0:ecaf3e593122 49 fclose(fp);
Tuxitheone 0:ecaf3e593122 50 }
Tuxitheone 0:ecaf3e593122 51
Tuxitheone 0:ecaf3e593122 52 int main()
Tuxitheone 0:ecaf3e593122 53 {
Tuxitheone 0:ecaf3e593122 54 time_t seconds = time(NULL);
Tuxitheone 0:ecaf3e593122 55 char timestr[32];
Tuxitheone 0:ecaf3e593122 56 lcd.printf("Establishing...\n");
Tuxitheone 0:ecaf3e593122 57 lcd.printf("Network Connection\n");
Tuxitheone 0:ecaf3e593122 58 EthernetInterface eth;
Tuxitheone 0:ecaf3e593122 59 eth.init(); //Use DHCP
Tuxitheone 0:ecaf3e593122 60 if (eth.connect()<0) NVIC_SystemReset();
Tuxitheone 0:ecaf3e593122 61 lcd.cls();
Tuxitheone 0:ecaf3e593122 62 lcd.printf("Located IP ADR....\n");
Tuxitheone 0:ecaf3e593122 63 lcd.printf("IP: - %s\n", eth.getIPAddress());
Tuxitheone 0:ecaf3e593122 64 printf("IP: %s\n", eth.getIPAddress());
Tuxitheone 0:ecaf3e593122 65 Websocket ws("ws://sockets.mbed.org/ws/gert_lauritsen/rw");
Tuxitheone 0:ecaf3e593122 66 wait(3);
Tuxitheone 0:ecaf3e593122 67 lcd.cls();
Tuxitheone 0:ecaf3e593122 68 lcd.printf("Reconnecting...\n");
Tuxitheone 0:ecaf3e593122 69 lcd.printf("TapTraQ Uplink\n");
Tuxitheone 0:ecaf3e593122 70 while (!ws.connect());
Tuxitheone 0:ecaf3e593122 71 lcd.cls();
Tuxitheone 0:ecaf3e593122 72 lcd.printf("Faild Connect...\n");
Tuxitheone 0:ecaf3e593122 73 lcd.printf("TapTraQ Uplink\n");
Tuxitheone 0:ecaf3e593122 74 wait(5);
Tuxitheone 0:ecaf3e593122 75 TapTraqUplink Uplink(&ws);
Tuxitheone 0:ecaf3e593122 76 /* Set up NTP */
Tuxitheone 0:ecaf3e593122 77 // printf("Setting up NTP \n\r");
Tuxitheone 0:ecaf3e593122 78 //ntp.setTime("0.dk.pool.ntp.org",123);
Tuxitheone 0:ecaf3e593122 79 //ntp.setTime("ntp1.jst.mfeed.ad.jp",123);
Tuxitheone 0:ecaf3e593122 80 lcd.cls();
Tuxitheone 0:ecaf3e593122 81 strftime(timestr, 32, "%H:%M", localtime(&seconds));
Tuxitheone 0:ecaf3e593122 82 lcd.printf("%s \n",timestr);
Tuxitheone 0:ecaf3e593122 83 wait(1);
Tuxitheone 0:ecaf3e593122 84 InitLog();
Tuxitheone 0:ecaf3e593122 85 while (1) { //Vis temperatur og mængde i tank
Tuxitheone 0:ecaf3e593122 86 lcd.cls();
Tuxitheone 0:ecaf3e593122 87 seconds = time(NULL)+offset_DK;
Tuxitheone 0:ecaf3e593122 88 strftime(timestr, 32, "%H:%M", localtime(&seconds));
Tuxitheone 0:ecaf3e593122 89 lcd.printf("TapTraQ Count System");
Tuxitheone 0:ecaf3e593122 90 lcd.printf("KOLKKEN - %s\n",timestr);
Tuxitheone 0:ecaf3e593122 91 lcd.printf("TEMP %.2f C\n",ReadTempetur());
Tuxitheone 0:ecaf3e593122 92 lcd.printf("TANK %.1f - %.1f L \n",TankContens(),T1.Flow());
Tuxitheone 0:ecaf3e593122 93 Uplink.FlowFrame("1000",1,T1.Count,0,0,0,0,0,0);
Tuxitheone 0:ecaf3e593122 94 LogToSd();
Tuxitheone 0:ecaf3e593122 95 wait(10);
Tuxitheone 0:ecaf3e593122 96 }
Tuxitheone 0:ecaf3e593122 97 }