Alexandru Albul Albul / Mbed 2 deprecated mbed1768FinalProject

Dependencies:   mbed MbedJSONValue HTTPClient TFT_fonts SPI_TFT_ILI9341 mbed-rtos picojsontest NTPClient SDFileSystem EthernetInterface DHT Stepper_Motor_X27168

Committer:
aalbul3
Date:
Wed May 01 04:11:18 2019 +0000
Revision:
4:1eea5edaffc4
Parent:
1:f9a4cbb4af0d
This is a test

Who changed what in which revision?

UserRevisionLine numberNew contents of line
colinmeikle 0:9ab281898a9b 1 #include "mbed.h"
colinmeikle 0:9ab281898a9b 2 #include "SPI_TFT_ILI9341.h"
colinmeikle 0:9ab281898a9b 3 #include "sansSerif19x20.h"
colinmeikle 0:9ab281898a9b 4 #include "DHT.h"
colinmeikle 0:9ab281898a9b 5 #include "EthernetInterface.h"
colinmeikle 0:9ab281898a9b 6 #include "NTPClient.h"
dshoneye 1:f9a4cbb4af0d 7 #include "HTTPClient.h"
colinmeikle 0:9ab281898a9b 8 #include "picojson.h"
colinmeikle 0:9ab281898a9b 9 #include "SDFileSystem.h"
dshoneye 1:f9a4cbb4af0d 10 #include <MbedJSONValue.h>
dshoneye 1:f9a4cbb4af0d 11 #include <string>
aalbul3 4:1eea5edaffc4 12 #include "StepperMotor_X27168.h"
colinmeikle 0:9ab281898a9b 13 //IMPORTANT YOU NEED TO SET THE API KEY for thingspeak look for "your key here"
colinmeikle 0:9ab281898a9b 14 //TAKE CARE URL doesn't excced buffer size
colinmeikle 0:9ab281898a9b 15 //sd card
dshoneye 1:f9a4cbb4af0d 16 // SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // MOSI, MISO, SCLK, SSEL
colinmeikle 0:9ab281898a9b 17 //Ethernet Interface
aalbul3 4:1eea5edaffc4 18
aalbul3 4:1eea5edaffc4 19 DigitalOut communicate(p30); ///Pull low until ready to go
aalbul3 4:1eea5edaffc4 20 DigitalOut motor3(p23);
aalbul3 4:1eea5edaffc4 21 StepperMotor_X27168 smotor3(p24,p25,p22,p21);
aalbul3 4:1eea5edaffc4 22
aalbul3 4:1eea5edaffc4 23
colinmeikle 0:9ab281898a9b 24 EthernetInterface eth;
colinmeikle 0:9ab281898a9b 25 //NTP service for date and Time used to set boards RTC
colinmeikle 0:9ab281898a9b 26 NTPClient ntp;
colinmeikle 0:9ab281898a9b 27 //HTTP Client for interfacing to web services
colinmeikle 0:9ab281898a9b 28 HTTPClient http;
dshoneye 1:f9a4cbb4af0d 29 //char resp[1024]; //buffer for the responce
colinmeikle 0:9ab281898a9b 30 //On board LED
dshoneye 1:f9a4cbb4af0d 31 //DigitalOut myled(LED_GREEN);
colinmeikle 0:9ab281898a9b 32 //serial over USB for debug and Info
colinmeikle 0:9ab281898a9b 33 Serial pc(USBTX, USBRX);
dshoneye 1:f9a4cbb4af0d 34 std::string resp;
colinmeikle 0:9ab281898a9b 35 //temperature sensor
dshoneye 1:f9a4cbb4af0d 36 //DHT sensor(PTB18,DHT22); // there are several libraries available, this reports CRC error sometimes
colinmeikle 0:9ab281898a9b 37 //320x240 Display ILI9341 controller (look on ebay)
dshoneye 1:f9a4cbb4af0d 38 //SPI_TFT_ILI9341 TFT(PTD2, PTD3, PTD1,PTC3 ,PTC4 ,PTD0 ,"TFT"); // mosi, miso, sclk, cs, reset, dc
dshoneye 1:f9a4cbb4af0d 39
dshoneye 1:f9a4cbb4af0d 40 MbedJSONValue v;
colinmeikle 0:9ab281898a9b 41
aalbul3 4:1eea5edaffc4 42 volatile char c = '\0'; // Initialized to the NULL character
aalbul3 4:1eea5edaffc4 43 volatile int check = 0;
aalbul3 4:1eea5edaffc4 44 Serial device(p28,p27); //9-10 11u; 28-27 1768
aalbul3 4:1eea5edaffc4 45 void onCharReceived()
aalbul3 4:1eea5edaffc4 46 {
aalbul3 4:1eea5edaffc4 47 c = device.getc();
aalbul3 4:1eea5edaffc4 48 check = 1;
aalbul3 4:1eea5edaffc4 49 }
aalbul3 4:1eea5edaffc4 50
colinmeikle 0:9ab281898a9b 51 int main()
colinmeikle 0:9ab281898a9b 52 {
aalbul3 4:1eea5edaffc4 53 communicate = 0;
aalbul3 4:1eea5edaffc4 54 pc.printf("\r\nWeather Reporter\n");
aalbul3 4:1eea5edaffc4 55
aalbul3 4:1eea5edaffc4 56 // while (1) {
aalbul3 4:1eea5edaffc4 57 // wait(.1);
aalbul3 4:1eea5edaffc4 58 // }
aalbul3 4:1eea5edaffc4 59 // check = 0;
aalbul3 4:1eea5edaffc4 60
aalbul3 4:1eea5edaffc4 61 // device.attach(&onCharReceived);
aalbul3 4:1eea5edaffc4 62
aalbul3 4:1eea5edaffc4 63
dshoneye 1:f9a4cbb4af0d 64
colinmeikle 0:9ab281898a9b 65 int ret=0;
colinmeikle 0:9ab281898a9b 66
dshoneye 1:f9a4cbb4af0d 67 //used to parse the json data
dshoneye 1:f9a4cbb4af0d 68 // picojson::value v;
dshoneye 1:f9a4cbb4af0d 69 // char * json;
dshoneye 1:f9a4cbb4af0d 70 std::string json;
dshoneye 1:f9a4cbb4af0d 71 HTTPMap map;
colinmeikle 0:9ab281898a9b 72
dshoneye 1:f9a4cbb4af0d 73 pc.printf("\r\nWeather Reporter\n");
dshoneye 1:f9a4cbb4af0d 74
colinmeikle 0:9ab281898a9b 75
colinmeikle 0:9ab281898a9b 76
dshoneye 1:f9a4cbb4af0d 77
colinmeikle 0:9ab281898a9b 78
dshoneye 1:f9a4cbb4af0d 79 eth.init(); //Use DHCP
dshoneye 1:f9a4cbb4af0d 80 eth.connect();
dshoneye 1:f9a4cbb4af0d 81 pc.printf("IP Address is %s\r\n", eth.getIPAddress());
dshoneye 1:f9a4cbb4af0d 82 std::string mac;
dshoneye 1:f9a4cbb4af0d 83 mac = eth.getMACAddress();
dshoneye 1:f9a4cbb4af0d 84 pc.printf("MAC: %s\r\n", mac);
dshoneye 1:f9a4cbb4af0d 85 TCPSocketConnection sock;
aalbul3 4:1eea5edaffc4 86
dshoneye 1:f9a4cbb4af0d 87 sock.connect("api.openweathermap.org", 80);
aalbul3 4:1eea5edaffc4 88 wait(5);
dshoneye 1:f9a4cbb4af0d 89 char http_cmd[] = "GET /data/2.5/weather?q=Atlanta,US&APPID=a1c3afc7f18b7ef578cef48d5163cda6 HTTP/1.0\r\n\r\n";
dshoneye 1:f9a4cbb4af0d 90 if (!sock.is_connected()){
aalbul3 4:1eea5edaffc4 91 pc.printf("This not working");
dshoneye 1:f9a4cbb4af0d 92 return -1;
dshoneye 1:f9a4cbb4af0d 93 }
dshoneye 1:f9a4cbb4af0d 94 sock.send_all(http_cmd, sizeof(http_cmd)-1);
dshoneye 1:f9a4cbb4af0d 95 char buffer[1000];
dshoneye 1:f9a4cbb4af0d 96 //int ret;
dshoneye 1:f9a4cbb4af0d 97 while (true) {
dshoneye 1:f9a4cbb4af0d 98 ret = sock.receive(buffer, sizeof(buffer)-1);
dshoneye 1:f9a4cbb4af0d 99 if (ret <= 0)
dshoneye 1:f9a4cbb4af0d 100 break;
dshoneye 1:f9a4cbb4af0d 101 buffer[ret] = '\0';
dshoneye 1:f9a4cbb4af0d 102 pc.printf("Received %d chars from server:\n%s\n", ret, buffer);
dshoneye 1:f9a4cbb4af0d 103 }
dshoneye 1:f9a4cbb4af0d 104
dshoneye 1:f9a4cbb4af0d 105 sock.close();
dshoneye 1:f9a4cbb4af0d 106
dshoneye 1:f9a4cbb4af0d 107 eth.disconnect();
aalbul3 4:1eea5edaffc4 108
colinmeikle 0:9ab281898a9b 109
dshoneye 1:f9a4cbb4af0d 110 float forecastHumidity=0;
dshoneye 1:f9a4cbb4af0d 111 // float temperature=-100;
colinmeikle 0:9ab281898a9b 112 float forcastTemp=0;
dshoneye 1:f9a4cbb4af0d 113 float forcastDescription = 0;
dshoneye 1:f9a4cbb4af0d 114 // float forcastPressure;
aalbul3 4:1eea5edaffc4 115 wait(2);
colinmeikle 0:9ab281898a9b 116 pc.printf("\r\nDHT Test program");
colinmeikle 0:9ab281898a9b 117 pc.printf("\r\n******************\r\n");
colinmeikle 0:9ab281898a9b 118 wait(1); // wait 1 second for device stable status
dshoneye 1:f9a4cbb4af0d 119 char cstr[resp.size() + 1];
colinmeikle 0:9ab281898a9b 120 while(1){
colinmeikle 0:9ab281898a9b 121
colinmeikle 0:9ab281898a9b 122 //GET forcast data
colinmeikle 0:9ab281898a9b 123 pc.printf("\nTrying to fetch page...\n");
colinmeikle 0:9ab281898a9b 124 //try getting the weather, 5 second timeout
dshoneye 1:f9a4cbb4af0d 125 // HTTPResult r = http.get("http://api.openweathermap.org/data/2.5/weather?q=Atlanta,US&APPID=a1c3afc7f18b7ef578cef48d5163cda6", resp, 5000);
dshoneye 1:f9a4cbb4af0d 126 // HTTPResult r = http.get("www.google.com", resp, 4096, 20000);
dshoneye 1:f9a4cbb4af0d 127 // ret = http.get("http://api.openweathermap.org/data/2.5/find?q=London",resp, 15000);
colinmeikle 0:9ab281898a9b 128 if(!ret){
dshoneye 1:f9a4cbb4af0d 129 std::string b2s(buffer, 1000);
dshoneye 1:f9a4cbb4af0d 130 int i = b2s.find("\r\n\r\n",0);
dshoneye 1:f9a4cbb4af0d 131 resp.assign(buffer+i+4);
colinmeikle 0:9ab281898a9b 132 pc.printf("responce=%s",resp);
dshoneye 1:f9a4cbb4af0d 133 strcpy(cstr, resp.c_str());
dshoneye 1:f9a4cbb4af0d 134 int len = strlen(cstr);
dshoneye 1:f9a4cbb4af0d 135 cstr[5] = '\n';
dshoneye 1:f9a4cbb4af0d 136 // json = NULL;
dshoneye 1:f9a4cbb4af0d 137 //char * result = NULL;
dshoneye 1:f9a4cbb4af0d 138 // asprintf(&result, "%s%s", json, "NULL");
dshoneye 1:f9a4cbb4af0d 139 // std::string sJson(json);
dshoneye 1:f9a4cbb4af0d 140 // pc.printf("parsing");
dshoneye 1:f9a4cbb4af0d 141 // const char * json = "{\"my_array\": [\"demo_string\", 10], \"my_boolean\": true}";
dshoneye 1:f9a4cbb4af0d 142
dshoneye 1:f9a4cbb4af0d 143 //parse the previous string and fill the object demo
dshoneye 1:f9a4cbb4af0d 144 std::string newline= "\n";
dshoneye 1:f9a4cbb4af0d 145 resp.append(newline);
dshoneye 1:f9a4cbb4af0d 146
dshoneye 1:f9a4cbb4af0d 147
dshoneye 1:f9a4cbb4af0d 148
dshoneye 1:f9a4cbb4af0d 149 // std::string error = parse(v, cstr);
dshoneye 1:f9a4cbb4af0d 150 json = cstr;
dshoneye 1:f9a4cbb4af0d 151 struct weatherData{
dshoneye 1:f9a4cbb4af0d 152 std::string temp;
dshoneye 1:f9a4cbb4af0d 153 std::string desc;
dshoneye 1:f9a4cbb4af0d 154 std::string hum;
dshoneye 1:f9a4cbb4af0d 155 };
dshoneye 1:f9a4cbb4af0d 156
dshoneye 1:f9a4cbb4af0d 157 int beg, end;
dshoneye 1:f9a4cbb4af0d 158 weatherData weather;
dshoneye 1:f9a4cbb4af0d 159 //first find description
dshoneye 1:f9a4cbb4af0d 160 beg = json.find("description") + 14;
dshoneye 1:f9a4cbb4af0d 161 end = json.find("," , beg) - 1;
dshoneye 1:f9a4cbb4af0d 162 weather.desc = json.substr(beg,end-beg);
dshoneye 1:f9a4cbb4af0d 163 //next find temperature
dshoneye 1:f9a4cbb4af0d 164 beg = json.find("temp", end) + 6;
dshoneye 1:f9a4cbb4af0d 165 end = json.find("," , beg);
dshoneye 1:f9a4cbb4af0d 166 weather.temp = json.substr(beg,end-beg);;
dshoneye 1:f9a4cbb4af0d 167 //last find humidity
dshoneye 1:f9a4cbb4af0d 168 beg = json.find("humidity", end) + 10;
dshoneye 1:f9a4cbb4af0d 169 end = json.find(",", beg);
dshoneye 1:f9a4cbb4af0d 170 weather.hum = json.substr(beg,end-beg);
dshoneye 1:f9a4cbb4af0d 171 //
dshoneye 1:f9a4cbb4af0d 172 // pc.printf("humidity: %s", weather.hum);
dshoneye 1:f9a4cbb4af0d 173 // pc.printf("temp: %s", weather.temp);
dshoneye 1:f9a4cbb4af0d 174 // pc.printf("desc: %s", weather.desc);
dshoneye 1:f9a4cbb4af0d 175
dshoneye 1:f9a4cbb4af0d 176 // get time
dshoneye 1:f9a4cbb4af0d 177 std::time_t t = std::time(0);
dshoneye 1:f9a4cbb4af0d 178 std::time_t result = time(NULL);
dshoneye 1:f9a4cbb4af0d 179 // std::cout << std::asctime(std::localtime(&result))
dshoneye 1:f9a4cbb4af0d 180
aalbul3 4:1eea5edaffc4 181
aalbul3 4:1eea5edaffc4 182
aalbul3 4:1eea5edaffc4 183 // pc.printf("\n\nCurrent Time: %s \r\n",ctime(&result));
aalbul3 4:1eea5edaffc4 184 // pc.printf("Current Temperature: %s Kelvin \r\n",weather.temp);
aalbul3 4:1eea5edaffc4 185 // pc.printf("Humidity is %s \r\n",weather.hum);
aalbul3 4:1eea5edaffc4 186 // pc.printf("Weather Condition: %s \r\n", weather.desc);
aalbul3 4:1eea5edaffc4 187 //
aalbul3 4:1eea5edaffc4 188 double weather_temp = atof(weather.temp.c_str());
aalbul3 4:1eea5edaffc4 189 //double weather_temp = 301.483;
aalbul3 4:1eea5edaffc4 190 weather_temp = weather_temp - 273.15;
aalbul3 4:1eea5edaffc4 191 weather_temp = (weather_temp * 9/5) + 32;
aalbul3 4:1eea5edaffc4 192 // range is 60 - 85
aalbul3 4:1eea5edaffc4 193 weather_temp = weather_temp - 60;
aalbul3 4:1eea5edaffc4 194 weather_temp = weather_temp * 360/25;
colinmeikle 0:9ab281898a9b 195
aalbul3 4:1eea5edaffc4 196 wait(5);
aalbul3 4:1eea5edaffc4 197 motor3 = 1;
aalbul3 4:1eea5edaffc4 198 smotor3.step_position(weather_temp);
aalbul3 4:1eea5edaffc4 199 wait(1.5);
aalbul3 4:1eea5edaffc4 200 communicate = 1;
dshoneye 1:f9a4cbb4af0d 201
colinmeikle 0:9ab281898a9b 202
dshoneye 1:f9a4cbb4af0d 203 wait(60);
colinmeikle 0:9ab281898a9b 204 }
aalbul3 4:1eea5edaffc4 205
aalbul3 4:1eea5edaffc4 206 }}