how to put a variable on the link

Hello to all forum,

 

I have the following line of code:  client.println("GET /emoncms3/api/post.json?apikey=MyAPI&json={power:256} HTTP/1.0");

From what I know, the link above allows you to send data ... but how can I get the value of a variable? (not be always send the value 256)



I tried: client.println("GET /emoncms3/api/post.json?apikey=MyAPI&json={power:"variable"} HTTP/1.0"); 

Instead of sending 256, I replace it with "variable" that contains the value to send.

But without success, anyone can help me?

Since already thank you very much.

mharizanov's picture

Re: how to put a variable on the link

The code you use shows you are not using a NaNode, rather an Arduino with Ethernet shield, right?

 

Try something like this

 

Ethernet.begin(mac, ip, gw, subnet);

Client client(server, 80);

Serial.println();

Serial.println("Initiates connection…");

Serial.println("Connecting…");

delay(1000); //This one keeps it from hanging



if (client.connect()) {

Serial.println("Connected!");

client.print("GET http://xxxx/PHPFile.php?level=");

client.print(level);

client.println(" HTTP/1.1");

client.println("Host: www.xxxx");

client.println();

                           }



else

{

Serial.println("Connection failed");

}

//}

 //stop client

 client.stop();

 while(client.status() != 0)

{

  delay(5);

}

tipler's picture

Re: how to put a variable on the link

Hello mharizanov

I could not use your code, because I do not know what I should get in subnet.

I've tried, but without success.

I did

byte mac[] = {  0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };

byte ip[] = { 192, 168, 1, 144   }; 
byte gw[] = {192,168,1,1};
byte subnet = ????
 
Ethernet.begin(mac, ip, gw, subnet);
 
(...)


(And also changed: )

(...)

client.print("GET http://vis.openenergymonitor.org/PHPFile.php?level=");

 
client.print(level);
 
client.println(" HTTP/1.1");
 
client.println("Host: www.vis.openenergymonitor.org");
 
(...)
tipler's picture

Re: how to put a variable on the link

I have the following code:

#include <SPI.h>

#include <Ethernet.h>
 
byte mac[] = {  0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
char serverName[] = "vis.openenergymonitor.org";
 
EthernetClient client;
 
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 int postingInterval = 5000;  // delay between updates to Pachube.com
 
void setup() {
  Serial.begin(9600);
 
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    while(true);
  }
  delay(1000);
}
 
void loop()
{
  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();
  }
  // 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() {
  // if there's a successful connection:
  if (client.connect(serverName, 80)) {
    Serial.println("connecting...");
    client.println("GET /emoncms3/api/post.json?apikey=MyAPI&json={power:252.4} HTTP/1.0");
    client.println("Host: vis.openenergymonitor.org");      
    client.println(); 
    lastConnectionTime = millis();
  } else { Serial.println("connection failed"); }
}
 
 
 
Thanks very much to Trystan.
 
It works in my case, but wanted to send the value of a variable in place of "252.4", but I'm not getting ...

I tried to put between "" a variable, but without success!

Any idea to solve this problem?

Already thanks for any hint.

sidbin's picture

Re: how to put a variable on the link

I try to use this code to upload data to emoncms3:
https://github.com/openenergymonitor/WIZnet-Ethernet-Shield-emoncms-example
without success.
connection failed
:-(

glyn.hudson's picture

Re: how to put a variable on the link

Hi Sidbin,

We have been having problems with that example, it has been removed from Github. We don't have an official Arduino Ethernet shield in which to test code. If you have a working demo of an official wiznet chip Ardino Etherent shield posting to emoncms please could you post up the code on the forum. 

Cheers, 

jose_iee's picture

Re: how to put a variable on the link

 

I'm using these lines of code to do the client printing process on my arduino ethernet shield and it's working pretty good. My variables are i1, i2 and i3.
 
Try it
 
      if (client.connect()) {
        client.print("GET http://www.mysite.com/api/post?apikey=myEmoncmsApikey&json={");
        client.print("i1:");
        client.print(i1);
        client.print(",i2:");
        client.print(i2);
        client.print(",i3:");
        client.print(i3);
        client.print("}");
        client.println();
        client.stop();
      }

Comment viewing options

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