标题:
基于arduino的智能农业系统设计
[打印本页]
作者:
51hei电控2112311
时间:
2016-5-1 18:06
标题:
基于arduino的智能农业系统设计
这是帮同学做的毕业设计,但后来他说让我用51单片机做了,这个方案就摒弃了。
爱上arduino是去年上半年,一个偶然的机会从淘宝店里买了块arduino开发板,就使劲花时间和精力去学习。在我看来arduino开发系统是单片机嵌入式系统中最简单的,毕竟国外都是给小朋友和艺术家玩的,我们大朋友也可以用来玩玩,应付些简单的设计要求还是没有问题的。如果涉及工业生产化,这个还没有到这个境界。
现在的物联网比较火,国内最早起步的yeelink,乐为联网,还有些小的社区,贝壳物联,智能创客,当然也有大的企业在做,阿里,qq,微信,移动,机智云的,鱼龙混珠。国外的没有使用过,就不太清楚,有名的有谷歌的,亚马逊的,IBM等的。我只能说现在还处于探索阶段,鹿死谁手还未可知。
在大数据时代我们能够做什么,也许可以做一点点。以下是我的构想。
智能农业无非是在传统农业上加入入网功能,进行数据分析处理存储,很简单的一件事。说起来容易做起来难,吹逼的人大有人在,实施起来还是有些麻烦。
整体设计也就一个arduino单片机,一个时钟模块DS1302,一个温湿度传感器AM2301,一个加热ntc装置,一个电风扇作为均匀加热。花了半天时间完成。通过串口输入特定字符串可以达到控制温度,调节闹钟的功能。因为已经送人了,所以没有拍图。
#include <stdio.h>
#include <string.h>
#include <LiquidCrystal.h>
#include <DS1302.h>
#include <Wire.h>
#include <AM2321.h>
AM2321 ac;
uint8_t bell[8] = {0x4,0xe,0xe,0xe,0x1f,0x0,0x4};
uint8_t note[8] = {0x2,0x3,0x2,0xe,0x1e,0xc,0x0};
uint8_t clock[8] = {0x0,0xe,0x15,0x17,0x11,0xe,0x0};
uint8_t heart[8] = {0x0,0xa,0x1f,0x1f,0xe,0x4,0x0};
uint8_t duck[8] = {0x0,0xc,0x1d,0xf,0xf,0x6,0x0};
uint8_t check[8] = {0x0,0x1,0x3,0x16,0x1c,0x8,0x0};
uint8_t cross[8] = {0x0,0x1b,0xe,0x4,0xe,0x1b,0x0};
uint8_t retarrow[8] = { 0x1,0x1,0x5,0x9,0x1f,0x8,0x4};
LiquidCrystal lcd(A1, A0, 13, 12, 11, 10);
char buflcd[16];
long previousMillis = 0;
long interval = 1000;
const int heat_pwm= 3;
const int motor_pwm = 5;
int val_pwm;
int set_temperature=25;
int sensorPin = A3;
int sensorValue = 0;
int kp=-1,ki=0,kd=-1;
int last_p,now_p;
int last_d,now_d;
int now_i;
void pid()
{
now_p=ac.temperature-set_temperature*10;
now_d=now_p-last_p;
now_i=now_d-last_d;
last_p=now_p;
last_d=now_d;
val_pwm=val_pwm+kp*now_p+kd*now_d+ki*now_i;
if(val_pwm>255)
val_pwm=255;
else if(val_pwm<0)
val_pwm =0;
analogWrite(heat_pwm, val_pwm*4/5);
analogWrite(motor_pwm, val_pwm);
}
void AM2320_DATA()
{
if(ac.read())
{
sensorValue = analogRead(sensorPin);
snprintf(buflcd, sizeof(buflcd), "%2d.%dC %2d.%d%s %d ",
ac.temperature/10,ac.temperature%10,ac.humidity/10,ac.humidity%10,"%",sensorValue);
lcd.setCursor(0,0);
lcd.print(buflcd);
}
}
void lcd_init()
{
lcd.begin(16,2);
lcd.createChar(0, bell);
lcd.createChar(1, note);
lcd.createChar(2, clock);
lcd.createChar(3, heart);
lcd.createChar(4, duck);
lcd.createChar(5, check);
lcd.createChar(6, cross);
lcd.createChar(7, retarrow);
}
namespace {
// Set the appropriate digital I/O pin connections. These are the pin
// assignments for the Arduino as well for as the DS1302 chip. See the DS1302
// datasheet:
//
// [url=http://datasheets.maximintegrated.com/en/ds/DS1302.pdf]http://datasheets.maximintegrated.com/en/ds/DS1302.pdf[/url]
const int kCePin = 8; // Chip Enable
const int kIoPin = 7; // Input/Output
const int kSclkPin = 6; // Serial Clock
// Create a DS1302 object.
DS1302 rtc(kCePin, kIoPin, kSclkPin);
void ds1302_init()
{
rtc.writeProtect(false);
rtc.halt(false);
}
String dayAsString(const Time::Day day) {
switch (day) {
case Time::kSunday: return "Sunday";
case Time::kMonday: return "Monday";
case Time::kTuesday: return "Tuesday";
case Time::kWednesday: return "Wednesday";
case Time::kThursday: return "Thursday";
case Time::kFriday: return "Friday";
case Time::kSaturday: return "Saturday";
}
return "(unknown day)";
}
void printTime() {
// Get the current time and date from the chip.
Time t = rtc.time();
// Format the time and date and insert into the temporary buffer.
snprintf(buflcd, sizeof(buflcd), "%2d-%d-%d %d:%d:%d ",
t.yr-2000, t.mon, t.date,
t.hr, t.min, t.sec);
// Print the formatted string to serial so we can see the time.
lcd.setCursor(0,1);
lcd.print(buflcd);
}
void edit_time()
{
/* 串口数据缓存 */
String comdata = "";
int numdata[7] ={0}, j = 0, mark = 0;
while (Serial.available() > 0)
{
comdata += char(Serial.read());
delay(2);
mark = 1;
}
/* 以逗号分隔分解comdata的字符串,分解结果变成转换成数字到numdata[]数组 */
if(mark == 1)
{
Serial.print("You inputed : ");
Serial.println(comdata);
for(int i = 0; i < comdata.length() ; i++)
{
if(comdata[i] == ',' || comdata[i] == 0x10 || comdata[i] == 0x13)
{
j++;
}
else
{
numdata[j] = numdata[j] * 10 + (comdata[i] - '0');
}
}
if((numdata[0]>2000)&&(numdata[0]<2100))
{
/* 将转换好的numdata凑成时间格式,写入DS1302 */
Time t(numdata[0], numdata[1], numdata[2], numdata[3], numdata[4], numdata[5], Time::kSaturday );
rtc.time(t);
}
else
{
if((numdata[0]<60)&&(numdata[0]>10))
set_temperature=numdata[0];
}
}
}
} // namespace
void setup() {
Serial.begin(115200);
// set up the LCD's number of columns and rows:
lcd_init();
ds1302_init();
}
void loop() {
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval)
{
previousMillis = currentMillis;
printTime();
AM2320_DATA();
}
edit_time();
pid();
}
复制代码
作者:
51hei电控2112311
时间:
2016-5-1 18:08
这个是程序安装包,有2个,最新的为1.1版本的。
周义龙智能农业系统.zip
2016-5-1 18:08 上传
点击文件名下载附件
下载积分: 黑币 -5
4.36 KB, 下载次数: 33, 下载积分: 黑币 -5
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1