Étape 3: Télécharger Arduino code dans l’IDE Arduino
Coffre fort dans la Sandbox
Fichiers d’en-tête requis
Si non disponible, assurez-vous de les trouver dans les dépôts de Github
#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 »
Personnaliser les données nécessaires selon vos spécifications
#define WIFI_AP « votre nom de wifi »
#define WIFI_PASSWORD « votre mot de passe wifi »
#define DEVICEID « votre id de périphérique »
#define DEVICEKEY « votre clé de périphérique »
#define pConfig_site_url « api.mediatek.com »
#define WIFI_AUTH LWIFI_WPA
#define pour 50
#define PAR1 3
gpsSentenceInfoStruct info ; buff de char [256] ; double latitude ; double longitude ; double spd ; char buffer_latitude [8] ; char buffer_longitude [8] ; char buffer_spd [8] ; char buffer_sensor [8] ; public static unsigned char getComma (unsigned char num, const char * str) {unsigned char i, j = 0; int len=strlen(str) ; pour (j’ai = 0; j’ai < len ; j’ai ++) {si (str [i] == «, ») j ++; if(j == num) retourner i + 1;} return 0;} public static double getDoubleNumber (const char * s) {char buf [10]; unsigned char j’ai ; double rev ; j’ai = getComma (1 s); j’ai = i - 1; strncpy (buf s, i) ; buf [i] = 0 ; Rev=atof(BUF) ; retour rev ; } public static double getIntNumber (const char * s) {char buf [10]; unsigned char j’ai ; double rev ; j’ai = getComma (1 s); j’ai = i - 1; strncpy (buf, s, i); buf [i] = 0; rev=atoi(buf) ; retour rev;} //Esssential partie de code //GPS informatique //Determining quantités latitude, longitude et vitesse Sub parseDPAS (const char * DPASstr) {int tmp, heure, minute, seconde, num ; si (DPASstr [0] == « $») {tmp = getComma (1 DPASstr) ; heure = (DPASstr [tmp + 0] - « 0 ») * 10 + (DPASstr [tmp + 1] - « 0 ») ; minute = (DPASstr [tmp + 2] - « 0 ») * 10 + (DPASstr [tmp + 3] - « 0 ») ; deuxième = (DPASstr [tmp + 4] - « 0 ») * 10 + (DPASstr [tmp + 5] - « 0 ») ; sprintf (chamois, "UTC minuterie 2d-2d-% 2d", heure, minute, seconde) ; tmp = getComma (2, DPASstr) ; Latitude = getDoubleNumber(&DPASstr[tmp])/100.0 ; int latitude_int=floor(latitude) ; latitude_decimal double =(latitude-latitude_int) *100.0/60.0 ; Latitude = latitude_int + latitude_decimal ; tmp = getComma (4, DPASstr) ; Longitude = getDoubleNumber(&DPASstr[tmp])/100.0 ; int longitude_int=floor(longitude) ; longitude_decimal double =(longitude-longitude_int) *100.0/60.0 ; Longitude = longitude_int + longitude_decimal ; sprintf (chamois, "latitude = % 10 .4f, longitude = %10.4f", latitude, longitude) ; tmp = getComma (7, DPASstr) ; SPD = getDoubleNumber (& DPASstr[tmp]) ; sprintf (chamois, "vitesse = %F", spd) ; } else {Serial.println ("pas get data");}} void recdat() {LGPS.getData (& info); //Serial.println ((char*) info. DPAS) ; parseDPAS ((const char*) info. DPAS) ; } //AP connexion void AP_connect() {Serial.print ("connexion à AP..."), tout en (0 == LWiFi.connect (WIFI_AP, LWiFiLoginInfo (WIFI_AUTH, WIFI_PASSWORD))) {Serial.print(".") ; delay(500);} Serial.println("Success!") ; Serial.Print ("connexion site...") ;
tandis que (! drivepar.connect (pConfig_site_url, 80)) {Serial.print(".") ; delay(500);} Serial.println("Success!") ; Delay(100) ; } void getconnectInfo() {drivepar.print ("GET /mcs/v2/devices /"); drivepar.print(DEVICEID) ; drivepar.println ("/connections.csv HTTP/1.1"); drivepar.print ("hôte:") ; drivepar.println(SITE_URL) ; drivepar.Print ("deviceKey:") ; drivepar.println(DEVICEKEY) ; drivepar.println ("Connection : close") ; drivepar.println() ; Delay(500) ; int errorcount = 0 ; Serial.Print ("en attente de réponse HTTP...") ; tandis que (! drivepar.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 ; séparateur d’int = 0 ; tandis que (drivepar) {int v = (int)drivepar.read() ; if (v! = -1) {c = v; / / Serial.print(c) ; connection_info [ipcount] = c; if(c==',') séparateur = ipcount ; ipcount ++;} else {Serial.println ("pas plus de contenu, déconnecter"); drivepar.stop();}} int i ; pour (i = 0; i} void connectTCP() {c.stop() ; Serial.Print ("connexion TCP...") ; tandis que (0 == c.connect (ip, portnum)) {Serial.println ("re-connexion TCP"); delay(1000);} c.println(tcpdata) ; c.println() ; Serial.println("Success!") ; } void heartBeat() {Serial.println ("envoi TCP heartBeat"); c.println(tcpdata) ; c.println();} void uploadstatus() {tandis que (! drivepar.connect (pConfig_site_url, 80)) {Serial.print(".") ; delay(500);} delay(100) ; if(digitalRead(13)==1) upload_led = "ENGINE_MODE_DISPLAY,, 1"; else upload_led = « ENGINE_MODE_DISPLAY,, 0 »; int thislength = upload_led.length() ; HttpClient http(drivepar) ; drivepar.Print ("POST /mcs/v2/devices /") ; drivepar.Print(DEVICEID) ; drivepar.println ("/datapoints.csv HTTP/1.1") ; drivepar.Print ("hôte:") ; drivepar.println(SITE_URL) ; drivepar.Print ("deviceKey:") ; drivepar.println(DEVICEKEY) ; drivepar.Print ("Content-Length:") ; drivepar.println(thislength) ; drivepar.println ("Content-Type : text/csv") ; drivepar.println ("Connection : close") ; drivepar.println() ; drivepar.println(upload_led) ; Delay(500) ;
int errorcount = 0 ; tandis que (! drivepar.available()) {Serial.print(".") ; delay(100);} int err = http.skipResponseHeaders() ; int bodyLen = http.contentLength() ; tandis que (drivepar) {int v = drivepar.read() ; if (v! = -1) {Serial.print(char(v));} else {Serial.println ("pas plus de contenu, déconnecter"); drivepar.stop();}} Serial.println() ; } //Data streaming-vers-Sandbox Cloud Mediatek transdat() Sub {tandis que (! drivepar.connect (pConfig_site_url, 80)) {Serial.print(".") ; delay(500);} delay(100) ; capteur flotteur = analogRead(A0) ; Serial.printf("latitude=%.4f\tlongitude=%.4f\tspd=%.4f\tsensor=%.4f\n",Latitude,longitude,SPD,Sensor) ; Si (latitude > -90 & & latitude < = 90 & & longitude > = 0 & & longitude < 360) {sprintf (buffer_latitude, "%.4f", latitude); sprintf (buffer_longitude, "%.4f", longitude); sprintf ("%.4f", buffer_spd, spd); sprintf ("%.4f", buffer_sensor, capteur);} String upload_GPS = "GPS,,"+String(buffer_latitude)+","+String(buffer_longitude)+","+String(buffer_spd)+","+String(buffer_sensor)+","+"0"+"\n"+"LATITUDE,,"+buffer_latitude+"\n"+"LONGITUDE,,"+buffer_longitude+"\n"+"SPEED,,"+buffer_spd+"\n"+"STEERING_ANGLE,,"+buffer_sensor; int GPS_length = upload_GPS.length() ; HttpClient http(drivepar) ; drivepar.Print ("POST /mcs/v2/devices /") ; drivepar.Print(DEVICEID) ; drivepar.println ("/datapoints.csv HTTP/1.1") ; drivepar.Print ("hôte:") ; drivepar.println(SITE_URL) ; drivepar.Print ("deviceKey:") ; drivepar.println(DEVICEKEY) ; drivepar.Print ("Content-Length:") ; drivepar.println(GPS_length) ; drivepar.println ("Content-Type : text/csv") ; drivepar.println ("Connection : close") ; drivepar.println() ; drivepar.println(upload_GPS) ; Delay(500) ;
int errorcount = 0 ; tandis que (! drivepar.available()) {Serial.print(".") ; delay(100);} int err = http.skipResponseHeaders() ; int bodyLen = http.contentLength() ; tandis que (drivepar) {int v = drivepar.read() ; if (v! = -1) {Serial.print(char(v));} else {Serial.println ("pas plus de contenu, déconnecter"); drivepar.stop();}} Serial.println() ; } LGPRSClient c ; unsigned int CCF ; unsigned int CRTL ; unsigned int rtc1 ; unsigned int lrtc1 ; char de port [4] = {0} ; char connection_info [21] = {0} ; char d’ip [15] = {0} ; portnum int ; int val = 0 ; String tcpdata = String(DEVICEID) + «, » + String(DEVICEKEY) + «, 0 » ; String tcpcmd_led_on = « ENGINE_MODE_CONTROL, 1 » ; String tcpcmd_led_off = « ENGINE_MODE_CONTROL, 0 » ; String upload_led ; LGPRSClient drivepar ; HttpClient http(drivepar) ; Le programme d’installation void setup() {pinMode (13, sortie) ; LTask.begin() ; LWiFi.begin() ; Serial.Begin(115200) ; LGPS.powerOn() ; AP_connect() ; getconnectInfo() ; connectTCP() ; Serial.println("...");} Boucle de void loop() {String tcpcmd = ""; tout en (c.available()) {int v = c.read() ; if (v! = -1) {Serial.print((char)v) ; tcpcmd += (char) v; si (tcpcmd.substring(40).equals(tcpcmd_led_on)) {digitalWrite (13, HIGH) ; Serial.Print ("Switch LED sur") ; tcpcmd = "" ; } else if(tcpcmd.substring(40).equals(tcpcmd_led_off)) {digitalWrite (13, faible) ; Serial.Print ("éteindre LED") ; tcpcmd = "" ; {}} LDateTime.getRtc(&rtc) ; Si ((rtc-lrtc) > = par) {heartBeat() ; CRTL = CCF;} LDateTime.getRtc(&rtc1) ; Si ((rtc1-lrtc1) > = PAR1) {uploadstatus() ; recdat() ; transdat() ; lrtc1 = rtc1;}}