Pages: [1]   Go Down
Author Topic: How can I post a result of moisture sensor to twitter by loop?  (Read 707 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi

I made Arduino moisture sensor.
http://bunkeina.blogspot.jp/

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).

Code:
#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.
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 89
Posts: 3453
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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. 
Logged

UK
Offline Offline
Shannon Member
****
Karma: 183
Posts: 11154
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.:
Code:
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.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

New Jersey
Offline Offline
Faraday Member
**
Karma: 48
Posts: 3402
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley

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
Logged

California
Offline Offline
Faraday Member
**
Karma: 82
Posts: 3123
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

UK
Offline Offline
Shannon Member
****
Karma: 183
Posts: 11154
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

0
Offline Offline
Tesla Member
***
Karma: 114
Posts: 8911
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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=
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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  smiley-cool
Thanks PeterH!

Code:
#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 %.
« Last Edit: March 22, 2013, 06:39:02 pm by junkai » Logged

Offline Offline
Edison Member
*
Karma: 28
Posts: 2036
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Edison Member
*
Karma: 28
Posts: 2036
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, michinyon

Thank you.

Quote
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.
Code:
void setup() {
Ethernet.begin(mac);
Serial.begin(9600); //open serial port
delay(1000);
}


Quote
You should      take the moisture reading.   Send it.    Wait a while before doing it again.
Code:
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?
Logged

Pages: [1]   Go Up
Jump to: