[SOLVED] Changing a variable in particle over the net

Hi,
I am having problems with compiling my particle code. The goal is to recieve a text from input field in my view to update my location variable. Think it as assigning the particle device to a new location.

the code:

char location[64] = "room_23";
void setup() {
Particle.function("change_location", updateLocation)
...}

int updateLocation(String command){
  command.toCharArray(location,64);
  return 0;
}

i get this when i compile

i am suppose to publish the location to my payload, so if the location gets edited and posted from the web, i think the next time it publishes it will get a new name… Please help? :smiley:

Can you post your entire code?

#include "mq7.h"
#include "mhz19.h"
#include "pir.h"
#include "dht.h"
#include "light_sensor.h"

MQ7Sensor MQ7;
MHZ19Sensor MHZ19;
PIRSensor PIR;
DHT11Sensor DHT_11;
LIGHTSensor LIGHT;

//Delay for production:
unsigned long sendDelay = 2*60*1000;
char location[64] = "room_23";
//Delay for debugging:
//unsigned long sendDelay = 10000;
void setup() {
    Particle.function("change_location", updateLocation);
    Serial.begin(9600);
    MQ7.init();
    MHZ19.init();
    PIR.init();
    DHT_11.init();
    LIGHT.init();
    Serial.print("Preheating");
    delay(100000);

}

void loop() {

generatePayload();
delay(sendDelay);
}

int updateLocation(String command){
  command.toCharArray(location,64);
  return 0;
}

void generatePayload(){
  //INITIALIZING SENSOR VALUES
  int lightValue = LIGHT.getSensorValue();
  int CO2 = MHZ19.getSensorValue();
  float humidity = DHT_11.getHumidity();
  float celcius = DHT_11.getCelcius();
  bool motion;

  Serial.print("light value= ");
  Serial.println(lightValue);
  Serial.println("=================================");
  // OUTPUT for CO2
  Serial.print("CO2 = ");
  Serial.println(CO2);
  Serial.println("=================================");

// OUTPUT for Motion
  if(PIR.calibrated())
  {
    PIR.readSensorValue();
    PIR.outputMotion();
    Serial.println("=================================");
    motion = PIR.getMotionStatus();
    Serial.println(motion);

  }else{
    Serial.println("Calibrating...");
    Serial.println("=================================");
  }

Serial.print("Humidity: ");
Serial.println(humidity);
Serial.println("=================================");
Serial.print("Celcius: ");
Serial.println(celcius);
Serial.println("=================================");

char payload[255];
snprintf(payload, sizeof(payload),"{\"C2\": %d, \"t\": %f,\"h\": %f,\"l\": %d,\"m\":%s, \"loc\":\"%s\"}",CO2,celcius,humidity,lightValue, motion ? "true": "false", location);

//String message = String("{\"C2\" : \"" + (String)CO2 + "\", \"t\" : \"" + (String)celcius + "\", \"h\" : \"" + (String)humidity + "\", \"m\" : \"" + (String)motion + "\", \"l\" : \"" + (String)lightValue + "\", \"loc\" : \"" + (String)location + "\"}");
//Publishes to eventhub
//Particle.publish("PublishSensorData",payload);
//Publishes to iotHub
Particle.publish("sensor-hub",payload,PRIVATE);

}

How are you building? Do you have the libraries your code requires?

If you compile with particle-cli, you can get the full list of errors.

I am using the local IDE,
but when i remove

Particle.function("change_location", updateLocation);

   and

int updateLocation(String command){
  command.toCharArray(location,64);
  return 0;
}

it compiles again…

@hoaben13

Your Particle Function name is too long!

check the docs

3 Likes

Yup. What BulldogLowell said. The problem is that the function name has a maximum of 12 characters.

../wiring/inc/spark_wiring_cloud.h:188:9: error: static assertion failed: 

In Particle.function, name must be less than 12 characters


         static_assert(!IsStringLiteral(name) || sizeof(name) <= USER_FUNC_KEY_LENGTH + 1,
         ^
make[3]: *** [../build/target/user/platform-6-m/compiletest1/compiletest.o] Error 1
make[2]: *** [user] Error 2
make[1]: *** [modules/photon/user-part] Error 2
1 Like

This looks correct to me!

1 Like

You are right.

1 Like

Yes it was your code i took :slight_smile:
but the location string is now empty :o
HTML

<form class="form-group" action="https://api.particle.io/v1/devices/[device_ID]/setLoc?access_token=[acces_token]" method="POST">
        <input class="form-control" type="text" value="{{mainCtrl.location}}"/>
        <input class="btn btn-primary" type="submit" value="change room"/>
    </form>

You might want to try giving this field a name (such as adding name=“args”) to pass. I would use Javascript instead my self and there are lots of examples here in the forum about how to do that.

You shouldn’t have escaped quote marks around that final %s.

1 Like

are you sure? i added it because its messing up with the JSON because without it it is outputting:

"location": room_23

While the goal is to get

"location" : "room_23"

If not, my stream analytics will not output the data from inputstream properly (using Azure).

Edit:
i still get empty location after posting

No, I’m not sure; you may well be right. I’ve never been a big fan of JSON with all those escaped quote marks; it seems really prone to mistakes if you have to create it manually.

Hi @hoaben13

I would switch to curl at this point for debugging since you don’t know if your firmware or your HTML is causing the problem.

I’m not sure if i understand what you are saying… But the mainCtrl.location is getting the latest location name from my angular controller

Right: what is being sent to your Photon is the question here, right? So I would call your Particle.function setLoc with a known good value using curl to prove if the device is getting the correct value or not and sending back via the Particle.publish().

Another way to look at this: If you do not call setLoc, does your publish return “room_23” for the location? If you then change the location, what does the publish return?

Yes, at first it is returning “room_23”, but after call setLoc, the JSON is returning “location”: ""
I suspect that location is not set right in particle.

Would you please send me doc on how to debug with curl ? i am pretty new to this and trying my best to keep up :slight_smile:

curl is a command line tool that sends web requests similar to how a browser works but with full control over all the parts of a request. The Particle documentation has good install instructions for curl for Windows/Mac/Linux–just search for the word curl. Once installed you can call your Particle function like this (replacing the parts in <YOUR… > with your special numbers, no angle braces needed).

curl https://api.particle.io/v1/devices/<YOUR-DEVICE-ID>/setLoc -d access_token=<YOUR-ACCESS-TOKEN> -d arg="room_100"

It does not matter what “arg” is called. When you are done, the location should be set. Then you will know if the problem is in your firmware on the Photon or in your web code. I’m thinking web code but this will tell you for sure.

i have successfully installed curl and tested it,

i ran this command

curl https://api.particle.io/v1/devices?access_token=PASTE_YOUR_ACCESS_TOKEN_HERE

getting this output

but i get this error message when running your command.