With arduino intermediary between sensor and heltec.
#include <SoftwareSerial.h> //add another serial port
#include <Adafruit_SleepyDog.h> //sleep library
SoftwareSerial mySerial(2, 1); //rx pin WHITE WIRE, tx pin
//YELLOW CONTROL WIRE TO GND OR DATA READS AS ZERO
#define SENSOR_POWER_PIN 3
bool sensorToggle = false;
int loopcounter;
int16_t distance; // The last measured distance
bool newData = false; // Whether new data is available from the sensor
uint8_t buffer[4]; // our buffer for storing data
uint8_t idx = 0; // our idx into the storage buffer
void setup() {
// Open serial communications and wait for port to open:
// To monitor: USB CDC On Boot: “Enabled” (under Tools)
Serial.begin(9600);
mySerial.begin(9600);
pinMode(SENSOR_POWER_PIN, OUTPUT);
digitalWrite(SENSOR_POWER_PIN, LOW);
}
void loop() {
//delay(10); //sensor stabilize
// Serial.print("Main loop number: ");
// loopcounter ++;
// Serial.println(loopcounter);
if (sensorToggle == false) {
digitalWrite(SENSOR_POWER_PIN, HIGH); //sensor power supply on
// mySerial.begin(9600); //restart softwareSerial
sensorToggle = true;
// Serial.println("sensor on");
//Serial.println("delay");
}
for (int i = 1; i <= 4; i++) {
delay(5);
// Serial.print("ultrasonic read loop ");
// Serial.println(i);
//ultrasound
mySerial.begin(9600); //restart softwareSerial
if (mySerial.available()) {
uint8_t c = mySerial.read();
//Serial.println(c, HEX);
// See if this is a header byte
if (idx == 0 && c == 0xFF) {
buffer[idx++] = c;
}
// Two middle bytes can be anything
else if ((idx == 1) || (idx == 2)) {
buffer[idx++] = c;
}
else if (idx == 3) {
uint8_t sum = 0;
sum = buffer[0] + buffer[1] + buffer[2];
if (sum == c) {
distance = ((uint16_t)buffer[1] << 8) | buffer[2];
newData = true;
}
idx = 0;
}
}
//write out new data
if (newData) {
Serial.println(distance);
newData = false;
loopcounter = 0;
mySerial.end(); //(maybe not needed) turn off software serial to clear buffer
digitalWrite(SENSOR_POWER_PIN, LOW); //turn off sensor power supply
sensorToggle = false;
// Serial.println("sensor off");
}
}//end for
sleep();
sleep();
//Serial.println("end main loop");
}
void sleep(){
//Serial.println("delay here should sleep for several seconds");
delay(100); //minimum 80ms or serial corrupts
//delay here after serial print causes corrupt serial
int sleepMS = Watchdog.sleep(); //should sleep for like 8 seconds max, despite args
}
#include <SoftwareSerial.h> //add another serial port
#include <Adafruit_SleepyDog.h> //sleep library
SoftwareSerial mySerial(2, 1); //rx pin WHITE WIRE, tx pin
//YELLOW CONTROL WIRE TO GND OR DATA READS AS ZERO
void setDefaults(); //maybe fixes not declared in scope error
#define SENSOR_POWER_PIN 3
//int loopcounter;
int16_t distance; // The last measured distance
bool newData = false; // Whether new data is available from the sensor
uint8_t buffer[4]; // our buffer for storing data
uint8_t idx = 0; // our idx into the storage buffer
void setup() {
// Open serial communications and wait for port to open:
// To monitor: USB CDC On Boot: “Enabled” (under Tools)
Serial.begin(9600);
mySerial.begin(9600);
pinMode(SENSOR_POWER_PIN, OUTPUT);
digitalWrite(SENSOR_POWER_PIN, LOW);
delay(3000);//let heltec get settled
}
void loop() {
//first look for data
readSensor();
//send out new data only. Don't contaminate datalogger with zeros if no data
sendData();
//sleep no mater what and turn off sensor
sleep();//for about 8 seconds by default on atmega328
sleep();//sleep some more
}
void readSensor(){
digitalWrite(SENSOR_POWER_PIN, HIGH); //sensor power supply on
for (int i = 1; i <= 4; i++) {
delay(5);
// Serial.print("ultrasonic read loop ");
// Serial.println(i);
//ultrasound
mySerial.begin(9600); //restart softwareSerial
if (mySerial.available())
{
uint8_t c = mySerial.read();
//Serial.println(c, HEX);
// See if this is a header byte
if (idx == 0 && c == 0xFF) {
buffer[idx++] = c;
}
// Two middle bytes can be anything
else if ((idx == 1) || (idx == 2)) {
buffer[idx++] = c;
}
else if (idx == 3) {
uint8_t sum = 0;
sum = buffer[0] + buffer[1] + buffer[2];
if (sum == c) {
distance = ((uint16_t)buffer[1] << 8) | buffer[2];
newData = true;
}
idx = 0;
}
}
}
}
void sendData(){
if (newData) {
Serial.println(distance);
newData = false;
mySerial.end(); //(maybe not needed) turn off software serial to clear buffer
}
}
void sleep(){
digitalWrite(SENSOR_POWER_PIN, LOW); //turn off sensor power supply
//Serial.println("sleep");
delay(100); //minimum 80ms or serial corrupts
int sleepMS = Watchdog.sleep(); //should sleep for like 8 seconds max, despite args
}
#include <HardwareSerial.h> // Reference the ESP32 built-in serial port library
#include <Wire.h>
#include "HT_SSD1306Wire.h"
//Lora
#include "LoRaWan_APP.h"
#include "Arduino.h"
#define RF_FREQUENCY 915000000 // Hz
#define TX_OUTPUT_POWER 5 // dBm
#define LORA_BANDWIDTH 0 // [0: 125 kHz,
// 1: 250 kHz,
// 2: 500 kHz,
// 3: Reserved]
#define LORA_SPREADING_FACTOR 7 // [SF7..SF12]
#define LORA_CODINGRATE 1 // [1: 4/5,
// 2: 4/6,
// 3: 4/7,
// 4: 4/8]
#define LORA_PREAMBLE_LENGTH 8 // Same for Tx and Rx
#define LORA_SYMBOL_TIMEOUT 0 // Symbols
#define LORA_FIX_LENGTH_PAYLOAD_ON false
#define LORA_IQ_INVERSION_ON false
#define RX_TIMEOUT_VALUE 1000
#define BUFFER_SIZE 30 // Define the payload size here
char txpacket[BUFFER_SIZE];
char rxpacket[BUFFER_SIZE];
double txNumber;
bool lora_idle=true;
static RadioEvents_t RadioEvents;
void OnTxDone( void );
void OnTxTimeout( void );
//Display
static SSD1306Wire display(0x3c, 500000, SDA_OLED, SCL_OLED, GEOMETRY_128_64, RST_OLED); // addr , freq , i2c group , resolution , rst
HardwareSerial ultrasoundSerial(1); // Using serial port 1
#define RXD2 46 // This is the WHITE wire
#define TXD2 45 // This is the YELLOW wire, can be any data line. Actually, just tie yellow to ground.
#define CONTROL_PIN 45 // This is the YELLOW wire, can be any data line Actually, just tie yellow to ground.
int16_t distance; // The last measured distance
bool newData = false; // Whether new data is available from the sensor
uint8_t buffer[4]; // our buffer for storing data
uint8_t idx = 0; // our idx into the storage buffer
void setup() {
// Open serial communications and wait for port to open:
// To monitor: USB CDC On Boot: “Enabled” (under Tools)
Serial.begin(115200);
Mcu.begin(HELTEC_BOARD,SLOW_CLK_TPYE);//lora?
//more lora
txNumber=0;
RadioEvents.TxDone = OnTxDone;
RadioEvents.TxTimeout = OnTxTimeout;
Radio.Init( &RadioEvents );
Radio.SetChannel( RF_FREQUENCY );
Radio.SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH,
LORA_SPREADING_FACTOR, LORA_CODINGRATE,
LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON,
true, 0, 0, LORA_IQ_INVERSION_ON, 3000 );
// while (!Serial) {
// delay(10); // wait for serial port to connect. Needed for native USB port only
// }
// set the data rate for the Serial port, 9600 for the sensor
ultrasoundSerial.begin(9600, SERIAL_8N1, RXD2, TXD2); // Initializing serial port
pinMode(CONTROL_PIN, OUTPUT);
digitalWrite(CONTROL_PIN, HIGH);
// Initialising the UI will init the display too.
VextON(); // Turn on external voltage
delay(100); // Wait for display power stabilization
display.init();
display.setContrast(255);
display.setFont(ArialMT_Plain_16);
}
void VextON(void)
{
pinMode(Vext,OUTPUT);
digitalWrite(Vext, LOW);
}
void loop() {
//ultrasound
if (ultrasoundSerial.available()) {
uint8_t c = ultrasoundSerial.read();
//Serial.println(c, HEX);
// See if this is a header byte
if (idx == 0 && c == 0xFF) {
buffer[idx++] = c;
}
// Two middle bytes can be anything
else if ((idx == 1) || (idx == 2)) {
buffer[idx++] = c;
}
else if (idx == 3) {
uint8_t sum = 0;
sum = buffer[0] + buffer[1] + buffer[2];
if (sum == c) {
distance = ((uint16_t)buffer[1] << 8) | buffer[2];
newData = true;
}
idx = 0;
}
}
//Screen
if (newData) {
// Serial.print("Distance: ");
// Serial.print(distance);
// Serial.println(" mm");
Serial.println(distance);
display.clear();
display.setTextAlignment(TEXT_ALIGN_LEFT);
char distancestring[50];
sprintf(distancestring, "Dist: %d mm\n", distance);
// display.drawString(0, 0, "Dist: ");
display.drawString(0, 0, distancestring );
display.display();
newData = false;
}
//LoRa
if(lora_idle == true)
{
Serial.println("LoRa active");
delay(1000);
txNumber += 0.01;
sprintf(txpacket, "%d",distance); //start a package
// sprintf(txpacket,"Distance: %d",distance); //start a package
Serial.printf("\r\nsending packet \"%s\" , length %d\r\n",txpacket, strlen(txpacket));
Serial.println(txpacket);
Radio.Send( (uint8_t *)txpacket, strlen(txpacket) ); //send the package out
//Radio.Send(txpacket); //send the package out //must have two parameters
lora_idle = false;
}
Radio.IrqProcess( );
/*
uint8_t buf[9] = {0}; // An array that holds data
if (ultrasoundSerial.available() > 0) {
ultrasoundSerial.readBytes(buf, 9); // Read 9 bytes of data
if( buf[0] == 0x59 && buf[1] == 0x59)
{
uint16_t distance = buf[2] + buf[3] * 256;
uint16_t strength = buf[4] + buf[5] * 256;
int16_t temperature = buf[6] + buf[7] * 256;
Serial.print("Distance: ");
Serial.print(distance);
Serial.print(" cm, strength: ");
Serial.print(strength);
Serial.print(", temperature: ");
Serial.println(temperature / 8.0 - 256.0);
display.clear();
display.setTextAlignment(TEXT_ALIGN_LEFT);
char distancestring[50];
char strengthstring[50];
char temperaturestring[50];
float corrtemperature = temperature / 8.0 - 256.0;
sprintf(distancestring, "Dist: %d cm\n", distance);
sprintf(strengthstring, "Strength: %d \n", strength);
sprintf(temperaturestring, "Temp: %2.1f degC\n", corrtemperature);
// display.drawString(0, 0, "Dist: ");
display.drawString(0, 0, distancestring );
display.drawString(0, 20, strengthstring );
display.drawString(0, 40, temperaturestring );
display.display();
}
}
delay(50);
*/
}
//LoRa
void OnTxDone( void )
{
Serial.println("TX done......");
lora_idle = true;
}
void OnTxTimeout( void )
{
Radio.Sleep( );
Serial.println("TX Timeout......");
lora_idle = true;
}