找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 8124|回复: 1
收起左侧

基于arduino的智能农业系统设计

[复制链接]
ID:117823 发表于 2016-5-1 18:06 | 显示全部楼层 |阅读模式
       这是帮同学做的毕业设计,但后来他说让我用51单片机做了,这个方案就摒弃了。
       爱上arduino是去年上半年,一个偶然的机会从淘宝店里买了块arduino开发板,就使劲花时间和精力去学习。在我看来arduino开发系统是单片机嵌入式系统中最简单的,毕竟国外都是给小朋友和艺术家玩的,我们大朋友也可以用来玩玩,应付些简单的设计要求还是没有问题的。如果涉及工业生产化,这个还没有到这个境界。
       现在的物联网比较火,国内最早起步的yeelink,乐为联网,还有些小的社区,贝壳物联,智能创客,当然也有大的企业在做,阿里,qq,微信,移动,机智云的,鱼龙混珠。国外的没有使用过,就不太清楚,有名的有谷歌的,亚马逊的,IBM等的。我只能说现在还处于探索阶段,鹿死谁手还未可知。
      在大数据时代我们能够做什么,也许可以做一点点。以下是我的构想。
      智能农业无非是在传统农业上加入入网功能,进行数据分析处理存储,很简单的一件事。说起来容易做起来难,吹逼的人大有人在,实施起来还是有些麻烦。
     整体设计也就一个arduino单片机,一个时钟模块DS1302,一个温湿度传感器AM2301,一个加热ntc装置,一个电风扇作为均匀加热。花了半天时间完成。通过串口输入特定字符串可以达到控制温度,调节闹钟的功能。因为已经送人了,所以没有拍图。

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <LiquidCrystal.h>
  4. #include <DS1302.h>
  5. #include <Wire.h>
  6. #include <AM2321.h>
  7. AM2321 ac;
  8. uint8_t bell[8]  = {0x4,0xe,0xe,0xe,0x1f,0x0,0x4};
  9. uint8_t note[8]  = {0x2,0x3,0x2,0xe,0x1e,0xc,0x0};
  10. uint8_t clock[8] = {0x0,0xe,0x15,0x17,0x11,0xe,0x0};
  11. uint8_t heart[8] = {0x0,0xa,0x1f,0x1f,0xe,0x4,0x0};
  12. uint8_t duck[8]  = {0x0,0xc,0x1d,0xf,0xf,0x6,0x0};
  13. uint8_t check[8] = {0x0,0x1,0x3,0x16,0x1c,0x8,0x0};
  14. uint8_t cross[8] = {0x0,0x1b,0xe,0x4,0xe,0x1b,0x0};
  15. uint8_t retarrow[8] = {    0x1,0x1,0x5,0x9,0x1f,0x8,0x4};
  16. LiquidCrystal lcd(A1, A0, 13, 12, 11, 10);
  17. char buflcd[16];
  18. long previousMillis = 0;  
  19. long interval = 1000;
  20. const int  heat_pwm= 3;
  21. const int motor_pwm = 5;
  22. int val_pwm;
  23. int set_temperature=25;
  24. int sensorPin = A3;
  25. int sensorValue = 0;
  26. int kp=-1,ki=0,kd=-1;
  27. int last_p,now_p;
  28. int last_d,now_d;
  29. int now_i;
  30. void pid()
  31. {
  32.   now_p=ac.temperature-set_temperature*10;
  33.   now_d=now_p-last_p;
  34.   now_i=now_d-last_d;
  35.   last_p=now_p;
  36.   last_d=now_d;
  37.   val_pwm=val_pwm+kp*now_p+kd*now_d+ki*now_i;
  38.   if(val_pwm>255)
  39.   val_pwm=255;
  40.   else if(val_pwm<0)
  41.   val_pwm =0;
  42.   analogWrite(heat_pwm, val_pwm*4/5);
  43.   analogWrite(motor_pwm, val_pwm);
  44. }

  45. void AM2320_DATA()
  46. {
  47. if(ac.read())
  48. {
  49.    sensorValue = analogRead(sensorPin);
  50.   snprintf(buflcd, sizeof(buflcd), "%2d.%dC %2d.%d%s %d   ",
  51.            ac.temperature/10,ac.temperature%10,ac.humidity/10,ac.humidity%10,"%",sensorValue);
  52.            
  53.    lcd.setCursor(0,0);
  54.    lcd.print(buflcd);     
  55. }
  56. }

  57. void lcd_init()
  58. {
  59.   lcd.begin(16,2);
  60.   lcd.createChar(0, bell);
  61.   lcd.createChar(1, note);
  62.   lcd.createChar(2, clock);
  63.   lcd.createChar(3, heart);
  64.   lcd.createChar(4, duck);
  65.   lcd.createChar(5, check);
  66.   lcd.createChar(6, cross);
  67.   lcd.createChar(7, retarrow);
  68. }
  69. namespace {

  70. // Set the appropriate digital I/O pin connections. These are the pin
  71. // assignments for the Arduino as well for as the DS1302 chip. See the DS1302
  72. // datasheet:
  73. //
  74. //   [url=http://datasheets.maximintegrated.com/en/ds/DS1302.pdf]http://datasheets.maximintegrated.com/en/ds/DS1302.pdf[/url]
  75. const int kCePin   = 8;  // Chip Enable
  76. const int kIoPin   = 7;  // Input/Output
  77. const int kSclkPin = 6;  // Serial Clock

  78. // Create a DS1302 object.
  79. DS1302 rtc(kCePin, kIoPin, kSclkPin);
  80. void ds1302_init()
  81. {
  82.   rtc.writeProtect(false);
  83.   rtc.halt(false);
  84. }

  85. String dayAsString(const Time::Day day) {
  86.   switch (day) {
  87.     case Time::kSunday: return "Sunday";
  88.     case Time::kMonday: return "Monday";
  89.     case Time::kTuesday: return "Tuesday";
  90.     case Time::kWednesday: return "Wednesday";
  91.     case Time::kThursday: return "Thursday";
  92.     case Time::kFriday: return "Friday";
  93.     case Time::kSaturday: return "Saturday";
  94.   }
  95.   return "(unknown day)";
  96. }

  97. void printTime() {
  98.   // Get the current time and date from the chip.
  99.   Time t = rtc.time();
  100.   // Format the time and date and insert into the temporary buffer.
  101.   snprintf(buflcd, sizeof(buflcd), "%2d-%d-%d %d:%d:%d   ",
  102.            t.yr-2000, t.mon, t.date,
  103.            t.hr, t.min, t.sec);

  104.   // Print the formatted string to serial so we can see the time.
  105.   lcd.setCursor(0,1);
  106.   lcd.print(buflcd);
  107. }

  108. void edit_time()
  109. {
  110.   /* 串口数据缓存 */
  111. String comdata = "";
  112. int numdata[7] ={0}, j = 0, mark = 0;
  113.   while (Serial.available() > 0)
  114.     {
  115.         comdata += char(Serial.read());
  116.         delay(2);
  117.         mark = 1;
  118.     }
  119.     /* 以逗号分隔分解comdata的字符串,分解结果变成转换成数字到numdata[]数组 */
  120.     if(mark == 1)
  121.     {
  122.         Serial.print("You inputed : ");
  123.         Serial.println(comdata);
  124.         for(int i = 0; i < comdata.length() ; i++)
  125.         {
  126.             if(comdata[i] == ',' || comdata[i] == 0x10 || comdata[i] == 0x13)
  127.             {
  128.                 j++;
  129.             }
  130.             else
  131.             {
  132.                 numdata[j] = numdata[j] * 10 + (comdata[i] - '0');
  133.             }
  134.         }
  135.         if((numdata[0]>2000)&&(numdata[0]<2100))
  136.         {
  137.         /* 将转换好的numdata凑成时间格式,写入DS1302 */
  138.         Time t(numdata[0], numdata[1], numdata[2], numdata[3], numdata[4], numdata[5], Time::kSaturday );
  139.         rtc.time(t);
  140.         }
  141.         else
  142.         {
  143.            if((numdata[0]<60)&&(numdata[0]>10))
  144.            set_temperature=numdata[0];
  145.         }
  146.     }
  147. }

  148. }  // namespace


  149. void setup() {
  150.    Serial.begin(115200);
  151.   // set up the LCD's number of columns and rows:
  152.   lcd_init();
  153.   ds1302_init();
  154. }

  155. void loop() {
  156. unsigned long currentMillis = millis();
  157. if(currentMillis - previousMillis > interval)
  158. {
  159.    previousMillis = currentMillis;
  160.    printTime();
  161.    AM2320_DATA();
  162.   }
  163.    edit_time();
  164.    pid();
  165. }
复制代码

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

ID:117823 发表于 2016-5-1 18:08 | 显示全部楼层
这个是程序安装包,有2个,最新的为1.1版本的。

周义龙智能农业系统.zip

4.36 KB, 下载次数: 33, 下载积分: 黑币 -5

评分

参与人数 1黑币 +100 收起 理由
admin + 100 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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