nothing

Dependencies:   MMA7660 ublox-at-cellular-interface ublox-cellular-base C12832 LM75B ublox-cellular-base-n2xx ublox-at-cellular-interface-n2xx

Committer:
Nourah1Hamad
Date:
Sat Apr 27 11:47:51 2019 +0000
Revision:
0:94c39fbc3923
Nothing;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Nourah1Hamad 0:94c39fbc3923 1 /* mbed Microcontroller Library
Nourah1Hamad 0:94c39fbc3923 2 * Copyright (c) 2017 u-blox
Nourah1Hamad 0:94c39fbc3923 3 *
Nourah1Hamad 0:94c39fbc3923 4 * Licensed under the Apache License, Version 2.0 (the "License");
Nourah1Hamad 0:94c39fbc3923 5 * you may not use this file except in compliance with the License.
Nourah1Hamad 0:94c39fbc3923 6 * You may obtain a copy of the License at
Nourah1Hamad 0:94c39fbc3923 7 *
Nourah1Hamad 0:94c39fbc3923 8 * http://www.apache.org/licenses/LICENSE-2.0
Nourah1Hamad 0:94c39fbc3923 9 *
Nourah1Hamad 0:94c39fbc3923 10 * Unless required by applicable law or agreed to in writing, software
Nourah1Hamad 0:94c39fbc3923 11 * distributed under the License is distributed on an "AS IS" BASIS,
Nourah1Hamad 0:94c39fbc3923 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Nourah1Hamad 0:94c39fbc3923 13 * See the License for the specific language governing permissions and
Nourah1Hamad 0:94c39fbc3923 14 * limitations under the License.
Nourah1Hamad 0:94c39fbc3923 15 */
Nourah1Hamad 0:94c39fbc3923 16
Nourah1Hamad 0:94c39fbc3923 17
Nourah1Hamad 0:94c39fbc3923 18 /* This example program for the u-blox C030 board.
Nourah1Hamad 0:94c39fbc3923 19 * The UbloxATCellularInterface or OnboardCellularInterface uses it
Nourah1Hamad 0:94c39fbc3923 20 * to make a simple HTTP connection to a STC IOT server.
Nourah1Hamad 0:94c39fbc3923 21 * Progress may be monitored with a serial terminal running at 9600 baud or at https://ksu.aep1.iot.stcs.com.sa
Nourah1Hamad 0:94c39fbc3923 22 * The LED on the C030 board will turn green when this program is
Nourah1Hamad 0:94c39fbc3923 23 * operating correctly, pulse blue when a sockets operation is completed
Nourah1Hamad 0:94c39fbc3923 24 * and turn red if there is a failure.
Nourah1Hamad 0:94c39fbc3923 25
Nourah1Hamad 0:94c39fbc3923 26 * Modified by: Muhammad Faheem Khan (Digital Solutions, STCS)
Nourah1Hamad 0:94c39fbc3923 27 */
Nourah1Hamad 0:94c39fbc3923 28
Nourah1Hamad 0:94c39fbc3923 29 #include "mbed.h"
Nourah1Hamad 0:94c39fbc3923 30 #include "UbloxATCellularInterface.h"
Nourah1Hamad 0:94c39fbc3923 31 #include "UbloxATCellularInterfaceN2xx.h"
Nourah1Hamad 0:94c39fbc3923 32 #include "LM75B.h" // Temperature Sensor Library. Import more libraries from https://os.mbed.com/components/mbed-Application-Shield/
Nourah1Hamad 0:94c39fbc3923 33 #include "C12832.h" // LCD Library. Import more libraries from https://os.mbed.com/components/mbed-Application-Shield/
Nourah1Hamad 0:94c39fbc3923 34 #include "MMA7660.h" //HERE
Nourah1Hamad 0:94c39fbc3923 35
Nourah1Hamad 0:94c39fbc3923 36 // Sensor Pin and Object Activation
Nourah1Hamad 0:94c39fbc3923 37 C12832 lcd(D11, D13, D12, D7, D10);
Nourah1Hamad 0:94c39fbc3923 38 //LM75B sensor(D14,D15);
Nourah1Hamad 0:94c39fbc3923 39 MMA7660 MMA(D14,D15); //HERE
Nourah1Hamad 0:94c39fbc3923 40 PwmOut spkr(D6);
Nourah1Hamad 0:94c39fbc3923 41
Nourah1Hamad 0:94c39fbc3923 42 #define INTERFACE_CLASS UbloxATCellularInterface //This interface is used to initiate modem commands for sending data to sever
Nourah1Hamad 0:94c39fbc3923 43
Nourah1Hamad 0:94c39fbc3923 44 // The credentials of the SIM in the board. If PIN checking is enabled
Nourah1Hamad 0:94c39fbc3923 45 // for your SIM card you must set this to the required PIN.
Nourah1Hamad 0:94c39fbc3923 46 #define PIN "0000"
Nourah1Hamad 0:94c39fbc3923 47 #define APN "jtm2m" //APN remains the same for all the kits
Nourah1Hamad 0:94c39fbc3923 48 #define USERNAME NULL
Nourah1Hamad 0:94c39fbc3923 49 #define PASSWORD NULL
Nourah1Hamad 0:94c39fbc3923 50 #define TCP_SERVER "integration.campus.stcs.io" //STC IoT parsing server address
Nourah1Hamad 0:94c39fbc3923 51
Nourah1Hamad 0:94c39fbc3923 52 // LEDs
Nourah1Hamad 0:94c39fbc3923 53 //DigitalOut connectionLed(LED1);//operation confirmed HERE
Nourah1Hamad 0:94c39fbc3923 54 DigitalOut servoProxyLed(LED2);//score outcome, proxy for servo HERE
Nourah1Hamad 0:94c39fbc3923 55 DigitalOut ledRed(LED1, 1);
Nourah1Hamad 0:94c39fbc3923 56 DigitalOut ledGreen(LED2, 1);
Nourah1Hamad 0:94c39fbc3923 57 DigitalOut ledBlue(LED3, 1);
Nourah1Hamad 0:94c39fbc3923 58 DigitalIn fire(D4);
Nourah1Hamad 0:94c39fbc3923 59
Nourah1Hamad 0:94c39fbc3923 60
Nourah1Hamad 0:94c39fbc3923 61 // The user button
Nourah1Hamad 0:94c39fbc3923 62 volatile bool buttonPressed = false;
Nourah1Hamad 0:94c39fbc3923 63
Nourah1Hamad 0:94c39fbc3923 64
Nourah1Hamad 0:94c39fbc3923 65 static void good() {
Nourah1Hamad 0:94c39fbc3923 66 ledGreen = 0;
Nourah1Hamad 0:94c39fbc3923 67 ledBlue = 1;
Nourah1Hamad 0:94c39fbc3923 68 ledRed = 1;
Nourah1Hamad 0:94c39fbc3923 69 }
Nourah1Hamad 0:94c39fbc3923 70
Nourah1Hamad 0:94c39fbc3923 71 static void bad() {
Nourah1Hamad 0:94c39fbc3923 72 ledRed = 0;
Nourah1Hamad 0:94c39fbc3923 73 ledGreen = 1;
Nourah1Hamad 0:94c39fbc3923 74 ledBlue = 1;
Nourah1Hamad 0:94c39fbc3923 75 }
Nourah1Hamad 0:94c39fbc3923 76
Nourah1Hamad 0:94c39fbc3923 77 static void event() {
Nourah1Hamad 0:94c39fbc3923 78 ledBlue = 0;
Nourah1Hamad 0:94c39fbc3923 79 ledRed = 1;
Nourah1Hamad 0:94c39fbc3923 80 ledGreen = 1;
Nourah1Hamad 0:94c39fbc3923 81 }
Nourah1Hamad 0:94c39fbc3923 82
Nourah1Hamad 0:94c39fbc3923 83 static void pulseEvent() {
Nourah1Hamad 0:94c39fbc3923 84 event();
Nourah1Hamad 0:94c39fbc3923 85 wait_ms(500);
Nourah1Hamad 0:94c39fbc3923 86 good();
Nourah1Hamad 0:94c39fbc3923 87 }
Nourah1Hamad 0:94c39fbc3923 88
Nourah1Hamad 0:94c39fbc3923 89 static void ledOff() {
Nourah1Hamad 0:94c39fbc3923 90 ledBlue = 1;
Nourah1Hamad 0:94c39fbc3923 91 ledRed = 1;
Nourah1Hamad 0:94c39fbc3923 92 ledGreen = 1;
Nourah1Hamad 0:94c39fbc3923 93 }
Nourah1Hamad 0:94c39fbc3923 94 static void cbButton()
Nourah1Hamad 0:94c39fbc3923 95 {
Nourah1Hamad 0:94c39fbc3923 96 buttonPressed = true;
Nourah1Hamad 0:94c39fbc3923 97 pulseEvent();
Nourah1Hamad 0:94c39fbc3923 98 }
Nourah1Hamad 0:94c39fbc3923 99
Nourah1Hamad 0:94c39fbc3923 100
Nourah1Hamad 0:94c39fbc3923 101
Nourah1Hamad 0:94c39fbc3923 102
Nourah1Hamad 0:94c39fbc3923 103 float stable= 0 ;
Nourah1Hamad 0:94c39fbc3923 104 const int n = 10; //number of readings to be averaged, change globally here HERE
Nourah1Hamad 0:94c39fbc3923 105 float score = 0; //reserved for later HERE
Nourah1Hamad 0:94c39fbc3923 106 int angle = 0; //reserved for later HERE
Nourah1Hamad 0:94c39fbc3923 107 float pulseXT =0, pulseYT = 0; //Total holders for summation from axis arrays HERE
Nourah1Hamad 0:94c39fbc3923 108
Nourah1Hamad 0:94c39fbc3923 109 float pulseXa, pulseYa; //averaged values for each axis over n HERE
Nourah1Hamad 0:94c39fbc3923 110 float pulseX[n], pulseY[n]; //arrays to hold n readings for each axis HERE
Nourah1Hamad 0:94c39fbc3923 111 int i, j; //indexing variables HERE
Nourah1Hamad 0:94c39fbc3923 112 char xval[100]; // this buffer will be used to store sensor values
Nourah1Hamad 0:94c39fbc3923 113 char yval[100]; // this buffer will be used to store sensor values
Nourah1Hamad 0:94c39fbc3923 114 char zval[100]; // this buffer will be used to store sensor values
Nourah1Hamad 0:94c39fbc3923 115 char buf[1024]; // this buffer is used to send POST data to platform
Nourah1Hamad 0:94c39fbc3923 116 char deviceID[20] = "357520074039765";
Nourah1Hamad 0:94c39fbc3923 117 //void fun(char *str,float x)
Nourah1Hamad 0:94c39fbc3923 118 //{
Nourah1Hamad 0:94c39fbc3923 119 //snprintf(str,sizeof str,"X-axis=%.2f", x);
Nourah1Hamad 0:94c39fbc3923 120 //snprintf(buf, sizeof buf, "%s%s%s%s%s%s", "POST /ksu/?deviceID=",deviceID,"&",str,"&unit=T", " HTTP/1.0\r\n\r\n");
Nourah1Hamad 0:94c39fbc3923 121
Nourah1Hamad 0:94c39fbc3923 122 // printf(buf);
Nourah1Hamad 0:94c39fbc3923 123
Nourah1Hamad 0:94c39fbc3923 124 //}
Nourah1Hamad 0:94c39fbc3923 125 int main()
Nourah1Hamad 0:94c39fbc3923 126 {
Nourah1Hamad 0:94c39fbc3923 127 INTERFACE_CLASS *interface = new INTERFACE_CLASS();
Nourah1Hamad 0:94c39fbc3923 128 // If you need to debug the cellular interface, comment out the
Nourah1Hamad 0:94c39fbc3923 129 // instantiation above and uncomment the one below.
Nourah1Hamad 0:94c39fbc3923 130 // For the N2xx interface, change xxx to MBED_CONF_UBLOX_CELL_BAUD_RATE,
Nourah1Hamad 0:94c39fbc3923 131 // while for the non-N2xx interface change it to MBED_CONF_UBLOX_CELL_N2XX_BAUD_RATE.
Nourah1Hamad 0:94c39fbc3923 132 // INTERFACE_CLASS *interface = new INTERFACE_CLASS(MDMTXD, MDMRXD,
Nourah1Hamad 0:94c39fbc3923 133 // xxx,
Nourah1Hamad 0:94c39fbc3923 134 // true);
Nourah1Hamad 0:94c39fbc3923 135 #ifndef TARGET_UBLOX_C030_N211
Nourah1Hamad 0:94c39fbc3923 136 TCPSocket sockTcp;
Nourah1Hamad 0:94c39fbc3923 137 TCPSocket sockTcp2;
Nourah1Hamad 0:94c39fbc3923 138 TCPSocket sockTcp3;
Nourah1Hamad 0:94c39fbc3923 139
Nourah1Hamad 0:94c39fbc3923 140 #endif
Nourah1Hamad 0:94c39fbc3923 141
Nourah1Hamad 0:94c39fbc3923 142 SocketAddress tcpServer;
Nourah1Hamad 0:94c39fbc3923 143 //char deviceID[20] = "357520074039765";
Nourah1Hamad 0:94c39fbc3923 144 // char buf[1024]; // this buffer is used to send POST data to platform
Nourah1Hamad 0:94c39fbc3923 145 char buf2[1024];
Nourah1Hamad 0:94c39fbc3923 146 char buf3[1024];
Nourah1Hamad 0:94c39fbc3923 147 char temp[50]; // this buffer will be used to store sensor values
Nourah1Hamad 0:94c39fbc3923 148 int x;
Nourah1Hamad 0:94c39fbc3923 149
Nourah1Hamad 0:94c39fbc3923 150 #ifdef TARGET_UBLOX_C027
Nourah1Hamad 0:94c39fbc3923 151 // No user button on C027
Nourah1Hamad 0:94c39fbc3923 152 InterruptIn userButton(NC);
Nourah1Hamad 0:94c39fbc3923 153 #else
Nourah1Hamad 0:94c39fbc3923 154 InterruptIn userButton(SW0);
Nourah1Hamad 0:94c39fbc3923 155 #endif
Nourah1Hamad 0:94c39fbc3923 156
Nourah1Hamad 0:94c39fbc3923 157 // Attach a function to the user button
Nourah1Hamad 0:94c39fbc3923 158 userButton.rise(&cbButton);
Nourah1Hamad 0:94c39fbc3923 159
Nourah1Hamad 0:94c39fbc3923 160 good();
Nourah1Hamad 0:94c39fbc3923 161 printf("Starting up, please wait up to 180 seconds for network registration to complete...\n");
Nourah1Hamad 0:94c39fbc3923 162 interface->set_credentials(APN, USERNAME, PASSWORD);
Nourah1Hamad 0:94c39fbc3923 163 for (x = 0; interface->connect(PIN) != 0; x++) {
Nourah1Hamad 0:94c39fbc3923 164 if (x > 0) {
Nourah1Hamad 0:94c39fbc3923 165 bad();
Nourah1Hamad 0:94c39fbc3923 166 printf("Retrying (have you checked that an antenna is plugged in and your APN is correct?)...\n");
Nourah1Hamad 0:94c39fbc3923 167 }
Nourah1Hamad 0:94c39fbc3923 168 }
Nourah1Hamad 0:94c39fbc3923 169 pulseEvent();
Nourah1Hamad 0:94c39fbc3923 170
Nourah1Hamad 0:94c39fbc3923 171 printf("Getting the IP address of \"integration.campus.stcs.io\"...\n");
Nourah1Hamad 0:94c39fbc3923 172 if (interface->gethostbyname(TCP_SERVER, &tcpServer) == 0) {
Nourah1Hamad 0:94c39fbc3923 173 pulseEvent();
Nourah1Hamad 0:94c39fbc3923 174
Nourah1Hamad 0:94c39fbc3923 175 tcpServer.set_port(1880);
Nourah1Hamad 0:94c39fbc3923 176 //tcpServer2.set_port(1881);
Nourah1Hamad 0:94c39fbc3923 177 printf("\"integration.campus.stcs.io\" address: %s on port %d.\n", tcpServer.get_ip_address(), tcpServer.get_port());
Nourah1Hamad 0:94c39fbc3923 178
Nourah1Hamad 0:94c39fbc3923 179 printf("Performing socket operations in a loop (until the user button is pressed on C030)...\n");
Nourah1Hamad 0:94c39fbc3923 180 while (!buttonPressed) {
Nourah1Hamad 0:94c39fbc3923 181
Nourah1Hamad 0:94c39fbc3923 182 #ifndef TARGET_UBLOX_C030_N211
Nourah1Hamad 0:94c39fbc3923 183 lcd.cls();
Nourah1Hamad 0:94c39fbc3923 184 lcd.locate(0,3);
Nourah1Hamad 0:94c39fbc3923 185 lcd.printf("Connecting");
Nourah1Hamad 0:94c39fbc3923 186
Nourah1Hamad 0:94c39fbc3923 187 // TCP Sockets
Nourah1Hamad 0:94c39fbc3923 188 printf("=== TCP ===\n");
Nourah1Hamad 0:94c39fbc3923 189 printf("Opening a TCP socket...\n");
Nourah1Hamad 0:94c39fbc3923 190 if (sockTcp.open(interface) == 0) {
Nourah1Hamad 0:94c39fbc3923 191 pulseEvent();
Nourah1Hamad 0:94c39fbc3923 192 printf("TCP socket open.\n");
Nourah1Hamad 0:94c39fbc3923 193 sockTcp.set_timeout(10000);
Nourah1Hamad 0:94c39fbc3923 194 printf("Connecting socket to %s on port %d...\n", tcpServer.get_ip_address(), tcpServer.get_port());
Nourah1Hamad 0:94c39fbc3923 195 if (sockTcp.connect(tcpServer) == 0) {
Nourah1Hamad 0:94c39fbc3923 196 pulseEvent();
Nourah1Hamad 0:94c39fbc3923 197 lcd.cls();
Nourah1Hamad 0:94c39fbc3923 198 lcd.locate(0,3);
Nourah1Hamad 0:94c39fbc3923 199 lcd.printf("Connected");
Nourah1Hamad 0:94c39fbc3923 200 wait(2.0);
Nourah1Hamad 0:94c39fbc3923 201 printf("Connected, sending HTTP GET request to %s over socket...\n", TCP_SERVER);
Nourah1Hamad 0:94c39fbc3923 202 lcd.cls();
Nourah1Hamad 0:94c39fbc3923 203 lcd.locate(0,3);
Nourah1Hamad 0:94c39fbc3923 204 wait(1.0);
Nourah1Hamad 0:94c39fbc3923 205 printf("=== TCP ===\n");
Nourah1Hamad 0:94c39fbc3923 206 printf("Opening a TCP socket 2...\n");
Nourah1Hamad 0:94c39fbc3923 207 if (sockTcp2.open(interface) == 0) {
Nourah1Hamad 0:94c39fbc3923 208 pulseEvent();
Nourah1Hamad 0:94c39fbc3923 209 printf("TCP socket open.\n");
Nourah1Hamad 0:94c39fbc3923 210 sockTcp2.set_timeout(10000);
Nourah1Hamad 0:94c39fbc3923 211 printf("Connecting socket to %s on port %d...\n", tcpServer.get_ip_address(), tcpServer.get_port());// kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
Nourah1Hamad 0:94c39fbc3923 212 if (sockTcp2.connect(tcpServer) == 0) {
Nourah1Hamad 0:94c39fbc3923 213 pulseEvent();
Nourah1Hamad 0:94c39fbc3923 214 lcd.cls();
Nourah1Hamad 0:94c39fbc3923 215 lcd.locate(0,3);
Nourah1Hamad 0:94c39fbc3923 216 lcd.printf("Connected");
Nourah1Hamad 0:94c39fbc3923 217 wait(2.0);
Nourah1Hamad 0:94c39fbc3923 218 printf("Connected, sending HTTP GET request to %s over socket...\n", TCP_SERVER);
Nourah1Hamad 0:94c39fbc3923 219 lcd.cls();
Nourah1Hamad 0:94c39fbc3923 220 lcd.locate(0,3);
Nourah1Hamad 0:94c39fbc3923 221 wait(1.0);
Nourah1Hamad 0:94c39fbc3923 222 printf("=== TCP ===\n");
Nourah1Hamad 0:94c39fbc3923 223 printf("Opening a TCP socket3...\n");
Nourah1Hamad 0:94c39fbc3923 224 if (sockTcp3.open(interface) == 0) {
Nourah1Hamad 0:94c39fbc3923 225 pulseEvent();
Nourah1Hamad 0:94c39fbc3923 226 printf("TCP socket open.\n");
Nourah1Hamad 0:94c39fbc3923 227 sockTcp3.set_timeout(10000);
Nourah1Hamad 0:94c39fbc3923 228 printf("Connecting socket to %s on port %d...\n", tcpServer.get_ip_address(), tcpServer.get_port());
Nourah1Hamad 0:94c39fbc3923 229 if (sockTcp3.connect(tcpServer) == 0) {
Nourah1Hamad 0:94c39fbc3923 230 pulseEvent();
Nourah1Hamad 0:94c39fbc3923 231 lcd.cls();
Nourah1Hamad 0:94c39fbc3923 232 lcd.locate(0,3);
Nourah1Hamad 0:94c39fbc3923 233 lcd.printf("Connected");
Nourah1Hamad 0:94c39fbc3923 234 wait(2.0);
Nourah1Hamad 0:94c39fbc3923 235 printf("Connected, sending HTTP GET request to %s over socket...\n", TCP_SERVER);
Nourah1Hamad 0:94c39fbc3923 236 lcd.cls();
Nourah1Hamad 0:94c39fbc3923 237 lcd.locate(0,3);
Nourah1Hamad 0:94c39fbc3923 238
Nourah1Hamad 0:94c39fbc3923 239 // Sensor Data is stored in temp variable before pushing to platform
Nourah1Hamad 0:94c39fbc3923 240
Nourah1Hamad 0:94c39fbc3923 241 // while(1) { //HERE
Nourah1Hamad 0:94c39fbc3923 242
Nourah1Hamad 0:94c39fbc3923 243 // for (i = 0; i < n; i = i + 1) { //read n values into each axis array
Nourah1Hamad 0:94c39fbc3923 244 // pulseX[i] = MMA.x();
Nourah1Hamad 0:94c39fbc3923 245 // pulseY[i] = MMA.y();
Nourah1Hamad 0:94c39fbc3923 246 //}
Nourah1Hamad 0:94c39fbc3923 247 //pulseXT = 0; //reset Totala
Nourah1Hamad 0:94c39fbc3923 248 // pulseYT = 0; //reset Totala
Nourah1Hamad 0:94c39fbc3923 249 // for (j = 0; j < n; j = j + 1) { //summation of the contents of each array into axis Totals
Nourah1Hamad 0:94c39fbc3923 250 // pulseXT = pulseXT+pulseX[j];
Nourah1Hamad 0:94c39fbc3923 251 // pulseYT = pulseYT+pulseY[j];
Nourah1Hamad 0:94c39fbc3923 252 //}
Nourah1Hamad 0:94c39fbc3923 253 //pulseXa = pulseXT/n; //axis average over n
Nourah1Hamad 0:94c39fbc3923 254 pulseXa = MMA.x();
Nourah1Hamad 0:94c39fbc3923 255 // pulseYa = pulseYT/n; //axis average over n
Nourah1Hamad 0:94c39fbc3923 256 pulseYa = MMA.y();
Nourah1Hamad 0:94c39fbc3923 257 //if (MMA.testConnection())
Nourah1Hamad 0:94c39fbc3923 258 // connectionLed = 1;
Nourah1Hamad 0:94c39fbc3923 259
Nourah1Hamad 0:94c39fbc3923 260 if (pulseXa > (-0.2) && pulseXa < (0.2) && pulseYa > (-0.2) && pulseYa < (0.2)) {//average result within stability range; x, y
Nourah1Hamad 0:94c39fbc3923 261 lcd.cls();//clear LCD for next reading round
Nourah1Hamad 0:94c39fbc3923 262 lcd.locate(3,3);//first LCD column label
Nourah1Hamad 0:94c39fbc3923 263 lcd.printf("x-axis | ");//label column
Nourah1Hamad 0:94c39fbc3923 264 lcd.locate(3,12);//xdata location
Nourah1Hamad 0:94c39fbc3923 265 lcd.printf("%.2f\n",pulseXa);//print x to LCD
Nourah1Hamad 0:94c39fbc3923 266 lcd.locate(40,3);//second LCD column label
Nourah1Hamad 0:94c39fbc3923 267 lcd.printf("y-axis | ");//label column
Nourah1Hamad 0:94c39fbc3923 268 lcd.locate(40,12);//ydata location
Nourah1Hamad 0:94c39fbc3923 269 lcd.printf("%.2f\n",pulseYa);//print y to LCD
Nourah1Hamad 0:94c39fbc3923 270 lcd.locate(77,3);//initial LCD location
Nourah1Hamad 0:94c39fbc3923 271 lcd.printf("z-axis");//label column
Nourah1Hamad 0:94c39fbc3923 272 lcd.locate(77,12);//zdata location
Nourah1Hamad 0:94c39fbc3923 273 lcd.printf("%.2f\n",MMA.z());//print z to LCD
Nourah1Hamad 0:94c39fbc3923 274 lcd.locate(3,21);//flag location
Nourah1Hamad 0:94c39fbc3923 275 lcd.printf("STABLE");//flag
Nourah1Hamad 0:94c39fbc3923 276 stable = 1 ;
Nourah1Hamad 0:94c39fbc3923 277 wait_ms(2000);
Nourah1Hamad 0:94c39fbc3923 278
Nourah1Hamad 0:94c39fbc3923 279 }
Nourah1Hamad 0:94c39fbc3923 280
Nourah1Hamad 0:94c39fbc3923 281 else {////average result not within stability range; x, y
Nourah1Hamad 0:94c39fbc3923 282 lcd.cls();//clear LCD for next reading round
Nourah1Hamad 0:94c39fbc3923 283 lcd.locate(3,3);//first LCD column label
Nourah1Hamad 0:94c39fbc3923 284 lcd.printf("x-axis | ");//label column
Nourah1Hamad 0:94c39fbc3923 285 lcd.locate(3,12);//xdata location
Nourah1Hamad 0:94c39fbc3923 286 lcd.printf("%.2f\n",pulseXa);//print x to LCD
Nourah1Hamad 0:94c39fbc3923 287 lcd.locate(40,3);//second LCD column label
Nourah1Hamad 0:94c39fbc3923 288 lcd.printf("y-axis | ");//label column
Nourah1Hamad 0:94c39fbc3923 289 lcd.locate(40,12);//ydata location
Nourah1Hamad 0:94c39fbc3923 290 lcd.printf("%.2f\n",pulseYa);//print y to LCD
Nourah1Hamad 0:94c39fbc3923 291 lcd.locate(77,3);//initial LCD location
Nourah1Hamad 0:94c39fbc3923 292 lcd.printf("z-axis");//label column
Nourah1Hamad 0:94c39fbc3923 293 lcd.locate(77,12);//zdata location
Nourah1Hamad 0:94c39fbc3923 294 lcd.printf("%.2f\n",MMA.z());//print z to LCD
Nourah1Hamad 0:94c39fbc3923 295 lcd.locate(3,21);//flag location
Nourah1Hamad 0:94c39fbc3923 296 lcd.printf("UNSTABLE");//flag
Nourah1Hamad 0:94c39fbc3923 297 stable = 0 ;
Nourah1Hamad 0:94c39fbc3923 298 wait_ms(2000);
Nourah1Hamad 0:94c39fbc3923 299
Nourah1Hamad 0:94c39fbc3923 300 int snd = 0;
Nourah1Hamad 0:94c39fbc3923 301 while (snd != 1) {
Nourah1Hamad 0:94c39fbc3923 302 snd++;
Nourah1Hamad 0:94c39fbc3923 303 for (float i=2000.0; i<10000.0; i+=100) {
Nourah1Hamad 0:94c39fbc3923 304 spkr.period(1.0/i);
Nourah1Hamad 0:94c39fbc3923 305 spkr=0.1;
Nourah1Hamad 0:94c39fbc3923 306 wait(0.5);
Nourah1Hamad 0:94c39fbc3923 307 }
Nourah1Hamad 0:94c39fbc3923 308 spkr=0.0;
Nourah1Hamad 0:94c39fbc3923 309 }
Nourah1Hamad 0:94c39fbc3923 310
Nourah1Hamad 0:94c39fbc3923 311
Nourah1Hamad 0:94c39fbc3923 312 }
Nourah1Hamad 0:94c39fbc3923 313 //}
Nourah1Hamad 0:94c39fbc3923 314 // vall val ;
Nourah1Hamad 0:94c39fbc3923 315
Nourah1Hamad 0:94c39fbc3923 316 // reading_value(xval,pulseXa);
Nourah1Hamad 0:94c39fbc3923 317 // val.x = pulseXa;
Nourah1Hamad 0:94c39fbc3923 318 //val.y = pulseYa;
Nourah1Hamad 0:94c39fbc3923 319 // snprintf(temp,sizeof temp,"Temperature=%.1f", sensor.temp()); //Sensor Key "Temperature" and Value is Sensor Data
Nourah1Hamad 0:94c39fbc3923 320 //fun(xval,pulseXa);
Nourah1Hamad 0:94c39fbc3923 321 /*float values []= {pulseXa , pulseYa};
Nourah1Hamad 0:94c39fbc3923 322 char* v [] = {xval,yval};
Nourah1Hamad 0:94c39fbc3923 323 int ptr = 0;*/
Nourah1Hamad 0:94c39fbc3923 324 //for( i = 0; i < 2; i++)
Nourah1Hamad 0:94c39fbc3923 325 //{
Nourah1Hamad 0:94c39fbc3923 326 // ptr += snprintf(xval + ptr, sizeof(xval) - ptr, "X-axis=%.2f", values[i]);
Nourah1Hamad 0:94c39fbc3923 327
Nourah1Hamad 0:94c39fbc3923 328
Nourah1Hamad 0:94c39fbc3923 329 //}
Nourah1Hamad 0:94c39fbc3923 330 snprintf(xval,sizeof xval,"X-axis=%.2f", pulseXa); //Sensor Key "Temperature" and Value is Sensor Data
Nourah1Hamad 0:94c39fbc3923 331 snprintf(yval,sizeof yval,"Y-axis=%.2f", pulseYa); //Sensor Key "Temperature" and Value is Sensor Data
Nourah1Hamad 0:94c39fbc3923 332 snprintf(zval,sizeof zval,"Stability Status=%.2f", stable ); //Sensor Key "Temperature" and Value is Sensor Data
Nourah1Hamad 0:94c39fbc3923 333
Nourah1Hamad 0:94c39fbc3923 334
Nourah1Hamad 0:94c39fbc3923 335 //lcd.printf("%s", temp); // Display Temperature value on LCD
Nourah1Hamad 0:94c39fbc3923 336
Nourah1Hamad 0:94c39fbc3923 337 /* create a buf with HTTP POST call to platform, user must define below structure
Nourah1Hamad 0:94c39fbc3923 338
Nourah1Hamad 0:94c39fbc3923 339 1. deviceID --- This is the IMEI of the device, available on back side of the board
Nourah1Hamad 0:94c39fbc3923 340 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
Nourah1Hamad 0:94c39fbc3923 341 3. unit -- This is sensor data unit. For example, C for Temperature
Nourah1Hamad 0:94c39fbc3923 342
Nourah1Hamad 0:94c39fbc3923 343 Repeat the steps for sending more than 1 value to platform. Alternatively, user can define this process as a function for sending multiple values.
Nourah1Hamad 0:94c39fbc3923 344
Nourah1Hamad 0:94c39fbc3923 345 */
Nourah1Hamad 0:94c39fbc3923 346
Nourah1Hamad 0:94c39fbc3923 347 //snprintf(buf, sizeof buf, "%s%s%s%s%s%s", "POST /ksu/?deviceID=",deviceID,"&",temp,"&unit=T"," HTTP/1.0\r\n\r\n");
Nourah1Hamad 0:94c39fbc3923 348
Nourah1Hamad 0:94c39fbc3923 349
Nourah1Hamad 0:94c39fbc3923 350 snprintf(buf, sizeof buf, "%s%s%s%s%s%s", "POST /ksu/?deviceID=",deviceID,"&",xval,"&unit=T", " HTTP/1.0\r\n\r\n");
Nourah1Hamad 0:94c39fbc3923 351 snprintf(buf2, sizeof buf2, "%s%s%s%s%s%s", "POST /ksu/?deviceID=",deviceID,"&",yval,"&unit=C"," HTTP/1.0\r\n\r\n");
Nourah1Hamad 0:94c39fbc3923 352 snprintf(buf3, sizeof buf3, "%s%s%s%s%s%s", "POST /ksu/?deviceID=",deviceID,"&",zval,"&unit=T"," HTTP/1.0\r\n\r\n");
Nourah1Hamad 0:94c39fbc3923 353
Nourah1Hamad 0:94c39fbc3923 354 printf("Output string is %s",buf);
Nourah1Hamad 0:94c39fbc3923 355
Nourah1Hamad 0:94c39fbc3923 356 // Note: since this is a short string we can send it in one go as it will
Nourah1Hamad 0:94c39fbc3923 357 // fit within the default buffer sizes. Normally you should call sock.send()
Nourah1Hamad 0:94c39fbc3923 358 // in a loop until your entire buffer has been sent.
Nourah1Hamad 0:94c39fbc3923 359 if (sockTcp.send((void *) buf, strlen(buf)) == (int) strlen(buf)) {
Nourah1Hamad 0:94c39fbc3923 360 pulseEvent();
Nourah1Hamad 0:94c39fbc3923 361 printf("Socket send completed, waiting for response...\n");
Nourah1Hamad 0:94c39fbc3923 362 x = sockTcp.recv(buf, sizeof (buf));
Nourah1Hamad 0:94c39fbc3923 363 if (x > 0) {
Nourah1Hamad 0:94c39fbc3923 364 pulseEvent();
Nourah1Hamad 0:94c39fbc3923 365 printf("Received %d byte response from server on TCP socket:\n"
Nourah1Hamad 0:94c39fbc3923 366 "----------------------------------------------------\n%.*s"
Nourah1Hamad 0:94c39fbc3923 367 "----------------------------------------------------\n",
Nourah1Hamad 0:94c39fbc3923 368 x, x, buf);
Nourah1Hamad 0:94c39fbc3923 369 }
Nourah1Hamad 0:94c39fbc3923 370 }
Nourah1Hamad 0:94c39fbc3923 371 if (sockTcp2.send((void *) buf2, strlen(buf2)) == (int) strlen(buf2)) {
Nourah1Hamad 0:94c39fbc3923 372 pulseEvent();
Nourah1Hamad 0:94c39fbc3923 373 printf("Socket send completed, waiting for response...\n");
Nourah1Hamad 0:94c39fbc3923 374 x = sockTcp2.recv(buf2, sizeof (buf2));
Nourah1Hamad 0:94c39fbc3923 375 if (x > 0) {
Nourah1Hamad 0:94c39fbc3923 376 pulseEvent();
Nourah1Hamad 0:94c39fbc3923 377 printf("Received %d byte response from server on TCP socket:\n"
Nourah1Hamad 0:94c39fbc3923 378 "----------------------------------------------------\n%.*s"
Nourah1Hamad 0:94c39fbc3923 379 "----------------------------------------------------\n",
Nourah1Hamad 0:94c39fbc3923 380 x, x, buf2);
Nourah1Hamad 0:94c39fbc3923 381 }
Nourah1Hamad 0:94c39fbc3923 382 }
Nourah1Hamad 0:94c39fbc3923 383 if (sockTcp3.send((void *) buf3, strlen(buf3)) == (int) strlen(buf3)) {
Nourah1Hamad 0:94c39fbc3923 384 pulseEvent();
Nourah1Hamad 0:94c39fbc3923 385 printf("Socket send completed, waiting for response...\n");
Nourah1Hamad 0:94c39fbc3923 386 x = sockTcp3.recv(buf3, sizeof (buf3));
Nourah1Hamad 0:94c39fbc3923 387 if (x > 0) {
Nourah1Hamad 0:94c39fbc3923 388 pulseEvent();
Nourah1Hamad 0:94c39fbc3923 389 printf("Received %d byte response from server on TCP socket:\n"
Nourah1Hamad 0:94c39fbc3923 390 "----------------------------------------------------\n%.*s"
Nourah1Hamad 0:94c39fbc3923 391 "----------------------------------------------------\n",
Nourah1Hamad 0:94c39fbc3923 392 x, x, buf3);
Nourah1Hamad 0:94c39fbc3923 393 }
Nourah1Hamad 0:94c39fbc3923 394 }
Nourah1Hamad 0:94c39fbc3923 395
Nourah1Hamad 0:94c39fbc3923 396
Nourah1Hamad 0:94c39fbc3923 397 }}}
Nourah1Hamad 0:94c39fbc3923 398
Nourah1Hamad 0:94c39fbc3923 399 lcd.cls();
Nourah1Hamad 0:94c39fbc3923 400 lcd.locate(0,3);
Nourah1Hamad 0:94c39fbc3923 401 lcd.printf("Resend after 10s");
Nourah1Hamad 0:94c39fbc3923 402 wait(1.0);
Nourah1Hamad 0:94c39fbc3923 403 printf("Closing socket...\n");
Nourah1Hamad 0:94c39fbc3923 404 sockTcp.close();
Nourah1Hamad 0:94c39fbc3923 405 sockTcp2.close();
Nourah1Hamad 0:94c39fbc3923 406 sockTcp3.close();
Nourah1Hamad 0:94c39fbc3923 407 pulseEvent();
Nourah1Hamad 0:94c39fbc3923 408 printf("Socket closed.\n");
Nourah1Hamad 0:94c39fbc3923 409 }}}
Nourah1Hamad 0:94c39fbc3923 410 #endif
Nourah1Hamad 0:94c39fbc3923 411 wait_ms(10000); //Loop delay in milliseconds. Keep it more than 10 seconds to save data usage on cellular
Nourah1Hamad 0:94c39fbc3923 412 #ifndef TARGET_UBLOX_C027
Nourah1Hamad 0:94c39fbc3923 413 printf("[Checking if user button has been pressed]\n");
Nourah1Hamad 0:94c39fbc3923 414 #endif
Nourah1Hamad 0:94c39fbc3923 415 }
Nourah1Hamad 0:94c39fbc3923 416
Nourah1Hamad 0:94c39fbc3923 417 pulseEvent();
Nourah1Hamad 0:94c39fbc3923 418 printf("User button was pressed, stopping...\n");
Nourah1Hamad 0:94c39fbc3923 419 interface->disconnect();
Nourah1Hamad 0:94c39fbc3923 420 ledOff();
Nourah1Hamad 0:94c39fbc3923 421 printf("Stopped.\n");
Nourah1Hamad 0:94c39fbc3923 422 }
Nourah1Hamad 0:94c39fbc3923 423 else {
Nourah1Hamad 0:94c39fbc3923 424 bad();
Nourah1Hamad 0:94c39fbc3923 425 printf("Unable to get IP address of \"integraton.campus.stcs.io\".\n");
Nourah1Hamad 0:94c39fbc3923 426 }
Nourah1Hamad 0:94c39fbc3923 427
Nourah1Hamad 0:94c39fbc3923 428
Nourah1Hamad 0:94c39fbc3923 429 }