I have very little knowledge of programming ,thank god at last i am able to upload arduino sketch at arduino and able to see value at serial monitor but i am not able to upload sensor data neither at emoncms.org nor at my local emoncms server so it my very humble request you all please please guide me why i am not able to upload data at emoncms .My eyes are really gona tired to watch my sensor value at emoncms.here is my test sketch.please help me !!!!
*/
#include <SPI.h>
#include <Ethernet.h>
// These constants won't change
const int lightSensorPin = A0;
const int threshold = 850;
// humidity and temperature
float temperature = 0, humidity = 0;
// the follow variables are long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long pulseCount = 0;
// used to measure power.
unsigned long pulseTime,lastTime;
// power and energy
double power = 0;
double elapsedkWh;
// number of readings we made since the last packet sent :
byte readings = 0;
// assign a MAC address for the ethernet controller.
// fill in your address here:
byte mac[] = {
0x90, 0xA2, 0xDA, 0x01, 0x66, 0xD6};
// fill in an available IP address on your network here,
// for auto configuration:
IPAddress ip(192, 168, 0, 64);
IPAddress subnet(255, 255, 255, 0);
IPAddress gw (192,168,0,1);
// initialize the library instance:
EthernetClient client;
char server[] = "emoncms.org"; //emoncms URL
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
boolean lastConnected = false; // state of the connection last time through the main loop
const unsigned long postingInterval = 4*1000; // delay between updates, in milliseconds
void setup() {
// start serial port:
Serial.begin(9600);
// give the ethernet module and DHT22 sensor time to boot up:
delay(1000);
// Display a welcome message
Serial.println("emoncms client starting...");
// attempt a DHCP connection:
Serial.println("Attempting to get an IP address using DHCP:");
if (!Ethernet.begin(mac)) {
// if DHCP fails, start with a hard-coded address:
Serial.println("failed to get an IP address using DHCP, trying manually");
Ethernet.begin(mac, ip);
}
// print the Ethernet board/shield's IP address:
Serial.print("My IP address: ");
Serial.println(Ethernet.localIP());
}
void loop() {
// if the meter flash, increment the counter
if (analogRead(lightSensorPin) > threshold) {
while (analogRead(lightSensorPin) > threshold) {}
//used to measure time between pulses.
lastTime = pulseTime;
pulseTime = micros();
//pulseCounter
pulseCount++;
// we don't want to miss a flash during sending
readings++;
// calculate power
power = 3600000000.0 / (pulseTime - lastTime);
// find kwh elapsed
elapsedkWh = (1.0 * pulseCount)/1000; //multiply by 1000 to pulses per wh to kwh convert wh to kwh
//Print the values
Serial.print("Power : ");
Serial.print(power,2);
Serial.print("W, ");
Serial.print(elapsedkWh,3);
Serial.println("kWh");
}
// if there's incoming data from the net connection.
// send it out the serial port. This is for debugging
// purposes only:
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 at least two seconds have
// passed sinceyour last connection, then connect again and
// send data:
if(!client.connected() && (millis() - lastConnectionTime > postingInterval) && power >= 0 && readings >= 2) {
sendData(power);
readings = 0;
}
// store the state of the connection for next time through
// the loop:
lastConnected = client.connected();
}
// this method makes a HTTP connection to the server:
void sendData(float power) {
// if there's a successful connection:
if (client.connect(server, 80)) {
Serial.println("Connecting...");
// send the HTTP PUT request:
client.print("GET /api/post?apikey=<6834396c9feb5********7d973b2d74f>&json={power}");
client.print(":");
client.print(power);
client.println("} HTTP/1.1");
client.println("Host: emoncms.org");
client.println("User-Agent: Arduino-ethernet");
client.println("Connection: close");
client.println();
// note the time that the connection was made:
lastConnectionTime = millis();
}
else {
// if you couldn't make a connection:
Serial.println("Connection failed");
Serial.println("Disconnecting...");
client.stop();
}
}
[APIKey obscured - Moderator (RW)]
Re: Not able to upload data to emoncms, please help
I am waiting for somebody reply please ..
Re: Not able to upload data to emoncms, please help
I can immediately see two things that are wrong.
1. You have <......> around your APIkey (and I have obscured it, because with it ANYONE can send data to your account.
2. The json format is wrong - a '}' in the wrong place. Look at Input API Help in emoncms, the correct format is:
json={power:200}
What you are sending is: ...?apikey=<6834396c9feb5********7d973b2d74f>&json={power}:200}
What you should be sending is
... ?apikey=6834396c9feb5********7d973b2d74f&json={power:200}
There might be other problems too that I have not seen.
Re: Not able to upload data to emoncms, please help
Dear Robert thankyou for your reply i have checked and correct it ..still not working .my arduino is getting DHCP ip from Router very well, can you suggest me what other problem might be.
client.print("GET/http://emoncms.org/input/post.json?json={power:200}&apikey=xxxxxx6c9feb5718e21637d973bxxxxx");
thankyou
sanju
Re: Not able to upload data to emoncms, please help
It is ok now ...can you please suggest my what other problem might be here..
client.print("GET/http://emoncms.org/input/post.json?json={power:200}&apikey=xxxxxxx6c9feb5718e21637d973bxxxxxx");
Thanks in advace
sanju
Re: Not able to upload data to emoncms, please help
Dear Robert thankyou for your reply it is ok now ..
client.print("GET/http://emoncms.org/input/post.json?json={power:200}&apikey=**************************");
my arduino is getting DHCP very well from router what other problem might be...please can you suggest me..
Regards
sanju
Re: Not able to upload data to emoncms, please help
Please have some patience. You are not the only person on this website, and we cannot answer your questions immediately. And please READ the notice at the top right of your screen that says "Read this before posting".
Re: Not able to upload data to emoncms, please help
Have you searched the forums for a similar problem, or for someone who is successfully using a sketch derived from the same place as yours?
You could compare yours with this one: http://openenergymonitor.org/emon/node/11499 The problem there was a simple copy-and-paste error that affected the content that was sent. How the data was being sent was correct, so it is a good one to look at.
Re: Not able to upload data to emoncms, please help
HI Robert
Good morning.
actually i haven't seen my post at forum .that's why i try again and again that's why post get double .hope you understand.
thankyou for the web link i will compare mine sketch with this ,let's hope this time i get sucuess.
thankyou
sanju .