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

標題: ARDUINO的簡單應用-課程設計報告 [打印本頁]

作者: 51黑ss    時間: 2016-3-30 19:20
標題: ARDUINO的簡單應用-課程設計報告
1、開始運行你的Arduino kit:
51黑論壇或者網上找到IDE1.01或者以上版本,下載并安裝。用USB轉串口線插入arduino板時,會提示需要裝驅動。在網上找到arduino與之相關的USB2.0驅動,就可以正常的運行你的arduino了。
因為IDE含有大量arduino例程,故可以先將例程導入編譯頁面。如下為4個用arduino編譯的例程。
//1一個簡單的程序讓讀取模擬端口ao的值并用串口發送
void setup() {
Serial.begin(9600);//串口波特率9.6kb//d0-tx//d1-rx
}
void loop()
{
int sensorValue = analogRead(A0);//讀取a0值(0-1023)
Serial.println(sensorValue);//串口發送數據
delay(1); //延時一毫秒
}
注:arduino由初始化函數setup()與循環函數loop()組成。
圖1
本例中所有程序均在IDE中完成編程,也可在keil,iar,或者第三方軟件中運行。
2、串口控制輸出:
下載程序后,打開IDE的串口助手,會出現圖一界面,發送a-e,即可將d2-d5分別點亮,輸入其他數據,則會關閉所有LED燈。
void setup() {
Serial.begin(9600); //串口波特率9.6kb//d0-tx//d1-rx
for (int thisPin = 2; thisPin < 7; thisPin++)
{
pinMode(thisPin, OUTPUT);//將d2-d7設置為輸出模式
}
}
void loop()
{
if (Serial.available() > 0) { //如果串口收到信息
int inByte = Serial.read(); //將串口發送的數據送入到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: //其他情況關閉所有LED燈
for (int thisPin = 2; thisPin < 7; thisPin++)
{
digitalWrite(thisPin, LOW); //其他情況就將d2-d7均置低
}
}
}
}

3、局域網控制LED燈:
在網站下載 庫,導入LEDs程序,可以得到如圖2所示的畫面。要實現聯網連接,需要用到enc28j60網卡。
#include
#define LED1PIN 3
#define LED2PIN 5
PROGMEM prog_char led_off[]= { //灰色圖標,關閉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, 0xE0,
0x27, 0x6D, 0x7A, 0x5B, 0xC1, 0x6E, 0xF3, 0x01, 0x92, 0x2C, 0x36, 0x56, 0x62, 0x61, 0x23, 0xA4, 0xB1, 0x12, 0xF2, 0x64,
0xDF, 0x5A, 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0D, 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 };//設置自己的ip地址
byte Ethernet::buffer[700]; //建立緩沖區用來存儲發送和接受到的數據
boolean led1Status; //設置LED1的狀態,0為關,1為開
boolean led2Status; //設置LED2的狀態,0為關,1為開
static void response_callback (byte status, word off, word len) {
Serial.print((const char*) Ethernet::buffer + off );
} //接受從網頁接受到的信息。
void setup () {
Serial.begin(57600); //串口波特率設置為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()) //自動獲取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設置為輸出模式并置低位
pinMode(LED2PIN, OUTPUT);
digitalWrite(LED1PIN, LOW);
digitalWrite(LED2PIN, LOW);
led1Status = false; //設置LED1,LED2,LED3設置為輸出,即默認模式下為關閉LED燈
led2Status = false;
}
void loop () {
word pos =ether.packetLoop(ether.packetReceive());
if(pos) //如果接受到網站發送來的信息,執行下面的程序
{
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被點擊,LED1狀態翻轉,LED1也翻轉。
digitalWrite(LED1PIN, led1Status);
}
if(strstr((char *)Ethernet::buffer + pos, "GET /?LED2") != 0) { //如果LED2被點擊,LED2狀態翻轉,LED2也翻轉。
led2Status = !led2Status;
digitalWrite(LED2PIN, led2Status);
}
BufferFiller bfill = ether.tcpOffset(); //清除緩沖區,開始準備將網頁信息輸入緩沖區
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()); //發送生成網頁程序
}
}
}
void send_png_image(PGM_P png_image, unsigned int image_size)
{
BufferFiller bfill = ether.tcpOffset(); // //清除緩沖區,開始準備將網頁信息輸入緩沖區
bfill.emit_p(PSTR("HTTP/1.0 200 OK" //將相應圖片發送到網頁界面
"Content-Type: image/png"));
bfill.emit_raw_p(png_image, image_size);
ether.httpServerReply(bfill.position());
}
圖2
4、讓你的arduino連入物聯網:
圖中所示為我們在物聯網平臺yeelink上傳的數據:圖3個是開關控制,圖4個是傳感器數據曲線:
因為我們用enc28j60網關無法調出登陸物聯網界面,故我們用w5100網關和arduino聯系起來,參考網上程序,將自己的arduino與物聯網平臺聯系起來。通過點擊操作控制來控制LED的點亮與關閉。
#include
#include
#include
#include
byte buff[2];
// for yeelink api
#define APIKEY "20314f31cb6934a5187f43b676be2886" // 此處替換為你自己的API KEY
#define DEVICEID 19119 // 此處替換為你的設備編號
#define SENSORID1 33314 // 此處替換為你的傳感器編號
// 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

小節:通過一周對arduino的學習,我們意識到雖然arduino的開源軟硬件都很多,在消費類電子市場占有很大比例,但 對于資深工程師來說,arduino只是AVR的入門硬件,在精密電子儀器,中高端設備中有明顯的不足。對學習嵌入式,物聯網方面有一定的輔助作用。





作者: 51hei電控2112311    時間: 2016-5-1 17:17
又盜我圖哈。
作者: 51hei電控2112311    時間: 2016-6-4 01:41
這個是我寫的。




歡迎光臨 (http://www.zg4o1577.cn/bbs/) Powered by Discuz! X3.1
主站蜘蛛池模板: 青娱乐国产 | 日韩电影中文字幕 | 一区二区三区国产在线观看 | 国产精品九九视频 | 欧美精品一二三区 | 免费一级毛片 | 亚洲性视频 | 懂色av蜜桃av | 一区视频在线播放 | 国产精品久久久久久一区二区三区 | 欧美综合一区 | 日韩一区欧美一区 | 久久久久亚洲精品 | 成人亚洲视频 | 亚洲毛片 | 久草久| 亚洲成人免费电影 | 一区影院 | 精品久久中文字幕 | 欧美成人免费在线 | 韩国电影久久 | 亚洲自拍偷拍欧美 | 亚洲xx在线 | 色毛片 | 97av视频| 伦理午夜电影免费观看 | 国产精品久久久久aaaa樱花 | 亚洲精品www | 亚洲美女在线一区 | 国产高清在线精品一区二区三区 | 久久国产成人 | 成人午夜在线观看 | 三级成人在线 | 国产午夜精品一区二区三区四区 | 中文精品视频 | 欧美精品综合 | 网站国产| 欧洲色| 免费的av | 色婷婷综合久久久久中文一区二区 | 久久激情五月丁香伊人 |