Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed MbedJSONValue HTTPClient TFT_fonts SPI_TFT_ILI9341 mbed-rtos picojsontest NTPClient SDFileSystem EthernetInterface DHT Stepper_Motor_X27168
main.cpp
- Committer:
- dshoneye
- Date:
- 2019-04-29
- Revision:
- 1:f9a4cbb4af0d
- Parent:
- 0:9ab281898a9b
- Child:
- 4:1eea5edaffc4
File content as of revision 1:f9a4cbb4af0d:
#include "mbed.h"
#include "SPI_TFT_ILI9341.h"
#include "sansSerif19x20.h"
#include "DHT.h"
#include "EthernetInterface.h"
#include "NTPClient.h"
#include "HTTPClient.h"
#include "picojson.h"
#include "SDFileSystem.h"
#include <MbedJSONValue.h>
#include <string>
//IMPORTANT YOU NEED TO SET THE API KEY for thingspeak look for "your key here"
//TAKE CARE URL doesn't excced buffer size
//sd card
// SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // MOSI, MISO, SCLK, SSEL
//Ethernet Interface
EthernetInterface eth;
//NTP service for date and Time used to set boards RTC
NTPClient ntp;
//HTTP Client for interfacing to web services
HTTPClient http;
//char resp[1024]; //buffer for the responce
//On board LED
//DigitalOut myled(LED_GREEN);
//serial over USB for debug and Info
Serial pc(USBTX, USBRX);
std::string resp;
//temperature sensor
//DHT sensor(PTB18,DHT22); // there are several libraries available, this reports CRC error sometimes
//320x240 Display ILI9341 controller (look on ebay)
//SPI_TFT_ILI9341 TFT(PTD2, PTD3, PTD1,PTC3 ,PTC4 ,PTD0 ,"TFT"); // mosi, miso, sclk, cs, reset, dc
MbedJSONValue v;
int main()
{
int ret=0;
//used to parse the json data
// picojson::value v;
// char * json;
std::string json;
HTTPMap map;
pc.printf("\r\nWeather Reporter\n");
eth.init(); //Use DHCP
eth.connect();
pc.printf("IP Address is %s\r\n", eth.getIPAddress());
std::string mac;
mac = eth.getMACAddress();
pc.printf("MAC: %s\r\n", mac);
TCPSocketConnection sock;
sock.connect("api.openweathermap.org", 80);
char http_cmd[] = "GET /data/2.5/weather?q=Atlanta,US&APPID=a1c3afc7f18b7ef578cef48d5163cda6 HTTP/1.0\r\n\r\n";
if (!sock.is_connected()){
return -1;
}
sock.send_all(http_cmd, sizeof(http_cmd)-1);
char buffer[1000];
//int ret;
while (true) {
ret = sock.receive(buffer, sizeof(buffer)-1);
if (ret <= 0)
break;
buffer[ret] = '\0';
pc.printf("Received %d chars from server:\n%s\n", ret, buffer);
}
sock.close();
eth.disconnect();
float forecastHumidity=0;
// float temperature=-100;
float forcastTemp=0;
float forcastDescription = 0;
// float forcastPressure;
wait(10);
pc.printf("\r\nDHT Test program");
pc.printf("\r\n******************\r\n");
wait(1); // wait 1 second for device stable status
char cstr[resp.size() + 1];
while(1){
//GET forcast data
pc.printf("\nTrying to fetch page...\n");
//try getting the weather, 5 second timeout
// HTTPResult r = http.get("http://api.openweathermap.org/data/2.5/weather?q=Atlanta,US&APPID=a1c3afc7f18b7ef578cef48d5163cda6", resp, 5000);
// HTTPResult r = http.get("www.google.com", resp, 4096, 20000);
// ret = http.get("http://api.openweathermap.org/data/2.5/find?q=London",resp, 15000);
if(!ret){
std::string b2s(buffer, 1000);
int i = b2s.find("\r\n\r\n",0);
resp.assign(buffer+i+4);
pc.printf("responce=%s",resp);
strcpy(cstr, resp.c_str());
int len = strlen(cstr);
cstr[5] = '\n';
// json = NULL;
//char * result = NULL;
// asprintf(&result, "%s%s", json, "NULL");
// std::string sJson(json);
// pc.printf("parsing");
// const char * json = "{\"my_array\": [\"demo_string\", 10], \"my_boolean\": true}";
//parse the previous string and fill the object demo
std::string newline= "\n";
resp.append(newline);
// std::string error = parse(v, cstr);
json = cstr;
struct weatherData{
std::string temp;
std::string desc;
std::string hum;
};
int beg, end;
weatherData weather;
//first find description
beg = json.find("description") + 14;
end = json.find("," , beg) - 1;
weather.desc = json.substr(beg,end-beg);
//next find temperature
beg = json.find("temp", end) + 6;
end = json.find("," , beg);
weather.temp = json.substr(beg,end-beg);;
//last find humidity
beg = json.find("humidity", end) + 10;
end = json.find(",", beg);
weather.hum = json.substr(beg,end-beg);
//
// pc.printf("humidity: %s", weather.hum);
// pc.printf("temp: %s", weather.temp);
// pc.printf("desc: %s", weather.desc);
// get time
std::time_t t = std::time(0);
std::time_t result = time(NULL);
// std::cout << std::asctime(std::localtime(&result))
printf("\n\nCurrent Time: %s \r\n",ctime(&result));
printf("Current Temperature: %s Kelvin \r\n",weather.temp);
printf("Humidity is %s \r\n",weather.hum);
printf("Weather Condition: %s \r\n", weather.desc);
wait(60);
}
}
}