I have been trying to get UNO with HLK-RM04 to connect to Xively but somehow nothing is received at Xively. Looks like I may be doing something wrong in setting up the UNO as a web-client.
I have set up the HLK-RM04 as a network client, pointing the serial port to api.xively.com port 8081 (also tried port 80). I tried the following sketch but nothing shows up at xively.
/*
Web Client to Xively via Serial of HLK-RM04 of temperature and humidity
*/
#include <stdlib.h>
#include <dht11.h>
dht11 DHT11;
#define DHT11PIN 8
#define APIKEY "d8tmQjGkRFKJZKZxa3b1CHfuyKWsa2EnldO7jYaj0aylH1o4"// replace your xively api key here
#define FEEDID 1924070958 // replace your feed ID
#define USERAGENT "Test" // user agent is the project name
void setup()
{
Serial.begin(115200); //to match wifi modue
Serial.println("Setting up Sensor to publish to Xively");
}
boolean currentLineIsBlank = false;
char c; //To collect serial inputs
String s = ""; //Complete serial input
void loop()
{
if (Serial.available()>0)
{
//Serial.println("1st loop");
while (Serial.available()>0)
{
//Serial.println("2nd loop");
sendData();
/* c = Serial.read();
s += (String)c;
if (c =='|')
{
//for debugging if you want to send OK manually??
sendData();
//following is also send back to client but not show on client unless Serial.print("Content-Length: xxx") is long enough
Serial.println();
Serial.println("MANAck---------------");
Serial.print(s);
Serial.println("---------------------");
s=""; //clear for next request
}
if (c == '\n' && currentLineIsBlank)
{
sendData();
//following is also send back to client but not show on client unless Serial.print("Content-Length: xxx") is long enough
Serial.println();
Serial.println("AUTOAck---------------");
Serial.print(s);
Serial.println("---------------------");
s =""; //clear for next request
break;
}
//wait for blank line, crude but it works
if (c == '\n')
{
currentLineIsBlank = true; // you're starting a new line
}
else if (c != '\r')
{
currentLineIsBlank = false; // you've gotten a character on the current line
}
*/
}
}
delay(1); // delay in between reads for stability
}
void sendData()
{
//Get humidity and temperature info//
int chk = DHT11.read(DHT11PIN);
/*
Serial.print("Read sensor: ");
switch (chk)
{
case DHTLIB_OK:
Serial.println("OK");
break;
case DHTLIB_ERROR_CHECKSUM:
Serial.println("Checksum error");
break;
case DHTLIB_ERROR_TIMEOUT:
Serial.println("Time out error");
break;
default:
Serial.println("Unknown error");
break;
}
*/
char buffer1 [6];
char buffer2 [6];
//Gathering Data points for sending
String sr = "";
sr += "Humidity,";
sr += dtostrf(DHT11.humidity, 4, 2, buffer1);
sr += "Temperature,";
sr += dtostrf(DHT11.temperature, 4, 2, buffer2);
// send the HTTP PUT request:
Serial.print("PUT /v2/feeds/");
Serial.print(FEEDID);
Serial.println(".csv");
Serial.println("Host: api.xively.com");
Serial.print("X-ApiKey: ");
Serial.println(APIKEY);
// Serial.print("User-Agent: ");
// Serial.println(USERAGENT);
Serial.print("Content-Length: ");
Serial.println(sr.length());
// last pieces of the HTTP PUT request:
Serial.println("Content-Type: text/csv");
Serial.println("Connection: close");
Serial.println();
// here's the actual content of the PUT request:
Serial.println(sr);
delay(1000);
}
Can anyone help? Have been searching for a web-client example for HLK-RM04 but could not find any so far... Most are examples of HLK-RM04 as web-server which I have managed to get it to work. I have skipped using
chunlinhan's library presently to keep memory usage low on the UNO.
Thanks!
Thanks