old_client.ino (4708B)
1 #include "include/config.h" 2 #include "include/http.h" 3 #include "include/modem.h" 4 #include "include/remote.h" 5 6 #include <Sodaq_LSM303AGR.h> 7 #include <Sodaq_UBlox_GPS.h> 8 #include <Wire.h> 9 10 #define ADC_AREF 3.3f 11 #define BATVOLT_R1 4.7f 12 #define BATVOLT_R2 10.0f 13 #define BATVOLT_PIN BAT_VOLT 14 15 #define statusDelay 0.5 // seconds 16 17 #define batteryFactor (0.978 * (BATVOLT_R1 / BATVOLT_R2 + 1) / ADC_AREF) 18 19 interface http; 20 21 sara_modem modem; 22 Sodaq_LSM303AGR accel; 23 serial_remote remote; 24 25 void setup() { 26 // -*- hardware initiation -*- 27 28 usbSerial.begin(remoteBaud); 29 while (true && !usbSerial) 30 ; 31 32 pinMode(BATVOLT_PIN, INPUT); 33 pinMode(CHARGER_STATUS, INPUT); 34 35 // modem.init(); 36 remote.begin(); 37 38 Wire.begin(); 39 delay(1000); 40 sodaq_gps.init(GPS_ENABLE); 41 42 accel.rebootAccelerometer(); 43 delay(1000); 44 45 // Enable the Accelerometer 46 accel.enableAccelerometer(); 47 48 remote.connect("127.0.0.1", 5000); 49 50 // modem.send("ATE0"); // disable command-echo 51 52 // if (modem.send("AT+CPIN=\"" SIM_PIN "\"") == sara_modem::COMMAND_ERROR) { 53 // // usbSerial.println("[EROR] sim can't be unlocked, wrong PIN"); 54 // return; 55 // } 56 // usbSerial.println(prefixInfo "sim successful unlocked"); 57 /* 58 59 modem.send("AT+CPSMS=0"); // Disable Power Saving Mode 60 modem.send("AT+CEDRXS=0"); // Disable eDRX 61 // usbSerial.println(prefixInfo "disabled power safe"); 62 63 64 // -*- internet initialization -*- 65 char info[100]; 66 67 modem.send("AT+CFUN=15", sara_modem::COMMAND_BLOCK); // Reset the module 68 modem.send("AT+UMNOPROF=1", sara_modem::COMMAND_BLOCK); // Set MNO profile (1=automatic,100=standard europe) 69 modem.send("AT+URAT?", info); 70 // usbSerial.print(prefixInfo "urat: "); 71 // usbSerial.println(info); 72 modem.send("AT+URAT=8", sara_modem::COMMAND_IGNORE); // Set URAT to LTE-M/NB-IOT 73 modem.send("AT+CEREG=3", sara_modem::COMMAND_IGNORE); // Enable URCs 74 modem.send("AT+CGDCONT=1,\"IP\",\"" simAPN "\"", sara_modem::COMMAND_BLOCK); // Set the APN 75 modem.send("AT+CFUN=1"); // enable radio 76 modem.send("AT+COPS=0,2", sara_modem::COMMAND_BLOCK); // Autoselect the operator 77 78 // usbSerial.print(prefixInfo "waiting for connection"); 79 80 char response[100]; 81 82 // Check Siganl strenght, repeat till you have a valid CSQ (99,99 means no signal) 83 while (modem.send("AT+CSQ", response, sara_modem::COMMAND_SILENT) == sara_modem::COMMAND_OK && !strcmp(response, "+CSQ: 99,99")) { 84 delay(1000); 85 // usbSerial.print("."); 86 } 87 88 // Wait for attach, 1 = attached 89 while (modem.send("AT+CGATT?", response, sara_modem::COMMAND_SILENT) == sara_modem::COMMAND_OK && strcmp(response, "+CGATT: 1")) { 90 delay(1000); 91 // usbSerial.print("."); 92 } 93 // usbSerial.println(); 94 95 // usbSerial.println(prefixInfo "connected!"); 96 97 // -*- server connection -*- 98 99 100 //AT+UHTTP=0,0,"86.92.67.21" 101 //AT+UHTTP=0,5,80 102 //AT+UHTTPC=0,5,"/api/search_connect","","TEST!",1 103 104 modem.send("AT+UHTTP=0,0,\"86.92.67.21\""); 105 modem.send("AT+UHTTP=0,5,80"); 106 modem.send("AT+UHTTPC=0,5,\"/api/search_connect\",\"\",\"TEST!\",1");*/ 107 108 109 // usbSerial.println(prefixInfo "initiation completed, starting remote:"); 110 } 111 112 113 void loop() { 114 /* // -*- remote for custom commands -*- 115 while (usbSerial.available()) 116 modemSerial.write(usbSerial.read()); 117 118 while (modemSerial.available()) 119 usbSerial.write(modemSerial.read()); 120 121 char buffer[512]; 122 gps.read(buffer); 123 124 if (buffer[0] != '\0') { 125 // usbSerial.print("gps | "); 126 // usbSerial.println(buffer); 127 }*/ 128 129 static int last = 0; 130 int now = millis(); 131 132 static double lat = 0, lon = 0, accuracy = 0; 133 134 if (now - last > statusDelay * 1000) { 135 if (sodaq_gps.scan(true, 10000)) { 136 lat = sodaq_gps.getLat(); 137 lon = sodaq_gps.getLon(); 138 accuracy = 1.0 / sodaq_gps.getHDOP() * 100; 139 // -> 100% the best, 0% the worst 140 // usbSerial.print(sodaq_gps.getLat(), 13); 141 // usbSerial.print(" - "); 142 // usbSerial.print(sodaq_gps.getLon(), 13); 143 // usbSerial.print(" ~ accuracy "); 144 // usbSerial.print(1.0 / sodaq_gps.getHDOP() * 100, 1); 145 // usbSerial.println("%"); 146 } 147 148 serial_remote::http_packet req, res; 149 req.method = "POST"; 150 req.endpoint = "/api/update"; 151 req.body["charging"] = (bool) digitalRead(CHARGER_STATUS); 152 req.body["latitude"] = lat; 153 req.body["longitude"] = lon; 154 req.body["accuracy"] = accuracy; 155 req.body["battery"] = batteryVoltage(); 156 req.body["temperature"] = temperature(); 157 158 remote.send(req); 159 last = now; 160 } 161 162 // usbSerial.print(batteryVoltage()); 163 // usbSerial.println("V battery"); 164 165 // usbSerial.print(temperature()); 166 // usbSerial.println(" deg celsius"); 167 } 168 169 int temperature() { 170 return accel.getTemperature(); 171 } 172 173 int batteryVoltage() { 174 return batteryFactor * (float) analogRead(BATVOLT_PIN); 175 }