I am in my way

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

Committer:
reeml3
Date:
Mon Apr 29 12:40:03 2019 +0000
Revision:
1:135f5adc847c
Parent:
0:0044354f0061
I am in my way

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mfaheem2004 0:0044354f0061 1 /* mbed Microcontroller Library
mfaheem2004 0:0044354f0061 2 * Copyright (c) 2017 u-blox
mfaheem2004 0:0044354f0061 3 *
mfaheem2004 0:0044354f0061 4 * Licensed under the Apache License, Version 2.0 (the "License");
mfaheem2004 0:0044354f0061 5 * you may not use this file except in compliance with the License.
mfaheem2004 0:0044354f0061 6 * You may obtain a copy of the License at
mfaheem2004 0:0044354f0061 7 *
mfaheem2004 0:0044354f0061 8 * http://www.apache.org/licenses/LICENSE-2.0
mfaheem2004 0:0044354f0061 9 *
mfaheem2004 0:0044354f0061 10 * Unless required by applicable law or agreed to in writing, software
mfaheem2004 0:0044354f0061 11 * distributed under the License is distributed on an "AS IS" BASIS,
mfaheem2004 0:0044354f0061 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mfaheem2004 0:0044354f0061 13 * See the License for the specific language governing permissions and
mfaheem2004 0:0044354f0061 14 * limitations under the License.
mfaheem2004 0:0044354f0061 15 */
mfaheem2004 0:0044354f0061 16
mfaheem2004 0:0044354f0061 17
mfaheem2004 0:0044354f0061 18 /* This example program for the u-blox C030 board.
mfaheem2004 0:0044354f0061 19 * The UbloxATCellularInterface or OnboardCellularInterface uses it
mfaheem2004 0:0044354f0061 20 * to make a simple HTTP connection to a STC IOT server.
mfaheem2004 0:0044354f0061 21 * Progress may be monitored with a serial terminal running at 9600 baud or at https://ksu.aep1.iot.stcs.com.sa
mfaheem2004 0:0044354f0061 22 * The LED on the C030 board will turn green when this program is
mfaheem2004 0:0044354f0061 23 * operating correctly, pulse blue when a sockets operation is completed
mfaheem2004 0:0044354f0061 24 * and turn red if there is a failure.
mfaheem2004 0:0044354f0061 25
mfaheem2004 0:0044354f0061 26 * Modified by: Muhammad Faheem Khan (Digital Solutions, STCS)
mfaheem2004 0:0044354f0061 27 */
mfaheem2004 0:0044354f0061 28
mfaheem2004 0:0044354f0061 29 #include "mbed.h"
mfaheem2004 0:0044354f0061 30 #include "UbloxATCellularInterface.h"
mfaheem2004 0:0044354f0061 31 #include "UbloxATCellularInterfaceN2xx.h"
mfaheem2004 0:0044354f0061 32 #include "LM75B.h" // Temperature Sensor Library. Import more libraries from https://os.mbed.com/components/mbed-Application-Shield/
mfaheem2004 0:0044354f0061 33 #include "C12832.h" // LCD Library. Import more libraries from https://os.mbed.com/components/mbed-Application-Shield/
mfaheem2004 0:0044354f0061 34
mfaheem2004 0:0044354f0061 35 // Sensor Pin and Object Activation
mfaheem2004 0:0044354f0061 36 C12832 lcd(D11, D13, D12, D7, D10);
mfaheem2004 0:0044354f0061 37 LM75B sensor(D14,D15); // 1st Temperature sensor
reeml3 1:135f5adc847c 38 //AnalogIn pot2 (A1); // 2nd Potentiometer sensor
reeml3 1:135f5adc847c 39 AnalogIn moisture(A0);
mfaheem2004 0:0044354f0061 40 #define INTERFACE_CLASS UbloxATCellularInterface //This interface is used to initiate modem commands for sending data to sever
mfaheem2004 0:0044354f0061 41
mfaheem2004 0:0044354f0061 42 // The credentials of the SIM in the board. If PIN checking is enabled
mfaheem2004 0:0044354f0061 43 // for your SIM card you must set this to the required PIN.
mfaheem2004 0:0044354f0061 44 #define PIN "0000"
mfaheem2004 0:0044354f0061 45 #define APN "jtm2m" //APN remains the same for all the kits
mfaheem2004 0:0044354f0061 46 #define USERNAME NULL
mfaheem2004 0:0044354f0061 47 #define PASSWORD NULL
mfaheem2004 0:0044354f0061 48 #define TCP_SERVER "integration.campus.stcs.io" //STC IoT parsing server address
mfaheem2004 0:0044354f0061 49
mfaheem2004 0:0044354f0061 50 // LEDs
mfaheem2004 0:0044354f0061 51 DigitalOut ledRed(LED1, 1);
mfaheem2004 0:0044354f0061 52 DigitalOut ledGreen(LED2, 1);
mfaheem2004 0:0044354f0061 53 DigitalOut ledBlue(LED3, 1);
mfaheem2004 0:0044354f0061 54
mfaheem2004 0:0044354f0061 55 // The user button
mfaheem2004 0:0044354f0061 56 volatile bool buttonPressed = false;
mfaheem2004 0:0044354f0061 57
reeml3 1:135f5adc847c 58
reeml3 1:135f5adc847c 59 float map(float in, float inMin, float inMax, float outMin, float outMax) {
reeml3 1:135f5adc847c 60 // check it's within the range
reeml3 1:135f5adc847c 61 if (inMin<inMax) {
reeml3 1:135f5adc847c 62 if (in <= inMin)
reeml3 1:135f5adc847c 63 return outMin;
reeml3 1:135f5adc847c 64 if (in >= inMax)
reeml3 1:135f5adc847c 65 return outMax;
reeml3 1:135f5adc847c 66 } else { // cope with input range being backwards.
reeml3 1:135f5adc847c 67 if (in >= inMin)
reeml3 1:135f5adc847c 68 return outMin;
reeml3 1:135f5adc847c 69 if (in <= inMax)
reeml3 1:135f5adc847c 70 return outMax;
reeml3 1:135f5adc847c 71 }
reeml3 1:135f5adc847c 72 // calculate how far into the range we are
reeml3 1:135f5adc847c 73 float scale = (in-inMin)/(inMax-inMin);
reeml3 1:135f5adc847c 74 // calculate the output.
reeml3 1:135f5adc847c 75 return outMin + scale*(outMax-outMin);
reeml3 1:135f5adc847c 76 }
reeml3 1:135f5adc847c 77
mfaheem2004 0:0044354f0061 78 static void good() {
mfaheem2004 0:0044354f0061 79 ledGreen = 0;
mfaheem2004 0:0044354f0061 80 ledBlue = 1;
mfaheem2004 0:0044354f0061 81 ledRed = 1;
mfaheem2004 0:0044354f0061 82 }
mfaheem2004 0:0044354f0061 83
mfaheem2004 0:0044354f0061 84 static void bad() {
mfaheem2004 0:0044354f0061 85 ledRed = 0;
mfaheem2004 0:0044354f0061 86 ledGreen = 1;
mfaheem2004 0:0044354f0061 87 ledBlue = 1;
mfaheem2004 0:0044354f0061 88 }
mfaheem2004 0:0044354f0061 89
mfaheem2004 0:0044354f0061 90 static void event() {
mfaheem2004 0:0044354f0061 91 ledBlue = 0;
mfaheem2004 0:0044354f0061 92 ledRed = 1;
mfaheem2004 0:0044354f0061 93 ledGreen = 1;
mfaheem2004 0:0044354f0061 94 }
mfaheem2004 0:0044354f0061 95
mfaheem2004 0:0044354f0061 96 static void pulseEvent() {
mfaheem2004 0:0044354f0061 97 event();
mfaheem2004 0:0044354f0061 98 wait_ms(500);
mfaheem2004 0:0044354f0061 99 good();
mfaheem2004 0:0044354f0061 100 }
mfaheem2004 0:0044354f0061 101
mfaheem2004 0:0044354f0061 102 static void ledOff() {
mfaheem2004 0:0044354f0061 103 ledBlue = 1;
mfaheem2004 0:0044354f0061 104 ledRed = 1;
mfaheem2004 0:0044354f0061 105 ledGreen = 1;
mfaheem2004 0:0044354f0061 106 }
mfaheem2004 0:0044354f0061 107 static void cbButton()
mfaheem2004 0:0044354f0061 108 {
mfaheem2004 0:0044354f0061 109 buttonPressed = true;
mfaheem2004 0:0044354f0061 110 pulseEvent();
mfaheem2004 0:0044354f0061 111 }
mfaheem2004 0:0044354f0061 112
mfaheem2004 0:0044354f0061 113 int main()
mfaheem2004 0:0044354f0061 114 {
mfaheem2004 0:0044354f0061 115 INTERFACE_CLASS *interface = new INTERFACE_CLASS();
mfaheem2004 0:0044354f0061 116 // If you need to debug the cellular interface, comment out the
mfaheem2004 0:0044354f0061 117 // instantiation above and uncomment the one below.
mfaheem2004 0:0044354f0061 118 // For the N2xx interface, change xxx to MBED_CONF_UBLOX_CELL_BAUD_RATE,
mfaheem2004 0:0044354f0061 119 // while for the non-N2xx interface change it to MBED_CONF_UBLOX_CELL_N2XX_BAUD_RATE.
mfaheem2004 0:0044354f0061 120 // INTERFACE_CLASS *interface = new INTERFACE_CLASS(MDMTXD, MDMRXD,
mfaheem2004 0:0044354f0061 121 // xxx,
mfaheem2004 0:0044354f0061 122 // true);
mfaheem2004 0:0044354f0061 123 #ifndef TARGET_UBLOX_C030_N211
mfaheem2004 0:0044354f0061 124 TCPSocket sockTcp; // for 1st sensor
mfaheem2004 0:0044354f0061 125 TCPSocket sockTcp1; // for 2nd Sensor
mfaheem2004 0:0044354f0061 126 #endif
mfaheem2004 0:0044354f0061 127
mfaheem2004 0:0044354f0061 128 SocketAddress tcpServer;
reeml3 1:135f5adc847c 129 char deviceID[20] = "357520073946010";
mfaheem2004 0:0044354f0061 130 char buf[1024]; // this buffer is used to send POST data to platform
mfaheem2004 0:0044354f0061 131 char temp[50]; // this buffer will be used to store sensor values
reeml3 1:135f5adc847c 132 char soil[50]; // this buffer will be used to store 2nd sensor values
mfaheem2004 0:0044354f0061 133 int x;
mfaheem2004 0:0044354f0061 134
mfaheem2004 0:0044354f0061 135 #ifdef TARGET_UBLOX_C027
mfaheem2004 0:0044354f0061 136 // No user button on C027
mfaheem2004 0:0044354f0061 137 InterruptIn userButton(NC);
mfaheem2004 0:0044354f0061 138 #else
mfaheem2004 0:0044354f0061 139 InterruptIn userButton(SW0);
mfaheem2004 0:0044354f0061 140 #endif
mfaheem2004 0:0044354f0061 141
mfaheem2004 0:0044354f0061 142 // Attach a function to the user button
mfaheem2004 0:0044354f0061 143 userButton.rise(&cbButton);
mfaheem2004 0:0044354f0061 144
mfaheem2004 0:0044354f0061 145 good();
mfaheem2004 0:0044354f0061 146 printf("Starting up, please wait up to 180 seconds for network registration to complete...\n");
mfaheem2004 0:0044354f0061 147 interface->set_credentials(APN, USERNAME, PASSWORD);
mfaheem2004 0:0044354f0061 148 for (x = 0; interface->connect(PIN) != 0; x++) {
mfaheem2004 0:0044354f0061 149 if (x > 0) {
mfaheem2004 0:0044354f0061 150 bad();
mfaheem2004 0:0044354f0061 151 printf("Retrying (have you checked that an antenna is plugged in and your APN is correct?)...\n");
mfaheem2004 0:0044354f0061 152 }
mfaheem2004 0:0044354f0061 153 }
mfaheem2004 0:0044354f0061 154 pulseEvent();
mfaheem2004 0:0044354f0061 155
mfaheem2004 0:0044354f0061 156 printf("Getting the IP address of \"integration.campus.stcs.io\"...\n");
mfaheem2004 0:0044354f0061 157 if (interface->gethostbyname(TCP_SERVER, &tcpServer) == 0) {
mfaheem2004 0:0044354f0061 158 pulseEvent();
mfaheem2004 0:0044354f0061 159
mfaheem2004 0:0044354f0061 160 tcpServer.set_port(1880);
mfaheem2004 0:0044354f0061 161 printf("\"integration.campus.stcs.io\" address: %s on port %d.\n", tcpServer.get_ip_address(), tcpServer.get_port());
mfaheem2004 0:0044354f0061 162
mfaheem2004 0:0044354f0061 163 printf("Performing socket operations in a loop (until the user button is pressed on C030)...\n");
mfaheem2004 0:0044354f0061 164 while (!buttonPressed) {
mfaheem2004 0:0044354f0061 165
mfaheem2004 0:0044354f0061 166 #ifndef TARGET_UBLOX_C030_N211
mfaheem2004 0:0044354f0061 167 lcd.cls();
mfaheem2004 0:0044354f0061 168 lcd.locate(0,3);
mfaheem2004 0:0044354f0061 169 lcd.printf("Connecting");
mfaheem2004 0:0044354f0061 170
mfaheem2004 0:0044354f0061 171 // TCP Sockets
mfaheem2004 0:0044354f0061 172 printf("=== TCP ===\n");
mfaheem2004 0:0044354f0061 173 printf("Opening a TCP socket...\n");
mfaheem2004 0:0044354f0061 174 if (sockTcp.open(interface) == 0 && sockTcp1.open(interface) == 0 ) {
mfaheem2004 0:0044354f0061 175 pulseEvent();
mfaheem2004 0:0044354f0061 176 printf("TCP socket open.\n");
mfaheem2004 0:0044354f0061 177 sockTcp.set_timeout(10000);
mfaheem2004 0:0044354f0061 178 printf("Connecting socket to %s on port %d...\n", tcpServer.get_ip_address(), tcpServer.get_port());
mfaheem2004 0:0044354f0061 179 if (sockTcp.connect(tcpServer) == 0 && sockTcp1.connect(tcpServer) == 0) {
mfaheem2004 0:0044354f0061 180 pulseEvent();
mfaheem2004 0:0044354f0061 181
mfaheem2004 0:0044354f0061 182
mfaheem2004 0:0044354f0061 183 lcd.cls();
mfaheem2004 0:0044354f0061 184 lcd.locate(0,3);
mfaheem2004 0:0044354f0061 185 lcd.printf("Connected");
mfaheem2004 0:0044354f0061 186 wait(2.0);
mfaheem2004 0:0044354f0061 187 printf("Connected, sending HTTP GET request to %s over socket...\n", TCP_SERVER);
mfaheem2004 0:0044354f0061 188
mfaheem2004 0:0044354f0061 189
mfaheem2004 0:0044354f0061 190 ////////////////// SENDING 1st SENSOR VALUE /////////////////////
mfaheem2004 0:0044354f0061 191
mfaheem2004 0:0044354f0061 192 lcd.cls();
mfaheem2004 0:0044354f0061 193 lcd.locate(0,3);
mfaheem2004 0:0044354f0061 194
mfaheem2004 0:0044354f0061 195
mfaheem2004 0:0044354f0061 196 snprintf(temp,sizeof temp,"Temperature=%.1f", sensor.temp()); //Sensor Key "Temperature" and Value is Sensor Data
mfaheem2004 0:0044354f0061 197
mfaheem2004 0:0044354f0061 198 lcd.printf("%s", temp); // Display Temperature value on LCD
mfaheem2004 0:0044354f0061 199
mfaheem2004 0:0044354f0061 200
reeml3 1:135f5adc847c 201 snprintf(buf, sizeof buf, "%s%s%s%s%s%s", "POST /ksu/?deviceID=",deviceID,"&",temp,"&unit=C"," HTTP/1.0\r\n\r\n");
mfaheem2004 0:0044354f0061 202
mfaheem2004 0:0044354f0061 203 printf("Output string is %s",buf);
mfaheem2004 0:0044354f0061 204
mfaheem2004 0:0044354f0061 205 // Note: since this is a short string we can send it in one go as it will
mfaheem2004 0:0044354f0061 206 // fit within the default buffer sizes. Normally you should call sock.send()
mfaheem2004 0:0044354f0061 207 // in a loop until your entire buffer has been sent.
mfaheem2004 0:0044354f0061 208 if (sockTcp.send((void *) buf, strlen(buf)) == (int) strlen(buf)) {
mfaheem2004 0:0044354f0061 209 pulseEvent();
mfaheem2004 0:0044354f0061 210 printf("Socket send completed, waiting for response...\n");
mfaheem2004 0:0044354f0061 211 x = sockTcp.recv(buf, sizeof (buf));
mfaheem2004 0:0044354f0061 212 if (x > 0) {
mfaheem2004 0:0044354f0061 213 pulseEvent();
mfaheem2004 0:0044354f0061 214 printf("Received %d byte response from server on TCP socket:\n"
mfaheem2004 0:0044354f0061 215 "----------------------------------------------------\n%.*s"
mfaheem2004 0:0044354f0061 216 "----------------------------------------------------\n",
mfaheem2004 0:0044354f0061 217 x, x, buf);
mfaheem2004 0:0044354f0061 218 }
mfaheem2004 0:0044354f0061 219 }
mfaheem2004 0:0044354f0061 220
mfaheem2004 0:0044354f0061 221
mfaheem2004 0:0044354f0061 222
mfaheem2004 0:0044354f0061 223
mfaheem2004 0:0044354f0061 224
mfaheem2004 0:0044354f0061 225
mfaheem2004 0:0044354f0061 226 /////////////////////////////////// Sending 2nd Sensor Value //////////////////////
mfaheem2004 0:0044354f0061 227
mfaheem2004 0:0044354f0061 228
reeml3 1:135f5adc847c 229 float final_2 = moisture.read() * 3300;
reeml3 1:135f5adc847c 230 uint16_t meas = (moisture.read_u16());
reeml3 1:135f5adc847c 231 float final_1 = (float) 3300 / 65535 * (float) meas;
reeml3 1:135f5adc847c 232 float newval = map(final_1,660,770,100,0);
mfaheem2004 0:0044354f0061 233
mfaheem2004 0:0044354f0061 234
mfaheem2004 0:0044354f0061 235 lcd.cls();
mfaheem2004 0:0044354f0061 236 lcd.locate(0,3);
mfaheem2004 0:0044354f0061 237
reeml3 1:135f5adc847c 238 snprintf(soil,sizeof soil,"Moisture=%.1f", (float)map(final_1,660,770,100,0)); //Sensor Key "Moisture" and Value is Sensor Data
mfaheem2004 0:0044354f0061 239
reeml3 1:135f5adc847c 240 lcd.printf("%s", soil); // Display Moisture value on LCD
mfaheem2004 0:0044354f0061 241 wait(2.0);
mfaheem2004 0:0044354f0061 242
mfaheem2004 0:0044354f0061 243
reeml3 1:135f5adc847c 244 snprintf(buf, sizeof buf, "%s%s%s%s%s%s", "POST /ksu/?deviceID=",deviceID,"&",soil,"&unit=%"," HTTP/1.0\r\n\r\n");
mfaheem2004 0:0044354f0061 245
mfaheem2004 0:0044354f0061 246 printf("Output string is %s",buf);
mfaheem2004 0:0044354f0061 247
mfaheem2004 0:0044354f0061 248 // Note: since this is a short string we can send it in one go as it will
mfaheem2004 0:0044354f0061 249 // fit within the default buffer sizes. Normally you should call sock.send()
mfaheem2004 0:0044354f0061 250 // in a loop until your entire buffer has been sent.
mfaheem2004 0:0044354f0061 251 if (sockTcp1.send((void *) buf, strlen(buf)) == (int) strlen(buf)) {
mfaheem2004 0:0044354f0061 252 pulseEvent();
mfaheem2004 0:0044354f0061 253 printf("Socket send completed, waiting for response...\n");
mfaheem2004 0:0044354f0061 254 x = sockTcp1.recv(buf, sizeof (buf));
mfaheem2004 0:0044354f0061 255 if (x > 0) {
mfaheem2004 0:0044354f0061 256 pulseEvent();
mfaheem2004 0:0044354f0061 257 printf("Received %d byte response from server on TCP socket:\n"
mfaheem2004 0:0044354f0061 258 "----------------------------------------------------\n%.*s"
mfaheem2004 0:0044354f0061 259 "----------------------------------------------------\n",
mfaheem2004 0:0044354f0061 260 x, x, buf);
mfaheem2004 0:0044354f0061 261 }
mfaheem2004 0:0044354f0061 262 }
mfaheem2004 0:0044354f0061 263
mfaheem2004 0:0044354f0061 264 }
mfaheem2004 0:0044354f0061 265 lcd.cls();
mfaheem2004 0:0044354f0061 266 lcd.locate(0,3);
mfaheem2004 0:0044354f0061 267 lcd.printf("Resend after 10s");
mfaheem2004 0:0044354f0061 268 wait(1.0);
mfaheem2004 0:0044354f0061 269 printf("Closing socket...\n");
mfaheem2004 0:0044354f0061 270 sockTcp.close();
mfaheem2004 0:0044354f0061 271 sockTcp1.close();
mfaheem2004 0:0044354f0061 272 pulseEvent();
mfaheem2004 0:0044354f0061 273 printf("Socket closed.\n");
mfaheem2004 0:0044354f0061 274 }
mfaheem2004 0:0044354f0061 275 #endif
mfaheem2004 0:0044354f0061 276 wait_ms(10000); //Loop delay in milliseconds. Keep it more than 10 seconds to save data usage on cellular
mfaheem2004 0:0044354f0061 277 #ifndef TARGET_UBLOX_C027
mfaheem2004 0:0044354f0061 278 printf("[Checking if user button has been pressed]\n");
mfaheem2004 0:0044354f0061 279 #endif
mfaheem2004 0:0044354f0061 280 }
mfaheem2004 0:0044354f0061 281
mfaheem2004 0:0044354f0061 282 pulseEvent();
mfaheem2004 0:0044354f0061 283 printf("User button was pressed, stopping...\n");
mfaheem2004 0:0044354f0061 284 interface->disconnect();
mfaheem2004 0:0044354f0061 285 ledOff();
mfaheem2004 0:0044354f0061 286 printf("Stopped.\n");
mfaheem2004 0:0044354f0061 287 }
mfaheem2004 0:0044354f0061 288 else {
mfaheem2004 0:0044354f0061 289 bad();
mfaheem2004 0:0044354f0061 290 printf("Unable to get IP address of \"integraton.campus.stcs.io\".\n");
mfaheem2004 0:0044354f0061 291 }
mfaheem2004 0:0044354f0061 292 }
mfaheem2004 0:0044354f0061 293
mfaheem2004 0:0044354f0061 294
mfaheem2004 0:0044354f0061 295 // End Of File