Gateway code for sensor network version 4
Dependencies: C12832_lcd HTTPClient WiflyInterface mbed
main.cpp@0:b28a97d78384, 2018-07-16 (annotated)
- Committer:
- mreda
- Date:
- Mon Jul 16 08:09:57 2018 +0000
- Revision:
- 0:b28a97d78384
Fsktm UM Malaysia
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mreda | 0:b28a97d78384 | 1 | //Code to recieve serial comms on |
mreda | 0:b28a97d78384 | 2 | //mbed appBoard with xbees |
mreda | 0:b28a97d78384 | 3 | #include "mbed.h" |
mreda | 0:b28a97d78384 | 4 | #include "C12832_lcd.h" // Include for LCD code |
mreda | 0:b28a97d78384 | 5 | #include "stdlib.h" |
mreda | 0:b28a97d78384 | 6 | #include"string" |
mreda | 0:b28a97d78384 | 7 | #include"stdio.h" |
mreda | 0:b28a97d78384 | 8 | #include "WiflyInterface.h" |
mreda | 0:b28a97d78384 | 9 | #include "HTTPClient.h" |
mreda | 0:b28a97d78384 | 10 | |
mreda | 0:b28a97d78384 | 11 | |
mreda | 0:b28a97d78384 | 12 | // watchdog |
mreda | 0:b28a97d78384 | 13 | class Watchdog { |
mreda | 0:b28a97d78384 | 14 | public: |
mreda | 0:b28a97d78384 | 15 | // Load timeout value in watchdog timer and enable |
mreda | 0:b28a97d78384 | 16 | void kick(float s) { |
mreda | 0:b28a97d78384 | 17 | LPC_WDT->WDCLKSEL = 0x1; // Set CLK src to PCLK |
mreda | 0:b28a97d78384 | 18 | uint32_t clk = SystemCoreClock / 16; // WD has a fixed /4 prescaler, PCLK default is /4 |
mreda | 0:b28a97d78384 | 19 | LPC_WDT->WDTC = s * (float)clk; |
mreda | 0:b28a97d78384 | 20 | LPC_WDT->WDMOD = 0x3; // Enabled and Reset |
mreda | 0:b28a97d78384 | 21 | kick(); |
mreda | 0:b28a97d78384 | 22 | } |
mreda | 0:b28a97d78384 | 23 | // "kick" or "feed" the dog - reset the watchdog timer |
mreda | 0:b28a97d78384 | 24 | // by writing this required bit pattern |
mreda | 0:b28a97d78384 | 25 | void kick() { |
mreda | 0:b28a97d78384 | 26 | LPC_WDT->WDFEED = 0xAA; |
mreda | 0:b28a97d78384 | 27 | LPC_WDT->WDFEED = 0x55; |
mreda | 0:b28a97d78384 | 28 | } |
mreda | 0:b28a97d78384 | 29 | }; |
mreda | 0:b28a97d78384 | 30 | |
mreda | 0:b28a97d78384 | 31 | // Setup the watchdog timer |
mreda | 0:b28a97d78384 | 32 | Watchdog wdt; |
mreda | 0:b28a97d78384 | 33 | |
mreda | 0:b28a97d78384 | 34 | |
mreda | 0:b28a97d78384 | 35 | // Variable |
mreda | 0:b28a97d78384 | 36 | // In lab version |
mreda | 0:b28a97d78384 | 37 | //WiflyInterface wifly(p9, p10, p30, p29, "stroustrup", "stroustrupfsktm", WPA); |
mreda | 0:b28a97d78384 | 38 | WiflyInterface wifly(p9, p10, p30, p29, "FSKTM-Training", "Training2018", WPA); |
mreda | 0:b28a97d78384 | 39 | HTTPClient http; |
mreda | 0:b28a97d78384 | 40 | char str[5]; |
mreda | 0:b28a97d78384 | 41 | int iPrint; |
mreda | 0:b28a97d78384 | 42 | C12832_LCD lcd; |
mreda | 0:b28a97d78384 | 43 | Serial xbee(p13, p14); |
mreda | 0:b28a97d78384 | 44 | DigitalOut rst1(p15); |
mreda | 0:b28a97d78384 | 45 | DigitalOut myled(LED1); |
mreda | 0:b28a97d78384 | 46 | |
mreda | 0:b28a97d78384 | 47 | Ticker myTicker; |
mreda | 0:b28a97d78384 | 48 | int loadingStatus = 0; |
mreda | 0:b28a97d78384 | 49 | |
mreda | 0:b28a97d78384 | 50 | void loadingcharLcd(void) |
mreda | 0:b28a97d78384 | 51 | { |
mreda | 0:b28a97d78384 | 52 | lcd.locate(120,20); |
mreda | 0:b28a97d78384 | 53 | if(loadingStatus == 0) |
mreda | 0:b28a97d78384 | 54 | lcd.printf("-"); |
mreda | 0:b28a97d78384 | 55 | else if(loadingStatus == 1) |
mreda | 0:b28a97d78384 | 56 | lcd.printf("\\"); |
mreda | 0:b28a97d78384 | 57 | else if(loadingStatus == 2) |
mreda | 0:b28a97d78384 | 58 | lcd.printf("|"); |
mreda | 0:b28a97d78384 | 59 | else if(loadingStatus == 3) |
mreda | 0:b28a97d78384 | 60 | { |
mreda | 0:b28a97d78384 | 61 | lcd.printf("/"); |
mreda | 0:b28a97d78384 | 62 | loadingStatus = -1; |
mreda | 0:b28a97d78384 | 63 | } |
mreda | 0:b28a97d78384 | 64 | loadingStatus++; |
mreda | 0:b28a97d78384 | 65 | |
mreda | 0:b28a97d78384 | 66 | |
mreda | 0:b28a97d78384 | 67 | } |
mreda | 0:b28a97d78384 | 68 | |
mreda | 0:b28a97d78384 | 69 | // Function |
mreda | 0:b28a97d78384 | 70 | void SendDataToThingSpeak (int iValue); |
mreda | 0:b28a97d78384 | 71 | void InitiateWifi(void); |
mreda | 0:b28a97d78384 | 72 | void InitiateXbee(void); |
mreda | 0:b28a97d78384 | 73 | void ReadXbee (); |
mreda | 0:b28a97d78384 | 74 | // int main |
mreda | 0:b28a97d78384 | 75 | int main() |
mreda | 0:b28a97d78384 | 76 | { |
mreda | 0:b28a97d78384 | 77 | wdt.kick(50.0); |
mreda | 0:b28a97d78384 | 78 | while(1) |
mreda | 0:b28a97d78384 | 79 | { |
mreda | 0:b28a97d78384 | 80 | // call read xbee |
mreda | 0:b28a97d78384 | 81 | |
mreda | 0:b28a97d78384 | 82 | |
mreda | 0:b28a97d78384 | 83 | ReadXbee(); |
mreda | 0:b28a97d78384 | 84 | lcd.cls(); |
mreda | 0:b28a97d78384 | 85 | lcd.locate(0,1); |
mreda | 0:b28a97d78384 | 86 | lcd.printf("The distance is : %d \n",iPrint); |
mreda | 0:b28a97d78384 | 87 | wait(1); |
mreda | 0:b28a97d78384 | 88 | lcd.cls(); |
mreda | 0:b28a97d78384 | 89 | lcd.locate(0,1); |
mreda | 0:b28a97d78384 | 90 | lcd.printf("Wifi-connection verification... \n"); |
mreda | 0:b28a97d78384 | 91 | |
mreda | 0:b28a97d78384 | 92 | myTicker.attach(&loadingcharLcd, 0.25); |
mreda | 0:b28a97d78384 | 93 | |
mreda | 0:b28a97d78384 | 94 | InitiateWifi(); |
mreda | 0:b28a97d78384 | 95 | |
mreda | 0:b28a97d78384 | 96 | myTicker.detach(); |
mreda | 0:b28a97d78384 | 97 | lcd.cls(); |
mreda | 0:b28a97d78384 | 98 | lcd.locate(0,1); |
mreda | 0:b28a97d78384 | 99 | lcd.printf("Wifi-connection verification\nDone!"); |
mreda | 0:b28a97d78384 | 100 | wait_ms(400); |
mreda | 0:b28a97d78384 | 101 | |
mreda | 0:b28a97d78384 | 102 | SendDataToThingSpeak(iPrint); |
mreda | 0:b28a97d78384 | 103 | wifly.disconnect(); |
mreda | 0:b28a97d78384 | 104 | // wifly.disconnect(); |
mreda | 0:b28a97d78384 | 105 | // InitiateXbee(); |
mreda | 0:b28a97d78384 | 106 | // wait (2); |
mreda | 0:b28a97d78384 | 107 | } //while(1) |
mreda | 0:b28a97d78384 | 108 | }// int main() |
mreda | 0:b28a97d78384 | 109 | |
mreda | 0:b28a97d78384 | 110 | void SendDataToThingSpeak (int iValue) |
mreda | 0:b28a97d78384 | 111 | { |
mreda | 0:b28a97d78384 | 112 | int iCounter=-1; |
mreda | 0:b28a97d78384 | 113 | char reverseData[4]; |
mreda | 0:b28a97d78384 | 114 | int toSend; |
mreda | 0:b28a97d78384 | 115 | toSend=iValue; |
mreda | 0:b28a97d78384 | 116 | char tempStr[] = "https://api.thingspeak.com/update?api_key=PQXV093BQK2UNXIV&field2= "; |
mreda | 0:b28a97d78384 | 117 | |
mreda | 0:b28a97d78384 | 118 | |
mreda | 0:b28a97d78384 | 119 | do |
mreda | 0:b28a97d78384 | 120 | { |
mreda | 0:b28a97d78384 | 121 | iCounter++; |
mreda | 0:b28a97d78384 | 122 | reverseData[iCounter]=toSend%10; |
mreda | 0:b28a97d78384 | 123 | reverseData[iCounter]+=48; |
mreda | 0:b28a97d78384 | 124 | toSend/=10; |
mreda | 0:b28a97d78384 | 125 | }while(toSend!=0); |
mreda | 0:b28a97d78384 | 126 | |
mreda | 0:b28a97d78384 | 127 | |
mreda | 0:b28a97d78384 | 128 | |
mreda | 0:b28a97d78384 | 129 | |
mreda | 0:b28a97d78384 | 130 | for(int i= 0; i < iCounter+1; i++) |
mreda | 0:b28a97d78384 | 131 | { |
mreda | 0:b28a97d78384 | 132 | tempStr[66+i]= reverseData[iCounter-i]; |
mreda | 0:b28a97d78384 | 133 | } |
mreda | 0:b28a97d78384 | 134 | // tempStr[67+iCounter]='/'; |
mreda | 0:b28a97d78384 | 135 | printf("iC %d RD %s \n", iCounter, reverseData); |
mreda | 0:b28a97d78384 | 136 | |
mreda | 0:b28a97d78384 | 137 | lcd.cls(); |
mreda | 0:b28a97d78384 | 138 | lcd.locate(0,1); |
mreda | 0:b28a97d78384 | 139 | lcd.printf("iC %d RD %s \n", iCounter, reverseData); |
mreda | 0:b28a97d78384 | 140 | |
mreda | 0:b28a97d78384 | 141 | HTTPMap map; |
mreda | 0:b28a97d78384 | 142 | HTTPText text(str, 1); |
mreda | 0:b28a97d78384 | 143 | map.put("", ""); |
mreda | 0:b28a97d78384 | 144 | // tempStr[66]=55; |
mreda | 0:b28a97d78384 | 145 | char tempStr1[] = "https://api.thingspeak.com/"; |
mreda | 0:b28a97d78384 | 146 | printf("Trying to post data to the Cloud...%s \n", tempStr1); |
mreda | 0:b28a97d78384 | 147 | |
mreda | 0:b28a97d78384 | 148 | lcd.cls(); |
mreda | 0:b28a97d78384 | 149 | lcd.locate(0,1); |
mreda | 0:b28a97d78384 | 150 | lcd.printf("Uploading To Cloud\nTHINGSPEAK"); |
mreda | 0:b28a97d78384 | 151 | |
mreda | 0:b28a97d78384 | 152 | myTicker.attach(&loadingcharLcd, 0.25); |
mreda | 0:b28a97d78384 | 153 | |
mreda | 0:b28a97d78384 | 154 | http.post(tempStr, map, &text,1); |
mreda | 0:b28a97d78384 | 155 | |
mreda | 0:b28a97d78384 | 156 | myTicker.detach(); |
mreda | 0:b28a97d78384 | 157 | |
mreda | 0:b28a97d78384 | 158 | }// void SendDataToThingSpeak |
mreda | 0:b28a97d78384 | 159 | |
mreda | 0:b28a97d78384 | 160 | void InitiateWifi(void) |
mreda | 0:b28a97d78384 | 161 | { |
mreda | 0:b28a97d78384 | 162 | |
mreda | 0:b28a97d78384 | 163 | wifly.init(); //Use DHCP |
mreda | 0:b28a97d78384 | 164 | wifly.connect(); |
mreda | 0:b28a97d78384 | 165 | // wait (2); |
mreda | 0:b28a97d78384 | 166 | }// wifi |
mreda | 0:b28a97d78384 | 167 | |
mreda | 0:b28a97d78384 | 168 | void InitiateXbee(void) |
mreda | 0:b28a97d78384 | 169 | { |
mreda | 0:b28a97d78384 | 170 | rst1 = 0; |
mreda | 0:b28a97d78384 | 171 | wait_ms(10); |
mreda | 0:b28a97d78384 | 172 | rst1 = 1; |
mreda | 0:b28a97d78384 | 173 | wait_ms(10); |
mreda | 0:b28a97d78384 | 174 | |
mreda | 0:b28a97d78384 | 175 | } // xbee |
mreda | 0:b28a97d78384 | 176 | |
mreda | 0:b28a97d78384 | 177 | void ReadXbee (void) |
mreda | 0:b28a97d78384 | 178 | { |
mreda | 0:b28a97d78384 | 179 | printf("Waiting For The Sensor Node To Send Sensor Data To The Gateway \n"); |
mreda | 0:b28a97d78384 | 180 | |
mreda | 0:b28a97d78384 | 181 | lcd.cls(); |
mreda | 0:b28a97d78384 | 182 | lcd.locate(0,1); |
mreda | 0:b28a97d78384 | 183 | lcd.printf("Receiving Data From Sensor Node...\n"); |
mreda | 0:b28a97d78384 | 184 | |
mreda | 0:b28a97d78384 | 185 | myTicker.attach(&loadingcharLcd, 0.25); |
mreda | 0:b28a97d78384 | 186 | |
mreda | 0:b28a97d78384 | 187 | |
mreda | 0:b28a97d78384 | 188 | |
mreda | 0:b28a97d78384 | 189 | InitiateXbee(); |
mreda | 0:b28a97d78384 | 190 | int toPrint=0; |
mreda | 0:b28a97d78384 | 191 | for(int j=0;j<18;j++) |
mreda | 0:b28a97d78384 | 192 | toPrint=xbee.getc(); |
mreda | 0:b28a97d78384 | 193 | |
mreda | 0:b28a97d78384 | 194 | iPrint=0; |
mreda | 0:b28a97d78384 | 195 | |
mreda | 0:b28a97d78384 | 196 | do |
mreda | 0:b28a97d78384 | 197 | { |
mreda | 0:b28a97d78384 | 198 | toPrint=xbee.getc(); |
mreda | 0:b28a97d78384 | 199 | //printf("toPrint %c \n", toPrint); |
mreda | 0:b28a97d78384 | 200 | }while(toPrint!='$'); |
mreda | 0:b28a97d78384 | 201 | while (!(xbee.readable())); |
mreda | 0:b28a97d78384 | 202 | toPrint=xbee.getc(); |
mreda | 0:b28a97d78384 | 203 | //printf("toPrint %c \n", toPrint); |
mreda | 0:b28a97d78384 | 204 | iPrint=0; |
mreda | 0:b28a97d78384 | 205 | if(toPrint=='$') |
mreda | 0:b28a97d78384 | 206 | { |
mreda | 0:b28a97d78384 | 207 | do{ |
mreda | 0:b28a97d78384 | 208 | toPrint=xbee.getc(); |
mreda | 0:b28a97d78384 | 209 | }while(toPrint=='$'); |
mreda | 0:b28a97d78384 | 210 | } |
mreda | 0:b28a97d78384 | 211 | |
mreda | 0:b28a97d78384 | 212 | while (!(toPrint=='$')) |
mreda | 0:b28a97d78384 | 213 | { |
mreda | 0:b28a97d78384 | 214 | iPrint=(toPrint-48)+iPrint*10; |
mreda | 0:b28a97d78384 | 215 | //printf("iPrint %d \n", iPrint); |
mreda | 0:b28a97d78384 | 216 | while (!(xbee.readable())); |
mreda | 0:b28a97d78384 | 217 | toPrint=xbee.getc(); |
mreda | 0:b28a97d78384 | 218 | printf("toPr %d , iPr %d \n", toPrint, iPrint); |
mreda | 0:b28a97d78384 | 219 | if ((LPC_WDT->WDMOD >> 2) & 1) |
mreda | 0:b28a97d78384 | 220 | printf("WatchDog :: Status On"); |
mreda | 0:b28a97d78384 | 221 | |
mreda | 0:b28a97d78384 | 222 | |
mreda | 0:b28a97d78384 | 223 | } |
mreda | 0:b28a97d78384 | 224 | printf("The Sensor Data Has Been Received \n"); |
mreda | 0:b28a97d78384 | 225 | |
mreda | 0:b28a97d78384 | 226 | lcd.cls(); |
mreda | 0:b28a97d78384 | 227 | lcd.locate(0,1); |
mreda | 0:b28a97d78384 | 228 | lcd.printf("he Sensor Data Has Been Received \n"); |
mreda | 0:b28a97d78384 | 229 | |
mreda | 0:b28a97d78384 | 230 | wdt.kick(); |
mreda | 0:b28a97d78384 | 231 | |
mreda | 0:b28a97d78384 | 232 | |
mreda | 0:b28a97d78384 | 233 | myTicker.detach(); |
mreda | 0:b28a97d78384 | 234 | |
mreda | 0:b28a97d78384 | 235 | } |