ESP32 LoRA SIM800 TinyGSM board connection to Cayenne

share the full code, when you make any changes.

Apologies,

Please find below the full code:

//#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

your code looks good. I am not sure what to do mean by slow, where you still having Serial.println() code reporting which should have been SerialMon.println()

image002.jpg

shramik Dear Shramik,

Hope you are well.

I have managed to solve the coding issue and now the sensor works fine. I have also succeeded in registering a LoRa WiMOd module installed on an Arduino 2560 to the Actility servers.

I am now trying to import the sensor to Cayenne as per the instructions included and I am facing a problem as when using the LoRA Actility import menu I do not get the option of selecting a custom board but instead only sensors provided by other manufacturers.

The code I used from WiMod provided libraries and examples is:

/*

* This is a simple example file to show how to use the WiMOD Arduino

* library to communicate with a WiMOD Module by IMST GmbH

*

* http://www.wireless-solutions.de

*

*/

/*

* Example:

*

* This example demonstrates how to start a LoRaWAN OTAA procedure to “register”

* the WiMOD to a LoRaWAN server

*

* Setup requirements:

* -------------------

* - 1 WiMOD module running WiMOD_LoRaWAN_EndNode_Modemfirmware

*

* Usage:

* -------

* - Change the keys according to your LoRaWAN server before starting

* - Start the program and watch the serial monitor @ 115200 baud

*/

// make sure to use only the WiMODLoRaWAN.h

// the WiMODLR_BASE.h must not be used for LoRaWAN firmware.

#include <WiMODLoRaWAN.h>

#if defined(ARDUINO_ARCH_AVR)

#include <avr/pgmspace.h>

#endif

//-----------------------------------------------------------------------------

// platform defines

//-----------------------------------------------------------------------------

/*

* Note: This sketch is for Arduino devices with two separate serial interfaces

* (e.g. DUE). One interface is connected to a PC and one is used for WiMOD.

*

* For single serial interface boards (e.g. UNO) it is recommended to disbale

* the PC / Debug messages

*/

#define WIMOD_IF Serial3 // for WiMODino use: SerialWiMOD

#define PC_IF Serial // for WiMODino use: SerialUSB

//-----------------------------------------------------------------------------

// constant values

//-----------------------------------------------------------------------------

/*

* OTAA Parameters

*/

// application key (64bit)

const unsigned char APPEUI[] = { 0xAC, 0xDE, 0x48, 0x23, 0x45, 0x67, 0xAB, 0xCD };

// application key (128bit)

const unsigned char APPKEY[] = { 0xBE, 0xC4, 0x99, 0xC6, 0x9E, 0x9C, 0x93, 0x9E,

0x41, 0x3B, 0x066, 0x39, 0x61, 0x63, 0x6C, 0x61 };

int txdata = 30;

//-----------------------------------------------------------------------------

// user defined types

//-----------------------------------------------------------------------------

typedef enum TModemState

{

ModemState_Disconnected = 0,

ModemState_ConnectRequestSent,

ModemState_Connected,

ModemState_FailedToConnect,

} TModemState;

typedef struct TRuntimeInfo

{

TModemState ModemState;

} TRuntimeInfo;

//-----------------------------------------------------------------------------

// section RAM

//-----------------------------------------------------------------------------

/*

* Create in instance of the interface to the WiMOD-LR-Base firmware

*/

WiMODLoRaWAN wimod(WIMOD_IF); // use the Arduino Serial3 as serial interface

TRuntimeInfo RIB = { };

static uint32_t loopCnt = 0;

static TWiMODLORAWAN_TX_Data txData;

//-----------------------------------------------------------------------------

// section code

//-----------------------------------------------------------------------------

/*****************************************************************************

* Function for printing out some debug infos via serial interface

****************************************************************************/

void debugMsg(String msg)

{

PC_IF.print(msg); // use default Arduino serial interface

}

void debugMsg(int a)

{

PC_IF.print(a, DEC);

}

void debugMsgChar(char c)

{

PC_IF.print(c);

}

void debugMsgHex(int a)

{

if (a < 0x10) {

PC_IF.print(F(“0”));

}

PC_IF.print(a, HEX);

}

/*****************************************************************************

* join tx indication callback

****************************************************************************/

void onJoinTx(TWiMODLR_HCIMessage& rxMsg) {

TWiMODLORAWAN_TxIndData txData = {};

wimod.convert(rxMsg, &txData);

debugMsg(F("joining attempt: "));

if (txData.FieldAvailability == LORAWAN_OPT_TX_IND_INFOS_INCL_PKT_CNT) {

debugMsg((int) txData.NumTxPackets);

}

debugMsg(F(“\n”));

}

/*****************************************************************************

* joined network indication

****************************************************************************/

void onJoinedNwk(TWiMODLR_HCIMessage& rxMsg) {

TWiMODLORAWAN_RX_JoinedNwkData joinedData;

debugMsg(F(“Join-Indication received.\n”));

if (wimod.convert(rxMsg, &joinedData)) {

if ((LORAWAN_JOIN_NWK_IND_FORMAT_STATUS_JOIN_OK == joinedData.StatusFormat)

|| (LORAWAN_JOIN_NWK_IND_FORMAT_STATUS_JOIN_OK_CH_INFO == joinedData.StatusFormat)){

//Ok device is now joined to nwk (server)

RIB.ModemState = ModemState_Connected;

debugMsg(F(“Device has joined a network.\n”));

debugMsg(F("New Device address is: "));

debugMsg((int) joinedData.DeviceAddress);

debugMsg(F(“\n”));

} else {

// error joining procedure did not succeed

RIB.ModemState = ModemState_FailedToConnect;

debugMsg(F(“Failed to join a network.\n”));

}

}

}

/*****************************************************************************

* rx data callback

****************************************************************************/

void onRxData(TWiMODLR_HCIMessage& rxMsg) {

TWiMODLORAWAN_RX_Data radioRxMsg;

int i;

debugMsg(“Rx-Data Indication received.\n”);

// convert/copy the raw message to RX radio buffer

if (wimod.convert(rxMsg, &radioRxMsg)) {

if (radioRxMsg.StatusFormat & LORAWAN_FORMAT_ACK_RECEIVED) {

// yes, this is an ack

debugMsg(F(“Ack-Packet received.”));

}

// print out the received message as hex string

if (radioRxMsg.Length > 0) {

// print out the length

[ debugMsg(F("Rx-Message: "));

debugMsg(radioRxMsg.Length);

[ debugMsg(F("]: "));](Profile - shramik_salgaonkar - myDevices Cayenne Community)

// print out the payload

for (i = 0; i < radioRxMsg.Length; i++) {

debugMsgHex(radioRxMsg.Payload[i]);

debugMsg(F(" "));

}

debugMsg(F(“\n”));

} else {

// no payload included

// debugMsg(F("Rx-Message with no Payload received; Status: "));

// debugMsg((int) radioRxMsg.StatusFormat);

debugMsg(F(“\n”));

}

}

}

/*****************************************************************************

* print out a welcome message

****************************************************************************/

void printStartMsg()

{

debugMsg(F(“==================================================\n”));

debugMsg(F("This is FileName: "));

debugMsg(F(FILE));

debugMsg(F(“\r\n”));

debugMsg(F(“Starting…\n”));

debugMsg(F("This simple demo will try to "));

debugMsg(F("do the OTAA procedure and "));

debugMsg(F(“send a demo message each 30 sec.\n”));

debugMsg(F(“==================================================\n”));

}

/*****************************************************************************

* Arduino setup function

****************************************************************************/

void setup()

{

// wait for the PC interface to be ready (max 10 sec); usefull for USB

while (!PC_IF && millis() < 10000 ){}

// init / setup the serial interface connected to WiMOD

WIMOD_IF.begin(WIMOD_LORAWAN_SERIAL_BAUDRATE);

// init the communication stack

wimod.begin();

// debug interface

PC_IF.begin(115200);

printStartMsg();

// do a software reset of the WiMOD

delay(100);

wimod.Reset();

delay(100);

// deactivate device in order to get a clean start for this demo

wimod.DeactivateDevice();

// do a simple ping to check the local serial connection

debugMsg(F("Ping WiMOD: "));

if (wimod.Ping() != true) {

debugMsg(F(“FAILED\n”));

} else {

debugMsg(F(“OK\n”));

// try to register the device at network server via OTAA procedure

debugMsg(F(“Starting join OTAA procedure…\n”));

TWiMODLORAWAN_JoinParams joinParams;

//setup OTAA parameters

memcpy(joinParams.AppEUI, APPEUI, 8);

memcpy(joinParams.AppKey, APPKEY, 16);

// transfer parameters to WiMOD

wimod.SetJoinParameter(joinParams);

// Register callbacks for join related events

wimod.RegisterJoinedNwkIndicationClient(onJoinedNwk);

wimod.RegisterJoinTxIndicationClient(onJoinTx);

// send join request

if (wimod.JoinNetwork()) {

RIB.ModemState = ModemState_ConnectRequestSent;

debugMsg(F(“…waiting for nwk response…\n”));

} else {

debugMsg("Error sending join request: ");

debugMsg((int) wimod.GetLastResponseStatus());

debugMsg(F(“\n”));

}

}

}

/*****************************************************************************

* Arduino loop function

****************************************************************************/

void loop()

{

// check of OTAA procedure has finished

if (RIB.ModemState == ModemState_Connected) {

// send out a hello world every 30 sec ( =6* 50*100 ms)

// (due to duty cycle restrictions 30 sec is recommended

if ((loopCnt > 1) && (loopCnt % (6*50)) == 0) {

// send out a simple HelloWorld messsage

debugMsg(F(“Sending HelloWorld message…\n”));

// prepare TX data structure

txData.Port = 0x01;

txData.Length = strlen_P(PSTR(“Hello World!”));

strcpy_P((char*) txData.Payload, PSTR(“Hello World!”));

// try to send a message

if (false == wimod.SendUData(&txData)) {

// an error occurred

// check if we have got a duty cycle problem

if (LORAWAN_STATUS_CHANNEL_BLOCKED == wimod.GetLastResponseStatus()) {

// yes; it is a duty cycle violation

// → try again later

debugMsg(F(“TX failed: Blocked due to DutyCycle…\n”));

}

}

}

}

// check for any pending data of the WiMOD

wimod.Process();

delay(100);

loopCnt++;

}

Thank you very much for yuour support once more!Best

_salgaonkar Community Manager
March 24

your code looks good. I am not sure what to do mean by slow, where you still having Serial.println() code reporting which should have been SerialMon.println()

if you have custom board then select cayenneLPP in the device list and the device should send data in cayenne LPP format. the above code is not using cayenne LPP payload format.

Hi Shramik,

Is there an example on how to deploy LPP in my sensor’s and LoRa transceiver board?

Best

Spyros

image002.jpg

you can try this arduino library for cayenneLPP GitHub - ElectronicCats/CayenneLPP: Library for Arduino compatible with Cayenne Low Power Payload

Shramik,

Thank you I did have a go previously and could not locate the lorawan.h library to donload. Is there a way around this?

image002.jpg

why you need lorawan.h?

It is included in the following example script

/**

  • Example of ABP device

  • Authors:

  • Ivan Moreno

  • Eduardo Contreras

  • June 2019

image002.jpg

can you share a link to the example

/**

  • Example of ABP device

  • Authors:

  • Ivan Moreno

  • Eduardo Contreras

  • June 2019

image002.jpg

what are you sharing? there is nothing in the post,

Dear Shramik,

I had a close look at other confs found in Github and would like to update that I am using OTAA registration. Does this effect the LPP config?

I also had a look at the community posts and did not find any posts/examples on hot to setup LPP.

If you do have the chance to look at my code the next following days, I would be most grateful!

Best

Spyros

image002.jpg

you can me the code which is sending data in cayenne LPP format based on the above library i shared above.

/**

  • Example of ABP device

  • Authors:

  • Ivan Moreno

  • Eduardo Contreras

  • June 2019

LPP.txt (1.68 KB)

i dont see any cayenneLPP code in it.

Hi,

I used the code found here:

image002.jpg

oh, my bad, the library is recently updated. can you try adding this version Release 1.0.4 · ElectronicCats/CayenneLPP · GitHub and have a look at the examples inside.

Ηi,

Do yu surest altering and using the TTN example?

image002.jpg