for test
Dependencies: mbed STM32F4_RNG DHT BMP180
main.cpp
00001 #include "mbed.h" 00002 #include "STM32F4_RNG.h" 00003 #include "DHT.h" 00004 #include "BMP180.h" 00005 Serial pc(USBTX, USBRX); 00006 Serial esp(D8, D2); // tx D8, rx D2 00007 DigitalOut reset(D4); 00008 Timer t; 00009 00010 STM32F4_RNG RANDOM1; 00011 00012 int mycount,ended,timeout; 00013 char buf[1024]; 00014 char snd[255]; 00015 char ssid[32] = "King5"; // enter WiFi router ssid inside the quotes 00016 char pwd [32] = "88888888"; // enter WiFi router password inside the quotes 00017 00018 char apiKey[32] = "UADVBX13ELBYAO7M"; 00019 00020 unsigned long ranval; 00021 00022 #define TEMPVAL 23 00023 #define HUMIVAL 67 00024 #define RAYVAL 45 00025 #define PREVAL 340 00026 00027 int tempVal = 23; 00028 int humiVal = 67; 00029 int rayVal = 45; 00030 int pressVal = 340; 00031 unsigned long randVal; 00032 unsigned char shortrandVal= 0; 00033 float fhumi,ftemp,fray,fpress; 00034 00035 00036 void SendCMD(),getreply(),ESPconfig(),ESPsetbaudrate(),HTTPConfig(),PostData(); 00037 AnalogIn analog_value(A0); 00038 00039 00040 00041 DHT devDht(D7,DHT11); 00042 BMP180 bmp180(PB_14, PB_13); 00043 00044 00045 00046 int main() 00047 { 00048 reset=0; //hardware reset for 8266 00049 pc.baud(115200); // set what you want here depending on your terminal program speed 00050 pc.printf("\f\n\r-------------ESP8266 Hardware Rese444t-------------\n\r"); 00051 wait(0.5); 00052 reset=1; 00053 timeout=2; 00054 getreply(); 00055 esp.baud(115200); // change this to the new ESP8266 baudrate if it is changed at any time. 00056 //ESPsetbaudrate(); //****************** include this routine to set a different ESP8266 baudrate ****************** 00057 //ESPconfig(); //****************** include Config to set the ESP8266 configuration *********************** 00058 00059 00060 HTTPConfig(); 00061 00062 00063 // continuosly get AP list and IP 00064 while(1) 00065 { 00066 //pc.printf("get data from server\r\n"); 00067 00068 00069 PostData(); 00070 00071 00072 00073 } 00074 00075 } 00076 // Sets new ESP8266 baurate, change the esp.baud(xxxxx) to match your new setting once this has been executed 00077 void ESPsetbaudrate() 00078 { 00079 strcpy(snd, "AT+CIOBAUD=115200\r\n"); // change the numeric value to the required baudrate 00080 SendCMD(); 00081 } 00082 // +++++++++++++++++++++++++++++++++ This is for ESP8266 config only, run this once to set up the ESP8266 +++++++++++++++ 00083 void ESPconfig() 00084 { 00085 00086 } 00087 void SendCMD() 00088 { 00089 esp.printf("%s", snd); 00090 } 00091 void getreply() 00092 { 00093 memset(buf, '\0', sizeof(buf)); 00094 t.start(); 00095 ended=0; 00096 mycount=0; 00097 while(!ended) { 00098 if(esp.readable()) { 00099 buf[mycount] = esp.getc(); 00100 mycount++; 00101 } 00102 if(t.read() > timeout) { 00103 ended = 1; 00104 t.stop(); 00105 t.reset(); 00106 } 00107 } 00108 buf[mycount]=0; 00109 pc.printf("At respon:%s\r\n",buf); 00110 00111 00112 } 00113 00114 void HTTPConfig() 00115 { 00116 pc.printf("\n---------- Get Version ----------\r\n"); 00117 strcpy(snd,"AT+GMR\r\n"); 00118 SendCMD(); 00119 timeout=4; 00120 getreply(); 00121 pc.printf(buf); 00122 wait(3); 00123 // set CWMODE to 1=Station,2=AP,3=BOTH, default mode 1 (Station) 00124 pc.printf("\n---------- Setting Mode ----------\r\n"); 00125 strcpy(snd, "AT+CWMODE_DEF=1\r\n"); 00126 SendCMD(); 00127 timeout=4; 00128 getreply(); 00129 pc.printf(buf); 00130 wait(2); 00131 00132 pc.printf("\n---------- Connecting to AP ----------\r\n"); 00133 pc.printf("ssid = %s pwd = %s\r\n",ssid,pwd); 00134 strcpy(snd, "AT+CWJAP_DEF=\""); 00135 strcat(snd, ssid); 00136 strcat(snd, "\",\""); 00137 strcat(snd, pwd); 00138 strcat(snd, "\"\r\n"); 00139 SendCMD(); 00140 timeout=10; 00141 getreply(); 00142 pc.printf(buf); 00143 wait(5); 00144 00145 pc.printf("\n-----------set to auto connect -----\r\n"); 00146 strcpy(snd,"AT+CWAUTOCONN=1\r\n"); 00147 SendCMD(); 00148 timeout=4; 00149 getreply(); 00150 pc.printf(buf); 00151 wait(2); 00152 00153 pc.printf("\n-----------set sigle connect -----\r\n"); 00154 strcpy(snd,"AT+CIPMUX=0\r\n"); 00155 SendCMD(); 00156 timeout=4; 00157 getreply(); 00158 pc.printf(buf); 00159 wait(2); 00160 00161 pc.printf("\n------------connect to server --------\r\n"); 00162 strcpy(snd,"AT+CIPSTART=\"TCP\",\"api.thingspeak.com\",80\r\n"); 00163 SendCMD(); 00164 timeout=4; 00165 getreply(); 00166 pc.printf(buf); 00167 wait(2); 00168 00169 00170 pc.printf("\n------------set to touchuan --------\r\n"); 00171 strcpy(snd,"AT+CIPMODE=1\r\n"); 00172 SendCMD(); 00173 timeout=4; 00174 getreply(); 00175 pc.printf(buf); 00176 wait(2); 00177 00178 pc.printf("\n-----------starting send--------\r\n"); 00179 strcpy(snd,"AT+CIPSEND\r\n"); 00180 SendCMD(); 00181 timeout=4; 00182 getreply(); 00183 pc.printf(buf); 00184 wait(2); 00185 00186 00187 00188 00189 } 00190 char postStr[256]; 00191 void PostData() 00192 { 00193 int len; 00194 00195 00196 if(devDht.readData() == ERROR_NONE) 00197 { 00198 fhumi = devDht.ReadHumidity(); 00199 ftemp = devDht.ReadTemperature(CELCIUS); 00200 pc.printf("temp:%4.1f\thumi:%4.1f\n",ftemp,fhumi); 00201 } 00202 fray = analog_value.read()*3300; 00203 00204 bmp180.normalize(); 00205 fpress = bmp180.read_pressure(); 00206 //pressVal = bmp180.read_temperature(); 00207 00208 wait(2); 00209 //len = sprintf(snd,"GET https://api.thingspeak.com/update?api_key=07A5WIB5QT14WRGV&field1=%d&field2=%d&field3=%d&field4=%d\r\n",tempVal,humiVal,pressVal,rayVal); 00210 len = sprintf(snd,"GET https://api.thingspeak.com/update?api_key=07A5WIB5QT14WRGV&field1=%.1f&field2=%.1f&field3=%.1f&field4=%.2f\r\n",ftemp,fhumi,fpress,fray); 00211 00212 SendCMD(); 00213 00214 timeout=10; 00215 getreply(); 00216 pc.printf(buf); 00217 00218 pc.printf("\r\n%s",snd); 00219 wait(28); 00220 00221 } 00222
Generated on Tue Jul 12 2022 18:49:55 by 1.7.2