simple weather app that uses sensor and forecast data from opeanweather.org . The results are shown on TFT and published on Thingspeak. (there may be some of my charts at https://thingspeak.com/channels/26357) This was built to show using k64F ethernet capabilities for K64F roadtest at http://www.element14.com/community/groups/roadtest (there will be a write there shortly)
Dependencies: DHT EthernetInterface HTTPClient NTPClient SDFileSystem SPI_TFT_ILI9341 TFT_fonts mbed-rtos mbed picojson
main.cpp
00001 #include "mbed.h" 00002 #include "SPI_TFT_ILI9341.h" 00003 #include "sansSerif19x20.h" 00004 #include "DHT.h" 00005 #include "EthernetInterface.h" 00006 #include "NTPClient.h" 00007 #include "HTTPClient.h" 00008 #include "picojson.h" 00009 #include "SDFileSystem.h" 00010 //IMPORTANT YOU NEED TO SET THE API KEY for thingspeak look for "your key here" 00011 //TAKE CARE URL doesn't excced buffer size 00012 //sd card 00013 SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // MOSI, MISO, SCLK, SSEL 00014 //Ethernet Interface 00015 EthernetInterface eth; 00016 //NTP service for date and Time used to set boards RTC 00017 NTPClient ntp; 00018 //HTTP Client for interfacing to web services 00019 HTTPClient http; 00020 char resp[1024]; //buffer for the responce 00021 //On board LED 00022 DigitalOut myled(LED_GREEN); 00023 //serial over USB for debug and Info 00024 Serial pc(USBTX, USBRX); 00025 //temperature sensor 00026 DHT sensor(PTB18,DHT22); // there are several libraries available, this reports CRC error sometimes 00027 //320x240 Display ILI9341 controller (look on ebay) 00028 SPI_TFT_ILI9341 TFT(PTD2, PTD3, PTD1,PTC3 ,PTC4 ,PTD0 ,"TFT"); // mosi, miso, sclk, cs, reset, dc 00029 00030 int main() 00031 { 00032 int cnt = 0; 00033 int err; 00034 int ret=0; 00035 char val[4][16]; 00036 //used to parse the json data 00037 picojson::value v; 00038 char * json; 00039 HTTPMap map; 00040 HTTPText inText(resp, 1024); 00041 char sdfile[32]; 00042 FILE *fp; 00043 pc.printf("Weather Reporter\n"); 00044 00045 00046 //eithernet init 00047 eth.init(); //Use DHCP 00048 ret= eth.connect(); 00049 if (!ret) { 00050 pc.printf("Connected, IP: %s, MASK: %s, GW: %s\n", 00051 eth.getIPAddress(), eth.getNetworkMask(), eth.getGateway()); 00052 } else { 00053 pc.printf("Error eth.connect() - ret = %d\n", ret); 00054 } 00055 00056 00057 //setup the TFT DISPLAY 00058 TFT.claim(stdout); // send stdout to the TFT display 00059 //ensure the output is write and not buffered 00060 setvbuf ( stdout , NULL , _IONBF , NULL ); 00061 //TFT.claim(stderr); // send stderr to the TFT display 00062 TFT.set_orientation(1); 00063 TFT.background(Black); // set background to black 00064 //centerx = TFT.width() >> 1; 00065 //centery = TFT.height() >> 1; 00066 00067 00068 TFT.cls(); 00069 TFT.locate(0,0); 00070 TFT.set_font((unsigned char*) sansserif); 00071 //printf will now for to TFT because of the TFT.claim(stdout) 00072 printf("Weather Test\n"); 00073 //setup the RTC with date and time 00074 printf("Trying to update time...\r\n"); 00075 time_t ctTime; 00076 NTPResult result; 00077 result = ntp.setTime("pool.ntp.org"); 00078 if (result == NTP_OK) { 00079 time(&ctTime); 00080 printf("Time is set to (UTC):\n%s\n", ctime(&ctTime)); 00081 strcpy(sdfile,"/sd/results/default_results"); //could use time in name 00082 }else{ 00083 printf("Error setting Time\n"); 00084 strcpy(sdfile,"/sd/results/default_results"); 00085 } 00086 00087 //write to sdcard, we could use the time and date in the filename defained above to give a new file for every session 00088 mkdir("/sd/results", 0777); 00089 fp = fopen(sdfile, "w"); 00090 if (fp == NULL) { 00091 pc.printf("Unable to write the file \n"); 00092 } else { 00093 pc.printf("Writing data to SDCARD \n"); 00094 //time,temp,humidity,forcast,pressure 00095 fprintf(fp, "time,temperature,humidity,forcastTemp,forcastPressure\n"); 00096 fclose(fp); 00097 } 00098 00099 float humidity=0; 00100 float temperature=-100; 00101 float forcastTemp=0; 00102 float forcastPressure; 00103 wait(10); 00104 pc.printf("\r\nDHT Test program"); 00105 pc.printf("\r\n******************\r\n"); 00106 wait(1); // wait 1 second for device stable status 00107 while(1){ 00108 00109 //GET forcast data 00110 pc.printf("\nTrying to fetch page...\n"); 00111 //try getting the weather, 5 second timeout 00112 ret = http.get("http://api.openweathermap.org/data/2.5/weather?q=Strathaven,uk", resp, 5000); 00113 if(!ret){ 00114 pc.printf("responce=%s",resp); 00115 json=&resp[0]; 00116 pc.printf("parsing"); 00117 string err = picojson::parse(v, json, json + strlen(json)); 00118 if(err.empty()){ 00119 forcastTemp=v.get("main").get("temp").get<double>(); 00120 forcastTemp=forcastTemp-273.15; 00121 forcastPressure=v.get("main").get("pressure").get<double>(); 00122 // printf("Forcast temp: %f\n", forcastTemp); 00123 } 00124 else{ 00125 pc.printf("error parsing"); 00126 } 00127 } 00128 else{ 00129 pc.printf("ERROR=%d", ret); 00130 } 00131 00132 err = sensor.readData(); 00133 if (err == 0) { 00134 TFT.cls(); 00135 TFT.locate(0,0); 00136 time(&ctTime); 00137 printf("%s\n", ctime(&ctTime)); 00138 temperature=sensor.ReadTemperature(CELCIUS); 00139 humidity=sensor.ReadHumidity(); 00140 printf("Temperature: %4.2f C \r\n",temperature); 00141 printf("Forcast Temp: %4.2f C \r\n",forcastTemp); 00142 printf("Temperature is %4.2f F \r\n",sensor.ReadTemperature(FARENHEIT)); 00143 printf("Humidity is %4.2f \r\n",humidity); 00144 printf("Dew point is %4.2f \r\n",sensor.CalcdewPoint(sensor.ReadTemperature(CELCIUS), sensor.ReadHumidity())); 00145 printf("Forcast Pressure: %4.2f C \r\n",forcastPressure); 00146 myled = 1; 00147 } else{ 00148 pc.printf("\r\nErr %i \n",err); 00149 myled = 0; 00150 } 00151 00152 00153 //sent to thingsspeak 00154 if(cnt==10){ //don't update as often 00155 cnt=0; 00156 memset(resp, '\0', sizeof(resp)); 00157 //POST data 00158 if(temperature!=-100){ //just check we have a valid temp 00159 map.put("api_key","YOUR KEY HERE"); 00160 sprintf(val[0],"%4.1f",temperature); 00161 map.put("field1", val[0]); 00162 sprintf(val[1],"%4.1f",humidity); 00163 map.put("field2", val[1]); 00164 sprintf(val[2],"%4.1f",forcastTemp); 00165 map.put("field3", val[2]); 00166 sprintf(val[3],"%4.1f",forcastPressure); 00167 map.put("field4", val[3]); 00168 pc.printf("\nTrying to post data...\n"); 00169 ret = http.post("https://api.thingspeak.com/update", map, &inText); 00170 if (!ret) 00171 { 00172 pc.printf("Executed POST successfully - read %d characters\n", strlen(resp)); 00173 pc.printf("Result: %s\n", resp); 00174 } 00175 else 00176 { 00177 pc.printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); 00178 } 00179 //Write Data to SDCARD 00180 fp = fopen(sdfile, "a"); 00181 if (fp == NULL) { 00182 pc.printf("Unable to write the file \n"); 00183 } else { 00184 pc.printf("Writing data to SDCARD \n"); 00185 //time,temp,humidity,forcast,pressure 00186 fprintf(fp, "%s,%4.1f,%4.1f,%4.1f,%4.1f\n", ctime(&ctTime),temperature,humidity,forcastTemp,forcastPressure); 00187 fclose(fp); 00188 } 00189 } 00190 00191 } 00192 00193 cnt++; 00194 wait_ms(6000); 00195 } 00196 }
Generated on Fri Jul 15 2022 02:15:53 by 1.7.2