找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1558|回复: 0
打印 上一主题 下一主题
收起左侧

Arduino lora code

[复制链接]
跳转到指定楼层
楼主
ID:366005 发表于 2018-7-6 02:28 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#include <SPI.h>
#include "LoRa.h"
#include <Wire.h>

LoRaClass LoRa1;


#define DOOR 8
#define TBV A0
#define TBV1 A7
#define BUTTON A6
#define ADC_ADDRESS 104
#define NUKE A3
#define LED A1

Int counter = 0;
Char getstr[255];
Int flag=0;

Int pay_load=0;
Byte address, Hi, Lo, Config;
Long ADVal;
Char float_str[20];
//LCD
Int sensorValue;
Double tbvValue;
Double tbvValue1;
Double tbvAdcValue;
Int rfm_version;
Int rfm_version1;
Unsigned long cmdpreviousMillis = 0;  
Unsigned long previousMillis = 0; // will store last time LED was updated
Unsigned long serialTimeOut=0;
Const long serialinterval=6000;
Int ledState = LOW; // ledState used to set the LED
Const long interval = 1000; // interval at which to blink (milliseconds)
Const long cmdinterval =500;
Bool doorStatus=0;

//SSD1306AsciiAvrI2c oled;

char *ftoa(char *a, double f, int precision)
{
long p[] = {0,10,100,1000,10000,100000,1000000,10000000,100000000};

char *ret = a;
long heiltal = (long)f;
itoa(heiltal, a, 10);
while (*a != '\0') a++;
*a++ = '.';
long desimal = abs((long)((f - heiltal) * p[precision]));
itoa(desimal, a, 10);
return ret;
}

double get_adc_tbv()
{   
      address =0x68;
      Wire.beginTransmission(address);
      Wire.write(0x88);   // config register %1000 1000
                         // /RDY = 1, One Conversion, 15 samples per, PGA = X1

      Wire.endTransmission();

      delay(1000);
      Wire.requestFrom((int)address, (int) 3);
      Hi = Wire.read();
      Lo = Wire.read();
      Config = Wire.read();
      Wire.endTransmission();

      ADVal = Hi;
      ADVal = ADVal * 256 + Lo;
      double value= (ADVal+423.75)/448.4;
      Serial.println();
      Serial.print("ADC Value: ");
      Serial.print(ADVal, DEC);
      Serial.print("  ");
      Serial.print(value);
      Serial.println(" Volts");
      return value;
}

void check_nuke()
{

    if(analogRead(BUTTON)>900)
        {

            digitalWrite(NUKE, HIGH);
        }
        else
            digitalWrite(NUKE, LOW);
}

void setup()
{
  delay(5000);
  pinMode(DOOR,INPUT);
  pinMode(LED, OUTPUT); // LED D4 Blink
  pinMode(TBV, INPUT); //V_IN_Sense
  pinMode(TBV1, INPUT); // V_IN_SEnse
  pinMode(NUKE,OUTPUT);
  digitalWrite(NUKE,LOW);
  pinMode(BUTTON, INPUT);// Button input of arduino
  Serial.begin(38400);

  while (!Serial);
  delay(1000);
  Serial.println("LoRa Sender");
  LoRa.setPins(10,9,2);
  if (!LoRa.begin(434E6))
  {
    Serial.println("Starting LoRa failed!");
  }
  LoRa1.setPins(6,7,4);
  delay(1000);

  if (!LoRa1.begin(434E6))
  {
    Serial.println("Starting LoRa 1 failed!");  
  }
  LoRa1.setSpreadingFactor(10);
  LoRa1.setSignalBandwidth(125E3);
  LoRa1.setPreambleLength(16);
  LoRa1.setSyncWord(0x12);
  LoRa1.setCodingRate4(5);

  LoRa.setSpreadingFactor(10);
  LoRa.setSignalBandwidth(125E3);
  LoRa.setPreambleLength(16);
  LoRa.setSyncWord(0x12);
  LoRa.setCodingRate4(5);

  Serial.print("================================================");

  delay(1000);
  LoRa.idle();
  for (int i = 0; i < 128; i++)
  {
    Serial.print("0x");
    Serial.print(i, HEX);
    Serial.print(": 0x");
    Serial.print(LoRa.readRegister(i), HEX);
    Serial.print(" ");
  }
  // put the radio into receive mode

  LoRa.receive();
  LoRa1.receive();

  delay(1000);
  Wire.begin();
  for (uint8_t add = 0X0; add < 0X80; add++) {
    Wire.requestFrom(add, (uint8_t)1);
    If (Wire.available()) {
      Serial.print("Add: ");
      Serial.println(add, HEX);
    }
  }
}


Void LED_blink()
{

  Unsigned long currentMillis = millis();
  If (currentMillis - previousMillis >= interval)
  {
    previousMillis = currentMillis;

  // if the LED is off turn it on and vice-versa:
    If (ledState == LOW) {
      ledState = HIGH;
    }
    Else {
        ledState = LOW;
    }
    // set the LED with the ledState of the variable:
    digitalWrite(LED, ledState);
  }
}

Void loop() {
  Check_nuke();
  LED_blink();
  sensorValue = analogRead(TBV); // Read Voltage
  tbvValue = (sensorValue/20.0)+0.85; //compute value
  sensorValue = analogRead(TBV1); // Read Voltage
  tbvValue1 = (sensorValue/20.0)+0.85; //compute value
  doorStatus =digitalRead(DOOR);
  tbvAdcValue= get_adc_tbv();
  Serial.print("$$$");
  Serial.print(tbvValue);
  Serial.print(",");
  Serial.print(tbvValue1);
  Serial.print(",");
  Serial.print(tbvAdcValue);
  Serial.print(",");
  Serial.print(doorStatus);
  Serial.println("$$$");

}


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表