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: ATParser MPL3115A2 mbed
main.cpp
00001 #include "mbed.h" 00002 #include "MPL3115A2.h" 00003 #include "string" 00004 #include "ctype.h" 00005 #include "ATParser.h" 00006 #include "BufferedSerial.h" 00007 #include "MyBuffer.h" 00008 #include "TSL2561.h" 00009 #include <cstdarg> 00010 #include<stdio.h> 00011 #include<math.h> 00012 00013 TSL2561 lightsensor(PB_7,PB_6); 00014 DigitalOut led1(LED1); 00015 Serial pc(SERIAL_TX, SERIAL_RX, 115200); 00016 //Serial device(PA_9, PA_10, 115200); // tx, rx 00017 00018 00019 BufferedSerial serial = BufferedSerial(PA_9, PA_10); 00020 00021 DigitalOut myled(LED1); 00022 MPL3115A2 pressure_sensor(PB_7,PB_6,0x60); 00023 00024 bool s = true; 00025 char test[1]; 00026 00027 struct dataPoints 00028 { 00029 double pres; 00030 double temp; 00031 00032 }; 00033 00034 00035 00036 double lux[50]; 00037 dataPoints data[100]; 00038 00039 00040 00041 //function to join the network 00042 int JoinNetwork(){ 00043 00044 00045 pc.printf("attempting to join the network \n\r"); 00046 serial.baud(115200); 00047 pc.printf("Buffered serial created \n\r"); 00048 ATParser at = ATParser(serial, "\r\n" ,256, 8000, false); 00049 at.send("AT+NI=1,MTCDT-19400691"); 00050 wait(1); 00051 if(at.recv("OK")){ 00052 pc.printf("HEYO\n\r"); 00053 } else { 00054 pc.printf("BOO.\n\r"); 00055 } 00056 at.send("AT+NK=1,MTCDT-19400691"); 00057 wait(1); 00058 if(at.recv("OK")){ 00059 pc.printf("HEYO\n\r"); 00060 } else { 00061 pc.printf("BOO.\n\r"); 00062 } 00063 at.send("AT+FSB=1"); 00064 wait(1); 00065 if(at.recv("OK")){ 00066 pc.printf("HEYO\n\r"); 00067 } else { 00068 pc.printf("BOO.\n\r"); 00069 } 00070 at.send("AT+JOIN"); 00071 wait(1); 00072 if(at.recv("OK")){ 00073 pc.printf("HEYO\n\r"); 00074 return 1; 00075 } else { 00076 pc.printf("BOO.\n\r"); 00077 return 0; 00078 } 00079 00080 } 00081 00082 void Sdata(){ 00083 ATParser at = ATParser(serial, "\r\n" ,256, 8000, false); 00084 serial.baud(115200); 00085 dataPoints p1 = {0,0}; 00086 p1.temp = pressure_sensor.getTemperature(); 00087 p1.pres = pressure_sensor.getPressure(); 00088 double tempVal = p1.temp; 00089 double presVal = p1.pres; 00090 char tempStr[10]; 00091 char presStr[10]; 00092 sprintf(tempStr, "%f", tempVal); 00093 sprintf(presStr, "%f", presVal); 00094 at.send("AT+SEND=%0.5s,%0.5s", presStr, tempStr); 00095 00096 } 00097 00098 void sendData(){ 00099 dataPoints p1 = {0,0}; 00100 int counter = 0; 00101 int send = 1; 00102 while(1){ 00103 ATParser at = ATParser(serial, "\r\n" ,256, 8000, false); 00104 serial.baud(115200); 00105 00106 00107 00108 /* 00109 at.send("AT+NJS"); 00110 wait(1); 00111 if(at.recv("1")){} 00112 else { 00113 JoinNetwork(); 00114 } 00115 */ 00116 00117 00118 led1 = 1; 00119 double light = lightsensor.lux(); 00120 pc.printf("light %f \n\r", light); 00121 char luxStr[10]; 00122 lux[counter] = light; 00123 00124 pc.printf("lux val %f %d\n\r", lux[counter], counter); 00125 counter++; 00126 00127 p1.temp = pressure_sensor.getTemperature(); 00128 p1.pres = pressure_sensor.getPressure(); 00129 double tempVal = p1.temp; 00130 double presVal = p1.pres; 00131 char tempStr[10]; 00132 char presStr[10]; 00133 sprintf(tempStr, "%f", tempVal); 00134 sprintf(presStr, "%f", presVal); 00135 at.send("AT+SEND=%0.5s,%0.5s", presStr, tempStr); 00136 pc.printf("Pressure: %f hPA, temp: %f \n\r",presVal,tempVal); 00137 pc.printf("AT+SEND=%0.6s,%0.6s\0", presStr, tempStr); 00138 wait(1); 00139 if(at.recv("OK")){ 00140 pc.printf("data sent browski\n\r"); 00141 } else { 00142 pc.printf("That shit didnt send....rejoining network\n\r"); 00143 while(!JoinNetwork()); 00144 at.send("AT+SEND=%0.5s,%0.5s", presStr, tempStr); 00145 pc.printf("sent again \n\r"); 00146 } 00147 00148 00149 pc.printf("back to waiting \r"); 00150 wait(3600); 00151 //dump light data every 24hrs 00152 if(counter == 24){ 00153 while(counter > 0){ 00154 sprintf(luxStr, "%f", lux[counter]); 00155 wait(5); 00156 pc.printf("dumping light data: %s \n\r", luxStr); 00157 at.send("AT+SEND=%0.7s", luxStr); 00158 if(at.recv("OK")){ 00159 pc.printf("data sent browski\n\r"); 00160 } else { 00161 pc.printf("That shit didnt send :(\n\r"); 00162 while(!JoinNetwork()); 00163 at.send("AT+SEND=%0.5s,%0.5s", presStr, tempStr); 00164 pc.printf("sent again \n\r"); 00165 } 00166 counter--; 00167 } 00168 } 00169 } 00170 } 00171 00172 00173 00174 00175 00176 int main() { 00177 00178 // serial.baud(115200); 00179 // while(!JoinNetwork()); 00180 // pc.printf("Network Joined Successfully! \n\r"); 00181 // sendData(); 00182 } 00183 /* 00184 //pass through for tests and s press 00185 while(1 && s) { 00186 00187 if (pc.readable()){ 00188 test[0] = pc.getc(); 00189 if(test[0] == 'S'){ 00190 pc.printf("GOT AN S"); 00191 Sdata(); 00192 } 00193 } 00194 } 00195 sendData(); 00196 00197 if(pc.readable()) { 00198 serial.putc(pc.getc()); 00199 myled = !myled; 00200 } 00201 if(serial.readable()) { 00202 pc.putc(serial.getc()); 00203 myled = !myled; 00204 } 00205 } 00206 */ 00207 00208 00209 00210 00211 00212
Generated on Thu Jul 14 2022 18:15:15 by
1.7.2