成绩评定表课程设计任务书目录1 微博机器人 (2)1.1 编写代码 (3)1.2 代码回顾 (6)2 交通灯+手控延时 (11)2.1 功能 (12)2.2 电路原理 (12)2.3 代码清单 (13)2.4 测试结果 (16)3 卫星天线及接收机调试 (16)1微博机器人在这个项卡具中还是需要使用两个温度传感器的电路。
这次,你要有规律地在微博上更新两个传感器的状态。
用发送微博的办法使你可以了解已经连接到Arduion上的和、任何传感器的状态。
Twitter是一条微博服务器,允许你发送长度在140个字符以内的微博或短消息。
任何人只要进行下搜索,或那些已选择了关注你的微博的人,都可以看到你发布的微博的内容。
微博是非常流行的网络应用,可以从任何网络浏览器或许多可用微博客户机中浏览微博内容,甚至电话终端也可以发送微博。
这使得微博成为发送简单消息的理想选择。
你也可以通过移动终端查看微博内容。
你需要连到注册一个新用户。
我推荐创建一个只被你的Arduino使用的账号。
因为2010年8月31日以后,Twitter改变了它的第三方程序入口注册策略,使用了一种叫做OAuth的认证方法,使得直接从Arduino发送微博非常困难。
在这个改变之前,向Twitter发送微博是一个简单的过程,现在只能通过第三方实现。
换句话说,你发布微博到一个网站或代理服务器,代理服务器使使用OAuth协议代替你发布微博,Arduino 当前的微博库主使用这种方法。
如果你已有账号,输入下的代码。
输入代码在你上传代码之前,需要注册一条微博账号。
你使用这个库已比由NeaCat完成,可以使用它的网址作为代理服务器发送微博。
这意味你必须首先获得一个口令,它是一个你自己的要进入微博网站的用户名和口令的加密版本。
要得到这个版本可以去网址 ,并单击”stepq”链接口令,复制并粘贴结果到口令代码段。
注意,因为你使用代理服务器而且必须给出你的微博用户名和口令来获得OAuth 令牌,所以推荐创建一个新的微博账号保持它为匿名。
我相信你用自己的账号使用库也是安全的但是相比较而言匿名更安全。
之后,单击”step2”链连,获得两套相关的库代码。
安装这些库文件夹到之前你下载和安装的0018版本的Arduino IDE内。
使用这些库之前需要重启IDE。
Twitter库也有一些例子可以试一下。
1.1 编写代码// 项目–微博机器人#include <Ethernet.h>#include <EthernetDHCP.h>#include <EthernetDNS.h>#include <Twitter.h>#include <OneWire.h>#include <DallasTemperature.h>// Data wire is plugged into pin 3 on the Arduino#define ONE_WIRE_BUS 3#define TEMPERATURE_PRECISION 12float itempC, itempF, etempC, etempF;boolean firstTweet = true;// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallastemperature ICs)OneWire oneWire(ONE_WIRE_BUS);// Pass our oneWire reference to Dallas Temperature.DallasTemperature sensors(&oneWire);// arrays to hold device addressesDeviceAddress insideThermometer = { 0x10, 0x7A, 0x3B, 0xA9, 0x01, 0x08, 0x00, 0xBF };DeviceAddress outsideThermometer = { 0x10, 0xCD, 0x39, 0xA9, 0x01, 0x08, 0x00, 0xBE};byte mac[] = { 0x64, 0xB9, 0xE8, 0xC3, 0xC7, 0xE2 };// Your Token to tweet (get it from /)Twitter twitter("608048201-CxY1yQi8ezhvjz60ZVfPHVdzIHbMOD1h2gvoaAIx"); unsigned long interval = 600000; // 10 minutesunsigned long lastTime; // time since last tweet// Message to postchar message[140], serialString[60];// function to get the temperature for a devicevoid getTemperatures(){itempC = sensors.getTempC(insideThermometer);itempF = DallasTemperature::toFahrenheit(itempC);etempC = sensors.getTempC(outsideThermometer);etempF = DallasTemperature::toFahrenheit(etempC);}void tweet(char msg[]) {Serial.println("connecting ...");if (twitter.post(msg)) {int status = twitter.wait();if (status == 200) {Serial.println("OK. Tweet sent.");Serial.println();lastTime = millis();firstTweet = false;} else {Serial.print("failed : code ");Serial.println(status);}} else {Serial.println("connection failed.");}}void setup(){EthernetDHCP.begin(mac);Serial.begin(9600);sensors.begin();// set the resolutionsensors.setResolution(insideThermometer, TEMPERATURE_PRECISION);sensors.setResolution(outsideThermometer, TEMPERATURE_PRECISION);sensors.requestTemperatures()getTemperatures();// compile the string to be tweetedwhile (firstTweet) {sprintf(message, "Int. Temp: %d C (%d F) Ext. Temp: %d C (%d F). Tweeted from Arduino. %ld",int(itempC), int(itempF), int(etempC), int(etempF), millis()); tweet(message);}}void loop(){EthernetDHCP.maintain();sensors.requestTemperatures();// compile the string to be printed to the serial monitorsprintf(serialString, "Internal Temp: %d C %d F. External Temp: %d C %d F", int(itempC),int(itempF), int(etempC), int(etempF));delay(500);Serial.println(serialString);Serial.println();if (millis() >= (lastTime + interval)) {// compile the string to be tweetedsprintf(message, "Int. Temp: %d C (%d F) Ext. Temp: %d C (%d F). Tweeted from Arduino. %ld",int(itempC), int(itempF), int(etempC), int(etempF), millis()); tweet(message);}delay(10000); // 10 seconds}After you have uploaded the code to your Arduino, open the serial monitor window. The Arduinowill attempt to connect to Twitter (actually NeoCat’s website) and send the tweet. If the first tweet issuccessful, the output in the serial monitor window will be a bit like this:connecting ...OK. Tweet sent.Internal Temp: 26 C 79 F. External Temp: 26 C 79 FInternal Temp: 26 C 79 F. External Temp: 26 C 79 FInternal Temp: 26 C 79 F. External Temp: 26 C 79 F程序第一次运行时,在进入主循环之前它将在setup函数中获得温度之后保持尝试连接到微博。
尝试连接不会停止,直到成功连接。
如程序连接失败,返回一个failed:code403或connection failed信息。
如果发送微博成功,程序将不再发送直到间隔时间已过,这个时间的默认设置是10分钟。