Send Sensor Signal

Very new at all this stuff and I’ve been trying to learn over the last couple weeks; and while I have come far; I am in need of your help…

How do I actually send my sensor data?

I have a Wireless Stick v3 with a sound sensor hooked up.
sound_analog = A4
sound_digital = 47

Basically when sound_digital goes to 1, I’d like the stick to wake up and send “Sound Sensor” or something…

I keep reading different things, but since I’m learning, I’m getting lost… Any input would help greatly! Sorry there is a bunch of extra stuff in this code, I’ve been trying all kinds of crazy things.

indent preformatted text by 4 spaces

#include <LoRaWan_APP.h>

#define WIRELESS_STICK_V3

#define INT_PIN 0

//______________Sound Sensor

int led = 46;

int sound_analog = A4;

int sound_digital = 47;

/* OTAA para*/

uint8_t devEui[] = { 0x0b, 0xb8, 0xd4, 0xa7, 0x24, 0x65, 0x64, 0x23 };

uint8_t appEui[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };

uint8_t appKey[] = { 0x16, 0xfb, 0xb0, 0x5b, 0xbb, 0x87, 0xdb, 0x86, 0x4a, 0x7c, 0x74, 0xf4, 0xed, 0xdf, 0x6b, 0xf5 };

/* ABP para*/

uint8_t nwkSKey[] = { 0x15, 0xb1, 0xd0, 0xef, 0xa4, 0x63, 0xdf, 0xbe, 0x3d, 0x11, 0x18, 0x1e, 0x1e, 0xc7, 0xda,0x85 };

uint8_t appSKey[] = { 0xd7, 0x2c, 0x78, 0x75, 0x8c, 0xdc, 0xca, 0xbf, 0x55, 0xee, 0x4a, 0x77, 0x8d, 0x16, 0xef,0x67 };

uint32_t devAddr = ( uint32_t )0x002e6ae1;

/LoraWan channelsmask, default channels 0-7/

uint16_t userChannelsMask[6]={ 0x00FF,0x0000,0x0000,0x0000,0x0000,0x0000 };

/LoraWan region, select in arduino IDE tools/

LoRaMacRegion_t loraWanRegion = ACTIVE_REGION;

/LoraWan Class, Class A and Class C are supported/

DeviceClass_t loraWanClass = CLASS_A;

/the application data transmission duty cycle. value in [ms]./

uint32_t appTxDutyCycle = 300000; //was 3600241000 set to 5min for testing

/OTAA or ABP/

bool overTheAirActivation = true;

/ADR enable/

bool loraWanAdr = true;

/* Indicates if the node is sending confirmed or unconfirmed messages */

bool isTxConfirmed = true;

/* Application port */

uint8_t appPort = 2;

/*!

  • Number of trials to transmit the frame, if the LoRaMAC layer did not

  • receive an acknowledgment. The MAC performs a datarate adaptation,

  • according to the LoRaWAN Specification V1.0.2, chapter 18.4, according

  • to the following table:

  • Transmission nb | Data Rate

  • ----------------|-----------

  • 1 (first) | DR

  • 2 | DR

  • 3 | max(DR-1,0)

  • 4 | max(DR-1,0)

  • 5 | max(DR-2,0)

  • 6 | max(DR-2,0)

  • 7 | max(DR-3,0)

  • 8 | max(DR-3,0)

  • Note, that if NbTrials is set to 1 or 2, the MAC will not decrease

  • the datarate, in case the LoRaMAC layer did not receive an acknowledgment

*/

uint8_t confirmedNbTrials = 8;

static void prepareTxFrame( uint8_t port )

{

appDataSize = 4;

appData[0] = 0x00;

appData[1] = 0x01;

appData[2] = 0x02;

appData[3] = 0x03;

}

RTC_DATA_ATTR bool firstrun = true;

//__________Sensor

void sensor() {

pinMode(led, OUTPUT);

pinMode(sound_digital, INPUT);

}

//________DATA???

void downLinkAckHandle(McpsIndication_t *mcpsIndication)

{

printf("\n\tdownLinkAckHandle: ACK received\n");

printf("\n\t+REV DATA:%s,\n\tRXSIZE %d,\n\tPORT %d, RSSI: %d,\n\t SNR: %d,\n\t DATA_RATE:%d,\r\n",

        mcpsIndication->RxSlot ? "RXWIN2" : "RXWIN1", mcpsIndication->BufferSize, mcpsIndication->Port,

        mcpsIndication->Rssi, (int)mcpsIndication->Snr, (int)mcpsIndication->RxDoneDatarate);

}

void keyDown()

{

delay(10);

if(digitalRead(INT_PIN)==0 && IsLoRaMacNetworkJoined)

{

deviceState = DEVICE_STATE_SEND;

}

}

void setup() {

Serial.begin(115200);

Mcu.begin();

if(loraWanClass==CLASS_C)

{

pinMode(INT_PIN,INPUT);

attachInterrupt(INT_PIN,keyDown,FALLING);

}

if(firstrun)

{

LoRaWAN.displayMcuInit();

firstrun = false;

}

deviceState = DEVICE_STATE_INIT;

}

// The loop function is called in an endless loop

void loop(){

//_____ Reading digital and analog values

int val_digital = digitalRead(sound_digital);

int val_analog = analogRead(sound_analog);

Serial.print("Digital Sound => ");

Serial.println(val_digital);

if (val_digital !=0)

{

digitalWrite (led, HIGH);

delay(500);

}

else

{

digitalWrite (led, LOW);

delay(500);

}

//_____ Check if the INT_PIN is HIGH and the device is joined to a LoRaWAN network

if (digitalRead(sound_digital) != 0 ) {

  digitalWrite(led, HIGH); // Turn on the LED to indicate activity

  Serial.print("Digital Signal: ");

  Serial.print(sound_digital);

//_____ Schedule next packet transmission with a randomized duty cycle

txDutyCycleTime = appTxDutyCycle + randr(-APP_TX_DUTYCYCLE_RND, APP_TX_DUTYCYCLE_RND);

LoRaWAN.cycle(txDutyCycleTime);



//_____ Check if the device is in a sleep state

if (deviceState == DEVICE_STATE_SLEEP) {

//_____ Enable wake-up by external interrupt

  esp_sleep_enable_ext0_wakeup((gpio_num_t)INT_PIN, 0);

    }

     

  prepareTxFrame(appPort);

  LoRaWAN.send();

  deviceState = DEVICE_STATE_CYCLE;

}

switch( deviceState )

{

case DEVICE_STATE_INIT:

{

#if(LORAWAN_DEVEUI_AUTO)

  LoRaWAN.generateDeveuiByChipID();

#endif

  LoRaWAN.init(loraWanClass,loraWanRegion);

  break;

}

case DEVICE_STATE_JOIN:

{

  LoRaWAN.join();

  break;

}

case DEVICE_STATE_SEND:

{

  prepareTxFrame( appPort );

  LoRaWAN.send();

  deviceState = DEVICE_STATE_CYCLE;

  break;

}

case DEVICE_STATE_CYCLE:

{

  // Schedule next packet transmission

  txDutyCycleTime = appTxDutyCycle + randr( -APP_TX_DUTYCYCLE_RND, APP_TX_DUTYCYCLE_RND );

  LoRaWAN.cycle(txDutyCycleTime);

  deviceState = DEVICE_STATE_SLEEP;

  break;

}

case DEVICE_STATE_SLEEP:

{

  if(loraWanClass==CLASS_A)

  {

#ifdef WIRELESS_MINI_SHELL

    esp_deep_sleep_enable_gpio_wakeup(1<<INT_PIN,ESP_GPIO_WAKEUP_GPIO_LOW);

#else

    esp_sleep_enable_ext0_wakeup((gpio_num_t)INT_PIN,0);

#endif

  }

  LoRaWAN.sleep(loraWanClass);

  break;

}

default:

{

  deviceState = DEVICE_STATE_INIT;

  break;

}

}

}