How can I post a result of moisture sensor to twitter by loop?

Hi

I made Arduino moisture sensor.

I'd like to post a result of moisture sensor to twitter every 1 minuet by loop, but I cannot write it well.
Could you help me to correct my program? I am using Stewitter(http://arms22.googlecode.com/files/Stewitter-1.0.zip).

#include <SPI.h>
#include <Ethernet.h>
#include <Stewitter.h>

//This mac is dummy
byte mac[] = { 
  0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA };

// You can get a token from stewgate.appspot.com
Stewitter twitter("Write Your token which get on Stewitter here");

int moistureSensor = 0;
int moisture_val;

void setup() {
  delay(1000);

  Ethernet.begin(mac);
Serial.begin(9600); //open serial port

}

void loop() {
moisture_val = analogRead(moistureSensor); // read the value from the moisture-sensing probes
Serial.print("moisture sensor reads ");
Serial.println( moisture_val );
  // To connect to twitter and post a message
  if (twitter.post(moisture_val)) {
    // Wait to done posting
    int status = twitter.wait();}
    //If status is 200, it means succeed
    if (status == 200) {
      // Showing the response
      Serial.println(twitter.response());
delay(60000);
 } 
}

Thanks.

What does your current code do that you want/need to correct ?

Hi, UKHeliBob

Thanks replied to me.
Because Arduino sketch says

sketch_mar22d.ino: In function 'void loop()':
sketch_mar22d:28: error: invalid conversion from 'int' to 'const char*'
sketch_mar22d:28: error: initializing argument 1 of 'bool Stewitter::post(const char*)'
sketch_mar22d:32: error: 'status' was not declared in this scope

I cannot understand how should I correct the program by following the messages which Arduino sketch said.
I want you to give me advice to write the program without errors.

The twitter.post() method requires a char pointer as the argument.

You are passing a number. This doesn't compile because it's the wrong type.

To convert your number to a char pointer you need to print the number to produce a string, then you can post that string.

e.g.:

char text[32]; // make sure this is long enough to hold your entire message, including the null terminator
sprintf(text, "My sensor reading is %d", moisture_val);
if (twitter.post(text)) {
   etc ...

Here the '%d' is replaced by a decimal representation of moisture_val. You can change the surrounding text, or remove it and just print the number on its own, if you want.

Your other issue is that you're using status outside the braces where you declared it. Your simplest fix would be to make it global.

Thanks everybody replied

I appreciate you gave me an advice.
Actually I have learned Arduino and C just 1 week.
I am very happy to listen to advice from specialists for improving me :slight_smile:

I'd like to see your good example which makes by my sketch.
If you were me, how will you make my sketch improve?

junkai

junkai:
I'd like to see your good example which makes by my sketch.
If you were me, how will you make my sketch improve?

junkai

Translation: Fix my code for me and give me the working code.

junkai:
If you were me, how will you make my sketch improve?

I'd fix compilation problems until it compiled, and then run it and test it and fix any bugs in the behaviour until it did what I wanted.

I'd like to see your good example which makes by my sketch.
If you were me, how will you make my sketch improve?

Reading material on previous twitter post.

https://www.google.com/search?hl=en&as_q=twitter&as_epq=&as_oq=&as_eq=&as_nlo=&as_nhi=&lr=&cr=&as_qdr=all&as_sitesearch=http%3A%2F%2Farduino.cc%2Fforum%2F&as_occt=any&safe=images&tbs=&as_filetype=&as_rights=

Translation: Fix my code for me and give me the working code.

Good translation it is. But I have written working code already.

Working code here 8)
Thanks PeterH!

#include <SPI.h>
#include <Ethernet.h>
#include <Stewitter.h>

//This mac is dummy
byte mac[] = { 
  0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA };

// You can get a token from stewgate.appspot.com
Stewitter twitter("Your token here");

int moistureSensor = 0;
int moisture_val;

void setup() {
  delay(1000);

  Ethernet.begin(mac);
Serial.begin(9600); //open serial port

}

void loop() {
moisture_val = analogRead(moistureSensor); // read the value from the moisture-sensing probes
Serial.print("moisture sensor reads ");
Serial.println( moisture_val );
delay(120000);//2min
char text[32]; // make sure this is long enough to hold your entire message, including the null terminator
sprintf(text, "My sensor reading is %d", moisture_val);
if (twitter.post(text)) {
    // Wait to done posting
    int status = twitter.wait();}
    //If status is 200, it means succeed
    if (twitter.wait() == 200) {
      // Showing the response
      Serial.println(twitter.response());

 } 
}

Even though, it is working well. But I want to know what is my wrong on this code.
Please show me how can you make it better, if you can.

I'm going to add it light sensor and temperature sensor today.
And I want to post moisture by %.

Does twitter need an update on the status of your flowerpot, every minute ?? No wonder it's slow.

There is no point having the delay() function at the start of setup(), before anything else.

Maybe you need a delay AFTER you call the initialization functions for Serial and Ethernet, so that
the hardware has time to initialize properly, before you try to use it.

But having a delay() before those calls, makes little sense.

The location of your 2 minute delay in loop() also makes little sense.

You should take the moisture reading. Send it. Wait a while before doing it again.

It makes no sense to put the delay between taking the moisture reading, and sending it. That just makes
all the data which you send, two minutes out of date.

Hi, michinyon

Thank you.

Maybe you need a delay AFTER you call the initialization functions for Serial and Ethernet, so that
the hardware has time to initialize properly, before you try to use it.

But having a delay() before those calls, makes little sense.

I fixed the code likes it.

void setup() {
Ethernet.begin(mac);
Serial.begin(9600); //open serial port
delay(1000);
}

You should take the moisture reading. Send it. Wait a while before doing it again.

void loop() {
moisture_val = analogRead(moistureSensor); // read the value from the moisture-sensing probes
Serial.print("moisture sensor reads ");
Serial.println( moisture_val );
char text[32]; // make sure this is long enough to hold your entire message, including the null terminator
sprintf(text, "My sensor reading is %d", moisture_val);
if (twitter.post(text)) {
    // Wait to done posting
    int status = twitter.wait();}
    //If status is 200, it means succeed
    if (twitter.wait() == 200) {
      // Showing the response
      Serial.println(twitter.response());
      delay(120000);//2min
 } 
}

Is it correct?