til TT
Dependencies: EthernetInterface ThingCloud mbed-rtos mbed
main.cpp@2:20a840e0ff72, 2016-07-28 (annotated)
- Committer:
- gert_lauritsen
- Date:
- Thu Jul 28 22:01:13 2016 +0000
- Revision:
- 2:20a840e0ff72
- Parent:
- 1:4e7bedad6920
skanderborg1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gert_lauritsen | 0:f349bb750aa4 | 1 | #include "mbed.h" |
gert_lauritsen | 0:f349bb750aa4 | 2 | #include "EthernetInterface.h" |
gert_lauritsen | 1:4e7bedad6920 | 3 | #include "thingspeak.h" |
gert_lauritsen | 0:f349bb750aa4 | 4 | |
gert_lauritsen | 1:4e7bedad6920 | 5 | #define apikey1 "29KO1SSTACFU295A" |
gert_lauritsen | 1:4e7bedad6920 | 6 | #define apikey2 "262BP2WZLBS2VHWV" |
gert_lauritsen | 0:f349bb750aa4 | 7 | |
gert_lauritsen | 0:f349bb750aa4 | 8 | EthernetInterface eth; |
gert_lauritsen | 0:f349bb750aa4 | 9 | char* thingSpeakKey1 = apikey1; |
gert_lauritsen | 0:f349bb750aa4 | 10 | char* thingSpeakKey2 = apikey2; |
gert_lauritsen | 0:f349bb750aa4 | 11 | Serial pc(USBTX, USBRX); // serial comms over usb back to console |
gert_lauritsen | 1:4e7bedad6920 | 12 | Serial extpc(p28,p27); |
gert_lauritsen | 1:4e7bedad6920 | 13 | Serial Radio(p9,p10); |
gert_lauritsen | 0:f349bb750aa4 | 14 | THINGSPEAK Channel; |
gert_lauritsen | 1:4e7bedad6920 | 15 | DigitalOut led(LED1); |
gert_lauritsen | 1:4e7bedad6920 | 16 | DigitalOut flowindi(LED2); |
gert_lauritsen | 1:4e7bedad6920 | 17 | |
gert_lauritsen | 0:f349bb750aa4 | 18 | AnalogIn transmitter(p15); |
gert_lauritsen | 0:f349bb750aa4 | 19 | AnalogIn amb(p16); |
gert_lauritsen | 1:4e7bedad6920 | 20 | InterruptIn flow(p17); |
gert_lauritsen | 0:f349bb750aa4 | 21 | AnalogIn vand(p18); |
gert_lauritsen | 0:f349bb750aa4 | 22 | AnalogIn retur(p19); |
gert_lauritsen | 0:f349bb750aa4 | 23 | AnalogIn oltemp(p20); |
gert_lauritsen | 0:f349bb750aa4 | 24 | |
gert_lauritsen | 1:4e7bedad6920 | 25 | int flowCount; |
gert_lauritsen | 1:4e7bedad6920 | 26 | char InBuff[100]; |
gert_lauritsen | 1:4e7bedad6920 | 27 | char *p; |
gert_lauritsen | 0:f349bb750aa4 | 28 | |
gert_lauritsen | 0:f349bb750aa4 | 29 | float Tryk; |
gert_lauritsen | 0:f349bb750aa4 | 30 | float Flow; |
gert_lauritsen | 0:f349bb750aa4 | 31 | float AmbiantTemp,AmbiantTemp2; |
gert_lauritsen | 0:f349bb750aa4 | 32 | float WaterTemp; |
gert_lauritsen | 0:f349bb750aa4 | 33 | float MidtTemp; //2 |
gert_lauritsen | 0:f349bb750aa4 | 34 | float ReturTemp; |
gert_lauritsen | 0:f349bb750aa4 | 35 | float BeerTemp,BeerTemp2; |
gert_lauritsen | 0:f349bb750aa4 | 36 | float PumpeEnde; //2 |
gert_lauritsen | 0:f349bb750aa4 | 37 | float SlangeEnde; |
gert_lauritsen | 0:f349bb750aa4 | 38 | |
gert_lauritsen | 1:4e7bedad6920 | 39 | size_t parse ( char *line, char *list[], size_t size ) |
gert_lauritsen | 1:4e7bedad6920 | 40 | { |
gert_lauritsen | 1:4e7bedad6920 | 41 | char *p; |
gert_lauritsen | 1:4e7bedad6920 | 42 | size_t n; |
gert_lauritsen | 1:4e7bedad6920 | 43 | |
gert_lauritsen | 1:4e7bedad6920 | 44 | p = line; |
gert_lauritsen | 1:4e7bedad6920 | 45 | n = 0; |
gert_lauritsen | 1:4e7bedad6920 | 46 | for ( ; ; ) { |
gert_lauritsen | 1:4e7bedad6920 | 47 | /* Ditch leading commas */ |
gert_lauritsen | 1:4e7bedad6920 | 48 | while ( *p == ' ' ) |
gert_lauritsen | 1:4e7bedad6920 | 49 | p++; |
gert_lauritsen | 1:4e7bedad6920 | 50 | /* Nothing of use */ |
gert_lauritsen | 1:4e7bedad6920 | 51 | if ( *p == '\0' ) |
gert_lauritsen | 1:4e7bedad6920 | 52 | return n; |
gert_lauritsen | 1:4e7bedad6920 | 53 | /* Save the string */ |
gert_lauritsen | 1:4e7bedad6920 | 54 | list[n++] = p; |
gert_lauritsen | 1:4e7bedad6920 | 55 | /* Find the next field */ |
gert_lauritsen | 1:4e7bedad6920 | 56 | while ( *p != ' ' && *p != '\0' ) |
gert_lauritsen | 1:4e7bedad6920 | 57 | p++; |
gert_lauritsen | 1:4e7bedad6920 | 58 | /* Nothing else of use or too many fields */ |
gert_lauritsen | 1:4e7bedad6920 | 59 | if ( *p == '\0' || n >= size ) |
gert_lauritsen | 1:4e7bedad6920 | 60 | return n; |
gert_lauritsen | 1:4e7bedad6920 | 61 | /* Split the field */ |
gert_lauritsen | 1:4e7bedad6920 | 62 | *p++ = '\0'; |
gert_lauritsen | 1:4e7bedad6920 | 63 | } |
gert_lauritsen | 1:4e7bedad6920 | 64 | } |
gert_lauritsen | 1:4e7bedad6920 | 65 | |
gert_lauritsen | 1:4e7bedad6920 | 66 | |
gert_lauritsen | 1:4e7bedad6920 | 67 | void GetData() |
gert_lauritsen | 1:4e7bedad6920 | 68 | { |
gert_lauritsen | 1:4e7bedad6920 | 69 | Timer t; |
gert_lauritsen | 1:4e7bedad6920 | 70 | char *list[4]; |
gert_lauritsen | 1:4e7bedad6920 | 71 | char a,ch; |
gert_lauritsen | 1:4e7bedad6920 | 72 | char waiting; |
gert_lauritsen | 1:4e7bedad6920 | 73 | p=&InBuff[0]; |
gert_lauritsen | 1:4e7bedad6920 | 74 | Radio.printf("!"); |
gert_lauritsen | 1:4e7bedad6920 | 75 | waiting=1; |
gert_lauritsen | 1:4e7bedad6920 | 76 | a=0; |
gert_lauritsen | 1:4e7bedad6920 | 77 | led=1; |
gert_lauritsen | 1:4e7bedad6920 | 78 | t.start(); |
gert_lauritsen | 1:4e7bedad6920 | 79 | while (waiting) { |
gert_lauritsen | 1:4e7bedad6920 | 80 | if (t.read_ms()>1000) { |
gert_lauritsen | 1:4e7bedad6920 | 81 | Radio.printf("!"); |
gert_lauritsen | 1:4e7bedad6920 | 82 | t.reset(); |
gert_lauritsen | 1:4e7bedad6920 | 83 | t.start(); |
gert_lauritsen | 1:4e7bedad6920 | 84 | a=0; |
gert_lauritsen | 1:4e7bedad6920 | 85 | pc.printf("Retry\r\n"); |
gert_lauritsen | 1:4e7bedad6920 | 86 | } |
gert_lauritsen | 1:4e7bedad6920 | 87 | if (Radio.readable()) { |
gert_lauritsen | 1:4e7bedad6920 | 88 | ch=Radio.getc(); |
gert_lauritsen | 1:4e7bedad6920 | 89 | if ((ch!=0x0A) && (ch!=0x0D)) InBuff[a++]=ch; |
gert_lauritsen | 1:4e7bedad6920 | 90 | if (ch==0x0D) { |
gert_lauritsen | 1:4e7bedad6920 | 91 | InBuff[a]=0; |
gert_lauritsen | 1:4e7bedad6920 | 92 | |
gert_lauritsen | 1:4e7bedad6920 | 93 | t.stop(); |
gert_lauritsen | 1:4e7bedad6920 | 94 | led=0; |
gert_lauritsen | 1:4e7bedad6920 | 95 | //pc.printf("%s\r\n",InBuff); |
gert_lauritsen | 1:4e7bedad6920 | 96 | |
gert_lauritsen | 1:4e7bedad6920 | 97 | if (parse(p,list,100)==4) { |
gert_lauritsen | 1:4e7bedad6920 | 98 | AmbiantTemp2=atof(list[0]); |
gert_lauritsen | 1:4e7bedad6920 | 99 | MidtTemp=atof(list[1]); |
gert_lauritsen | 1:4e7bedad6920 | 100 | BeerTemp2=atof(list[2]); |
gert_lauritsen | 1:4e7bedad6920 | 101 | SlangeEnde=atof(list[3]); |
gert_lauritsen | 1:4e7bedad6920 | 102 | waiting=0; |
gert_lauritsen | 1:4e7bedad6920 | 103 | } |
gert_lauritsen | 1:4e7bedad6920 | 104 | else { |
gert_lauritsen | 1:4e7bedad6920 | 105 | pc.printf("decodeError\r\n"); |
gert_lauritsen | 1:4e7bedad6920 | 106 | waiting=0; |
gert_lauritsen | 1:4e7bedad6920 | 107 | /* Radio.printf("!"); |
gert_lauritsen | 1:4e7bedad6920 | 108 | t.reset(); |
gert_lauritsen | 1:4e7bedad6920 | 109 | t.start(); |
gert_lauritsen | 1:4e7bedad6920 | 110 | a=0; |
gert_lauritsen | 1:4e7bedad6920 | 111 | p=&InBuff[0];*/ |
gert_lauritsen | 1:4e7bedad6920 | 112 | } |
gert_lauritsen | 1:4e7bedad6920 | 113 | |
gert_lauritsen | 1:4e7bedad6920 | 114 | // sscanf(InBuff,"%f %f %f %f",AmbiantTemp2,MidtTemp,BeerTemp2,SlangeEnde); |
gert_lauritsen | 1:4e7bedad6920 | 115 | //pc.printf("%f %f %f %f\r\n",AmbiantTemp2,MidtTemp,BeerTemp2,SlangeEnde); |
gert_lauritsen | 1:4e7bedad6920 | 116 | } |
gert_lauritsen | 1:4e7bedad6920 | 117 | } |
gert_lauritsen | 1:4e7bedad6920 | 118 | } |
gert_lauritsen | 1:4e7bedad6920 | 119 | led=0; |
gert_lauritsen | 1:4e7bedad6920 | 120 | } |
gert_lauritsen | 1:4e7bedad6920 | 121 | |
gert_lauritsen | 0:f349bb750aa4 | 122 | void readAnalog() |
gert_lauritsen | 0:f349bb750aa4 | 123 | { |
gert_lauritsen | 1:4e7bedad6920 | 124 | Flow=(float)flowCount/(17*1200); |
gert_lauritsen | 0:f349bb750aa4 | 125 | Tryk=(transmitter.read()*3.3-0.597)*4,16; |
gert_lauritsen | 0:f349bb750aa4 | 126 | AmbiantTemp=(amb.read()*3.3-0.25)/0.028; |
gert_lauritsen | 0:f349bb750aa4 | 127 | WaterTemp=(vand.read()*3.3-0.25)/0.028; |
gert_lauritsen | 0:f349bb750aa4 | 128 | ReturTemp=(retur.read()*3.3-0.25)/0.028; |
gert_lauritsen | 0:f349bb750aa4 | 129 | BeerTemp=(oltemp.read()*3.3-0.25)/0.028; |
gert_lauritsen | 0:f349bb750aa4 | 130 | Channel.AddFloat(AmbiantTemp); |
gert_lauritsen | 0:f349bb750aa4 | 131 | Channel.AddFloat(AmbiantTemp2); |
gert_lauritsen | 0:f349bb750aa4 | 132 | Channel.AddFloat(WaterTemp); |
gert_lauritsen | 0:f349bb750aa4 | 133 | Channel.AddFloat(MidtTemp); |
gert_lauritsen | 0:f349bb750aa4 | 134 | Channel.AddFloat(ReturTemp); |
gert_lauritsen | 0:f349bb750aa4 | 135 | Channel.AddFloat(Flow); |
gert_lauritsen | 0:f349bb750aa4 | 136 | Channel.SendData(thingSpeakKey1); |
gert_lauritsen | 0:f349bb750aa4 | 137 | Channel.AddFloat(PumpeEnde); |
gert_lauritsen | 0:f349bb750aa4 | 138 | Channel.AddFloat(SlangeEnde); |
gert_lauritsen | 0:f349bb750aa4 | 139 | Channel.AddFloat(BeerTemp2); |
gert_lauritsen | 0:f349bb750aa4 | 140 | Channel.AddFloat(BeerTemp); |
gert_lauritsen | 0:f349bb750aa4 | 141 | Channel.SendData(thingSpeakKey2); |
gert_lauritsen | 1:4e7bedad6920 | 142 | flowCount=0; |
gert_lauritsen | 1:4e7bedad6920 | 143 | extpc.printf("T1 %.2f T2 %.2f Wt1 %.2f Wt2 %.2f Wt3 %.2f Flow %.2f Tr1 %.2f Tr2 %.2f B2 %.2f B1 %.2f \r\n",AmbiantTemp,AmbiantTemp2,WaterTemp,MidtTemp,ReturTemp,Flow,PumpeEnde,SlangeEnde,BeerTemp2,BeerTemp); |
gert_lauritsen | 1:4e7bedad6920 | 144 | pc.printf("T1 %.2f T2 %.2f Wt1 %.2f Wt2 %.2f Wt3 %.2f Flow %.2f Tr1 %.2f Tr2 %.2f B2 %.2f B1 %.2f \r\n",AmbiantTemp,AmbiantTemp2,WaterTemp,MidtTemp,ReturTemp,Flow,PumpeEnde,SlangeEnde,BeerTemp2,BeerTemp); |
gert_lauritsen | 1:4e7bedad6920 | 145 | } |
gert_lauritsen | 1:4e7bedad6920 | 146 | |
gert_lauritsen | 1:4e7bedad6920 | 147 | void flowcounter() |
gert_lauritsen | 1:4e7bedad6920 | 148 | { |
gert_lauritsen | 1:4e7bedad6920 | 149 | flowCount++; //tæller |
gert_lauritsen | 1:4e7bedad6920 | 150 | flowindi=!flowindi; |
gert_lauritsen | 0:f349bb750aa4 | 151 | } |
gert_lauritsen | 0:f349bb750aa4 | 152 | |
gert_lauritsen | 0:f349bb750aa4 | 153 | int main() |
gert_lauritsen | 0:f349bb750aa4 | 154 | { |
gert_lauritsen | 0:f349bb750aa4 | 155 | pc.printf("\n\r---------------------------------------------------------------\n\r"); |
gert_lauritsen | 0:f349bb750aa4 | 156 | eth.init(); //Use DHCP |
gert_lauritsen | 0:f349bb750aa4 | 157 | eth.connect(); |
gert_lauritsen | 0:f349bb750aa4 | 158 | pc.printf("IP Address is %s\n\r", eth.getIPAddress()); |
gert_lauritsen | 1:4e7bedad6920 | 159 | flow.fall(&flowcounter); |
gert_lauritsen | 0:f349bb750aa4 | 160 | while(1) { |
gert_lauritsen | 1:4e7bedad6920 | 161 | GetData(); |
gert_lauritsen | 0:f349bb750aa4 | 162 | readAnalog(); |
gert_lauritsen | 1:4e7bedad6920 | 163 | wait(17); |
gert_lauritsen | 0:f349bb750aa4 | 164 | } |
gert_lauritsen | 0:f349bb750aa4 | 165 | } |