npk_test/src/main.cpp

139 lines
2.7 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include <Arduino.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Wire.h>
#include <SPI.h>
#include <LoRa.h>
#include "main.h"
#define RE 8
#define DE 7
// const byte code[]= {0x01, 0x03, 0x00, 0x1e, 0x00, 0x03, 0x65, 0xCD};
const byte nitro[] = {0x01, 0x03, 0x00, 0x1e, 0x00, 0x01, 0xe4, 0x0c};
const byte phos[] = {0x01, 0x03, 0x00, 0x1f, 0x00, 0x01, 0xb5, 0xcc};
const byte pota[] = {0x01, 0x03, 0x00, 0x20, 0x00, 0x01, 0x85, 0xc0};
byte values[11];
void setup()
{
Serial.begin(9600);
while (!Serial)
;
Serial.println("LoRa Gönderen");
Serial.println();
if (!LoRa.begin(433E6))
{
Serial.println("LoRa'yı başlatamadım");
while (1)
;
}
LoRa.setTxPower(20);
Serial.begin(9600);
Serial2.begin(9600);
pinMode(RE, OUTPUT);
pinMode(DE, OUTPUT);
}
void loop()
{
byte val1, val2, val3;
val1 = nitrogen();
delay(250);
val2 = phosphorous();
delay(250);
val3 = potassium();
delay(250);
Serial.print("Nitrogen: ");
Serial.print(val1);
Serial.println(" mg/kg");
Serial.print("Phosphorous: ");
Serial.print(val2);
Serial.println(" mg/kg");
Serial.print("Potassium: ");
Serial.print(val3);
Serial.println(" mg/kg");
delay(2000);
LoRa.beginPacket();
LoRa.println("LoRa Alıcı");
LoRa.println();
LoRa.print("Nitrogen ");
LoRa.print(val1);
LoRa.println(" mg/kg");
LoRa.print("Phosphorous ");
LoRa.print(val2);
LoRa.println(" mg/kg");
LoRa.print("Potassium ");
LoRa.print(val3);
LoRa.println(" mg/kg");
LoRa.endPacket();
delay(1000);
}
byte nitrogen()
{
digitalWrite(DE, HIGH);
digitalWrite(RE, HIGH);
delay(10);
if (Serial2.write(nitro, sizeof(nitro)) == 8)
{
digitalWrite(DE, LOW);
digitalWrite(RE, LOW);
for (byte i = 0; i < 7; i++)
{
// Serial.print(Serial2.read(),HEX);
values[i] = Serial2.read();
// Serial.print(values[i],HEX);
}
Serial.println();
}
return values[4];
}
byte phosphorous()
{
digitalWrite(DE, HIGH);
digitalWrite(RE, HIGH);
delay(10);
if (Serial2.write(phos, sizeof(phos)) == 8)
{
digitalWrite(DE, LOW);
digitalWrite(RE, LOW);
for (byte i = 0; i < 7; i++)
{
// Serial.print(Serial2.read(),HEX);
values[i] = Serial2.read();
// Serial.print(values[i],HEX);
}
Serial.println();
}
return values[4];
}
byte potassium()
{
digitalWrite(DE, HIGH);
digitalWrite(RE, HIGH);
delay(10);
if (Serial2.write(pota, sizeof(pota)) == 8)
{
digitalWrite(DE, LOW);
digitalWrite(RE, LOW);
for (byte i = 0; i < 7; i++)
{
// Serial.print(Serial2.read(),HEX);
values[i] = Serial2.read();
// Serial.print(values[i],HEX);
}
Serial.println();
}
return values[4];
}