Paso 3: Añadir el código de Arduino en el IDE de Arduino
Caja de seguridad en Sandbox
Archivos de encabezado necesarios
Si no está disponible, asegúrese de encontrar en los repositorios 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"
Personalizar los datos necesarios según sus especificaciones
#define WIFI_AP "tu nombre de wifi"
#define WIFI_PASSWORD "tu contraseña wifi"
#define DEVICEID "tu id de dispositivo"
#define DEVICEKEY "su clave de dispositivo"
#define SITE_URL "api.mediatek.com"
#define WIFI_AUTH LWIFI_WPA
#define por 50
#define per1 3
información de gpsSentenceInfoStruct; buff de Char [256]; latitud doble; longitud doble; spd doble; char buffer_latitude [8]; char buffer_longitude [8]; char buffer_spd [8]; char buffer_sensor [8]; Static unsigned char getComma (unsigned char num, const char * str) {unsigned char i, j = 0; int len=strlen(str); para (i = 0; me < len; me ++) {si (str [i] == ',') j ++; if(j == num) volver i + 1;} return 0;} estática doble getDoubleNumber (const char * s) {char buf [10]; unsigned char i doble rev; me = getComma (1, s); me = i - 1; strncpy (buf s, i); buf [i] = 0; Rev=atof(BUF); volver rev; } estática doble getIntNumber (const char * s) {char buf [10]; unsigned char i doble rev; me = getComma (1, s); i = i - 1; strncpy (buf, s, i); buf [i] = 0; rev=atoi(buf); return rev;} //Esssential parte del código //GPS informática //Determining cantidades latitud, longitud y velocidad nula parseDPAS (const char * DPASstr) {int tmp, hora, minuto, segundo, num; si (DPASstr [0] == '$') {tmp = getComma (1 DPASstr); hora = (DPASstr [tmp + 0] - '0') * 10 + (DPASstr [tmp + 1] - '0'); minutos = (DPASstr [tmp + 2] - '0') * 10 + (DPASstr [tmp + 3] - '0'); en segundo lugar = (DPASstr [tmp + 4] - '0') * 10 + (DPASstr [tmp + 5] - '0'); sprintf (buff, "UTC temporizador % 2d-2d-% 2d", hora, minuto, segundo); tmp = getComma (2, DPASstr); latitud = getDoubleNumber(&DPASstr[tmp])/100.0; int latitude_int=floor(latitude); latitude_decimal doble =(latitude-latitude_int) *100.0/60.0; latitud = latitude_int + latitude_decimal; tmp = getComma (4, DPASstr); longitud = getDoubleNumber(&DPASstr[tmp])/100.0; int longitude_int=floor(longitude); longitude_decimal doble =(longitude-longitude_int) *100.0/60.0; longitud = longitude_int + longitude_decimal; sprintf (buff, "latitude = % .4f 10, longitud = %10.4f", latitud, longitud); tmp = getComma (7, DPASstr); SPD = getDoubleNumber (& DPASstr[tmp]); sprintf (buff, "velocidad = %F", spd); } else {Serial.println ("no obtener datos");}} void recdat() {LGPS.getData (e información); //Serial.println ((char*) info. DPAS); parseDPAS ((const char*) información. DPAS); } //AP conexión void AP_connect() {Serial.print ("conectando a AP..."); mientras que (0 == LWiFi.connect (WIFI_AP, LWiFiLoginInfo (WIFI_AUTH, WIFI_PASSWORD))) {Serial.print("."); delay(500);} Serial.println("Success!"); Serial.Print ("conexión sitio...");
mientras (! drivepar.connect (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 ("Host:"); drivepar.println(SITE_URL); drivepar.Print ("deviceKey:"); drivepar.println(DEVICEKEY); drivepar.println ("conexión: cerrar"); drivepar.println(); Delay(500); int errorcount = 0; Serial.Print ("en espera de respuesta HTTP..."); mientras (! drivepar.available()) {Serial.print("."); errorcount += 1; delay(150);} Serial.println(); int err = http.skipResponseHeaders(); int bodyLen = http.contentLength(); char c; int ipcount = 0; int cuenta = 0; separador de int = 0; mientras (drivepar) {int v = (int)drivepar.read(); si (v! = -1) {c = v; / / Serial.print(c); connection_info [ipcount] = c; separador de if(c==',') = ipcount; ipcount ++;} else {Serial.println ("no más contenido, desconectar"); drivepar.stop();}} int i; para (i = 0; i} void connectTCP() {c.stop(); Serial.Print ("conexión a TCP..."); mientras que (0 == c.connect (ip, portnum)) {Serial.println ("volver a conectar a TCP"); delay(1000);} c.println(tcpdata); c.println(); Serial.println("Success!"); } void heartBeat() {Serial.println ("envío TCP del latido del corazón"); c.println(tcpdata); c.println();} void uploadstatus() {mientras que (! drivepar.connect (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 ("Host:"); drivepar.println(SITE_URL); drivepar.Print ("deviceKey:"); drivepar.println(DEVICEKEY); drivepar.Print ("Content-Length:"); drivepar.println(thislength); drivepar.println ("tipo de contenido: texto/csv"); drivepar.println ("conexión: cerrar"); drivepar.println(); drivepar.println(upload_led); Delay(500);
int errorcount = 0; mientras (! drivepar.available()) {Serial.print("."); delay(100);} int err = http.skipResponseHeaders(); int bodyLen = http.contentLength(); mientras (drivepar) {int v = drivepar.read(); if (v! = -1) {Serial.print(char(v));} else {Serial.println ("no más contenido, desconectar"); drivepar.stop();}} Serial.println(); } //Data streaming a Mediatek nube Sandbox void transdat() {mientras que (! drivepar.connect (SITE_URL, 80)) {Serial.print("."); delay(500);} delay(100); sensor de flotador = analogRead(A0); Serial.printf("Latitude=%.4f\tlongitude=%.4f\tspd=%.4f\tsensor=%.4f\n",Latitude,longitude,SPD,sensor); Si (latitud > -90 & & latitud < = 90 & & longitud > = 0 & & longitud < 360) {sprintf (buffer_latitude, "%.4f", latitud) sprintf (buffer_longitude, "%.4f", longitud); sprintf (buffer_spd, "%.4f", spd) sprintf (buffer_sensor, "%.4f", sensor);} 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 ("Host:"); drivepar.println(SITE_URL); drivepar.Print ("deviceKey:"); drivepar.println(DEVICEKEY); drivepar.Print ("Content-Length:"); drivepar.println(GPS_length); drivepar.println ("tipo de contenido: texto/csv"); drivepar.println ("conexión: cerrar"); drivepar.println(); drivepar.println(upload_GPS); Delay(500);
int errorcount = 0; mientras (! drivepar.available()) {Serial.print("."); delay(100);} int err = http.skipResponseHeaders(); int bodyLen = http.contentLength(); mientras (drivepar) {int v = drivepar.read(); if (v! = -1) {Serial.print(char(v));} else {Serial.println ("no más contenido, desconectar"); drivepar.stop();}} Serial.println(); } LGPRSClient c; unsigned int rtc; unsigned int lrtc; unsigned int rtc1; unsigned int lrtc1; carbón puerto [4] = {0}; char connection_info [21] = {0}; char ip [15] = {0}; int portnum; 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); Configuración void setup() {pinMode (13, salida); LTask.begin(); LWiFi.begin(); Serial.Begin(115200); LGPS.powerOn(); AP_connect(); getconnectInfo(); connectTCP(); Serial.println("...");} Bucle void loop() {String tcpcmd = ""; mientras que (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 ("encender LED"); tcpcmd = ""; } else if(tcpcmd.substring(40).equals(tcpcmd_led_off)) {digitalWrite (13, bajo); Serial.Print ("apagar LED"); tcpcmd = ""; }}} LDateTime.getRtc(&rtc); Si ((rtc-lrtc) > = por) {heartBeat(); lrtc = rtc;} LDateTime.getRtc(&rtc1); Si ((rtc1-lrtc1) > = per1) {uploadstatus(); recdat(); transdat(); lrtc1 = rtc1;}}