String.toCharArray

Support forum for the mightyohm.com Geiger Counter.
http://mightyohm.com/geiger
Post Reply
ronym
Posts: 1
Joined: Wed Apr 25, 2012 3:47 am

String.toCharArray

Post by ronym »

I don't know why I did not start here!? I've been posting this question on C++ and Arduino forums and have had not kind responses.
I am trying to create a Geiger counter that posts a Tweet using an Arduino and ethernet shield. (eventually to use WiFi shield)

The MightyOhm geiger counter has an awesome serial port that supplies all the details needed. A SerialEvent sketch reads and can println the String with no issue. http://www.arduino.cc/en/Tutorial/SerialEvent

I use the Tweet library for Arduino http://arduino-tweet.appspot.com/
That also works perfectly alone to post information.

The issue: I am trying to create a 'String.toCharArray' with the serial data from the geiger counter that can be (twitter.post(charArray)) posted to twitter on a set interval.
I believe it's the 'String.toCharArray' where my problem is. it just won't put the String into anything I can use?
also, there may be a way to use a buffer like this?

Code: Select all

char buffer[140] = {0};
any help would be great!

all the code:

Code: Select all

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

// from http://arduino-tweet.appspot.com/
// The includion of EthernetDNS is not needed in Arduino IDE 1.0 or later.
// Please uncomment below in Arduino IDE 0022 or earlier.
//#include <EthernetDNS.h>

// Ethernet Shield Settings
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

// If you don't specify the IP address, DHCP is used(only in Arduino 1.0 or later).
byte ip[] = { 192, 168, 1, 66 };

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

//from SerialEvent
const String g("geiger");
String inputString = "g";         // a string to hold incoming data
  char charArray[80];
  String:g:toCharArray(charArray, 80);
  boolean stringComplete = false;  // whether the string is complete

void setup()
{
  delay(1000);
  Ethernet.begin(mac, ip);
  // Ethernet.begin(mac);
  Serial.begin(9600);
//  String::toCharArray()  //testing
  inputString.reserve(200); //(200
//  inputString.toCharArray(g, 80);
  if (twitter.post(charArray))
  {
  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() {
  // print the string when a newline arrives:
  if (stringComplete) {
    Serial.println(inputString); 
    // clear the string:
    inputString = "";
    stringComplete = false;
  }
}

/*
  SerialEvent occurs whenever a new data comes in the
 hardware serial RX.  This routine is run between each
 time loop() runs, so using delay inside loop can delay
 response.  Multiple bytes of data may be available.
 */
void serialEvent() {
  while (Serial.available()) {
    // get the new byte:
    char inChar = (char)Serial.read(); 
    // add it to the inputString:
    inputString += inChar;
 delay (10000);
    // if the incoming character is a newline, set a flag
    // so the main loop can do something about it:
    if (inChar == '\n') {
      stringComplete = true;
    } 
  }
}
Post Reply