CubeCel HTCC-AB02A with HC-SR04

Hello,
I have problem using CubeCell HTCC-AB02A and HC-SR04 sensor for sending the distance measurement to LoraWAN. The Lora transmission is done successfully, data came and could be seen in the server, but it has wrong measurement. Power still use USB (will use battery later after succeed).

If I use samples without LoraWAN, the distance measurement is correct. I use the same measurement code for both situation.

Here is the code without LoraWan (using Heltec sample for SR04).


#define trigPin GPIO5
#define echoPin GPIO6

float distance;
float pulse_width;

void setup()
{
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(115200); // Starts the serial communication
}

uint32_t out_flag_1 = 0;
uint32_t out_flag_2 = 0;
uint32_t t1;
uint32_t t2;

void time1()
{
out_flag_1 = 1;
t1 = micros();
}

void time2()
{
out_flag_2 = 1;
t2 = micros();
}

void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(10);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
attachInterrupt(echoPin,time1,RISING);

 while(1)
 {
    delayMicroseconds(1);
    if(out_flag_1)
    {
      out_flag_1 = 0;
      detachInterrupt(echoPin);
      delayMicroseconds(1);
      attachInterrupt(echoPin,time2,FALLING);
    }
    if(out_flag_2)
    {
      out_flag_2 = 0;
      detachInterrupt(echoPin);
      break;
    }
  }

pulse_width = t2 - t1;
distance=pulse_width*0.034/2;
// Prints the distance(The unit is centimeter) on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
delay(5000);

}

And here is the result from Serial :

07:08:55.113 -> Distance: 93.60
07:09:00.224 -> Distance: 93.58
07:09:05.309 -> Distance: 94.33
07:09:10.398 -> Distance: 93.57
07:09:15.491 -> Distance: 93.11
07:09:20.551 -> Distance: 93.93
07:09:25.648 -> Distance: 93.18
07:09:30.738 -> Distance: 93.13

The distance measurement is accurate enough.

For LoraWAN, I use proven sample from CubeCell, which is originally use for Cubecell sensor (I use HDC1080) and modify the code in sensor part for using HC-SR04. The cable/jumper connectivity is exactly the same with the previous, but the measurement result is wrong. Here is the code:


#include “LoRaWan_APP.h”
#include “Arduino.h”
#include <Wire.h>

/* OTAA para*/
… deleted

/* ABP para*/
… deleted

/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 = LORAWAN_CLASS;

/the application data transmission duty cycle. value in [ms]./
uint32_t appTxDutyCycle = 30000;

/OTAA or ABP/
bool overTheAirActivation = LORAWAN_NETMODE;

/ADR enable/
bool loraWanAdr = LORAWAN_ADR;

/* set LORAWAN_Net_Reserve ON, the node could save the network info to flash, when node reset not need to join again */
bool keepNet = LORAWAN_NET_RESERVE;

/* Indicates if the node is sending confirmed or unconfirmed messages */
bool isTxConfirmed = LORAWAN_UPLINKMODE;

/* 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 = 4;

#define trigPin GPIO5
#define echoPin GPIO6

float distance;
float pulse_width;
uint32_t out_flag_1 = 0;
uint32_t out_flag_2 = 0;
uint32_t t1;
uint32_t t2;

static void prepareTxFrame( uint8_t port )
{
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(10);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
attachInterrupt(echoPin,time1,RISING);

 while(1)
 {
    delayMicroseconds(1);
    if(out_flag_1)
    {
      out_flag_1 = 0;
      detachInterrupt(echoPin);
      delayMicroseconds(1);
      attachInterrupt(echoPin,time2,FALLING);
    }
    if(out_flag_2)
    {
      out_flag_2 = 0;
      detachInterrupt(echoPin);
      break;
    }
  }
pulse_width = t2 - t1;
distance=pulse_width*0.034/2;

uint16_t batteryVoltage = getBatteryVoltage();
unsigned char *puc;

puc = (unsigned char *)(&distance);
appDataSize = 6;
appData[0] = puc[0];
appData[1] = puc[1];
appData[2] = puc[2];
appData[3] = puc[3];

appData[4] = (uint8_t)(batteryVoltage>>8);
appData[5] = (uint8_t)batteryVoltage;

Serial.println("------------------------");
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm, ");
Serial.print("BatteryVoltage:");
Serial.println(batteryVoltage);
delay(5000);

}

void setup() {
// boardInitMcu();
Serial.begin(115200);
#if(AT_SUPPORT)
enableAt();
#endif
deviceState = DEVICE_STATE_INIT;
LoRaWAN.ifskipjoin();
}

void time1()
{
out_flag_1 = 1;
t1 = micros();
}

void time2()
{
out_flag_2 = 1;
t2 = micros();
}

void loop()
{
switch( deviceState )
{
case DEVICE_STATE_INIT:
{
#if(AT_SUPPORT)
getDevParam();
#endif
printDevParam();
LoRaWAN.init(loraWanClass,loraWanRegion);
deviceState = DEVICE_STATE_JOIN;
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( 0, APP_TX_DUTYCYCLE_RND );
LoRaWAN.cycle(txDutyCycleTime);
deviceState = DEVICE_STATE_SLEEP;
break;
}
case DEVICE_STATE_SLEEP:
{
LoRaWAN.sleep();
break;
}
default:
{
deviceState = DEVICE_STATE_INIT;
break;
}
}
}


I got the wrong measurement result, but the packet was successfully sent to Lorawan server


7:19:56.475 -> ------------------------
07:19:56.521 -> Distance: 73014432.00 cm,
07:19:56.521 -> BatteryVoltage:3356
07:20:01.569 -> confirmed uplink sending …
07:20:02.690 -> receive data: rssi = -43, snr = 12, datarate = 5
07:20:32.284 -> ------------------------
07:20:32.284 -> Distance: 73014432.00 cm,
07:20:32.284 -> BatteryVoltage:3356
07:20:37.392 -> confirmed uplink sending …
07:20:38.513 -> receive data: rssi = -45, snr = 12, datarate = 5
07:21:07.581 -> ------------------------
07:21:07.581 -> Distance: 73014432.00 cm,
07:21:07.581 -> BatteryVoltage:3356
07:21:12.696 -> confirmed uplink sending …
07:21:13.770 -> receive data: rssi = -40, snr = 12, datarate = 5
07:21:43.716 -> ------------------------
07:21:43.716 -> Distance: 73014432.00 cm,
07:21:43.716 -> BatteryVoltage:3356
07:21:48.768 -> confirmed uplink sending …
07:21:49.895 -> receive data: rssi = -40, snr = 11, datarate = 5

Anyone can help with right code ? Thanks in advance.

Regards,

Hi,

HC-SR04 sensor’s code is implemented through interrupts.

you should use the lorawan-interrupt: https://github.com/HelTecAutomation/ASR650x-Arduino/tree/master/libraries/LoRa/examples/LoRaWAN/LoRaWan_interrupt.

The LoRaWAN Interrupt wakes up Lora to send a message.

There must be a way to use “normal” interrupts in the code!

Greetings E_T