Pasos a aseguir
Las librerias de Microtweet de Codeplex, aquí
Crear una nueva cuenta de twitter.
Regístrese nueva aplicación según las instrucciones. Agregar 4 claves para el código.
Se usara un ssnsor de 1 hilo S1820 (de la parte posterior de Java material Tini en 2000/01. Obsoleta DS1820, pero funciona bien)1
Tambien se usa un pulsador para realizar elenvio de tweets
Cargue este codigo en su netduino , asegurese que este est coenctado a Internet y Pulse el botón: esto , leera la temperatura,y enviara tweets.
A continuación el codigo para nuestro Netduino:
using System; |
using
System.Net;
002 |
using
System.Net.Sockets;
003 |
using
System.Threading;
004 |
using
Microsoft.SPOT;
005 |
using
Microsoft.SPOT.Hardware;
006 |
using
SecretLabs.NETMF.Hardware;
007 |
using
SecretLabs.NETMF.Hardware.NetduinoPlus;
008 |
using
Komodex.NETMF.MicroTweet;
009 |
using
Microsoft.SPOT.Net.NetworkInformation;
010 |
using
CW.NETMF.Hardware;
011 |
012 |
namespace
TweetButtonTest
013 |
{
014 |
public
class
Program
015 |
{
016 |
private
const
string
consumerKey =
"KeyGoesHere"
;
017 |
private
const
string
consumerSecret =
"ItsASecret"
;
018 |
private
const
string
userToken =
"InsertCoin"
;
019 |
private
const
string
userSecret =
"Shh...DontTellAnyone"
;
020 |
021 |
private
const
string
ntpServer =
"time-a.nist.gov"
;
022 |
023 |
private
OutputPort led =
null
;
024 |
private
TwitterClient twitterClient =
null
;
025 |
private
OneWire oneWire;
026 |
027 |
public
static
void
Main()
028 |
{
029 |
new
Program().Run();
030 |
}
031 |
032 |
public
void
Run()
033 |
{
034 |
035 |
NTP.UpdateTimeFromNTPServer(ntpServer);
036 |
037 |
twitterClient =
new
TwitterClient(consumerKey, consumerSecret, userToken, userSecret);
038 |
039 |
oneWire =
new
OneWire(Pins.GPIO_PIN_D1);
040 |
led =
new
OutputPort(Pins.GPIO_PIN_D13,
false
);
041 |
042 |
Button button =
new
Button(Pins.GPIO_PIN_D0);
043 |
button.Released +=
new
NoParamEventHandler(button_Released);
044 |
045 |
Thread.Sleep(Timeout.Infinite);
046 |
}
047 |
048 |
void
button_Released()
049 |
{
050 |
try
051 |
{
052 |
float
temperature = GetTemperature();
053 |
string
msg =
"Today I'm tweeting the temperature from an DS1820. It's "
+temperature+
"C #netdunio"
;
054 |
Debug.Print(msg);
055 |
056 |
bool
tweetSent = twitterClient.SendTweet(msg);
057 |
if
(tweetSent)
058 |
{
059 |
FlashLed(1);
060 |
}
061 |
else
062 |
{
063 |
FlashLed(2);
064 |
}
065 |
}
066 |
catch
067 |
{
068 |
FlashLed(3);
069 |
}
070 |
}
071 |
072 |
private
void
FlashLed(
int
flashCount)
073 |
{
074 |
while
(flashCount-- > 0)
075 |
{
076 |
led.Write(
true
);
077 |
Thread.Sleep(250);
078 |
led.Write(
false
);
079 |
Thread.Sleep(250);
080 |
}
081 |
}
082 |
083 |
private
float
GetTemperature()
084 |
{
085 |
oneWire.Reset();
086 |
oneWire.WriteByte(OneWire.SkipRom);
//All devices
087 |
oneWire.WriteByte(DS1820.ConvertT);
//Do convert
088 |
Thread.Sleep(750);
//Wait for conversion
089 |
090 |
oneWire.Reset();
091 |
oneWire.WriteByte(OneWire.SkipRom);
//All devices
092 |
oneWire.WriteByte(DS1820.ReadScratchpad);
//Read data
093 |
094 |
//Read the first two bytes - tempLow and tempHigh
095 |
byte
tempLow = oneWire.ReadByte();
096 |
byte
tempHigh = oneWire.ReadByte();
097 |
return
DS1820.GetTemperature(tempLow, tempHigh);
098 |
099 |
}
100 |
}
101 |
}
102 |