passing variables of letters and numbers?

I use an arduino mega to upload the status of lightswitches and powersockets to emoncms

but the code gets really long when you use for example this code about 15 times for different lightswitches/powersockets to update the status troughout the code

if (client.connect(webserver,80))
{
  client.print("GET /emoncms/input/post.json?apikey=xxxxxxxxxxxxxxxxxxxxxxxxxxx&json={");
  client.print("livingroom");
  client.print(";");
  client.print("1");
  client.println("} HTTP/1.1");
  client.println("Host: someserver.net");
  client.println("User-Agent: Arduino-ethernet");
  client.println("Connection: close");
  client.println();
  client.stop();
}

and then you have to reuse it everywhere for example for hall lights, beroom lights, etc.. for about 15 times for different lightswitches/powersockets

instead I want to use somegting like this:

void emon_status()
{
  if (client.connect(webserver,80))
  {
    client.print("GET /emoncms/input/post.json?apikey=xxxxxxxxxxxxxxxxxxxxxxxxxxx&json={");
    client.print(device);
    client.print(";");
    client.print(status);
    client.println("} HTTP/1.1");
    client.println("Host: someserver.net");
    client.println("User-Agent: Arduino-ethernet");
    client.println("Connection: close");
    client.println();
    client.stop();
  }
}

and then later in code whenever I need it use someting like:

device = "livingroom";
status = 1;
emon_status(device,status);

and later for example:

device = "bedroom";
status = 0;
emon_status(device,status);

but that doesn't work I don't know how to pass these 2 variables, because one variable is a name that changes...

You are missing arguments in your function definition. Try this...

void emon_status(char *device, byte status)
{
  if (client.connect(webserver,80))
  {
    client.print("GET /emoncms/input/post.json?apikey=xxxxxxxxxxxxxxxxxxxxxxxxxxx&json={");
    client.print(device);
    client.print(";");
    client.print(status);
    client.println("} HTTP/1.1");
    client.println("Host: avenant.net");
    client.println("User-Agent: Arduino-ethernet");
    client.println("Connection: close");
    client.println();
    client.stop();
  }
}

The great thing about JSON is that you could pass arrays of data(all your sensors and data) inside one array, like containers, so all you need to do is to create an array and the pass it in your JSON object.
The the emoncms you have a helper page showing you that.
See here:

http://emoncms.org/input/api

@David0Conner:

then I get the errors:

Domotica.ino:1382:21: error: ‘device’ was not declared in this scope
Domotica.ino:1383:21: error: ‘status’ was not declared in this scope

@HugoPT : I'm allready using arrays to upload temperature and thermostat data, but for single lightswitches, there is no need for an array, because you use only 1 lightswitch at a time, it wouldn't make sense to update 15 lightswitches statuses if you only switch 1 on or off

Post the entire sketch if you need help with error messages.

@Divid0Conner

I presume I need to globally declare the variable:

byte status;

but I don't know about the "device"

char device[] = "livingroom"?

If I use that code it compiles but it only puts the "1" in the emoncms in the field where the name is suposseds to go instead of the number field

@HugoPT : I'm allready using arrays to upload temperature and thermostat data, but for single lightswitches, there is no need for an array, because you use only 1 lightswitch at a time, it wouldn't make sense to update 15 lightswitches statuses if you only switch 1 on or off

In an int variable you could pass 16 status of lightswitchs or anything you want.
Passing that int to emoncms is trivial and does not put to much overhead.

char device[] = "livingroom"?
If I use that code it compiles but it only puts the "1" in the emoncms in the field where the name is suposseds to go instead of the number field

char device is just a pointer to string livingroom and it point to the first memory position which contains "l"
Maybe this way:

client.print("GET /emoncms/input/post.json?apikey=xxxxxxxxxxxxxxxxxxxxxxxxxxx&json={");
    client.print(*device);//Print what the pointer is pointing to

The arguments device and status have local scope inside the function and will hide any global variables of the same name. The values of these arguments are determined by what is passed in when the function is called. I might be able to help more if you post your code.

I used the thermoduino pro sketch code from here: Testers wanted! - Home Automation - Arduino Forum

wich is about 2100 lines of codes, and removed everything from it that I didn't need, like setting timers to activate the heating, wich i can also do from my android phone with Tasker, and then I cleaned up the code to reduce it to about 850 lines of code, and changed everything, to store data on emoncms, instead of saving it in EEPROM, because the code was doing like writes to EEPROM every 5 minutes, and I read in the documentation on this site that the EEPROM memory of an arduino only allows for 100.000 write cycles, so then basicly after a year of usage the arduino's EEPROM memmory would be destroyed, so then changed code to store stuff in emoncms instead of EEPROM and added about 750-800 lines of own code (and used code from other sketches), to basicly controll everything in my home, powerswitches, lightswicthes, send RF and Infrared signals to control stuff like amplifier, tv, lights, beamerscreen, send wake on lan to start htpc, intercept RF signal from doorbell to send message to my phone/tablet, etc...

I marked the code I added with comments: // Mijn code, and // Einde mijn code, wich translates to // My Code, and // End of my code

too long to post here about 1600 lines of code, so put it here:

http://codeviewer.org/view/code:3f74

@HugoPT: Serial.println only shows 1 letter of the word "livingroom" only the "l"

Maybe this way:

Nope. No *. Just

client.print(device);

yep then it prints "livingroom"

but the weird thing is it only puts "1" in emoncms

OMG this is really stupid LOL, found the problem...

client.print("GET /emoncms/input/post.json?apikey=xxxxxxxxxxxxxxxxxxxxxx&json={");
client.print(device);
client.print(";");   <=== stupid typo here, used ; instead of : DUH!
client.print(status);
client.println("} HTTP/1.1");

must have been staring to long at the code I guess, LOL

now it works, thanx everybody

globally declared; byte status;

and when needed:

char device[] = "livingroom";
status = 1;
emon_status(device,status);
char device[] = "bedroom";
status = 0;
emon_status(device,status);

etc...

That's how it works, presuming that's how it's supossed to work?

You can also do...

emon_status("living room",1);