//Header file for Myo ARM Band
#include <MyoController.h>
#include <Wire.h>
// Include LinkIt ONE GSM and GPS library
#include <LGSM.h>
#include <LGPS.h>
//Myo pin configuration
#define FIST_PIN 4
#define WAVEIN_PIN 13
#define WAVEOUT_PIN 6
#define FINGERSSPREAD_PIN 7
#define DOUBLETAP_PIN 8
//Cloud part header files
#include <LGPRS.h>
#include <LGPRSClient.h>
#include <LGPRSServer.h>
#include <LGPS.h>
#include <HttpClient.h>
#include <LTask.h>
#include <LWiFi.h>
#include <LWiFiClient.h>
#include <LDateTime.h>
//Defining WiFi Access Point name and Password
#define WIFI_AP "Devaki"
#define WIFI_PASSWORD "devakipandari&10"
#define WIFI_AUTH LWIFI_WPA
// choose from LWIFI_OPEN, LWIFI_WPA, or LWIFI_WEP.
//Connecting LinkIt ONE to cloud
#define per 50
#define per1 3
#define DEVICEID "Dps78GVy"
#define DEVICEKEY "C0nS81PKwm0F0uxN"
#define SITE_URL "api.mediatek.com"
//Myo Controller Function
MyoController myo = MyoController();
gpsSentenceInfoStruct info;
char buff[256];
int num;
unsigned long t = 3000;
unsigned long change;
byte scrno;
boolean main = true;
//GPS declarations
double latitude;
double longitude;
char buffer_latitude[8];
char buffer_longitude[8];
LGPRSClient c;
unsigned int rtc;
unsigned int lrtc;
unsigned int rtc1;
unsigned int lrtc1;
char port[4]={0};
char connection_info[21]={0};
char ip[15]={0};
int portnum;
int val = 0;
//Connecting external devices to cloud
String tcpdata = String(DEVICEID) + "," + String(DEVICEKEY) + ",0";
String tcpcmd_led_on = "LED_CONTROL,1";
String tcpcmd_led_off = "LED_CONTROL,0";
String upload_led;
LGPRSClient c2;
HttpClient http(c2);
//GPS location tracking data
static unsigned char getComma1(unsigned char num, const char *str)
{
unsigned char i, j = 0;
int len = strlen(str);
for (i = 0; i < len; i ++)
{
if (str[i] == ',')
j++;
if (j == num)
return i + 1;
}
return 0;
}
static double getDoubleNumber1(const char *s)
{
char buf[10];
unsigned char i;
double rev;
i = getComma1(1, s);
i = i - 1;
strncpy(buf, s, i);
buf[i] = 0;
rev = atof(buf);
return rev;
}
static double getIntNumber1(const char *s)
{
char buf[10];
unsigned char i;
double rev;
i = getComma1(1, s);
i = i - 1;
strncpy(buf, s, i);
buf[i] = 0;
rev = atoi(buf);
return rev;
}
void parseGPGGA1(const char* GPGGAstr)
{
double latitude;
double longitude;
double dilution, alt, hg;
int tmp, hour, minute, second;
//Starting of GPS data
if (GPGGAstr[0] == '$')
{
tmp = getComma1(1, GPGGAstr);
hour = (GPGGAstr[tmp + 0] - '0') * 10 + (GPGGAstr[tmp + 1] - '0');
minute = (GPGGAstr[tmp + 2] - '0') * 10 + (GPGGAstr[tmp + 3] - '0');
second = (GPGGAstr[tmp + 4] - '0') * 10 + (GPGGAstr[tmp + 5] - '0');
sprintf(buff, "UTC timer %2d-%2d-%2d", hour, minute, second);
tmp = getComma1(2, GPGGAstr);
latitude = getDoubleNumber1(&GPGGAstr[tmp]);
tmp = getComma1(4, GPGGAstr);
longitude = getDoubleNumber1(&GPGGAstr[tmp]);
int latdeg = latitude / 100;
int londeg = longitude / 100;
double latmin = latitude - (((double)latdeg) * 100);
double lonmin = longitude - (((double)londeg) * 100);
tmp = getComma1(7, GPGGAstr);
num = getIntNumber1(&GPGGAstr[tmp]);
sprintf(buff, "satellites number = %d ", num);
sprintf(buff, "Lat=%dd %5.4fm", latdeg, latmin);
sprintf(buff, "Lon=%dd %5.4fm", londeg, lonmin);
tmp = getComma1(8, GPGGAstr);
dilution = getDoubleNumber1(&GPGGAstr[tmp]);
sprintf(buff, "Horizontal dilution = %10.4f ", dilution);
tmp = getComma1(9, GPGGAstr);
alt = getDoubleNumber1(&GPGGAstr[tmp]);
sprintf(buff, "Altitude (meters) = %10.4f ", alt);
tmp = getComma1(11, GPGGAstr);
hg = getDoubleNumber1(&GPGGAstr[tmp]);
sprintf(buff, "Height from mean sea level (geoid) = %10.4f", hg);
if (millis() - change > t)
{
if (main)
{
sprintf(buff, "Lat: %dd %5.4fm", latdeg, latmin);
sprintf(buff, "Lon: %dd %5.4fm", londeg, lonmin);
t = 3000;
scrno++;
if (scrno > 2)
{
scrno = 1;
}
main = false;
}
else
{
switch (scrno)
{
case 1:
sprintf(buff, "Satellites: %d", num);
sprintf(buff, "H Dilution: %2.3f", dilution);
break;
case 2:
sprintf(buff, "Altitude: %5.4f", alt);
sprintf(buff, "UTC: %2d-%2d-%2d", hour, minute, second);
break;
}
t = 1000;
main = true;
}
change = millis();
}
}
}
String message;
// Variable to store message to be sent
void setup()
{
//Declaration of input and output
pinMode(FIST_PIN, OUTPUT);
pinMode(WAVEIN_PIN, OUTPUT);
pinMode(WAVEOUT_PIN, OUTPUT);
pinMode(FINGERSSPREAD_PIN, OUTPUT);
pinMode(DOUBLETAP_PIN, OUTPUT);
//Myo Initialisation
myo.initMyo();
//Enabling GPS
LGPS.powerOn();
delay(3000);
while (!LSMS.ready())
// Wait for the sim to initialize
{
delay(1000); // Wait for a second and then try again
}
// Saves the number where user wants to send SMS
LSMS.beginSMS("9789021906");
//Cloud part coding
pinMode(13, OUTPUT);
LTask.begin();
LWiFi.begin();
AP_connect();
getconnectInfo();
connectTCP();
}
void loop()
{
//Enabling LED according to Hand Gestures
myo.updatePose();
switch ( myo.getCurrentPose() )
{
case rest:
digitalWrite(FIST_PIN,LOW);
digitalWrite(WAVEIN_PIN,LOW);
digitalWrite(WAVEOUT_PIN,LOW);
digitalWrite(FINGERSSPREAD_PIN,LOW);
digitalWrite(DOUBLETAP_PIN,LOW);
break;
case fist:
digitalWrite(FIST_PIN,HIGH);
break;
case waveIn:
digitalWrite(WAVEIN_PIN,HIGH);
LGPS.getData(&info);
parseGPGGA1((const char*)info.GPGGA);
delay(2000);
message = (char*)info.GPGGA;
LSMS.print(message);
// Prepare message variable to be sent by LSMS
LSMS.endSMS();
break;
case waveOut:
digitalWrite(WAVEOUT_PIN,HIGH);
break;
case fingersSpread:
digitalWrite(FINGERSSPREAD_PIN,HIGH);
break;
case doubleTap:
digitalWrite(DOUBLETAP_PIN,HIGH);
break;
}
//Cloud part coding
String tcpcmd="";
while (c.available())
{
int v = c.read();
if (v != -1)
{
Serial.print((char)v);
tcpcmd += (char)v;
if (tcpcmd.substring(40).equals(tcpcmd_led_on))
{
digitalWrite(10, HIGH);
Serial.print("Switch LED ON ");
tcpcmd="";
}
else if(tcpcmd.substring(40).equals(tcpcmd_led_off))
{
digitalWrite(10, LOW);
Serial.print("Switch LED OFF");
tcpcmd="";
}
}
}
LDateTime.getRtc(&rtc);
if ((rtc - lrtc) >= per)
{
heartBeat();
lrtc = rtc;
}
//Check for report datapoint status interval
LDateTime.getRtc(&rtc1);
if ((rtc1 - lrtc1) >= per1)
{
uploadstatus();
GPS_receive();
uploadGPS();
lrtc1 = rtc1;
}
delay(100);
}
//Cloud_GPS…
static unsigned char getComma(unsigned char num,const char *str)
{
unsigned char i,j = 0;
int len=strlen(str);
for(i = 0;i < len;i ++)
{
if(str[i] == ',')
j++;
if(j == num)
return i + 1;
}
return 0;
}
static double getDoubleNumber(const char *s)
{
char buf[10];
unsigned char i;
double rev;
i=getComma(1, s);
i = i - 1;
strncpy(buf, s, i);
buf[i] = 0;
rev=atof(buf);
return rev;
}
static double getIntNumber(const char *s)
{
char buf[10];
unsigned char i;
double rev;
i=getComma(1, s);
i = i - 1;
strncpy(buf, s, i);
buf[i] = 0;
rev=atoi(buf);
return rev;
}
void parseGPGGA(const char* GPGGAstr)
{
/* Refer to http://www.gpsinformation.org/dale/nmea.htm#GGA
* Sample data: $GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,545.4,M,46.9,M,,*47
* Where:
* GGA Global Positioning System Fix Data
* 123519 Fix taken at 12:35:19 UTC
* 4807.038,N Latitude 48 deg 07.038' N
* 01131.000,E Longitude 11 deg 31.000' E
* 1 Fix quality: 0 = invalid
* 1 = GPS fix (SPS)
* 2 = DGPS fix
* 3 = PPS fix
* 4 = Real Time Kinematic
* 5 = Float RTK
* 6 = estimated (dead reckoning) (2.3 feature)
* 7 = Manual input mode
* 8 = Simulation mode
* 08 Number of satellites being tracked
* 0.9 Horizontal dilution of position
* 545.4,M Altitude, Meters, above mean sea level
* 46.9,M Height of geoid (mean sea level) above WGS84
* ellipsoid
* (empty field) time in seconds since last DGPS update
* (empty field) DGPS station ID number
* *47 the checksum data, always begins with *
*/
int tmp, hour, minute, second, num ;
if(GPGGAstr[0] == '$')
{
tmp = getComma(1, GPGGAstr);
hour = (GPGGAstr[tmp + 0] - '0') * 10 + (GPGGAstr[tmp + 1] - '0');
minute = (GPGGAstr[tmp + 2] - '0') * 10 + (GPGGAstr[tmp + 3] - '0');
second = (GPGGAstr[tmp + 4] - '0') * 10 + (GPGGAstr[tmp + 5] - '0');
sprintf(buff, "UTC timer %2d-%2d-%2d", hour, minute, second);
//Serial.println(buff);
tmp = getComma(2, GPGGAstr);
latitude = getDoubleNumber(&GPGGAstr[tmp])/100.0;
int latitude_int=floor(latitude);
double latitude_decimal=(latitude-latitude_int)*100.0/60.0;
latitude=latitude_int+latitude_decimal;
tmp = getComma(4, GPGGAstr);
longitude = getDoubleNumber(&GPGGAstr[tmp])/100.0;
int longitude_int=floor(longitude);
double longitude_decimal=(longitude-longitude_int)*100.0/60.0;
longitude=longitude_int+longitude_decimal;
sprintf(buff, "latitude = %10.4f, longitude = %10.4f", latitude, longitude);
//Serial.println(buff);
tmp = getComma(7, GPGGAstr);
num = getIntNumber(&GPGGAstr[tmp]);
sprintf(buff, "satellites number = %d", num);
//Serial.println(buff);
}
}
void GPS_receive() {
LGPS.getData(&info);
parseGPGGA((const char*)info.GPGGA);
}
//Cloud_connect
void AP_connect(){
Serial.print("Connecting to AP...");
while (0 == LWiFi.connect(WIFI_AP, LWiFiLoginInfo(WIFI_AUTH, WIFI_PASSWORD)))
{
Serial.print(".");
delay(500);
}
Serial.println("Success!");
Serial.print("Connecting site...");
while (!c2.connect(SITE_URL, 80))
{
Serial.print(".");
delay(500);
}
Serial.println("Success!");
delay(100);
}
void getconnectInfo(){
//Calling RESTful API to get TCP socket connection
c2.print("GET /mcs/v2/devices/");
c2.print(DEVICEID);
c2.println("/connections.csv HTTP/1.1");
c2.print("Host: ");
c2.println(SITE_URL);
c2.print("deviceKey: ");
c2.println(DEVICEKEY);
c2.println("Connection: close");
c2.println();
delay(500);
int errorcount = 0;
Serial.print("waiting for HTTP response...");
while (!c2.available())
{
Serial.print(".");
errorcount += 1;
delay(150);
}
Serial.println();
int err = http.skipResponseHeaders();
int bodyLen = http.contentLength();
char c;
int ipcount = 0;
int count = 0;
int separater = 0;
while (c2)
{
int v = (int)c2.read();
if (v != -1)
{
c = v;
//Serial.print(c);
connection_info[ipcount]=c;
if(c==',')
separater=ipcount;
ipcount++;
}
else
{
Serial.println("no more content, disconnect");
c2.stop();
}
}
//connection_info[ipcount]=NULL;
int i;
for(i=0;i<separater;i++)
{ ip[i]=connection_info[i];
}
int j=0;
separater++;
for(i=separater;i<21 && j<5 && i < ipcount;i++)
{ port[j]=connection_info[i];
j++;
}
//port[j] = NULL;
portnum = atoi (port);
} //getconnectInfo
void connectTCP(){
//Establish TCP connection with TCP Server with designate IP and Port
c.stop();
Serial.print("Connecting to TCP...");
while (0 == c.connect(ip, portnum))
{
Serial.println("Re-Connecting to TCP");
delay(1000);
}
c.println(tcpdata);
c.println();
Serial.println("Success!");
} //connectTCP
void heartBeat(){
Serial.println("send TCP heartBeat");
c.println(tcpdata);
c.println();
} //heartbeat
//Cloud_upload
void uploadstatus(){
//calling RESTful API to upload datapoint to MCS to report LED status
while (!c2.connect(SITE_URL, 80))
{
Serial.print(".");
delay(500);
}
delay(100);
if(digitalRead(13)==1)
upload_led = "LED_DISPLAY,,1";
else
upload_led = "LED_DISPLAY,,0";
int thislength = upload_led.length();
HttpClient http(c2);
c2.print("POST /mcs/v2/devices/");
c2.print(DEVICEID);
c2.println("/datapoints.csv HTTP/1.1");
c2.print("Host: ");
c2.println(SITE_URL);
c2.print("deviceKey: ");
c2.println(DEVICEKEY);
c2.print("Content-Length: ");
c2.println(thislength);
c2.println("Content-Type: text/csv");
c2.println("Connection: close");
c2.println();
c2.println(upload_led);
delay(500);
int errorcount = 0;
while (!c2.available())
{
Serial.print(".");
delay(100);
}
int err = http.skipResponseHeaders();
int bodyLen = http.contentLength();
while (c2)
{
int v = c2.read();
if (v != -1)
{
Serial.print(char(v));
}
else
{
Serial.println("no more content, disconnect");
c2.stop();
}
}
Serial.println();
}
void uploadGPS(){
while (!c2.connect(SITE_URL, 80))
{
Serial.print(".");
delay(500);
}
delay(100);
float latitude_post=latitude;
float longitude_post=longitude;
Serial.printf("latitude=%.4f\tlongitude=%.4f\n",latitude,longitude);
if(latitude>-90 && latitude<=90 && longitude>=0 && longitude<360){
sprintf(buffer_latitude, "%.4f", latitude);
sprintf(buffer_longitude, "%.4f", longitude);
}
String upload_GPS = "GPS,,"+String(buffer_latitude)+","+String(buffer_longitude)+","+"0"+"\n"+"LATITUDE,,"+buffer_latitude+"\n"+"LONGITUDE,,"+buffer_longitude;//null altitude
int GPS_length = upload_GPS.length();
HttpClient http(c2);
c2.print("POST /mcs/v2/devices/");
c2.print(DEVICEID);
c2.println("/datapoints.csv HTTP/1.1");
c2.print("Host: ");
c2.println(SITE_URL);
c2.print("deviceKey: ");
c2.println(DEVICEKEY);
c2.print("Content-Length: ");
c2.println(GPS_length);
c2.println("Content-Type: text/csv");
c2.println("Connection: close");
c2.println();
c2.println(upload_GPS);
delay(500);
int errorcount = 0;
while (!c2.available())
{
Serial.print(".");
delay(100);
}
int err = http.skipResponseHeaders();
int bodyLen = http.contentLength();
while (c2)
{
int v = c2.read();
if (v != -1)
{
Serial.print(char(v));
}
else
{
Serial.println("no more content, disconnect");
c2.stop();
}
}
Serial.println();
}
Comments