Arduino Tweets what it is not supposed to (wrong values).

I am working on a project where Arduino tweets sensor readings but after converting those sensor readings to a string the resulting tweet is something like MZ when it is supposed to be a 'float' number. Whatever, the Tweet does not correspont to what it should be tweeted.

What is wrong in this sketch?

#include <SPI.h> // needed in Arduino 0019 or later
#include <WiFi.h>
#include <Twitter.h>

char ssid[] = "WIFISSID";
char pass[] = "WIFIPASSWD"; 

// Your Token to Tweet (get it from http://arduino-tweet.appspot.com/)
Twitter twitter("TOKEN HERE"); 

float temp =341.02;
char buffer [120];
char msg[162];

void setup() {
  
  dtostrf(temp, 10, 3, buffer); //Converts the 'float' into a string
  strcat("Temperature is:", msg);
  strcat(buffer, msg);
  char msg[] = {temp};
  delay(1000);
  WiFi.begin(ssid, pass);
  delay(10000);
  Serial.begin(9600);
  
  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.");
  }
}

void loop()
{
}

Thanks!

javierdemartin:
Whatever, the Tweet does not correspont to what it should be tweeted.

That begs the question of what it's sending and how is that different from what you are expecting?

What is wrong in this sketch?

  dtostrf(temp, 10, 3, buffer); //Converts the 'float' into a string

Ok, good start. Now you have a float stored in ASCII format as a string.

strcat("Temperature is:", msg);

And it all goes to hell. That constant string is only going to be big enough to hold its contents, not the its own plus the contents of another string, which doesn't matter I guess, since msg has nothing in it. So this line becomes pointless. Look at the documentation for strcat()

char * strcat ( char * destination, const char * source );

destination
Pointer to the destination array, which should contain a C string, and be large enough to contain the concatenated resulting string.

Looks like you're mixing up the source and destination parameters

  strcat(buffer, msg);

Same thing here.

  char msg[] = {temp};

What exactly is this supposed to accomplish? Why bother doing all that work with msg and buffer, if you're just going to create a new msg variable that has nothing to do with the previous one? You should only have tqo char arrays: one to store the converted float and one that stores the entire string and is sent to Twitter::post()

Deleted the strcat("Temperature is:", msg); line.

Then this

char * strcat ( char * destination, const char * source ); //Original
strcat(buffer, msg); //Compiled and everything´s okay

Now I do not exactly how to tweet the correct data, I´ve written float temp =341.02; so that is supposed to be a sensor reading but now tweets this

UL51684094184-kisC9ybEQGjSvacKAeHnJyigHGIcXKD6lh2KAey

Post your updated code, don't just say what you've changed.

This is it

#include <SPI.h> // needed in Arduino 0019 or later
#include <WiFi.h>
#include <Twitter.h>

char ssid[] = "WIFIPASSID";
char pass[] = "PASSWD"; 

// Your Token to Tweet (get it from http://arduino-tweet.appspot.com/)
Twitter twitter("TOKEN"); 

float temp =341.02;
char buffer [120];
char msg[162];

void setup() {
  
  dtostrf(temp, 10, 3, buffer); //Converts the 'float' into a string

  strcat(buffer, msg);
  char msg[] = {temp};
  delay(1000);
  WiFi.begin(ssid, pass);
  delay(10000);
  Serial.begin(9600);
  
  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.");
  }
}

void loop()
{
}

You addressed one single point out of the 4 that I made; try again.

Now I understand I missed the correct use of strcat() I hope this is okay.

#include <SPI.h> // needed in Arduino 0019 or later
#include <WiFi.h>
#include <Twitter.h>

char ssid[] = "WIFISSID";
char pass[] = "WIFIPASSWD"; 

// Your Token to Tweet (get it from http://arduino-tweet.appspot.com/)
Twitter twitter("TOKEN"); 

float temp =341.02;
char buffer [120];
char msg[162];

void setup() {
  
  dtostrf(temp, 10, 3, buffer); //Converts the 'float' into a string
  strcat(msg, "Temperature is:");
  strcat(msg, buffer);
  char msg[] = {temp};
  delay(1000);
  WiFi.begin(ssid, pass);
  delay(10000);
  Serial.begin(9600);
  
  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.");
  }
}

void loop()
{
}

Given this error on the Serial Monitor

coconnecting ...
HTTP/1.0 403 Forbidden
Content-Type: text/html; charset=utf-8
Cache-Control: no-cache
Vary: Accept-Encoding
Date: Wed, 21 Aug 2013 21:36:45 GMT
Server: Google Frontend
Alternate-Protocol: 80:quic

Error 403 - {"errors":[{"code":187,"message":"Status is a duplicate"}]}
failed : code 403
...
char buffer [120];
char msg[162];
...
  char msg[] = {temp};

Still have 3 char arrays, when you only need two, and one of them is way bigger than necessary (how many characters are you expecting this converted float to take up?). That last line also make no sense.

If I want to tweet: "The temperature is: (temp). Which one should I remove?

I augmented the size just in case the tweeting error was a problem of size in the String.

Well, the las line I really don't know what is wrong.

Thanks for your help.

I augmented the size just in case the tweeting error was a problem of size in the String.

Your float is 341.02 which contains 6 characters plus a null terminator. That means it needs 7 bytes. Your array is declared to be 120 bytes long. That's 17x the size you need it to be.

Well, the las line I really don't know what is wrong.

I'm not even convinced you know what it's doing, let alone that it's wrong. If you don't know what it's there for, why is it in your code in the first place? Get rid of it.

Everything´s solved, now prints in the Serial Monitor the temperature of my room correctly (around 28ºC).

I have a last question, it always gives me the same error in the Serial Monitor

Error 403 - {"errors":[{"code":187,"message":"Status is a duplicate"}]}
failed : code 403

Is there any way to solve this?

Thanks for your help.