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: MMA7660 ublox-at-cellular-interface ublox-cellular-base C12832 LM75B ublox-cellular-base-n2xx ublox-at-cellular-interface-n2xx
main.cpp
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2017 u-blox 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 00018 /* This example program for the u-blox C030 board. 00019 * The UbloxATCellularInterface or OnboardCellularInterface uses it 00020 * to make a simple HTTP connection to a STC IOT server. 00021 * Progress may be monitored with a serial terminal running at 9600 baud or at https://ksu.aep1.iot.stcs.com.sa 00022 * The LED on the C030 board will turn green when this program is 00023 * operating correctly, pulse blue when a sockets operation is completed 00024 * and turn red if there is a failure. 00025 00026 * Modified by: Muhammad Faheem Khan (Digital Solutions, STCS) 00027 */ 00028 00029 #include "mbed.h" 00030 #include "UbloxATCellularInterface.h" 00031 #include "UbloxATCellularInterfaceN2xx.h" 00032 #include "LM75B.h" // Temperature Sensor Library. Import more libraries from https://os.mbed.com/components/mbed-Application-Shield/ 00033 #include "C12832.h" // LCD Library. Import more libraries from https://os.mbed.com/components/mbed-Application-Shield/ 00034 #include "MMA7660.h" //HERE 00035 00036 // Sensor Pin and Object Activation 00037 C12832 lcd(D11, D13, D12, D7, D10); 00038 //LM75B sensor(D14,D15); 00039 MMA7660 MMA(D14,D15); //HERE 00040 PwmOut spkr(D6); 00041 00042 #define INTERFACE_CLASS UbloxATCellularInterface //This interface is used to initiate modem commands for sending data to sever 00043 00044 // The credentials of the SIM in the board. If PIN checking is enabled 00045 // for your SIM card you must set this to the required PIN. 00046 #define PIN "0000" 00047 #define APN "jtm2m" //APN remains the same for all the kits 00048 #define USERNAME NULL 00049 #define PASSWORD NULL 00050 #define TCP_SERVER "integration.campus.stcs.io" //STC IoT parsing server address 00051 00052 // LEDs 00053 //DigitalOut connectionLed(LED1);//operation confirmed HERE 00054 DigitalOut servoProxyLed(LED2);//score outcome, proxy for servo HERE 00055 DigitalOut ledRed(LED1, 1); 00056 DigitalOut ledGreen(LED2, 1); 00057 DigitalOut ledBlue(LED3, 1); 00058 DigitalIn fire(D4); 00059 00060 00061 // The user button 00062 volatile bool buttonPressed = false; 00063 00064 00065 static void good() { 00066 ledGreen = 0; 00067 ledBlue = 1; 00068 ledRed = 1; 00069 } 00070 00071 static void bad() { 00072 ledRed = 0; 00073 ledGreen = 1; 00074 ledBlue = 1; 00075 } 00076 00077 static void event() { 00078 ledBlue = 0; 00079 ledRed = 1; 00080 ledGreen = 1; 00081 } 00082 00083 static void pulseEvent() { 00084 event(); 00085 wait_ms(500); 00086 good(); 00087 } 00088 00089 static void ledOff() { 00090 ledBlue = 1; 00091 ledRed = 1; 00092 ledGreen = 1; 00093 } 00094 static void cbButton() 00095 { 00096 buttonPressed = true; 00097 pulseEvent(); 00098 } 00099 00100 00101 00102 00103 float stable= 0 ; 00104 const int n = 10; //number of readings to be averaged, change globally here HERE 00105 float score = 0; //reserved for later HERE 00106 int angle = 0; //reserved for later HERE 00107 float pulseXT =0, pulseYT = 0; //Total holders for summation from axis arrays HERE 00108 00109 float pulseXa, pulseYa; //averaged values for each axis over n HERE 00110 float pulseX[n], pulseY[n]; //arrays to hold n readings for each axis HERE 00111 int i, j; //indexing variables HERE 00112 char xval[100]; // this buffer will be used to store sensor values 00113 char yval[100]; // this buffer will be used to store sensor values 00114 char zval[100]; // this buffer will be used to store sensor values 00115 char buf[1024]; // this buffer is used to send POST data to platform 00116 char deviceID[20] = "357520074039765"; 00117 //void fun(char *str,float x) 00118 //{ 00119 //snprintf(str,sizeof str,"X-axis=%.2f", x); 00120 //snprintf(buf, sizeof buf, "%s%s%s%s%s%s", "POST /ksu/?deviceID=",deviceID,"&",str,"&unit=T", " HTTP/1.0\r\n\r\n"); 00121 00122 // printf(buf); 00123 00124 //} 00125 int main() 00126 { 00127 INTERFACE_CLASS *interface = new INTERFACE_CLASS(); 00128 // If you need to debug the cellular interface, comment out the 00129 // instantiation above and uncomment the one below. 00130 // For the N2xx interface, change xxx to MBED_CONF_UBLOX_CELL_BAUD_RATE, 00131 // while for the non-N2xx interface change it to MBED_CONF_UBLOX_CELL_N2XX_BAUD_RATE. 00132 // INTERFACE_CLASS *interface = new INTERFACE_CLASS(MDMTXD, MDMRXD, 00133 // xxx, 00134 // true); 00135 #ifndef TARGET_UBLOX_C030_N211 00136 TCPSocket sockTcp; 00137 TCPSocket sockTcp2; 00138 TCPSocket sockTcp3; 00139 00140 #endif 00141 00142 SocketAddress tcpServer; 00143 //char deviceID[20] = "357520074039765"; 00144 // char buf[1024]; // this buffer is used to send POST data to platform 00145 char buf2[1024]; 00146 char buf3[1024]; 00147 char temp[50]; // this buffer will be used to store sensor values 00148 int x; 00149 00150 #ifdef TARGET_UBLOX_C027 00151 // No user button on C027 00152 InterruptIn userButton(NC); 00153 #else 00154 InterruptIn userButton(SW0); 00155 #endif 00156 00157 // Attach a function to the user button 00158 userButton.rise(&cbButton); 00159 00160 good(); 00161 printf("Starting up, please wait up to 180 seconds for network registration to complete...\n"); 00162 interface->set_credentials(APN, USERNAME, PASSWORD); 00163 for (x = 0; interface->connect(PIN) != 0; x++) { 00164 if (x > 0) { 00165 bad(); 00166 printf("Retrying (have you checked that an antenna is plugged in and your APN is correct?)...\n"); 00167 } 00168 } 00169 pulseEvent(); 00170 00171 printf("Getting the IP address of \"integration.campus.stcs.io\"...\n"); 00172 if (interface->gethostbyname(TCP_SERVER, &tcpServer) == 0) { 00173 pulseEvent(); 00174 00175 tcpServer.set_port(1880); 00176 //tcpServer2.set_port(1881); 00177 printf("\"integration.campus.stcs.io\" address: %s on port %d.\n", tcpServer.get_ip_address(), tcpServer.get_port()); 00178 00179 printf("Performing socket operations in a loop (until the user button is pressed on C030)...\n"); 00180 while (!buttonPressed) { 00181 00182 #ifndef TARGET_UBLOX_C030_N211 00183 lcd.cls(); 00184 lcd.locate(0,3); 00185 lcd.printf("Connecting"); 00186 00187 // TCP Sockets 00188 printf("=== TCP ===\n"); 00189 printf("Opening a TCP socket...\n"); 00190 if (sockTcp.open(interface) == 0) { 00191 pulseEvent(); 00192 printf("TCP socket open.\n"); 00193 sockTcp.set_timeout(10000); 00194 printf("Connecting socket to %s on port %d...\n", tcpServer.get_ip_address(), tcpServer.get_port()); 00195 if (sockTcp.connect(tcpServer) == 0) { 00196 pulseEvent(); 00197 lcd.cls(); 00198 lcd.locate(0,3); 00199 lcd.printf("Connected"); 00200 wait(2.0); 00201 printf("Connected, sending HTTP GET request to %s over socket...\n", TCP_SERVER); 00202 lcd.cls(); 00203 lcd.locate(0,3); 00204 wait(1.0); 00205 printf("=== TCP ===\n"); 00206 printf("Opening a TCP socket 2...\n"); 00207 if (sockTcp2.open(interface) == 0) { 00208 pulseEvent(); 00209 printf("TCP socket open.\n"); 00210 sockTcp2.set_timeout(10000); 00211 printf("Connecting socket to %s on port %d...\n", tcpServer.get_ip_address(), tcpServer.get_port());// kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk 00212 if (sockTcp2.connect(tcpServer) == 0) { 00213 pulseEvent(); 00214 lcd.cls(); 00215 lcd.locate(0,3); 00216 lcd.printf("Connected"); 00217 wait(2.0); 00218 printf("Connected, sending HTTP GET request to %s over socket...\n", TCP_SERVER); 00219 lcd.cls(); 00220 lcd.locate(0,3); 00221 wait(1.0); 00222 printf("=== TCP ===\n"); 00223 printf("Opening a TCP socket3...\n"); 00224 if (sockTcp3.open(interface) == 0) { 00225 pulseEvent(); 00226 printf("TCP socket open.\n"); 00227 sockTcp3.set_timeout(10000); 00228 printf("Connecting socket to %s on port %d...\n", tcpServer.get_ip_address(), tcpServer.get_port()); 00229 if (sockTcp3.connect(tcpServer) == 0) { 00230 pulseEvent(); 00231 lcd.cls(); 00232 lcd.locate(0,3); 00233 lcd.printf("Connected"); 00234 wait(2.0); 00235 printf("Connected, sending HTTP GET request to %s over socket...\n", TCP_SERVER); 00236 lcd.cls(); 00237 lcd.locate(0,3); 00238 00239 // Sensor Data is stored in temp variable before pushing to platform 00240 00241 // while(1) { //HERE 00242 00243 // for (i = 0; i < n; i = i + 1) { //read n values into each axis array 00244 // pulseX[i] = MMA.x(); 00245 // pulseY[i] = MMA.y(); 00246 //} 00247 //pulseXT = 0; //reset Totala 00248 // pulseYT = 0; //reset Totala 00249 // for (j = 0; j < n; j = j + 1) { //summation of the contents of each array into axis Totals 00250 // pulseXT = pulseXT+pulseX[j]; 00251 // pulseYT = pulseYT+pulseY[j]; 00252 //} 00253 //pulseXa = pulseXT/n; //axis average over n 00254 pulseXa = MMA.x(); 00255 // pulseYa = pulseYT/n; //axis average over n 00256 pulseYa = MMA.y(); 00257 //if (MMA.testConnection()) 00258 // connectionLed = 1; 00259 00260 if (pulseXa > (-0.2) && pulseXa < (0.2) && pulseYa > (-0.2) && pulseYa < (0.2)) {//average result within stability range; x, y 00261 lcd.cls();//clear LCD for next reading round 00262 lcd.locate(3,3);//first LCD column label 00263 lcd.printf("x-axis | ");//label column 00264 lcd.locate(3,12);//xdata location 00265 lcd.printf("%.2f\n",pulseXa);//print x to LCD 00266 lcd.locate(40,3);//second LCD column label 00267 lcd.printf("y-axis | ");//label column 00268 lcd.locate(40,12);//ydata location 00269 lcd.printf("%.2f\n",pulseYa);//print y to LCD 00270 lcd.locate(77,3);//initial LCD location 00271 lcd.printf("z-axis");//label column 00272 lcd.locate(77,12);//zdata location 00273 lcd.printf("%.2f\n",MMA.z());//print z to LCD 00274 lcd.locate(3,21);//flag location 00275 lcd.printf("STABLE");//flag 00276 stable = 1 ; 00277 wait_ms(2000); 00278 00279 } 00280 00281 else {////average result not within stability range; x, y 00282 lcd.cls();//clear LCD for next reading round 00283 lcd.locate(3,3);//first LCD column label 00284 lcd.printf("x-axis | ");//label column 00285 lcd.locate(3,12);//xdata location 00286 lcd.printf("%.2f\n",pulseXa);//print x to LCD 00287 lcd.locate(40,3);//second LCD column label 00288 lcd.printf("y-axis | ");//label column 00289 lcd.locate(40,12);//ydata location 00290 lcd.printf("%.2f\n",pulseYa);//print y to LCD 00291 lcd.locate(77,3);//initial LCD location 00292 lcd.printf("z-axis");//label column 00293 lcd.locate(77,12);//zdata location 00294 lcd.printf("%.2f\n",MMA.z());//print z to LCD 00295 lcd.locate(3,21);//flag location 00296 lcd.printf("UNSTABLE");//flag 00297 stable = 0 ; 00298 wait_ms(2000); 00299 00300 int snd = 0; 00301 while (snd != 1) { 00302 snd++; 00303 for (float i=2000.0; i<10000.0; i+=100) { 00304 spkr.period(1.0/i); 00305 spkr=0.1; 00306 wait(0.5); 00307 } 00308 spkr=0.0; 00309 } 00310 00311 00312 } 00313 //} 00314 // vall val ; 00315 00316 // reading_value(xval,pulseXa); 00317 // val.x = pulseXa; 00318 //val.y = pulseYa; 00319 // snprintf(temp,sizeof temp,"Temperature=%.1f", sensor.temp()); //Sensor Key "Temperature" and Value is Sensor Data 00320 //fun(xval,pulseXa); 00321 /*float values []= {pulseXa , pulseYa}; 00322 char* v [] = {xval,yval}; 00323 int ptr = 0;*/ 00324 //for( i = 0; i < 2; i++) 00325 //{ 00326 // ptr += snprintf(xval + ptr, sizeof(xval) - ptr, "X-axis=%.2f", values[i]); 00327 00328 00329 //} 00330 snprintf(xval,sizeof xval,"X-axis=%.2f", pulseXa); //Sensor Key "Temperature" and Value is Sensor Data 00331 snprintf(yval,sizeof yval,"Y-axis=%.2f", pulseYa); //Sensor Key "Temperature" and Value is Sensor Data 00332 snprintf(zval,sizeof zval,"Stability Status=%.2f", stable ); //Sensor Key "Temperature" and Value is Sensor Data 00333 00334 00335 //lcd.printf("%s", temp); // Display Temperature value on LCD 00336 00337 /* create a buf with HTTP POST call to platform, user must define below structure 00338 00339 1. deviceID --- This is the IMEI of the device, available on back side of the board 00340 2. temp -- This is a temporary variable to hold sensor Key and Value. For example, "Temperature"="24.5". Here Key is Temperature and Value is 24.5 00341 3. unit -- This is sensor data unit. For example, C for Temperature 00342 00343 Repeat the steps for sending more than 1 value to platform. Alternatively, user can define this process as a function for sending multiple values. 00344 00345 */ 00346 00347 //snprintf(buf, sizeof buf, "%s%s%s%s%s%s", "POST /ksu/?deviceID=",deviceID,"&",temp,"&unit=T"," HTTP/1.0\r\n\r\n"); 00348 00349 00350 snprintf(buf, sizeof buf, "%s%s%s%s%s%s", "POST /ksu/?deviceID=",deviceID,"&",xval,"&unit=T", " HTTP/1.0\r\n\r\n"); 00351 snprintf(buf2, sizeof buf2, "%s%s%s%s%s%s", "POST /ksu/?deviceID=",deviceID,"&",yval,"&unit=C"," HTTP/1.0\r\n\r\n"); 00352 snprintf(buf3, sizeof buf3, "%s%s%s%s%s%s", "POST /ksu/?deviceID=",deviceID,"&",zval,"&unit=T"," HTTP/1.0\r\n\r\n"); 00353 00354 printf("Output string is %s",buf); 00355 00356 // Note: since this is a short string we can send it in one go as it will 00357 // fit within the default buffer sizes. Normally you should call sock.send() 00358 // in a loop until your entire buffer has been sent. 00359 if (sockTcp.send((void *) buf, strlen(buf)) == (int) strlen(buf)) { 00360 pulseEvent(); 00361 printf("Socket send completed, waiting for response...\n"); 00362 x = sockTcp.recv(buf, sizeof (buf)); 00363 if (x > 0) { 00364 pulseEvent(); 00365 printf("Received %d byte response from server on TCP socket:\n" 00366 "----------------------------------------------------\n%.*s" 00367 "----------------------------------------------------\n", 00368 x, x, buf); 00369 } 00370 } 00371 if (sockTcp2.send((void *) buf2, strlen(buf2)) == (int) strlen(buf2)) { 00372 pulseEvent(); 00373 printf("Socket send completed, waiting for response...\n"); 00374 x = sockTcp2.recv(buf2, sizeof (buf2)); 00375 if (x > 0) { 00376 pulseEvent(); 00377 printf("Received %d byte response from server on TCP socket:\n" 00378 "----------------------------------------------------\n%.*s" 00379 "----------------------------------------------------\n", 00380 x, x, buf2); 00381 } 00382 } 00383 if (sockTcp3.send((void *) buf3, strlen(buf3)) == (int) strlen(buf3)) { 00384 pulseEvent(); 00385 printf("Socket send completed, waiting for response...\n"); 00386 x = sockTcp3.recv(buf3, sizeof (buf3)); 00387 if (x > 0) { 00388 pulseEvent(); 00389 printf("Received %d byte response from server on TCP socket:\n" 00390 "----------------------------------------------------\n%.*s" 00391 "----------------------------------------------------\n", 00392 x, x, buf3); 00393 } 00394 } 00395 00396 00397 }}} 00398 00399 lcd.cls(); 00400 lcd.locate(0,3); 00401 lcd.printf("Resend after 10s"); 00402 wait(1.0); 00403 printf("Closing socket...\n"); 00404 sockTcp.close(); 00405 sockTcp2.close(); 00406 sockTcp3.close(); 00407 pulseEvent(); 00408 printf("Socket closed.\n"); 00409 }}} 00410 #endif 00411 wait_ms(10000); //Loop delay in milliseconds. Keep it more than 10 seconds to save data usage on cellular 00412 #ifndef TARGET_UBLOX_C027 00413 printf("[Checking if user button has been pressed]\n"); 00414 #endif 00415 } 00416 00417 pulseEvent(); 00418 printf("User button was pressed, stopping...\n"); 00419 interface->disconnect(); 00420 ledOff(); 00421 printf("Stopped.\n"); 00422 } 00423 else { 00424 bad(); 00425 printf("Unable to get IP address of \"integraton.campus.stcs.io\".\n"); 00426 } 00427 00428 00429 }
Generated on Wed Aug 3 2022 16:13:14 by
1.7.2