回到首页 返回首页
回到顶部 回到顶部
返回上一页 返回上一页

#创客来了#超远距离查看水位 简单

头像 gada888 2021.07.29 444 0

夏天来了,洪涝灾害也多了。虽然难以避免,但有些灾害可以早做预防。这里做了一个无线远程看水位的方案,给农村地区家有农田的人们一个参考,及时发现水患,尽早避免危害。以此向还在洪涝灾害中进行救援的人们表达一下敬意。

project-image

材料清单

  • arduino nano X2
  • max7219点阵8x8 X1
  • DF的防水电容土壤模块 X1
  • HC12千米无线模块 X2
project-image
project-image
project-image
project-image
project-image
project-image

项目的主要功能:项目分为两个arduino nano模块控制,分为发射端模块和接收端模块,发射端是有nano控制hc12无线模块和一个土壤模块。nano1采集土壤模块的模拟信号然后经过处理有hc12无线模块发射信号到另一个nano。nano2接收信号并通过8x8点阵模块来显示,如果干燥的话,点阵就显示一个光点。如果非常湿润就会显示全部的光点。

project-image
project-image
代码
//HC-12 Library
#include <SoftwareSerial.h>
SoftwareSerial HC12(10,11); //hc12 tx, hc12 rx
const int soil = A0;
int moistVal = 0;

void setup()
{
  //All Device Begin
  Serial.begin(9600);
  HC12.begin(9600);


  //Soil Moisture
  pinMode(soil, INPUT);
}

void loop()
{
moistVal = analogRead(soil);
  Serial.println(moistVal);
  HC12.println(moistVal);
  delay(1000);
  
    if (moistVal >= 0 && moistVal <= 35)
    {
      Serial.println("无水"); 
      HC12.write(45); 
      delay(1000);
    }
    else if (moistVal >= 35 && moistVal <= 45)
    {
      Serial.println("少水");
      HC12.write(46); 
      delay(1000);
    }
    else if (moistVal >= 45 && moistVal <= 55)
    {
      Serial.println("渐干");
      HC12.write(47); 
      delay(1000);
    }
    else if (moistVal >= 55 && moistVal <= 65)
    {
      Serial.println("干燥");
      HC12.write(48); 
      delay(1000);
    }
    else if (moistVal >= 65 && moistVal <= 75)
    {
      Serial.println("微干");
      HC12.write(49); 
      delay(1000);
    }
    else if (moistVal >= 75 && moistVal <= 85)
    {
      Serial.println("适宜");
      HC12.write(50);
      delay(1000);
    }
    else if (moistVal >= 85 && moistVal <= 95)
    {
      Serial.println("湿润");
      HC12.write(51);
      delay(1000);
    }
    else if (moistVal >= 95 && moistVal <= 105)
    {
      Serial.println("太湿");
      HC12.write(52);
      delay(1000);
    }
  }
代码
/*
* a 8x8 led matrix modual and a Soil Sensor.
* It was made by gada888 from Luoyang.28-06-2021
* email:gada888@hotmail.com
*/

#include <SoftwareSerial.h>
SoftwareSerial HC12(10,11); //hc12 tx, hc12 rx

int byteRead;

#include <LedControl.h>
//int Buzzer =6;
int DIN = 7;
int CS  = 8;
int CLK = 9;
 
LedControl lc = LedControl(DIN, CLK, CS, 0);
 
byte line1[8] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,};
byte line2[8] = {0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,}; 
byte line3[8] = {0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,};
byte line4[8] = {0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,};
byte line5[8] = {0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,};
byte line6[8] = {0x00,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,};
byte line7[8] = {0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,};
byte line8[8] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,};

void setup() {
    lc.shutdown(0, false);    //The MAX72XX is in power-saving mode on startup
    lc.setIntensity(0, 4);   // Set the brightness to maximum value
    lc.clearDisplay(0);       // and clear the display
    //pinMode(Buzzer, INPUT); 
    Serial.begin(9600);      // start Serial
    HC12.begin(9600);
}
 
void loop() {
  while(HC12.available()) {        // If Serial module has data
    byteRead = HC12.read();
      HC12.print(byteRead);   //data to Serial monitor
    if (byteRead==45) { //received number 1
        printByte(line8);
        Serial.println("8x8");
    }else if (byteRead==46){
        printByte(line7);
        Serial.println("7x7");
    }else if (byteRead==47){
        printByte(line6);
        Serial.println("6x6");
    }else if (byteRead==48){
        printByte(line5);
        Serial.println("5x5");
    }else if (byteRead==49){
        printByte(line4);
        Serial.println("4x4");
    }else if (byteRead==50){
        printByte(line3);
        Serial.println("3x3");
    }else if (byteRead==51){
        printByte(line2);
        Serial.println("2x2");
    }else if (byteRead==52){
        printByte(line1);
        Serial.println("1x1");
   }
  }
 }

void printByte(byte character []) {
    int i = 0;
    for(i = 0; i < 8; i++) {
        lc.setRow(0, i, character[i]);
    }
}

土壤传感部分的代码是写给老式土壤传感的,因为没找到,我记得家里很多的,最后凑合用了电容土壤传感,没想到能用的上。有时间会把代码再调整一下。

评论

user-avatar