Hola a todos!!! necesito una manito,
tomando sketch de varios lados he logrado hacer el siguiente programa para usar la plataforma thingspeak y twitter al mismo tiempo. El programa original es este Update ThingSpeak Channels and Twitter with Arduino Ethernet – About Things | A Hans Scharler Blog pero lo tuve que modicar para poder usarlo en la version de mi IDE, el que esta publicado es para versiones mas antiguas....
El problema que me surgio es que estoy leyendo el Pin analogo 2 pero no obtengo un valor logico de temperatura me muestra 1023 cuando me deberia entregar la temperatura que lee mi sensor, que es la que luego publica en twitter....
#include <DHT.h>
#define DHTPIN A2 // analog pin 2
#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
#include <SPI.h>
#include <Ethernet.h>
// Local Network Settings
byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 }; // Must be unique on local network
// ThingSpeak Settings
char thingSpeakAddress[] = "api.thingspeak.com";
String writeAPIKey = " "; // Write API Key for a ThingSpeak Channel
String thingtweetAPIKey = " "; // Write API Key for a ThingSpeak Channel twitter
const int updateThingSpeakInterval = 30000; // Time interval in milliseconds to update ThingSpeak (30000 = 30 seconds)
const long updateTwitterInterval = 3600000; // Time interval in milliseconds to update Twitter (3600000 = 1 hour)
// Variable Setup
long lastConnectionTime = 0;
long lastTweetTime = 0;
boolean lastConnected = false;
int failedCounter = 0;
// Initialize Arduino Ethernet Client
EthernetClient client;
void setup(){
dht.begin();
{
// Start Serial for debugging on the Serial Monitor
Serial.begin(9600);
// Start Ethernet on Arduino
startEthernet();
}
}
void loop()
{
// Read value from Analog Input Pin 0 and pin 1
String analogPin2 = String(analogRead(A2), DEC);
String analogPin1 = String(analogRead(A1), DEC);
// Print Update Response to Serial Monitor
if (client.available())
{
char c = client.read();
Serial.print(c);
}
// Disconnect from ThingSpeak
if (!client.connected() && lastConnected)
{
Serial.println("...disconnected");
Serial.println();
client.stop();
}
// Update
if(!client.connected() && (millis() - lastConnectionTime > updateThingSpeakInterval))
{
updateThingSpeak("field1="+analogPin2+"&field2="+analogPin1);
}
// Update Twitter Status via ThingTweet
if(!client.connected() && (millis() - lastTweetTime > updateTwitterInterval))
{
updateTwitterStatus("Mi temperatura es "+analogPin2+".");
}
// Check if Arduino Ethernet needs to be restarted
if (failedCounter > 3 ) {startEthernet();}
lastConnected = client.connected();
}
void updateThingSpeak(String tsData)
{
if (client.connect(thingSpeakAddress, 80))
{
client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: "+writeAPIKey+"\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(tsData.length());
client.print("\n\n");
client.print(tsData);
lastConnectionTime = millis();
if (client.connected())
{
Serial.println("Connecting to ThingSpeak...");
Serial.println();
failedCounter = 0;
}
else
{
failedCounter++;
Serial.println("Connection to ThingSpeak failed ("+String(failedCounter, DEC)+")");
Serial.println();
}
}
else
{
failedCounter++;
Serial.println("Connection to ThingSpeak Failed ("+String(failedCounter, DEC)+")");
Serial.println();
lastConnectionTime = millis();
}
}
void updateTwitterStatus(String tsData)
{
if (client.connect(thingSpeakAddress, 80))
{
// Create HTTP POST Data
tsData = "api_key="+thingtweetAPIKey+"&status="+tsData;
Serial.println("Connected to ThingTweet...");
Serial.println();
client.print("POST /apps/thingtweet/1/statuses/update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(tsData.length());
client.print("\n\n");
client.print(tsData);
lastTweetTime = millis();
}
}
void startEthernet()
{
client.stop();
Serial.println("Connecting Arduino to network...");
Serial.println();
delay(1000);
// Connect to network amd obtain an IP address using DHCP
if (Ethernet.begin(mac) == 0)
{
Serial.println("DHCP Failed, reset Arduino to try again");
Serial.println();
}
else
{
Serial.println("Arduino connected to network using DHCP");
Serial.println();
}
delay(1000);
}
Tambien les dejo el codigo del mas comun ejemplo de WEB SERVER pero en esta ocacion en vez de mostrar los valores de los pines analogos, me muestra los valores que lee mi sensor, en este caso los leer de forma correcta, y son los datos que quiero subir con el programa anterior al Twitter y al thingpeak...
#include <DHT.h>
#define DHTPIN A2 // Pin analogo 2
#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE);#include <SPI.h>
#include <Ethernet.h>byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
IPAddress ip(192,168,2,5);// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);void setup() {
dht.begin();// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
}void loop() {
int h = dht.readHumidity();
int t = dht.readTemperature();// listen for incoming clients
EthernetClient client = server.available();
if (client) {
Serial.println("new client");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connnection: close");
client.println();
client.println("");
client.println("");
// add a meta refresh tag, so the browser pulls again every 5 seconds:
client.println("<meta http-equiv="refresh" content="3">");
client.println("");
client.print("Lectura de Temperatura y Humedad con Arduino y DHT11");
client.println("");// output the value of temperature and humuidity from DHT
client.println("");
client.println("");
");
client.print("Arduino Reporte del Clima");
client.println("
client.println("");
");
client.print("El clima en nuestro hogar es:");
client.println("
client.println("");
");
client.print("Temperatura : ");
client.print(t);
client.print("0");
client.print("C");
client.println("
");
client.print("Humedad del ambiente : ");
client.print(h);
client.print("%");
client.println("
client.println("");client.println("");
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
Serial.println("client disonnected");
}
}
Ayuda amigos y gracias!!!