Hello, I am working on an Arduino Project to make a tweeting weather station, however I keep getting an error that I have no idea how to deal with. Whenever I start the program the serial monitor reads:
Arduino Twitter Home Weather Station
DHCP: 192.168.1.149
OK: Temp: 25 Humidity: 43
query_time: could not connect to server
I figured it was a problem with the Arduino connecting to twitter however I have no idea why. I have already inserted all of my unique Keys and Secrets and I do not know where to go from here.
Here is the Code (with my keys and secrets hidden):
#include <SPI.h>
#include <Ethernet.h>
#include <sha1.h>
#include <Time.h>
#include <EEPROM.h>
#include <Twitter.h>
#include <dht11.h>
#define DHT11PIN 6
dht11 DHT11; // create DHT11 as a dht11 class
int DHTread = 0; // set DHTread as an integer variable
int temp = 0; // set temp as integer variable
int humid = 0; // set humid as integer variable
int sensorCheck = 0;
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEC };
IPAddress twitter_ip(199, 59, 150, 39);
uint16_t twitter_port = 80;
unsigned long last_tweet = 0;
#define TWEET_DELTA (5L * 60L)
char buffer[512];
// Your Twitter consumer key and secret go here
const static char consumer_key[] PROGMEM = "XXXXXXXXXXXXXXXXXXXXXXXXXX";
const static char consumer_secret[] PROGMEM = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
Twitter twitter(buffer, sizeof(buffer));
void setup()
{
pinMode(7,OUTPUT); // set pin '7' to output mode
pinMode(4,OUTPUT); // set pin '4' to output mode
pinMode(6,INPUT_PULLUP);
digitalWrite(4,LOW); // push pin '4' low
digitalWrite(7,HIGH); // pull pin '7' high
Serial.begin(115200);
Serial.println("Arduino Twitter Home Weather Station");
if (Ethernet.begin(mac))
{
Serial.print("DHCP: ");
Ethernet.localIP().printTo(Serial);
Serial.println("");
}
else
Serial.println("DHCP configuration failed");
twitter.set_twitter_endpoint(PSTR("api.twitter.com"),
PSTR("/1.1/statuses/update.json"),
twitter_ip, twitter_port, false);
twitter.set_client_id(consumer_key, consumer_secret);
// Your Twitter App Access Token and Key go here, Token first
twitter.set_account_id(PSTR("XXXXXXXXXXX-XXXXXXXXXXXXXXXXXXXXXXXXXXX"),
PSTR("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"));
delay(500);
}
void loop()
{
delay(1000);
DHTread = DHT11.read(DHT11PIN);
temp = DHT11.temperature; // read the temperature data, store it in variable 'temp'
humid = DHT11.humidity; // do similar with humidity
switch (DHTread) // depending on the error code return, do one of the following:
{
case DHTLIB_OK:
Serial.print("OK: ");
sensorCheck = 1;
break;
case DHTLIB_ERROR_CHECKSUM:
Serial.print("Checksum error: ");
break;
case DHTLIB_ERROR_TIMEOUT:
Serial.print("timeout error: ");
sensorCheck = 0;
break;
default:
break;
}
Serial.print("Temp: ");
Serial.print(temp);
Serial.print(" Humidity: ");
Serial.println(humid);
if (twitter.is_ready())
{
unsigned long now = twitter.get_time();
if (last_tweet == 0)
{
Serial.print("Time: ");
Serial.println(now);
last_tweet = now - TWEET_DELTA + 15L;
}
if (now > last_tweet + TWEET_DELTA)
{
char msg[140];
if (sensorCheck == 1) {
sprintf(msg, "Temp: %dC Humidity: %d%% Time: %lu Sensor: OK IAmArduino.", temp, humid, now);
}
else {
sprintf(msg, "Temp: %dC Humidity: %d%% Time: %lu Sensor: **error** IAmArduino.", temp, humid, now);
}
Serial.print("Posting to Twitter: ");
Serial.println(msg);
last_tweet = now;
if (twitter.post_status(msg))
Serial.println("Status updated");
else
Serial.println("Update failed");
}
}
delay(10000);
}
Hopefully somebody who knows a lot more about this topic can help me.
Thank you!