Hello,
I am pretty new to Arduino and programming in general. I'm trying to combine a piece of code from the VirtualWire library with a piece of code from the Twitter library.
I built a simplex rf transmitter (Arduino 1) that sends a character message to a receiver (Arduino 2). After the message reaches the receiver, I want the receiver to take that message and send it to Twitter.
The first program is for generating and sending the message from the transmitter. This program is working fine.
#include <VirtualWire.h>
#undef int
#undef abs
#undef double
#undef float
#undef round
const int buttonPin = 6;
int buttonState = 0;
int lastButtonState = 0;
int buttonPushCounter = 0;
int randomValue = 0;
const char* msg = "meh";
void setup()
{
pinMode(buttonPin, INPUT);
// Initialise the IO and ISR
vw_set_ptt_inverted(true); // Required for RF Link module
vw_setup(2000); // Bits per sec
vw_set_tx_pin(2); // pin 2 is used as the transmit data out into the TX Link module.
}
void loop()
{
buttonState = digitalRead(buttonPin);
msg = "Trigger 1"; // Message to send
if (buttonState != lastButtonState) {
//if (buttonState == HIGH) {
{ buttonPushCounter++; }
lastButtonState = buttonState;
if (buttonPushCounter % 2 == 0) {
char S[5000]; //Define twitter message as "S"
randomValue = random();
sprintf(S, "%s(%i)", msg, randomValue);
vw_send((uint8_t *)S, strlen(S));
vw_wait_tx();
} // Wait for message to finish
delay(200);
}
}
Next, I have a sample sketch from the VirtualWire library that receives the message and writes it to serial. This code is also working fine....on its own.
#include <VirtualWire.h>
#undef int
#undef abs
#undef double
#undef float
#undef round
void setup()
{
Serial.begin(9600);
// Initialise the IO and ISR
vw_set_ptt_inverted(true); // Required for RX Link Module
vw_setup(2000); // Bits per sec
vw_set_rx_pin(3); // receive on pin 3
vw_rx_start(); // Start the receiver
}
void loop()
{
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
if (vw_get_message(buf, &buflen)) // check to see if anything has been received
{
int i;
// Message with a good checksum received.
for (i = 0; i < buflen; i++)
{
Serial.print(buf[i]); // the received data is stored in buffer
}
Serial.println("");
}
}
Finally, I have a sample sketch from the Twitter library which is also working fine....on its own.
#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[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// substitute an address on your own network here
byte ip[] = { 192, 168, 2, 250 };
// Your Token to Tweet (get it from http://arduino-tweet.appspot.com/)
Twitter twitter("YOUR-TOKEN-HERE");
// Message to post
char msg[] = "Hello, World! I'm Arduino!";
void setup()
{
delay(1000);
Ethernet.begin(mac, ip);
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()
{
}
I already know about the Oauth token and the duplicate Twitter message 'bug' and have resolved those issues.
My problem is that I can't seem to stitch the Twitter example together with the VirtualWire example in such a way that the resulting program will pick up the transmitted message and send it to Twitter.
Can anyone help? MUCH thanks if you can.