1、開始運(yùn)行你的Arduino kit: 在51黑論壇或者網(wǎng)上找到IDE1.01或者以上版本,下載并安裝。用USB轉(zhuǎn)串口線插入arduino板時(shí),會(huì)提示需要裝驅(qū)動(dòng)。在網(wǎng)上找到arduino與之相關(guān)的USB2.0驅(qū)動(dòng),就可以正常的運(yùn)行你的arduino了。 因?yàn)镮DE含有大量arduino例程,故可以先將例程導(dǎo)入編譯頁面。如下為4個(gè)用arduino編譯的例程。 //1一個(gè)簡單的程序讓讀取模擬端口ao的值并用串口發(fā)送 void setup() { Serial.begin(9600);//串口波特率9.6kb//d0-tx//d1-rx } void loop() { int sensorValue = analogRead(A0);//讀取a0值(0-1023) Serial.println(sensorValue);//串口發(fā)送數(shù)據(jù) delay(1); //延時(shí)一毫秒 } 注:arduino由初始化函數(shù)setup()與循環(huán)函數(shù)loop()組成。
1-1.jpg (20.25 KB, 下載次數(shù): 72)
下載附件
2016-3-30 19:21 上傳
圖1 本例中所有程序均在IDE中完成編程,也可在keil,iar,或者第三方軟件中運(yùn)行。 2、串口控制輸出: 下載程序后,打開IDE的串口助手,會(huì)出現(xiàn)圖一界面,發(fā)送a-e,即可將d2-d5分別點(diǎn)亮,輸入其他數(shù)據(jù),則會(huì)關(guān)閉所有LED燈。 void setup() { Serial.begin(9600); //串口波特率9.6kb//d0-tx//d1-rx for (int thisPin = 2; thisPin < 7; thisPin++) { pinMode(thisPin, OUTPUT);//將d2-d7設(shè)置為輸出模式 } } void loop() { if (Serial.available() > 0) { //如果串口收到信息 int inByte = Serial.read(); //將串口發(fā)送的數(shù)據(jù)送入到inbyte switch (inByte) { case 'a': //接受a,將d2置高 digitalWrite(2, HIGH); break; case 'b': digitalWrite(3, HIGH); //接受b,將d3置高 break; case 'c': digitalWrite(4, HIGH); //接受c,將d4置高 break; case 'd': digitalWrite(5, HIGH); //接受d,將d5置高 break; case 'e': digitalWrite(6, HIGH); //接受e,將d6置高 break; default: // turn all the LEDs off: //其他情況關(guān)閉所有LED燈 for (int thisPin = 2; thisPin < 7; thisPin++) { digitalWrite(thisPin, LOW); //其他情況就將d2-d7均置低 } } } }
1-2.jpg (49.91 KB, 下載次數(shù): 81)
下載附件
2016-3-30 19:21 上傳
3、局域網(wǎng)控制LED燈: 在網(wǎng)站下載 庫,導(dǎo)入LEDs程序,可以得到如圖2所示的畫面。要實(shí)現(xiàn)聯(lián)網(wǎng)連接,需要用到enc28j60網(wǎng)卡。 #include #define LED1PIN 3 #define LED2PIN 5 PROGMEM prog_char led_off[]= { //灰色圖標(biāo),關(guān)閉LED矩陣 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0x00, 0x00, 0x00, 0x0D, 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x0E, 0x00, 0x00, 0x00, 0x0E, 0x08, 0x04, 0x00, 0x00, 0x00, 0xB5, 0x41, 0xE5, 0x5A, 0x00, 0x00, 0x00, 0x01, 0x73, 0x52, 0x47, 0x42, 0x00, 0xAE, 0xCE, 0x1C, 0xE9, 0x00, 0x00, 0x00, 0x02, 0x62, 0x4B, 0x47, 0x44, 0x00, 0xFF, 0x87, 0x8F, 0xCC, 0xBF, 0x00, 0x00, 0x00, 0x09, 0x70, 0x48, 0x59, 0x73, 0x00, 0x00, 0x0B, 0x12, 0x00, 0x00, 0x0B, 0x12, 0x01, 0xD2, 0xDD, 0x7E, 0xFC, 0x00, 0x00, 0x00, 0x07, 0x74, 0x49, 0x4D, 0x45, 0x07, 0xDC, 0x01, 0x1D, 0x14, 0x21, 0x10, 0xBC, 0x3C, 0xDE, 0x2D, 0x00, 0x00, 0x00, 0xD2, 0x49, 0x44, 0x41, 0x54, 0x18, 0xD3, 0x75, 0xD0, 0xB1, 0x4A, 0x03, 0x51, 0x10, 0x05, 0xD0, 0xB3, 0x21, 0x1F, 0x10, 0x04, 0x3B, 0x53, 0x89, 0x5A, 0x09, 0x9B, 0x58, 0xD9, 0x25, 0x5F, 0x60, 0x63, 0x9F, 0x87, 0x5F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 0xCC, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4E, 0x44, 0xAE, 0x42, 0x60, 0x82 };
PROGMEM prog_char led_on[]= { //黃色圖標(biāo),點(diǎn)亮LED燈矩陣 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0x00, 0x00, 0x00, 0x0D, 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x0E, 0x00, 0x00, 0x00, 0x0E, 0x04, 0x03, 0x00, 0x00, 0x00, 0xED, 0x66, 0x30, 0xE2, 0x00, 0x00, 0x00, 0x01, 0x73, 0x52, 0x47, 0x42, 0x00, 0xAE, 0xCE, 0x1C, 0xE9, 0x00, 0x00, 0x00, 0x27, 0x50, 0x4C, 0x54, 0x45, 0x00, 0x00, 0x00, 0xFF, 0xCC, 0x00, 0xCC, 0x66, 0x00, 0xFF, 0xFF, 0x33, 0xFF, 0x99, 0x00, 0xFF, 0xCC, 0x33, 0xCC, 0x99, 0x66, 0xFF, 0xFF, 0x99, 0xCC, 0x99, 0x33, 0xCC, 0x99, 0x00, 0xFF, 0xCC, 0x66, 0x99, 0x66, 0x00, 0xFF, 0xCC, 0x99, 0x4A, 0x4E, 0xC9, 0x79, 0x00, 0x00, 0x00, 0x01, 0x74, 0x52, 0x4E, 0x53, 0x00, 0x40, 0xE6, 0xD8, 0x66, 0x00, 0x00, 0x00, 0x01, 0x62, 0x4B, 0x47, 0x44, 0x00, 0x88, 0x05, 0x1D, 0x48, 0x00, 0x00, 0x00, 0x09, 0x70, 0x48, 0x59, 0x73, 0x00, 0x00, 0x0B, 0x12, 0x00, 0x00, 0x0B, 0x12, 0x01, 0xD2, 0xDD, 0x7E, 0xFC, 0x00, 0x00, 0x00, 0x07, 0x74, 0x49, 0x4D, 0x45, 0x07, 0xDC, 0x01, 0x1D, 0x14, 0x23, 0x1C, 0x87, 0xBC, 0xF0, 0x84, 0x00, 0x00, 0x00, 0x48, 0x49, 0x44, 0x41, 0x54, 0x08, 0xD7, 0x63, 0x60, 0x80, 0x03, 0x26, 0x25, 0x05, 0x30, 0x95, 0x55, 0x3E, 0x0D, 0xC4, 0xD0, 0x31, 0x16, 0x14, 0x6C, 0x02, 0xD2, 0xC9, 0x86, 0x82, 0x82, 0xD2, 0x40, 0xE1, 0xC5, 0x40, 0x3A, 0x44, 0x81, 0x81, 0xA9, 0x18, 0x46, 0x0B, 0x0A, 0x8A, 0xBA, 0x00, 0xE9, 0x60, 0x08, 0xCD, 0x90, 0x0C, 0xA4, 0x9D, 0x80, 0xEA, 0x35, 0x80, 0xD2, 0x20, 0x9A, 0xA9, 0x53, 0xD0, 0x49, 0x01, 0xC9, 0x5C, 0x08, 0x00, 0x00, 0x9A, 0x56, 0x0B, 0x58, 0x9D, 0x6A, 0x76, 0x99, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4E, 0x44, 0xAE, 0x42, 0x60, 0x82 }; // ethernet interface mac address static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };//設(shè)置自己的ip地址 byte Ethernet::buffer[700]; //建立緩沖區(qū)用來存儲(chǔ)發(fā)送和接受到的數(shù)據(jù) boolean led1Status; //設(shè)置LED1的狀態(tài),0為關(guān),1為開 boolean led2Status; //設(shè)置LED2的狀態(tài),0為關(guān),1為開 static void response_callback (byte status, word off, word len) { Serial.print((const char*) Ethernet::buffer + off ); } //接受從網(wǎng)頁接受到的信息。 void setup () { Serial.begin(57600); //串口波特率設(shè)置為57.6k Serial.println("Arduino Nano"); if (ether.begin(sizeof Ethernet::buffer, mymac,10) == 0) Serial.println( "Failed to access Ethernet controller"); if (!ether.dhcpSetup()) //自動(dòng)獲取ip,gw,dns地址 Serial.println("DHCP failed"); else Serial.println("DHCP configuration done"); ether.printIp("My IP: ", ether.myip); // ether.printIp("Netmask: ", ether.mymask); ether.printIp("GW IP: ", ether.gwip); ether.printIp("DNS IP: ", ether.dnsip); pinMode(LED1PIN, OUTPUT); //將LED1,LED2,LED3設(shè)置為輸出模式并置低位 pinMode(LED2PIN, OUTPUT); digitalWrite(LED1PIN, LOW); digitalWrite(LED2PIN, LOW); led1Status = false; //設(shè)置LED1,LED2,LED3設(shè)置為輸出,即默認(rèn)模式下為關(guān)閉LED燈 led2Status = false; } void loop () { word pos =ether.packetLoop(ether.packetReceive()); if(pos) //如果接受到網(wǎng)站發(fā)送來的信息,執(zhí)行下面的程序 { if(strstr((char *)Ethernet::buffer + pos, "GET /led_off.png") != 0) send_png_image(led_off, sizeof(led_off)); else if(strstr((char *)Ethernet::buffer + pos, "GET /led_on.png") != 0) send_png_image(led_on, sizeof(led_on)); else { if(strstr((char *)Ethernet::buffer + pos, "GET /?LED1") != 0) { led1Status = !led1Status; //如果LED1被點(diǎn)擊,LED1狀態(tài)翻轉(zhuǎn),LED1也翻轉(zhuǎn)。 digitalWrite(LED1PIN, led1Status); } if(strstr((char *)Ethernet::buffer + pos, "GET /?LED2") != 0) { //如果LED2被點(diǎn)擊,LED2狀態(tài)翻轉(zhuǎn),LED2也翻轉(zhuǎn)。 led2Status = !led2Status; digitalWrite(LED2PIN, led2Status); } BufferFiller bfill = ether.tcpOffset(); //清除緩沖區(qū),開始準(zhǔn)備將網(wǎng)頁信息輸入緩沖區(qū) bfill.emit_p(PSTR("HTTP/1.0 200 OK" "Content-Type: text/htmlPragma: no-cache" "WebLeds" "Toggle leds: ")); if(led1Status) bfill.emit_p(PSTR("")); else bfill.emit_p(PSTR("")); if(led2Status) bfill.emit_p(PSTR("")); else bfill.emit_p(PSTR("")); bfill.emit_p(PSTR("")); ether.httpServerReply(bfill.position()); //發(fā)送生成網(wǎng)頁程序 } } } void send_png_image(PGM_P png_image, unsigned int image_size) { BufferFiller bfill = ether.tcpOffset(); // //清除緩沖區(qū),開始準(zhǔn)備將網(wǎng)頁信息輸入緩沖區(qū) bfill.emit_p(PSTR("HTTP/1.0 200 OK" //將相應(yīng)圖片發(fā)送到網(wǎng)頁界面 "Content-Type: image/png")); bfill.emit_raw_p(png_image, image_size); ether.httpServerReply(bfill.position()); }
1-3.jpg (10.92 KB, 下載次數(shù): 96)
下載附件
2016-3-30 19:21 上傳
圖2 4、讓你的arduino連入物聯(lián)網(wǎng): 圖中所示為我們在物聯(lián)網(wǎng)平臺(tái)yeelink上傳的數(shù)據(jù):圖3個(gè)是開關(guān)控制,圖4個(gè)是傳感器數(shù)據(jù)曲線: 因?yàn)槲覀冇胑nc28j60網(wǎng)關(guān)無法調(diào)出登陸物聯(lián)網(wǎng)界面,故我們用w5100網(wǎng)關(guān)和arduino聯(lián)系起來,參考網(wǎng)上程序,將自己的arduino與物聯(lián)網(wǎng)平臺(tái)聯(lián)系起來。通過點(diǎn)擊操作控制來控制LED的點(diǎn)亮與關(guān)閉。 #include #include #include #include byte buff[2]; // for yeelink api #define APIKEY "20314f31cb6934a5187f43b676be2886" // 此處替換為你自己的API KEY #define DEVICEID 19119 // 此處替換為你的設(shè)備編號(hào) #define SENSORID1 33314 // 此處替換為你的傳感器編號(hào) // assign a MAC address for the ethernet controller. byte mac[] = { 0x00, 0x1D, 0x72, 0x82, 0x35, 0x9D}; // initialize the library instance: EthernetClient client ; char server[] = "api.yeelink.net"; // name address for yeelink API unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds boolean lastConnected = false; // state of the connection last time through the main loop const unsigned long postingInterval = 3*1000; // delay between 2 datapoints, 30s String returnValue = ""; boolean ResponseBegin = false; void setup() { pinMode(5, OUTPUT); Wire.begin(); // start serial port: Serial.begin(57600); // start the Ethernet connection with DHCP: if (Ethernet.begin(mac) == 0) { Serial.println("Failed to configure Ethernet using DHCP"); for(;;); } else { Serial.println("Ethernet configuration OK"); } }
void loop() { // if there's incoming data from the net connection. // send it out the serial port. This is for debugging // purposes only: if (client.available()) { char c = client.read(); // Serial.print(c); if (c == '{') ResponseBegin = true; else if (c == '}') ResponseBegin = false;
if (ResponseBegin) returnValue += c; } if (returnValue.length() !=0 && (ResponseBegin == false)) { Serial.println(returnValue);
if (returnValue.charAt(returnValue.length() - 1) == '1') { Serial.println("turn on the LED"); digitalWrite(5, HIGH); } else if(returnValue.charAt(returnValue.length() - 1) == '0') { Serial.println("turn off the LED"); digitalWrite(5, LOW); } returnValue = ""; } // if there's no net connection, but there was one last time // through the loop, then stop the client: if (!client.connected() && lastConnected) { Serial.println(); Serial.println("disconnecting."); client.stop(); } // if you're not connected, and ten seconds have passed since // your last connection, then connect again and send data: if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) { // read sensor data, replace with your code //int sensorReading = readLightSensor(); Serial.print("yeelink:"); //get data from server getData(); } // store the state of the connection for next time through // the loop: lastConnected = client.connected(); } // this method makes a HTTP connection to the server and get data back void getData(void) { // if there's a successful connection: if (client.connect(server, 80)) { Serial.println("connecting..."); // send the HTTP GET request: client.print("GET /v1.0/device/"); client.print(DEVICEID); client.print("/sensor/"); client.print(SENSORID1); client.print("/datapoints"); client.println(" HTTP/1.1"); client.println("Host: api.yeelink.net"); client.print("Accept: *"); client.print("/"); client.println("*"); client.print("U-ApiKey: "); client.println(APIKEY); client.println("Content-Length: 0"); client.println("Connection: close"); client.println(); Serial.println("print get done.");
} else { // if you couldn't make a connection: Serial.println("connection failed"); Serial.println(); Serial.println("disconnecting."); client.stop(); } // note the time that the connection was made or attempted: lastConnectionTime = millis(); }
1-4.jpg (10.46 KB, 下載次數(shù): 88)
下載附件
2016-3-30 19:21 上傳
圖3
1-5.jpg (28.7 KB, 下載次數(shù): 75)
下載附件
2016-3-30 19:21 上傳
圖4
小節(jié):通過一周對arduino的學(xué)習(xí),我們意識(shí)到雖然arduino的開源軟硬件都很多,在消費(fèi)類電子市場占有很大比例,但 對于資深工程師來說,arduino只是AVR的入門硬件,在精密電子儀器,中高端設(shè)備中有明顯的不足。對學(xué)習(xí)嵌入式,物聯(lián)網(wǎng)方面有一定的輔助作用。
參考書籍: 《arduino的入門與精深》 《arduino的程序設(shè)計(jì)》 《動(dòng)手玩轉(zhuǎn)arduino》 《學(xué)arduino玩轉(zhuǎn)電子制作》 《arduino從基礎(chǔ)到實(shí)戰(zhàn)》
|