Search code examples
http-postwifiibm-cloudarduino-uno

How do use the AdaFruit CC3000 library to send a simple HTTP Post of my Uno JSON sensor data to Bluemix (node red)?


I am clueless and after a week of racking my brains. Background: I am out of memory on a perfectly working Arduino Uno weather station and flawless internet connectivity using sample wifi sketches with CC3000 wifi shield. MQTT samples work great as well, but! I can't fit the MQTT client so need bare bones. I am thinking HTTP POST is bare bones based on some advice received in forums.

I just can't figure out how to simply send a HTTP POST using ONLY the AdaFruit CC3000 library to my Bluemix Node Red app and display my 300 or so character JSON formatted string I want to send every 5 minutes. I just want those strings to display in the node red debug area. Absolutely clueless!!!

This HTTP GET code (CC3000 webclient sample) out to AdaFruit by the way works perfectly and shown below so you know what I have to work with. I don't understand what credentials to use either relative to Bluemix. Really confusing as I am new to much of this being a hardware product development engineer most of my life. Loving the learning experience though!

Essentially want to convert that GET to a POST and successfully pull in my string on Bluemix using Node Red. Please HELPPP!!!!

Here is the basic functional GET code on my Uno using AdaFruit site for test.

#define WEBSITE      "www.adafruit.com"
#define WEBPAGE      "/testwifi/index.html" 
...
ip = 0;
  // Try looking up the website's IP address
  Serial.print(WEBSITE); Serial.print(F(" -> "));
  while (ip == 0) {
    if (! cc3000.getHostByName(WEBSITE, &ip)) {
      Serial.println(F("Couldn't resolve!"));
    }
    delay(500);
  }
...
Adafruit_CC3000_Client www = cc3000.connectTCP(ip, 80);
  if (www.connected()) {
    www.fastrprint(F("GET "));
    www.fastrprint(WEBPAGE);
    www.fastrprint(F(" HTTP/1.1\r\n"));
    www.fastrprint(F("Host: ")); www.fastrprint(WEBSITE); www.fastrprint(F("\r\n"));
    www.fastrprint(F("\r\n"));
    www.println();
  } else {
    Serial.println(F("Connection failed"));    
    return;
  }

  Serial.println(F("-------------------------------------"));

  /* Read data until either the connection is closed, or the idle timeout is reached. */ 
  unsigned long lastRead = millis();
  while (www.connected() && (millis() - lastRead < IDLE_TIMEOUT_MS)) {
    while (www.available()) {
      char c = www.read();
      Serial.print(c);
      lastRead = millis();
    }
  }
  www.close();

Solution

  • The HTTP spec for a POST is not hugely different than for a GET.

    You will need to add a couple of extra headers, such as Content-Type and Content-Length.

    But something like this should be a good starting point

    ...
    Adafruit_CC3000_Client www = cc3000.connectTCP(ip, 80);
      if (www.connected()) {
        www.fastrprint(F("POST "));
        www.fastrprint(WEBPAGE);
        www.fastrprint(F(" HTTP/1.1\r\n"));
        www.fastrprint(F("Host: ")); 
        www.fastrprint(WEBSITE);
        www.fastrprint(F("\r\n"));
        www.fastprint(F("Content-Type: application/json\r\n"));
        www.fastprint(F("Content-Length: "));
        www.fastprint(payload.length));
        www.fastrprint(F("\r\n"));
        www.fastrprint(F("\r\n"));
        www.fastprint(payload);
        www.println();
    ...