Code not working which runs well on arduino?

Hi again, not really satisfying to day!

Extrem unstable to flash code, hoping you are on working on that!

here is a code which comes from my current arduino projects, but I can’t get it running on a spark core

so it would be helpfully to know more about limitations and when we can expect the local cloud and a none web based IDE :wink:

@zach , please take a look: If i have to rewrite some code, please tell me :smiley:

int led=D7;
int errled=D0;
bool ledison=false;


String logString;

unsigned long lastConnectionTime = 0;          // last time you connected to the server, in milliseconds
bool lastConnected = false;                 // state of the connection last time through the main loop
const unsigned long postingInterval = 10*1000;  // delay between updates, in milliseconds

TCPClient client;

char server[] = "87.139.63.138";    // address for couchdb server (NOT using DNS)

char key[]= "xxxxxxxxxxxxxx";

void setup() {
     Serial.begin(19200);
     pinMode(led,OUTPUT);
     digitalWrite(led,HIGH);

}

void loop() {

 //debug:

  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

  // if there's no net connection, but there was one last time
  // through the loop, then stop the client:
  if (!client.connected() && lastConnected) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
  }

  // if you're not connected, and ten seconds have passed since
  // your last connection, then connect again and send data:
  if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
    sendData(getData());
  }
  // store the state of the connection for next time through
  // the loop:
  lastConnected = client.connected();


}


void sendData(String logString ){


  int lng = logString.length() +1;
  char logRecord[lng]; 
  logString.toCharArray(logRecord, lng);
  Serial.println(logRecord);

  Serial.print("connecting...");
  Serial.println( server );
  //Serial.print(" port: ");

  // if you get a connection, report back via serial:
  if (client.connect(server, 5984)) {
    Serial.println("doing the post...");


    client.print("POST ");
    client.print("/emon/ "); // couchdatabase
    client.println("HTTP/1.0");
    client.print("Authorization: ");
    client.println(key);
    client.println("Content-Type: application/json");
    client.println("Host: home.dewaard.de"); // ??
    client.print("Content-Length: ");
    client.println(strlen(logRecord));
    client.println();
    client.println(logRecord);
    client.println("User-Agent: sparkyOne");
    client.println("Connection: close");
    client.println();
        // note the time that the connection was made:
    lastConnectionTime = millis();
  } 
  else {
    // you didn't get a connection to the server:
    Serial.println("connection failed");
    Serial.println("disconnecting.");
    client.stop();
  }


}



String getData() {
  
  //example only get data later ...
  long tmp=125;
  long prs=9806;
  long alt=2123;
  logString = "{\"type\":";
  logString += "\"logRecord\",";
  logString += "\"pressure\":";
  logString += prs;
  logString +=",";

  logString += "\"temperature\":";


  logString += tmp;
  logString +=",";

  logString += "\"altitude\":";


  logString += alt;
  logString +=",";
          logString += "\"distance\":";
          logString += "\"300m\"";
          logString += "}";
          return logString;
   }

OK, thank you for this tip, didn’t know …

where does Serial goto? What happend now, is that the core can’t be seen by my mac :frowning:

because i run the same (nearly) code on my arduino, I will remove the debug printings and use two LED’s instead …

my Serveraddress, is that a local address from view of my core or is it from ‘outside’, so the view is from out the spark-cloud?

thx for helping again

Pitt

??? The serial port which is enumerated when the Spark is plugged in is on Serial. Serial1 is the TX/RX pins.

http://docs.spark.io/#/firmware/communication

Serial: This channel communicates through the USB port and when connected to a computer, will show up as a virtual COM port.

Serial1: This channel is available via the Core’s TX and RX pins. To use these pins to communicate with your personal computer, you will need an additional USB-to-serial adapter. To use them to communicate with an external TTL serial device, connect the TX pin to your device’s RX pin, the RX to your device’s TX pin, and the ground of your Core to your device’s ground.

Dave O

You’re calling the wrong implementation of connect.

connect exists for:
int TCPClient::connect(IPAddress ip, uint16_t port)
int TCPClient::connect(const char* host, uint16_t port)

try:
const IPAddress server(87,139,63,138); // address for couchdb server (NOT using DNS)

1 Like

u are very much correct. Sorry I mixed them up. Serial is connected to the USB. I will remove my wrong answer to not cause more confusion

Hi kobaan,

thx for your tip, now it works as a charm with IPAddress, with ‘char* host’ it’s not very reliable …

thx again :smiley:

1 Like