So, if I replace sensorValue by result, it should work? Do I need to change anything else, as i didn’t declare “result” as a variable before? How do you know this, it is from the Google doc code?
Particle.variable("sensorValue", sensorValue);
// or the oldfashioned way
Particle.variable("sensorValue", &sensorValue, INT);
but don't mix and match.
Nope, sprintf() copies a string, constructed according to rules provided via the format string, into a character array/C string.
Type casting on the other hand is interpreting a variable of one type as if it was of another type, but it's still the same memory location as the original variable.
I read through them and did some modifications to my code but I still have the same issue. Particle.variable is still “undefined”. Any specific advice to solving the bug?
Considering you’ve apparently changed quite a few things, we need to see all the code. The URL in that error message has an ‘undefined’ in there, indication that the ‘variable’ wasn’t defined.
Without trying to haggle Google into this just yet, see and try if you can request a variable through the browser. What URL do you require for that?
YESS!! It works thank you! Changed “variable” to the name of my variable and it worked However, now I get another Error: 408, “Timed out”. After 30 seconds the Photon restarts itself stopping the data flow. I’ve reduce variable names to 9 characters and less, and still same thing happens. How do I fix this? What should I do?
Also, this is my Google Script:
var device_ID = '3e0020000247353138383138';
var access_token = '3ade0e61444cd9d8572143b5fff92158e7b096ed';
var variables = [''];
var checkLatestData = true;
var refreshTime = 1000;
// Select the active spreadsheet
var sheet = SpreadsheetApp.getActiveSheet();
// Function to create a header
function showHeader() {
// Clear the first row
sheet.deleteRow(1);
// At first add the timestamp
sheet.getRange(1,1).setValue('Timestamp');
// Loop through all the variables and add the names of them
for (var i = 0; i < variables.length; i++) {
sheet.getRange(1,2 + i).setValue(variables[i]);
};
if(checkLatestData) {
sheet.getRange(1, 3 + variables.length).setValue('Timestamp');
// Loop through all the variables and add the names of them
for (var i = 0; i < variables.length; i++) {
sheet.getRange(1,4 + variables.length).setValue(variables[i]);
};
}
collectData();
}
// Function to show the latest data
function showLatestData(timestamp, results) {
// At first add the timestamp
sheet.getRange(2,3 + variables.length).setValue(timestamp);
// Loop through all the variables and add the names of them
for (var i = 0; i < results.length; i++) {
sheet.getRange(2,4 + variables.length).setValue(results[i]);
};
}
// Function to get and parse the JSON variable from the spark API
function getResponse(variable){
var JSONResponse = UrlFetchApp.fetch("https://api.spark.io/v1/devices/" + device_ID + "/" + "sensorV" + "?access_token=" + access_token);
var response = JSON.parse(JSONResponse.getContentText());
return response.result;
}
function collectData() {
// Declare a result array
var timestamp = new Date();
var results = [timestamp];
var resultsWithKey = [];
// Loop through the array with variables and get their content
for (var i = 0; i < variables.length; i++) {
// Push the results in an array
results.push(getResponse(variables[i]));
resultsWithKey[variables[i]] = getResponse(variables[i]);
};
if(checkLatestData) showLatestData(timestamp, results)
// Add the responses to the spreadsheet
sheet.appendRow(results);
Utilities.sleep(refreshTime)
collectData();
}
And this is my new code (only difference is shorter names):
I can't see anything that would cause that issue in your Photon code.
Does this also happen when you don't run your google script?
But just for the sake of fighting superstition you could alter this
char sensorStr[16]; // give it some more space
// and
void loop() {
static uint32_t ms;
sensorV = analogRead(sensor1);
sprintf(sensorStr, "%d", sensorV);
if (millis() - ms > 1000)
{
ms = millis();
Particle.publish("Sensor", sensorStr, PRIVATE);
}
}
I've just seen your edit.
I was actually addressing this issue first
The issue seems to be independent of if I run google script or not. I.e. Issue happens when not running google script by checking dashboard.
With using your code and adding 2 lines at the beginning of it, issue continues :
int sensor1 = A1;
int sensorV = 0;
One thing to note, not sure if it’s normal. I’m getting values of 800-1150 when reading an analog port (A1), when un-plugged (reading air), in both cases. Also, I’m powering my Photon by usb.
Yes, that is normal. This is what we call a floating pin. Very, very simplified, what you are reading are minimal charge imbalances in the circtuitry and the surrounding media (e.g. air).
I'll flash your code to one of my devices and see what it does.
Updata: @benlandry5, I see what you mean now. Although my Photon never falls into green blinking, it does from time to time a brief handshake with the cloud (quick cyan blinking).
But this seems to go away whan I increase the delay between two publishes from 1000 to 1100.
It would also be advisable to not update the string variable that frequent.
So how does your device behave with this?
void loop() {
static uint32_t ms;
if (millis() - ms > 1100)
{
ms = millis();
sensorV = analogRead(sensor1);
sprintf(sensorStr, "%d", sensorV);
Particle.publish("Sensor", sensorStr, PRIVATE);
}
}