Is easy to use, but It accept only char for twitting, and i'm able to compose texts and sensors values only with string.
There is a way to convert a string into a char?
I did many search but seems to be a difficult point.
Thank you all...
Thank you, this is my code.
I know I'm not very expert about but i hope in your help.
#if defined(ARDUINO) && ARDUINO > 18 // Arduino 0019 or later
#include <SPI.h>
#endif
#include <Ethernet.h>
#include <EthernetDNS.h>
#include <Twitter.h>
// Ethernet Shield Settings
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x77, 0x5D };
byte ip[] = { 10,163,13,115 };
// Your Token to Tweet (get it from http://arduino-tweet.appspot.com/)
Twitter twitter("395212348-ahJnSbYm8auM40gvBoAJRlMBOsJc11lkIGlYtSQk");
//sensors i would like to use to tweet
void setup()
{
delay(2000);
Ethernet.begin(mac, ip);
Serial.begin(9600);
}
void loop()
{
char msg[] = "Hello World!";
int sensorOne =analogRead(A0);
int sensorTwo =analogRead(A1);
String tweet= "";
tweet = tweet + "the data from sensor one is: "+ sensorOne + ", and from the sensor two is: " + sensorTwo;
Serial.println(tweet);
Serial.println("connecting ...");
if (twitter.post(msg)) { //it work only with "msg" that is a char, i would like to use "tweet"
int status = twitter.wait(&Serial);
if (status == 200) {
Serial.println("OK.");
} else {
Serial.print("failed : code ");
Serial.println(status);
}
} else {
Serial.println("connection failed.");
}
delay(60000);
}
The String class has a method, toCharArray(), that can be used to get the character array from the String.
int strLen = tweet.length(); // Get the length
char *msg = (char *)malloc(strLen+1); Allocate space to hold the data
if(msg)
{
tweet.toCharArray(msg, strLen+1);
// Send the data in msg to twitter
free(msg);
}
Or, you could use sprintf() to create an array of chars with formatting:
char msg[64];
sprintf(msg, "Sensor 1: %d; Sensor 2 %d", sensorOne, sensorTwo);
// Send the data in msg to twitter
There needs to be some size. I picked 64 because it's not too large and not too small.
can i also insert other char or String in place of a Int inside the composition?
If you change the format specifier, yes, you can use some other types. %d is for ints. %s would be for strings (NULL terminated array of char). Strings are not a native type, so the toCharArray() method need to be used to extract the native type.