這是幫同學做的畢業設計,但后來他說讓我用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();
- }
復制代碼 |