Having Trouble with substring with the code below. The string is coming through correctly, the comma locations are identified, but it seems to hang before extracting substrings. On the console, the last message I get is " {“data”:“6”,“ttl”:“60”,“published_at”:“2017-02-11T14:09:51.191Z”,“coreid”:“1e0026001247343339383037”,“name”:“Comma Index 2 Set”}"
I’m sort of stuck… I’ve used this same method in another script without issue, so I’m unsure of why it’s hanging now.
This is on a Photon running V.6 of the firmware.
Any help would be greatly appreciated!
/* ======================= includes ================================= */
#include "application.h"
/* ====================== Definitions =============================== */
//Basic Globals
int led = D7; // built-in tiny LED
//Warm LED Strip above window
int warm_led = D0;
int warm_led_value = 0;
//int maxFreq = analogWriteMaxFrequency(warm_led);
void setup() {
Serial.begin(9600);
// On board LED initialized as output
pinMode(led, OUTPUT);
Particle.publish("Running Setup");
//Warm LED strip above window
Spark.function("WARM_STRIP", warm_fade);
pinMode(warm_led, OUTPUT);
analogWriteResolution(warm_led, 12); // sets analogWrite resolution to 12 bits
}
void loop (){
warm_fade("1,4095,600");
Particle.publish("Warm Light test");
}
// The following function is called Using a Cloud API
// Specifically CoAP packets sent over a TCP socket encrypted through an RSA asymmetric key handshake passed off to an AES encrypted tunnel
int warm_fade(String args){
Particle.publish("Entering warm_fade", String(args));
delay(100);
Serial.print(args);
//Decipher args
int commaIndex = args.indexOf(','); //Create index of arguments separated by commas
int secondCommaIndex = args.indexOf(',', commaIndex+1);
delay(100);
Particle.publish("Comma Index 1 Set", String(commaIndex));
Particle.publish("Comma Index 2 Set", String(secondCommaIndex));
//String id_led = args.substring(0, commaIndex);
String id_led = args.substring(0, 1);
Particle.publish("Substrings Id_led", String(id_led));
//String target_level = args.substring(commaIndex+1, secondCommaIndex);
String target_level = args.substring(2, 6);
Particle.publish("Substrings target_level", String(target_level));
//String dim_time = args.substring(secondCommaIndex+1);
String dim_time = args.substring(7);
Particle.publish("Substrings dim_time", String(dim_time));
delay(100);
int id_led_int = id_led.toInt();
int target_level_int = target_level.toInt();
int dim_time_int = dim_time.toInt();
Particle.publish("Integers Set");
//setup
int dim_time_step;
int dim_percent;
if( target_level_int > 4095 ) return -1;
if( target_level_int < 0 ) return -1;
Particle.publish("Target levels Calculated");
dim_time_step = int(dim_time_int/target_level_int*1000);
dim_percent = int(target_level_int/4095*100);
//dim_distance = int(target_level_int - warm_led_value);
Particle.publish("Dim Time Calculated");
analogWrite(warm_led, warm_led_value, 1000); // ensure we're starting at target brightness
if (warm_led_value < target_level_int){ //Fade up if target is greater than current
for (warm_led_value; warm_led_value < target_level_int; warm_led_value++){
analogWrite(warm_led, warm_led_value, 1000); // 4095 represents 100%
delay(dim_time_step); // Wait for per step ms
}
Particle.publish("Warm Light brightened");
return 1;
}
if (warm_led_value > target_level_int){ //Fade down if target is less than current
for (warm_led_value; warm_led_value > target_level_int; warm_led_value--){
analogWrite(warm_led, warm_led_value, 1000); // 4095 represents 100%
delay(dim_time_step); // Wait for per step ms
}
Particle.publish("Warm Light Dimmed");
return 1;
}
Particle.publish("Warm light is already at requested value");
return 1; //Assumption is that the target brightness is equal to the current brightness
}