Hi all
I'm a ammature, so please be gentle.
I'm needing a bit of help with my weather station code. I've managed to get the arduino (UNO) to read the sensors (BMP085 & DHT22) & DS1307 and write the values to the SDcard, LCD and Serial without many problems. I'm now wanting to incorporate twitter into my project. So the weather station will tweet the values periodically or when certain parameter are met etc.
I've had to order the Mega2560 as the code is now exceeding the limitation of my UNO since the addition of the Twitter (probably due to my poor coding skills).
I've used the Singlepost example in the Twitter library and got it to work, but it only sends the TEXT within the " ", how can I reconfigure the code so that I can pull all the data from the sensors etc into the // char msg[] = "Hello, World! I'm Arduino!"; // line? From what I understand the Twitter "line" need to be of a char nature, therefore I would need to put all the values (floats, int, ???) into a "string" and then convert into a char string? I'm losing my self here....
I've tried a bit of googling and it seems that printf() may work, but I'm never used it and not sure how to implement it, possibly the string function as well? I'm not sure!
In the text I would have to include the date and time from the DS1307, as if the tweet is the same (parameters haven't changed) it will be rejected as a duplicate, therefore including the date and time it would never seen as a duplication - I hope?
Any help is much appreciated.
/*
Weather datalogger
by Justin
The circuit:
* analog sensors on analog ins 0, 1, and 2
* SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4
*/
#include <SD.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h> // For I2C LCD
#include <RTClib.h> // For DS1307 RTC
#include <BMP085.h> // Barometric Sensor
#include "DHT.h" // Humidity Sensor
#include <SPI.h>
#include <Ethernet.h>
#include <Twitter.h>
#define DHTPIN 2 // Humidity sensor output pin to digital pin2
#define DHTTYPE DHT22 // DHT 22 (AM2302)
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal_I2C lcd(0x27,16,2); // Sets I2C LCD address is 0x27, some differ, for a 16 chars and 2 line display,
//"lcd" in command is LCD name, use "lcd2" for multile LCDs.
RTC_DS1307 RTC;
BMP085 bmp;
// Ethernet Shield Settings
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // Sets MAC address
// If you don't specify the IP address, DHCP is used(only in Arduino 1.0 or later).
byte ip[] = { 192, 168, 0, 100 };
// Your Token to Tweet (get it from http://arduino-tweet.appspot.com/)
Twitter twitter("Token here"); //Arduino01 - Tweet Account
// Message to post
// On the Ethernet Shield, CS is pin 4. Note that even if it's not
// used as the CS pin, the hardware CS pin (10 on most Arduino boards,
// 53 on the Mega) must be left as an output or the SD library
// functions will not work.
const int chipSelect = 4; // above
unsigned long pressure = 0;
int realalt = 0;
float bmptemp = 0;
char msg[] = "Hello, World! I'm Arduino!"; //twitter message here.
void setup()
{
delay(1000);
Ethernet.begin(mac, ip); // or you can use DHCP for autoomatic IP address configuration.
// Ethernet.begin(mac);
Serial.begin(9600);
Wire.begin();
bmp.begin();
dht.begin();
lcd.init(); // initialize the lcd
lcd.backlight();
lcd.print("Weather V1.0"); // Print a "startup" message to the LCD.
delay(2000);
lcd.clear();
RTC.begin(); // Startup RTC
if (!RTC.isrunning()) { // Checks if RTC is running
Serial.println("RTC is NOT Running"); // Prints line to serial monitor for debugging
RTC.adjust(DateTime(__DATE__,__TIME__)); // Line sets the RTC to the date & time this sketch was compiled
} // RTC setup subroutine?
Serial.print("Initializing SD card...");
lcd.print("Init SD Card");
// make sure that the default chip select pin is set to
// output, even if you don't use it:
pinMode(10, OUTPUT);
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
Serial.println("card initialized.");
lcd.clear();
lcd.print ("card initial");
File logFile = SD.open("WEATHER1.csv", FILE_WRITE);
if (logFile)
{
logFile.println(", , , ,"); //Just a leading blank line, incase there was previous data
String header = "Time, Pressure, Alt, Temp, Humidity, Temp2";
logFile.println(header);
logFile.close();
Serial.println(header);
}
else
{
Serial.println("Couldn't open log file");
lcd.print("problem card");
}
}
void loop(){
DateTime now = RTC.now();
unsigned long pressure = bmp.readPressure(); //testing
int realalt = bmp.readAltitude(101400);
float bmptemp = bmp.readTemperature();
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(t) || isnan(h)) {
Serial.println("Failed to read from DHT");
} else {
//Open a file to write to
//Only one file can be open at a time
File logFile = SD.open("WEATHER1.csv", FILE_WRITE);
if (logFile)
{
logFile.print(now.hour(), DEC);
logFile.print(":");
logFile.print(now.minute(), DEC);
logFile.print(":");
logFile.print(now.second(), DEC);
logFile.print(", ");
logFile.print(pressure);
logFile.print(", ");
logFile.print(realalt);
logFile.print(", ");
logFile.print(bmptemp);
logFile.print(", ");
logFile.print(h);
logFile.print(", ");
logFile.println(t);
logFile.close();
Serial.print(now.hour(), DEC);
Serial.print(":");
Serial.print(now.minute(), DEC);
Serial.print(":");
Serial.print(now.second(), DEC);
Serial.print(", Press = ");
Serial.print(pressure);
Serial.print(", Real Alt = ");
Serial.print(realalt);
Serial.print(", Humidity = ");
Serial.print(h);
Serial.print(", Temp2 = ");
Serial.print(t);
Serial.print(", Pressure unsigned long = "); //testing...
Serial.println(pressure);
lcd.clear();
lcd.print(now.hour(), DEC);
lcd.print(":");
lcd.print(now.minute(), DEC);
lcd.print(":");
lcd.print(now.second(), DEC);
lcd.print(" ");
lcd.print(h);
lcd.print("%");
lcd.setCursor(0, 1);
lcd.print(pressure);
lcd.setCursor(6, 1);
lcd.print(bmptemp);
lcd.print("/");
lcd.print(t);
if (h > 30)
{
Serial.println("connecting ...");
if (twitter.post(msg)) {
// Specify &Serial to output received response to Serial.
// If no output is required, you can just omit the argument, e.g.
// int status = twitter.wait();
int status = twitter.wait(&Serial);
if (status == 200) {
Serial.println("OK.");
} else {
Serial.print("failed : code ");
Serial.println(status);
}
} else {
Serial.println("connection failed.");
}
}
}
else
{
Serial.println("Couldn't open log file");
lcd.setCursor(0, 1);
lcd.print("Problem - Card");
}
delay(20000); // Refresh every 20 Seconds
}
}