posting to emoncms.org using IP address

Hi, i have a couple of standard emonTx's running, but now i am trying to use Arduino+wifi to send data directly to emoncms.org. I am using a CC3000 wifi shield with an Adafruit library, and it works for Xively. But when i post to emoncms.org i get a "408 Request Time-out". So i tried my json URL in my browser and noticed that it works fine when i use the hostname (which the Adafruit library does not allow me to do in the sketch), but not when i use the translated IP address:

http://emoncms.org/input/post.json?apikey=d66xxx76a8&node=10&csv=622,281 >>WORKS (returns 'ok')

http://80.243.190.58/input/post.json?apikey=d66xxx76a8&node=10&csv=622,281 >>DOES NOT WORK (returns '404 not found')

This IP address is looked up by a library function. I tried the IP address mentioned here http://openenergymonitor.org/emon/node/3696 but same error.

Any ideas what i am doing wrong? Thanks in advance.

 

pb66's picture

Re: posting to emoncms.org using IP address

tomtobback's picture

Re: posting to emoncms.org using IP address

Thanks Paul, indeed that solves the issue in the browser.

Now next issue is that when i add this to my sketch, it still returns the '408 Request Time-out' so there must be something wrong in my code. I haven't found any examples of Adafruit's CC3000 library posting to emoncms.org so would be great if somebody could help. The format of the json seems correct becauseit works when i copy it from the Serial output into the browser. Just no clue why the GET request is not received on the server, causing the time-out.

<code>

String data = "";
 
  data = data + "/emoncms/input/post.json?" + "apikey=" + apikey + "&node=10" + "&csv=" + String(humidity) + "," + String(temperature) + "\0";  

  Serial.println("data:");
  Serial.print(data);
  Serial.println();
 
  // Send request
  Adafruit_CC3000_Client client = cc3000.connectTCP(ip, 80);
  if (client.connected()) {
      Serial.println("connection successfull, sending data.. ");
    // Make a HTTP request:
    client.println("GET " + data + " HTTP/1.1");
    client.println("Host: www.emoncms.org");
    client.println("Connection: close");
    client.println();
    Serial.println("GET request sent");
  }
  else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed, will try again");
  }
  Serial.println(client.connected());
 
  Serial.println(F("-------------------------------------"));
  // if there are incoming bytes available from the server, read them and print them:
  while (client.available()) {
    char c = client.read();
    Serial.print(c);     // for debugging
  }
  client.close();
 
  Serial.println(F("-------------------------------------"));
 
  Serial.println(F("\n\nDisconnecting"));
  cc3000.disconnect();

</code>

pb66's picture

Re: posting to emoncms.org using IP address

It looks like your sketch is using www.emoncms,org AND /emoncms, that probably won't work, Have you tried replacing www.emoncms.org with the IP ?

client.println("Host: 80.243.190.58");

or maybe even just dropping the www. (without the /emoncms)

 

Paul

tomtobback's picture

Re: posting to emoncms.org using IP address

Thanks Paul, i have tried the options but still getting the '408 Request Time-out' so it seems the server does not even receive the HTTP request. Below is my Serial Monitor output, with the exact HTTP request, maybe somebody sees what's wrong. Not a lack of memory it seems.

----Serial Monitor------------

FreeRam: 1200

Initializing CC3000...
Connected to WLAN!
Request DHCP
emoncms.org -> 80.243.190.58
FreeRam: 1083
+++++++ HTTP request ++++++++++++++++++
GET /input/post.json?apikey=d66xxxx876a8&node=10&csv=571,281 HTTP/1.1
Host: www.emoncms.org
Connection: close

+++++++++++++++++++++++++++
FreeRam: 843
TCP connection to emoncms.org successfull, sending above HTTP request..
GET request sent
843
1
FreeRam: 843
--------- HTTP reply  ----------------------------
HTTP/1.1 408 Request Time-out
Date: Wed, 17 Sep 2014 01:42:05 GMT
Server: Apache/2.2.22 (Ubuntu)
Vary: Accept-Encoding
Content-Length: 325
Connection: close
Content-Type: text/html; charset=iso-8859-1

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>408 Request Time-out</title>
</head><body>
<h1>Request Time-out</h1>
<p>Server timeout waiting for the HTTP request from the client.</p>
<hr>
<address>Apache/2.2.22 (Ubuntu) Server at h80-243-190-58.host.redstation.co.uk Port 80</address>
</body></html>
-------------------------------------

Disconnecting from WLAN

tonypascal's picture

Re: posting to emoncms.org using IP address

Hello

I am also trying to make the CC3000 wifi shield post data. Could you please let me know where you found the code and provide it to me ?

Regards

Romain

Hayder's picture

Re: posting to emoncms.org using IP address

Hello 

I have same problem, Any help please.

B.R

Hayder

 

squiza's picture

Re: posting to emoncms.org using IP address

I would also love to find the answer to this problem.

i am trying to use an Arduino with hard wired ethernet to log optical pulses directly to the website. I am trying to avoid having to use an RF arrangement and dedicated base station.

snoopy's picture

Re: posting to emoncms.org using IP address

Anything new with this? What shield wifi or ethernet are you using to push data to website? 

 

 

lebritish's picture

Re: posting to emoncms.org using IP address

Hello to all,

Thanks to tomtobback I manage to post data to emoncms trough a CC3000 wifi shield.

You have to set a static IP to your shield first because DHCP is sooooooooooooooo long and rubbish.

However I am not using it anymore because the range of a CC3000 wifi shield is ridiculously short.

Anyway here is the code:

--------------------------

 

/*************************************************
  This is an example for the Adafruit CC3000 Wifi Breakout & Shield

  Designed specifically to work with the Adafruit WiFi products:
  --> https://www.adafruit.com/products/1469

  Adafruit invests time and resources providing this open source code,
  please support Adafruit and open-source hardware by purchasing
  products from Adafruit!

  Written by Limor Fried & Kevin Townsend for Adafruit Industries.  
  BSD license, all text above must be included in any redistribution
 **************************************************/
 
 /*
This example does a test of the TCP client capability:
  * Initialization
  * Optional: SSID scan
  * AP connection
  * DHCP printout
  * DNS lookup
  * Optional: Ping
  * Connect to website and print out webpage contents
  * Disconnect
SmartConfig is still beta and kind of works but is not fully vetted!
It might not work on all networks!
*/
#include <Adafruit_CC3000.h>
#include <ccspi.h>
#include <SPI.h>
#include <string.h>
#include "utility/debug.h"
#include <TimerOne.h>          // for software watchdog

#include "EmonLib.h"             // Include Emon Library

 

// These are the interrupt and control pins
#define ADAFRUIT_CC3000_IRQ   3  // MUST be an interrupt pin!
// These can be any two pins
#define ADAFRUIT_CC3000_VBAT  5
#define ADAFRUIT_CC3000_CS    10
// Use hardware SPI for the remaining pins
// On an UNO, SCK = 13, MISO = 12, and MOSI = 11
Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT,
                                         SPI_CLOCK_DIVIDER); // you can change this clock speed

#define WLAN_SSID       "yournetwork "        // cannot be longer than 32 characters!
#define WLAN_PASS       "yourpassword"
// const char WLAN_PASS[] = {0xAA, 0xAA, 0xCC, 0xBB, 0xCC, 0x00}; for WEP encryption ONLY

// Security can be WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA or WLAN_SEC_WPA2
//#define WLAN_SECURITY   WLAN_SEC_WEP
#define WLAN_SECURITY   WLAN_SEC_WPA2

 

#define IDLE_TIMEOUT_MS  3000      // Amount of time to wait (in milliseconds) with no data
                                   // received before closing the connection.  If you know the server
                                   // you're accessing is quick to respond, you can reduce this value.

// What page to grab!
#define WEBSITE      "www.emoncms.org"

uint32_t ip;

EnergyMonitor emon1;             // Create an instance

unsigned long starttime;
unsigned long sampletime_ms = 30000;   //time between sendind data minimum 30s ;
 
//last time the WDT was ACKd by the application
unsigned long lastUpdate=0;
 //time, in ms, after which a reset should be triggered
unsigned long timeout=60000;

void setup(void)
{
  Serial.begin(9600);  
 
  emon1.voltage(1, 248, 1.77);  // Voltage: input pin, calibration, phase_shift
  emon1.current(0, 28.87);       // Current: input pin, calibration.
 
  lastUpdate=millis();        // start watchdog
  //Timer1.initialize(1000000); //1 second pulses
  Timer1.attachInterrupt(longWDT); //code to execute
  starttime = millis();               //get the current time;
 
}

void loop(void) {
 
 
   
   lastUpdate=millis();  // tell watchdog all is fine

   if ((millis()-starttime) > sampletime_ms)               //if the sample time == 30s
    {
        
        emon1.calcVI(20,2000);         // Calculate all. No.of half wavelengths (crossings), time-out
        emon1.serialprint();           // Print out all variables (realpower, apparent power, Vrms, Irms, power factor)
        
        float realPower       = emon1.realPower;        //extract Real Power into variable
        float apparentPower   = emon1.apparentPower;    //extract Apparent Power into variable
        float powerFactor     = emon1.powerFactor;      //extract Power Factor into Variable
        float supplyVoltage   = emon1.Vrms;             //extract Vrms into Variable
        float Irms            = emon1.Irms;             //extract Irms into Variable
        
    // at the end of one reading cycle, connect and post to website
    
  Serial.println(F("starting CC3000"));
  Serial.print("interval: "); Serial.println(starttime/1000);
  Serial.print("Free RAM: "); Serial.println(getFreeRam(), DEC);
  /* Initialise the module */
  if (!cc3000.begin())
  {
    Serial.println(F("Couldn't start CC3000! Check your wiring?"));
    while(1);
  }
 
 
 
  Serial.print(F("Connecting to ")); Serial.println(WLAN_SSID);
  if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) {
    Serial.println(F("Failed!"));
    while(1);
  }
  Serial.println(F("Connected!"));
 
   /* Optional: Set a static IP address instead of using DHCP.
     Note that the setStaticIPAddress function will save its state
     in the CC3000's internal non-volatile memory and the details
     will be used the next time the CC3000 connects to a network.
     This means you only need to call the function once and the
     CC3000 will remember the connection details.  To switch back
     to using DHCP, call the setDHCP() function (again only needs
     to be called once).
  */
  /* //Your_network parameters
  uint32_t ipAddress = cc3000.IP2U32(192, 168, 2, 199);
  uint32_t netMask = cc3000.IP2U32(255, 255, 255, 0);
  uint32_t defaultGateway = cc3000.IP2U32(192, 168, 2, 1);
  uint32_t dns = cc3000.IP2U32(192, 168, 1, 254);
  if (!cc3000.setStaticIPAddress(ipAddress, netMask, defaultGateway, dns)) {
    Serial.println(F("Failed to set static IP!"));
    while(1);
  }
  */
 
  /* Optional: Revert back from static IP addres to use DHCP.
     See note for setStaticIPAddress above, this only needs to be
     called once and will be remembered afterwards by the CC3000.
  */
  /*
  if (!cc3000.setDHCP()) {
    Serial.println(F("Failed to set DHCP!"));
    while(1);
  }
   */

 
 
  /* Wait for DHCP to complete
  Serial.println(F("Requesting DHCP"));
  while (!cc3000.checkDHCP())
  {
    delay(100); // ToDo: Insert a DHCP timeout!
  }  
  */
  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(1000);
  }
  cc3000.printIPdotsRev(ip);
  /* Try connecting to the website.
     Note: HTTP/1.1 protocol is used to keep the server from closing the connection before all data is read.
  */
  Adafruit_CC3000_Client www = cc3000.connectTCP(ip, 80);
  if (www.connected()) {
    www.fastrprint(F("GET "));
    www.fastrprint("/input/post.json?node=0&apikey=yourAPIkey&csv=");
    www.print(powerFactor);
    www.print(",");
    www.print(apparentPower);
    www.print(",");
    www.print(realPower);
    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("\nConnection failed"));    
  }

  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();
  Serial.println(F("-----------------------------------"));
 
  /* You need to make sure to clean up after yourself or the CC3000 can freak out */
  /* the next time your try to connect ... */
  Serial.println(F("Disconnecting from CC3000"));
  cc3000.disconnect();
 
 
  starttime = millis();
  Serial.println(F("\n\nStarting a new sensor measurement"));
    }
}

/************************************************************************/
/*!
   @brief Begins an SSID scan and prints out all the visible networks
*/
/************************************************************************/
/*
void listSSIDResults(void)
{
  uint32_t index;
  uint8_t valid, rssi, sec;
  char ssidname[33];

  if (!cc3000.startSSIDscan(&index)) {
    Serial.println(F("SSID scan failed!"));
    return;
  }

  Serial.print(F("Networks found: ")); Serial.println(index);
  Serial.println(F("================================================"));

  while (index) {
    index--;

    valid = cc3000.getNextSSID(&rssi, &sec, ssidname);
    
    Serial.print(F("SSID Name    : ")); Serial.print(ssidname);
    Serial.println();
    Serial.print(F("RSSI         : "));
    Serial.println(rssi);
    Serial.print(F("Security Mode: "));
    Serial.println(sec);
    Serial.println();
  }
  Serial.println(F("================================================"));

  cc3000.stopSSIDscan();
}
*/
/************************************************************************/
/*!
   @brief Tries to read the IP address and other connection details
*/
/************************************************************************/
/*

bool displayConnectionDetails(void)
{
  uint32_t ipAddress, netmask, gateway, dhcpserv, dnsserv;
 
  if(!cc3000.getIPAddress(&ipAddress, &netmask, &gateway, &dhcpserv, &dnsserv))
  {
    Serial.println(F("Unable to retrieve the IP Address!\r\n"));
    return false;
  }
  else
  {
    Serial.print(F("\nIP Addr: ")); cc3000.printIPdotsRev(ipAddress);
    Serial.print(F("\nNetmask: ")); cc3000.printIPdotsRev(netmask);
    Serial.print(F("\nGateway: ")); cc3000.printIPdotsRev(gateway);
    Serial.print(F("\nDHCPsrv: ")); cc3000.printIPdotsRev(dhcpserv);
    Serial.print(F("\nDNSserv: ")); cc3000.printIPdotsRev(dnsserv);
    Serial.println();
    return true;
  }
}
*/

void (*resetFunc)(void) = 0;
 
void longWDT(void)
{
  if((millis()-lastUpdate) > timeout)
  {
    //enable interrupts so serial can work
    sei();
 
    //detach Timer1 interrupt so that if processing goes long, WDT isn't re-triggered
    Timer1.detachInterrupt();
 
    //flush, as Serial is buffered; and on hitting reset that buffer is cleared
    Serial.println("WDT triggered");
    Serial.flush();
 
    //call to bootloader / code at address 0
    resetFunc();
  }
}

--------------------

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.