1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
| ////////////////////////////////////////////////////////////////////////////////
// Librairies importation
////////////////////////////////////////////////////////////////////////////////
#include <arpa/inet.h>
#include <ctime>
#include <fstream>
#include <iostream>
#include <memory>
#include <mutex>
#include <sstream>
#include <stdio.h>
#include <string>
#include <sys/socket.h>
#include <sys/time.h>
#include <thread>
#include <unistd.h>
#include "Librairies/MPU9250.h"
////////////////////////////////////////////////////////////////////////////////
// Variables declaration
////////////////////////////////////////////////////////////////////////////////
using namespace std;
timed_mutex m;
bool lostConnection(false);
bool connected(false);
char message[1000];
int sock;
string path = "";
string lastSecond = "";
string toWriteFile = "";
struct sockaddr_in server;
MPU9250 imu;
////////////////////////////////////////////////////////////////////////////////
// Functions declaration
////////////////////////////////////////////////////////////////////////////////
void wifiConnection()
{
// Create socket
sock = socket(AF_INET, SOCK_STREAM, 0);
if(sock == -1)
{
printf("Could not create socket!\n");
}
else
{
printf("Socket created\n");
}
server.sin_addr.s_addr = inet_addr("192.168.0.25");
server.sin_family = AF_INET;
server.sin_port = htons(8888);
// Connect to remote server
if(connect(sock, (struct sockaddr *)&server, sizeof(server))<0)
{
connected = false;
printf("Connect failed. Error\n");
}
else
{
connected = true;
printf("Connected\n");
}
}
void connect_w_to(void) {
int res, valopt;
struct sockaddr_in addr;
long arg;
fd_set myset;
struct timeval tv;
socklen_t lon;
int newSock = sock;
// Create socket
newSock = socket(AF_INET, SOCK_STREAM, 0);
// Set non-blocking
arg = fcntl(newSock, F_GETFL, NULL);
arg |= O_NONBLOCK;
fcntl(newSock, F_SETFL, arg);
// Trying to connect with timeout
int yes = 1;
setsockopt(newSock, SOL_SOCKET, SO_REUSEADDR, (void*) &yes, (socklen_t) sizeof(yes));
addr.sin_family = AF_INET;
addr.sin_port = htons(8888);
addr.sin_addr.s_addr = inet_addr("192.168.0.24");
res = connect(newSock, (struct sockaddr *)&addr, sizeof(addr));
if (res < 0) {
printf("Problem at connection\n");
connected = false;
if (errno == EINPROGRESS) {
tv.tv_sec = 15;
tv.tv_usec = 0;
FD_ZERO(&myset);
FD_SET(newSock, &myset);
if (select(newSock+1, NULL, &myset, NULL, &tv) > 0) {
lon = sizeof(int);
getsockopt(newSock, SOL_SOCKET, SO_ERROR, (void*)(&valopt), &lon);
if (valopt) {
fprintf(stderr, "Error in connection() %d - %s\n", valopt, strerror(valopt));
// exit(0);
}
}
else {
fprintf(stderr, "Timeout or error() %d - %s\n", valopt, strerror(valopt));
// exit(0);
}
}
else {
fprintf(stderr, "Error connecting %d - %s\n", errno, strerror(errno));
// exit(0);
}
}
else
{
connected = true;
}
// Set to blocking mode again...
arg = fcntl(newSock, F_GETFL, NULL);
arg &= (~O_NONBLOCK);
fcntl(newSock, F_SETFL, arg);
if(!connected)
{
close(newSock);
sleep(10);
connect_w_to();
}
else
{
sock = newSock;
}
}
/*
Transform the date from "Thu Jul 12 09:44:50 2018" to "2018-07-12_09"
to have a better sort in the files.
*/
string formatDate(string dateRaw)
{
// Delete the "\n" at the end of the string
dateRaw.erase(dateRaw.length()-1, 1);
string day = "";
day += dateRaw[8];
day += dateRaw[9];
string month = "";
switch(dateRaw[4])
{
case 'J':
switch(dateRaw[5])
{
case 'a': month = "01";
break;
case 'u':
switch(dateRaw[6])
{
case 'n': month = "06";
break;
case 'l': month = "07";
break;
default: month = "00";
}
break;
default: month = "00";
}
break;
case 'F': month = "02";
break;
case 'M':
switch(dateRaw[6])
{
case 'r': month = "03";
break;
case 'y': month = "05";
break;
default: month = "00";
}
break;
case 'A':
switch(dateRaw[5])
{
case 'p': month = "04";
break;
case 'u': month = "08";
break;
default: month ="00";
}
break;
case 'S': month = "09";
break;
case 'O': month = "10";
break;
case 'N': month = "11";
break;
case 'D': month = "12";
break;
default: month = "00";
}
string year = "";
year += dateRaw[20];
year += dateRaw[21];
year += dateRaw[22];
year += dateRaw[23];
string hour = "";
hour += dateRaw[11];
hour += dateRaw[12];
string minute = "";
minute += dateRaw[14];
minute += dateRaw[15];
string second = "";
second += dateRaw[17];
second += dateRaw[18];
string dateFormated = year;
dateFormated += "-" + month;
dateFormated += "-" + day;
dateFormated += "_" + hour;
dateFormated += "-" + minute;
dateFormated += "-" + second;
return dateFormated;
}
// Allows to convert float values to string
string tostr(float f)
{
ostringstream os;
os << f;
return os.str();
}
// Create a new path for files when the hour has changed
void createPath()
{
time_t startProgram = time(0);
char* temp = ctime(&startProgram);
string timeFile = temp;
string dateFormated = formatDate(timeFile);
path = "/home/root/MPU-Wifi-Save-2/TXT-Files/Reach1_IMU_" + dateFormated;
path += "h.txt";
}
void getData()
{
float ax, ay, az;
float gx, gy, gz;
float mx, my, mz;
// Receive IMU data
imu.getMotion9(&ax, &ay, &az, &gx, &gy, &gz, &mx, &my, &mz);
/*
Research the current date and time.
String receive - example: Thu Jul 12 09:44:50 2018
*/
time_t now = time(0);
char* temp = ctime(&now);
string dt = temp;
string toSend = "";
toSend += dt.erase(dt.length()-1, 1) + "\t";
toSend += "Acc: " + tostr(ax) + " " + tostr(ay) + " " + tostr(az) + "\t";
toSend += "Gyr: " + tostr(gz) + " " + tostr(gy) + " " + tostr(gz) + "\t";
toSend += "Mag: " + tostr(mx) + " " + tostr(my) + " " + tostr(mz) + "\n";
strcpy(message, toSend.c_str());
// printf("%s", message);
if(!connected)
toWriteFile += toSend;
if(lastSecond != tostr(dt[18]))
{
ofstream file(path.c_str(), ios::out | ios::app);
if(file)
{
file << toWriteFile;
toWriteFile = "";
lastSecond = tostr(dt[18]);
}
}
// thread connectionToWifi(wifiConnection);
// connectionToWifi.detach();
}
else
{
if(toWriteFile != "")
{
ofstream file(path.c_str(), ios::out | ios::app);
if(file)
{
file << toWriteFile;
toWriteFile = "";
lastSecond = tostr(dt[18]);
}
}
// Send data
int receiveToSend = send(sock, message, strlen(message), MSG_DONTWAIT);
if(receiveToSend < 0)
{
close(sock);
connected = false;
createPath();
ofstream file(path.c_str(), ios::out | ios::trunc);
if(file)
{
file << "";
}
cout << "Connection lost" << endl;
toWriteFile = toSend;
thread connectionToWifi(connect_w_to);
connectionToWifi.detach();
}
}
}
////////////////////////////////////////////////////////////////////////////////
// Main
////////////////////////////////////////////////////////////////////////////////
int main()
{
// Configure the IMU
imu.initialize();
// Check IMU connection
if (imu.doSelfTest())
{
printf("MPU9250 self test passed\n");
}
else
{
printf("MPU9250 self test failed");
}
wifiConnection();
// Infinite loop of data reception
time_t startProgram = time(0);
char* temp = ctime(&startProgram);
string timeStartProgram = temp;
lastSecond = tostr(timeStartProgram[18]);
while(1)
{
getData();
usleep(10000);
}
close(sock);
return 1;
} |
Partager