//#include <CayenneArduinoDefines.h>
//#include <CayenneMQTTWiFi.h>
//#include <CayenneMQTTMKR1010.h>
#define RX 32
#define TX 33
#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#define VIRTUAL_CHANNEL 0
//char ssid = “”;
//char wifiPassword=“”;
/* For Arduinoboards with multiple serial ports like DUEboard, interpret above two pieces of code and
directly use Serial1 serial port*/
int dist; //actual distance measurements of LiDAR
int strength; //signal strength of LiDAR
float temprature;
int check; //save check value
int i;
int uart[9]; //save data measured by LiDAR
const int HEADER=0x59; //frame header of data package
// Your GPRS credentials (leave empty, if missing)
const char apn = “internet”; // Your APN
const char gprsUser = “”; // User
const char gprsPass = “”; // Password
const char simPIN = “6317”; // SIM card PIN code, if any
// TTGO T-Call pin definitions
#define MODEM_RST 5
#define MODEM_PWKEY 4
#define MODEM_POWER_ON 23
#define MODEM_TX 27
#define MODEM_RX 26
#define I2C_SDA 21
#define I2C_SCL 22
// Set serial for debug console (to the Serial Monitor, default speed 115200)
#define SerialMon Serial
// Set serial for AT commands (to the module)
#define SerialAT Serial1
// Configure TinyGSM library
#define TINY_GSM_MODEM_SIM800 // Modem is SIM800
#define TINY_GSM_RX_BUFFER 1024 // Set RX buffer to 1Kb
#include <CayenneMQTTGSM.h>
char username = “6ef6eee0-7ee8-11e9-beb3-736c9e4bf7d0”;
char password = “27075a7d55000a07809e76b1286c5c2e52b2b78d”;
char clientID = “d64dced0-c644-11e9-8221-599f77add412”;
// Define the serial console for debug prints, if needed
//#define TINY_GSM_DEBUG SerialMon
//#define DUMP_AT_COMMANDS
#include <Wire.h>
#include <TinyGsmClient.h>
TinyGsm modem(SerialAT);
void GSM_SETUP() {
// Set console baud rate
SerialMon.begin(9600);
delay(10);
// Set-up modem reset, enable, power pins
pinMode(MODEM_PWKEY, OUTPUT);
pinMode(MODEM_RST, OUTPUT);
pinMode(MODEM_POWER_ON, OUTPUT);
digitalWrite(MODEM_PWKEY, LOW);
digitalWrite(MODEM_RST, HIGH);
digitalWrite(MODEM_POWER_ON, HIGH);
// Set GSM module baud rate and UART pins
SerialAT.begin(115200, SERIAL_8N1, MODEM_RX, MODEM_TX);
delay(3000);
// Restart takes quite some time
// To skip it, call init() instead of restart()
SerialMon.println(“Initializing modem…”);
modem.restart();
// Or, use modem.init() if you don’t need the complete restart
String modemInfo = modem.getModemInfo();
SerialMon.print("Modem: ");
SerialMon.println(modemInfo);
// Unlock your SIM card with a PIN if needed
if (strlen(simPIN) && modem.getSimStatus() != 3 ) {
modem.simUnlock(simPIN);
Serial.println(“SIM Unlocked”);
}
Cayenne.begin(username, password, clientID, SerialAT, apn, gprsUser, gprsPass, simPIN);
}
void GSM_LOOP() {
Cayenne.loop();
}
void TFMINI_SETUP() {
Serial.begin(9600); //set bit rate of serial port connecting Arduino with computer
//Serial1.begin(115200); //set bit rate of serial port connecting LiDAR with Arduino
Serial2.begin(115200, SERIAL_8N1, RX, TX);
}
void TFMINI_LOOP() {
// Cayenne.loop();
if (Serial2.available()) { //check if serial port has data input
//Serial.print(“*”);
if(Serial2.read() == HEADER) { //assess data package frame header 0x59
uart[0]=HEADER;
if (Serial2.read() == HEADER) { //assess data package frame header 0x59
uart[1] = HEADER;
for (i = 2; i < 9; i++) { //save data in array
uart[i] = Serial2.read();
}
check = uart[0] + uart[1] + uart[2] + uart[3] + uart[4] + uart[5] + uart[6] + uart[7];
if (uart[8] == (check & 0xff)){ //verify the received data as per protocol
dist = uart[2] + uart[3] * 256; //calculate distance value
strength = uart[4] + uart[5] * 256; //calculate signal strength value
temprature = uart[6] + uart[7] *256;//calculate chip temprature
temprature = temprature/8 - 256;
//Serial.print(“HEllo5”);
Serial.print("dist = ");
Serial.println(dist); //output measure distance value of LiDAR
}
}
}
}
}
// This function is called at intervals to send data to Cayenne.
//CAYENNE_OUT(VIRTUAL_CHANNEL)
//{
// CAYENNE_LOG(“Send data for Virtual Channel %d”, VIRTUAL_CHANNEL);
// This command writes the device’s uptime in seconds to the Virtual Channel.
// Cayenne.virtualWrite(VIRTUAL_CHANNEL, dist);
//}
CAYENNE_OUT_DEFAULT()
{
// Write data to Cayenne here. This example just sends the current uptime in milliseconds on virtual channel 0.
//Cayenne.virtualWrite(0, millis());
Cayenne.virtualWrite(VIRTUAL_CHANNEL, dist);
}
// Default function for processing actuator commands from the Cayenne Dashboard.
// You can also use functions for specific channels, e.g CAYENNE_IN(1) for channel 1 commands.
//CAYENNE_IN_DEFAULT()
//{
// CAYENNE_LOG(“Channel %u, value %s”, request.channel, getValue.asString());
//Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError(“Error message”);
//}
void setup() {
GSM_SETUP();
TFMINI_SETUP();
}
void loop() {
GSM_LOOP();
TFMINI_LOOP();
}
![image002.jpg](https://us1.discourse-cdn.com/flex019/uploads/mydevices/original/2X/2/2485d26919316b1d80aa60f5ca8e2cb5bc0275fe.jpg)