I'd like to send a tweet using ethernet shield and found an example code in library. But it only allows me to send a predetermined const char such as this: "Hello World". I'd like to squeeze some integers in between the text. I have a DHT22 temp and humidity sensor with me and I'd like to send a tweet like "My room humidity is: 50% and my room temp is 23C". Is there a simple way to use both integers and const chars to feed my tweet? Here's the example code btw:
// Twitter client sketch for ENC28J60 based Ethernet Shield. Uses
// arduino-tweet.appspot.com as a OAuth gateway.
// Step by step instructions:
//
// 1. Get a oauth token:
// http://arduino-tweet.appspot.com/oauth/twitter/login
// 2. Put the token value in the TOKEN define below
// 3. Run the sketch!
//
// WARNING: Don't send more than 1 tweet per minute!
// NOTE: Twitter rejects tweets with identical content as dupes (returns 403)
#include <EtherCard.h>
// OAUTH key from http://arduino-tweet.appspot.com/
#define TOKEN "Insert-your-token-here"
// ethernet interface mac address, must be unique on the LAN
byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
const char website[] PROGMEM = "arduino-tweet.appspot.com";
static byte session;
byte Ethernet::buffer[700];
Stash stash;
static void sendToTwitter () {
Serial.println("Sending tweet...");
byte sd = stash.create();
const char tweet[] = "@solarkennedy the test Twitter sketch works!";
stash.print("token=");
stash.print(TOKEN);
stash.print("&status=");
stash.println(tweet);
stash.save();
int stash_size = stash.size();
// Compose the http POST request, taking the headers below and appending
// previously created stash in the sd holder.
Stash::prepare(PSTR("POST http://$F/update HTTP/1.0" "\r\n"
"Host: $F" "\r\n"
"Content-Length: $D" "\r\n"
"\r\n"
"$H"),
website, website, stash_size, sd);
// send the packet - this also releases all stash buffers once done
// Save the session ID so we can watch for it in the main loop.
session = ether.tcpSend();
}
void setup () {
Serial.begin(57600);
Serial.println("\n[Twitter Client]");
if (ether.begin(sizeof Ethernet::buffer, mymac) == 0)
Serial.println(F("Failed to access Ethernet controller"));
if (!ether.dhcpSetup())
Serial.println(F("DHCP failed"));
ether.printIp("IP: ", ether.myip);
ether.printIp("GW: ", ether.gwip);
ether.printIp("DNS: ", ether.dnsip);
if (!ether.dnsLookup(website))
Serial.println(F("DNS failed"));
ether.printIp("SRV: ", ether.hisip);
sendToTwitter();
}
void loop () {
ether.packetLoop(ether.packetReceive());
const char* reply = ether.tcpReply(session);
if (reply != 0) {
Serial.println("Got a response!");
Serial.println(reply);
}
}
I'd like to squeeze some integers in between the text. I have a DHT22 temp and humidity sensor with me and I'd like to send a tweet like "My room humidity is: 50% and my room temp is 23C".
printf/snprint is a bit tricky to use the first time.
Something like this should give you the general idea (untested):
byte humidity = 50;
byte temperature = 34;
char buffer[60]; // large enough to hold the whole string
snprintf(buffer, sizeof(buffer), "My room humidity is: %d%% and my room temp is %dC", humidity, temperature); // format the string we want to send
stash.print(buffer);
arduinodlb:
printf/snprint is a bit tricky to use the first time.
Something like this should give you the general idea (untested):
byte humidity = 50;
byte temperature = 34;
char buffer[60]; // large enough to hold the whole string
snprintf(buffer, sizeof(buffer), "My room humidity is: %d%% and my room temp is %dC", humidity, temperature); // format the string we want to send
stash.print(buffer);
Sprintf worked great, thanks. One last thing though. In my region "%" sign comes before the number so to do that I played with characters but all I could do was breaking the code. How do I do that?
KeithRB:
Use two %'s: "%%%d" will print a percent and then the integer.
It only print the number "4". I'm sure the sensor reports a higher humidity value becaue 4 is too low. So the code is probably omitting the last digit.
cagancelik:
It only print the number "4". I'm sure the sensor reports a higher humidity value becaue 4 is too low. So the code is probably omitting the last digit.