0

我正在完成一个学校项目,我正处于让我的设备 100% 正常工作的最后一步。有关该项目的一些信息。我正在开发一种无线速度计,它可以跟踪 rpm 并通过霍尔效应传感器将其转换为 MPH。除了通过 Arduino 的 LoRa 库之外,我还使用 Heltec LoRa esp32 的模块。我遇到的问题是我正在尝试使用接收到的数据包并使用发出的传感器值用于 if 语句来激活振动电机以指示它们是否达到某个最高速度。我的代码如下,任何关于如何正确使用数据包的输入将不胜感激。谢谢你。接收者:

//lora stuff
#include <SPI.h>
#include <LoRa.h>
#include "SSD1306.h"

SSD1306 display(0x3c, 4, 15);

#define SS      18
#define RST     14
#define DI0     26
#define BAND    433E6

//neopixel
#include <Adafruit_NeoPixel.h>
#define LED_PIN 32
#define LED_COUNT 1

Adafruit_NeoPixel strip = Adafruit_NeoPixel(1, 32, NEO_GRB + NEO_KHZ800);
const int buttonPin = 17;// button
const int neo = 32; //
const int neoCount = 1; //number of pixels
int counter = 0; //button state counter
int buttonState = 0;

//Vibration motor
int motorPin = 25;

//level of speed in mph to trigger motor
int beginner = 10;
int intermediate = 15;
int expert = 25;
int mphVal = 0;

String data;
unsigned int totalMPH;
void setup() {
  //lora
  pinMode(16, OUTPUT);
  digitalWrite(16, LOW);    // set GPIO16 low to reset OLED
  delay(50);
  digitalWrite(16, HIGH);
  display.init();
  display.flipScreenVertically();
  display.setFont(ArialMT_Plain_10);
  display.setTextAlignment(TEXT_ALIGN_LEFT);

  // button
  pinMode(buttonPin, INPUT);

  //motor
  pinMode(motorPin, OUTPUT);

  //neopixel
  strip.begin();
  strip.show();
  strip.setBrightness(50);
  Serial.begin(115200);

  //lora initial
  while (!Serial); //if just the the basic function, must connect to a computer
  delay(100);
  Serial.println("Speed Receiver");
  display.drawString(5, 5, "Speed Receiver");
  display.display();
  SPI.begin(5, 19, 27, 18);
  LoRa.setPins(SS, RST, DI0);

  if (!LoRa.begin(BAND)) {
    display.drawString(5, 25, "Starting LoRa failed!");
    while (1);
  }
  Serial.println("LoRa Initial OK!");
  display.drawString(5, 25, "LoRa Initializing OK!");
  display.display();

}

void loop() {
  // try to parse packet
  int packetSize = LoRa.parsePacket();
  if (packetSize) {
    // received a packets
    //Serial.print("Received packet. ");
    display.clear();
    display.setFont(ArialMT_Plain_16);
    display.drawString(3, 0, "Received Speed ");
    display.display();
    // read packet
    while (LoRa.available()) {
      String data = LoRa.readString();
      Serial.print(data);
      display.drawString(20, 22, data);
      display.display();
      // if speed reaches x then motor will buzz
      if (data.equals("MPH: 12.5")) {
        digitalWrite(motorPin, HIGH);
        delay(10);
        digitalWrite(motorPin, LOW);
        delay(10);
      }

    }
    // print RSSI of packet
    //Serial.print(" with RSSI ");
    //Serial.println(LoRa.packetRssi());
    //    display.drawString(20, 45, "RSSI:  ");
    //    display.drawString(70, 45, (String)LoRa.packetRssi());
    //    display.display();


  }

  //taking the packet and turning it into an int from string
  //String data = data.toInt();
  //int data = totalMPH;
  //float totalMPH = data.toFloat();
  //   char data;
  //   float totalMPH;

  //totalMPH = atof(data);
  //Serial.println(totalMPH);

  //button
  buttonState = digitalRead(buttonPin);

  if (buttonState == HIGH) {
    counter++;

    delay(150);
  }

  //button states with three colors
  //beginner
  else if (counter == 0) {
    //Serial.println("pushed");
    strip.setPixelColor(0, 255, 0, 0);
    strip.show();
    //Serial.println(0);
    display.drawString(10, 45, "REC: 10 MPH");
    display.display();
    //vibration motor will trigger if the speed recieved is less then or equal to 12mph
//    digitalWrite(motorPin, HIGH);
//    delay(4000);
//    digitalWrite(motorPin, LOW);
//    delay(120000);

  }
  //intermediate
  else if (counter == 1) {
    strip.setPixelColor(0, 0, 255, 0);
    strip.show();
    Serial.println(1);
    display.drawString(10, 45, "REC: 15 MPH");
    display.display();
    //vibration motor will trigger if the speed recieved is less then or equal to 17mph
    //      digitalWrite(motorPin, HIGH);
    //      delay(4000);
    //      digitalWrite(motorPin, LOW);
    //      delay(240000);
    //digitalWrite(motorPin, LOW);

  }
  //expert
  else if (counter == 2) {
    strip.setPixelColor(0, 0, 0, 255);
    strip.show();
    Serial.print(2);
    display.drawString(10, 45, "REC: 20 MPH");
    display.display();
    //digitalWrite(motorPin, LOW);
    
  }
  //reset settings
  else {
    counter = 0;
  }
}

发射机:

//lora
#include <SPI.h>
#include <LoRa.h>
#include "SSD1306.h"
#include <Arduino.h>
#include <Bounce2.h>

Bounce hall = Bounce();
SSD1306  display(0x3c, 4, 15);

#define SS      18
#define RST     14
#define DI0     26
#define BAND    433E6  //915E6 

int counter = 0;
int rpmTimer = 0;
int rpmInterval = 1000;

void setup() {
  //board and lcd
  pinMode(25, OUTPUT); //Send success, LED will bright 1 second
  pinMode(16, OUTPUT);
  digitalWrite(16, LOW);    // set GPIO16 low to reset OLED
  delay(50);
  digitalWrite(16, HIGH);

  // sensor and led
  hall.attach(37, INPUT);
  hall.interval(1);
  //pinMode(led, OUTPUT);
  //pinMode(hallSens, INPUT);
  Serial.begin(115200);

  //lcd
  while (!Serial); //If just the the basic function, must connect to a computer
  // Initialising the UI will init the display too.
  display.init();
  display.flipScreenVertically();
  display.setFont(ArialMT_Plain_10);
  display.setTextAlignment(TEXT_ALIGN_LEFT);
  display.drawString(5, 5, "LoRa Sender");
  display.display();
  //lora
  SPI.begin(5, 19, 27, 18);
  LoRa.setPins(SS, RST, DI0);
  Serial.println("LoRa Sender");
  if (!LoRa.begin(BAND)) {
    Serial.println("Starting LoRa failed!");
    while (1);
  }
  Serial.println("LoRa Initial OK!");
  display.drawString(5, 20, "LoRa Initializing OK!");
  display.display();
  delay(10);
}


void loop() {
 
  hall.update();

  if(hall.rose()){
    counter++;
  }
  if(millis() - rpmTimer > rpmInterval){
    float rpm = counter * 60;
    float rph = rpm * 60;
    float dia = 0.00004349;
    float cir = dia * 3.14;
    float totalMPH = rph * cir;

    Serial.println(totalMPH);

    rpmTimer = millis();
    counter = 0;

  //sending packet
  LoRa.beginPacket();
  LoRa.print("MPH: " + String(totalMPH));
  LoRa.endPacket();

  //LoRa.sleep(); //puts lora receiver to sleep probably dont need
  digitalWrite(25, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(10);                       // wait for a second
  digitalWrite(25, LOW);    // turn the LED off by making the voltage LOW
  delay(100);                       // wait for a second
  //delay(300);

  
  
  }
  
}

4

1 回答 1

0

在基于线的 API 设计方面:

// packet is the packet from the transmitter
String packet = "S12.5"
String measurement = packet.substring(0, 1); // "S"
if (measurement == "S") {
  // at this point we know that the rest of the message is the MPH
  String data = packet.substring(1, strlen(packet)-1); // "12.5"
  float mph = data.toFloat();
  if (mph >= TOP_SPEED) {
    doSomething();
  }
  else {
    doSomethingElse();
  }
}

如果您知道网络上的传感器少于 25 个,您可以使用charAt(0)measurement代替。substring()

每个测量在消息的开头都有自己char的,与该测量相关的数据将紧随其后。因此,发送者和接收者就像 API 交换中的客户端/服务器一样进行交互。它们之间有一个隐含的约定,当发射器说“S12.5”时,接收器知道这意味着 12.5MPH。这是您的系统,因此您可以按照自己喜欢的方式设计通信数据包。

于 2021-03-23T12:47:35.050 回答