久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 10197|回復(fù): 2
打印 上一主題 下一主題
收起左側(cè)

ARDUINO的簡單應(yīng)用-課程設(shè)計(jì)報(bào)告

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
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
本例中所有程序均在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均置低
}
}
}
}

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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x7F, 0x68, 0x9E, 0xBC, 0x29, 0xB4, 0x3E,
0x3D, 0x5B, 0x37, 0xE6, 0xEC, 0x9D, 0x10, 0x82, 0xC5, 0x3F, 0xEF, 0x3B, 0x8C, 0x5F, 0xC2, 0x84, 0x6A, 0x7B, 0xFB, 0x42,
0x44, 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());
}
圖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();
}

圖3
圖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)》



評分

參與人數(shù) 1黑幣 +100 收起 理由
admin + 100 共享資料的黑幣獎(jiǎng)勵(lì)!

查看全部評分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:117823 發(fā)表于 2016-5-1 17:17 | 只看該作者
又盜我圖哈。
回復(fù)

使用道具 舉報(bào)

板凳
ID:117823 發(fā)表于 2016-6-4 01:41 來自手機(jī) | 只看該作者
這個(gè)是我寫的。
回復(fù)

使用道具 舉報(bào)

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表
主站蜘蛛池模板: 色综合久久天天综合网 | 久久蜜桃av | 精品视频在线免费观看 | 欧美videosex性极品hd | 国产欧美在线播放 | 午夜播放器在线观看 | 精品综合久久久 | 色影视| www.99热.com| 国产99免费 | 国产免费xxx | 韩日一区二区三区 | 久久精品国产一区 | 毛片在线免费 | 久久天天躁狠狠躁夜夜躁2014 | 国产91在线播放 | 国产激情综合五月久久 | 一级毛片视频在线观看 | 国产精品一区二区三 | 99热.com | 国产精品mv在线观看 | 欧美黄色精品 | 365夜爽爽欧美性午夜免费视频 | 黄页网址在线观看 | 日本视频中文字幕 | 一级片在线免费看 | 久久精品一区二区三区四区 | 亚洲视频二区 | 在线观看中文字幕 | 国产精品小视频在线观看 | 欧美日韩精品久久久免费观看 | 少妇精品亚洲一区二区成人 | 综合另类| 盗摄精品av一区二区三区 | 无码一区二区三区视频 | 中文字幕高清av | 久久不卡区 | 欧美日韩91 | 中文字幕第一页在线 | 观看av| 久久一区|