Using too much data with only function and variable

My sim card paused on my last night. I am experimenting with using a web app to call particle functions to turn leds off and on and then making an API call to a particle variable to verify status. I did not think I would be incurring lots of data. The web app does continuously check the status of the variable (about every .5 seconds), but my understanding was that because I am not publishing continuously, or even posting the variable to the cloud continuously, that this would not use a lot of data. The variable is only changed when the function is called. I commented out all of the other manual functionality, just to test the cloud portion alone. In fact, the loop does nothing. Only the functions perform commands. I’ve pasted my firmware below. What have I done wrong? Why did I use up all of my data so quickly?

// This #include statement was automatically added by the Particle IDE.
#include <neopixel.h>


// // IMPORTANT: Set pixel COUNT, PIN and TYPE
#define PIXEL_COUNT 8
#define PIXEL_PIN TX
#define PIXEL_TYPE WS2812B

Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);


int gbutt1 = C0;
int gled1 = C1;
int ybutt1 = C2;
int yled1 = C3;
int rbutt1 = C4;
int rled1 = C5;
bool gbool1 = FALSE;
bool ybool1 = FALSE;
bool rbool1 = FALSE;

int gbutt2 = D0;
int gled2 = D1;
int ybutt2 = D2;
int yled2 = D3;
int rbutt2 = D4;
int rled2 = D5;
bool gbool2 = FALSE;
bool ybool2 = FALSE;
bool rbool2 = FALSE;

int gbutt3 = B0;
int gled3 = B1;
int ybutt3 = B2;
int yled3 = B3;
int rbutt3 = B4;
int rled3 = B5;
bool gbool3 = FALSE;
bool ybool3 = FALSE;
bool rbool3 = FALSE;

int gbutt4 = A0;
int gled4 = A1;
int ybutt4 = A2;
int yled4 = A3;
int rbutt4 = A4;
int rled4 = A5;
bool gbool4 = FALSE;
bool ybool4 = FALSE;
bool rbool4 = FALSE;

String stat1 = "1x";
String stat2 = "2x";
String stat3 = "3x";
String stat4 = "4x";


void setup() {
       
        pinMode(gbutt1, INPUT_PULLUP);
        pinMode(gled1, INPUT_PULLUP);
        pinMode(ybutt1, INPUT_PULLUP);
        pinMode(yled1, OUTPUT);
        pinMode(rbutt1, OUTPUT);
        pinMode(rled1, OUTPUT);
       
        
        pinMode(gbutt2, INPUT_PULLUP);
        pinMode(gled2, INPUT_PULLUP);
        pinMode(ybutt2, INPUT_PULLUP);
        pinMode(yled2, OUTPUT);
        pinMode(rbutt2, OUTPUT);
        pinMode(rled2, OUTPUT);
        
        
        pinMode(gbutt3, INPUT_PULLUP);
        pinMode(gled3, INPUT_PULLUP);
        pinMode(ybutt3, INPUT_PULLUP);
        pinMode(yled3, OUTPUT);
        pinMode(rbutt3, OUTPUT);
        pinMode(rled3, OUTPUT);
        
        
        pinMode(gbutt4, INPUT_PULLUP);
        pinMode(gled4, INPUT_PULLUP);
        pinMode(ybutt4, INPUT_PULLUP);
        pinMode(yled4, OUTPUT);
        pinMode(rbutt4, OUTPUT);
        pinMode(rled4, OUTPUT);
       
    strip.begin();
    strip.show(); // Initialize all pixels to 'off
    
    digitalWrite(gled1, LOW);
    digitalWrite(yled1, LOW);
    digitalWrite(rled1, LOW);
    digitalWrite(gled2, LOW);
    digitalWrite(yled2, LOW);
    digitalWrite(rled2, LOW);
    digitalWrite(gled3, LOW);
    digitalWrite(yled3, LOW);
    digitalWrite(rled3, LOW);
    digitalWrite(gled4, LOW);
    digitalWrite(yled4, LOW);
    digitalWrite(rled4, LOW);
    
    
    
    Particle.function("pwr1", pwr1);
    Particle.function("pwr2", pwr2);
    Particle.function("pwr3", pwr3);
    Particle.function("pwr4", pwr4);
   
    
    Particle.variable("stat1", stat1);
    Particle.variable("stat2", stat2);
    Particle.variable("stat3", stat3);
    Particle.variable("stat4", stat4);

}



void loop() {
    
//     //gbutt1
    
//     if(digitalRead(gbutt1) == 0)
//     {
//         gbool1 = !gbool1;
//         ybool1 = FALSE;
//         rbool1 = FALSE;
//         if(gbool1)
//             {
//                 digitalWrite(gled1, HIGH);
//                 digitalWrite(yled1, LOW);
//                 digitalWrite(rled1, LOW);
//                 strip.setPixelColor(0, 0,255,0);
//                 strip.setPixelColor(1, 255,255,255);
//                 strip.show();
//                 stat1 = "1g";
//             }
//             else
//             {
//                 digitalWrite(gled1, LOW);
//                 strip.setPixelColor(0, 0,0,0);
//                 strip.setPixelColor(1, 0,0,0);
//                 strip.show();
//                 // Particle.publish("1x", "",60,PRIVATE);
//                 stat1 = "1x";
//             }
        
//     }
//     while(digitalRead(gbutt1) == 0);
//     delay(50);
    
    
    
//      //ybutt1
    
//     if(digitalRead(ybutt1) == 0)
//     {
//         ybool1 = !ybool1;
//         gbool1 = FALSE;
//         rbool1 = FALSE;
//         if(ybool1)
//             {
//                 digitalWrite(gled1, LOW);
//                 digitalWrite(yled1, HIGH);
//                 digitalWrite(rled1, LOW);
//                 strip.setPixelColor(0, 255,255,0);
//                 strip.setPixelColor(1, 0,0,0);
//                 strip.show();
                
//                 // Particle.publish("1y", "",60,PRIVATE);
//                 stat1 = "1y";
//             }
//             else
//             {
//                 digitalWrite(yled1, LOW);
//                 strip.setPixelColor(0, 0,0,0);
//                 strip.setPixelColor(1, 0,0,0);
//                 strip.show();
//                 // Particle.publish("1x", "",60,PRIVATE);
//                 stat1 = "1x";
//             }
        
//     }
//     while(digitalRead(ybutt1) == 0);
//     delay(50);
    
    
//          //rbutt1
    
//     if(digitalRead(rbutt1) == 0)
//     {
//         rbool1 = !rbool1;
//         gbool1 = FALSE;
//         ybool1 = FALSE;
//         if(rbool1)
//             {
//                 digitalWrite(gled1, LOW);
//                 digitalWrite(yled1, LOW);
//                 digitalWrite(rled1, HIGH);
//                 strip.setPixelColor(0, 255,0,0);
//                 strip.setPixelColor(1, 0,0,0);
//                 strip.show();
                
//                 // Particle.publish("1r", "",60,PRIVATE);
//                 stat1 = "1r";
//             }
//             else
//             {
//                 digitalWrite(rled1, LOW);
//                 strip.setPixelColor(0, 0,0,0);
//                 strip.setPixelColor(1, 0,0,0);
//                 strip.show();
//                 // Particle.publish("1x", "",60,PRIVATE);
//                 stat1 = "1x";
//             }
        
//     }
//     while(digitalRead(rbutt1) == 0);
//     delay(50);
    
    
//      //gbutt2
    
//     if(digitalRead(gbutt2) == 0)
//     {
//         gbool2 = !gbool2;
//         ybool2 = FALSE;
//         rbool2 = FALSE;
//         if(gbool1)
//             {
//                 digitalWrite(gled2, HIGH);
//                 digitalWrite(yled2, LOW);
//                 digitalWrite(rled2, LOW);
//                 strip.setPixelColor(2, 0,255,0);
//                 strip.setPixelColor(3, 255,255,255);
//                 strip.show();
                
//                 // Particle.publish("2g", "",60,PRIVATE);
//                 stat2 = "2g";
//             }
//             else
//             {
//                 digitalWrite(gled2, LOW);
//                 strip.setPixelColor(2, 0,0,0);
//                 strip.setPixelColor(3, 0,0,0);
//                 strip.show();
//                 // Particle.publish("2x", "",60,PRIVATE);
//                 stat2 = "2x";
//             }
        
//     }
//   while(digitalRead(gbutt2) == 0);
//     delay(50);
    
    
    
//      //ybutt2
    
//     if(digitalRead(ybutt2) == 0)
//     {
//         ybool2 = !ybool2;
//         gbool2 = FALSE;
//         rbool2 = FALSE;
//         if(ybool2)
//             {
//                 digitalWrite(gled2, LOW);
//                 digitalWrite(yled2, HIGH);
//                 digitalWrite(rled2, LOW);
//                 strip.setPixelColor(2, 255,255,0);
//                 strip.setPixelColor(3, 0,0,0);
//                 strip.show();
                
//                 // Particle.publish("2y", "",60,PRIVATE);
//                 stat2 = "2y";
//             }
//             else
//             {
//                 digitalWrite(yled2, LOW);
//                 strip.setPixelColor(2, 0,0,0);
//                 strip.setPixelColor(3, 0,0,0);
//                 strip.show();
//                 // Particle.publish("2x", "",60,PRIVATE);
//                 stat2 = "2x";
//             }
        
//     }
//     while(digitalRead(ybutt2) == 0);
//     delay(50);
    
    
//          //rbutt2
    
//     if(digitalRead(rbutt2) == 0)
//     {
//         rbool2 = !rbool2;
//         gbool2 = FALSE;
//         ybool2 = FALSE;
//         if(rbool2)
//             {
//                 digitalWrite(gled2, LOW);
//                 digitalWrite(yled2, LOW);
//                 digitalWrite(rled2, HIGH);
//                 strip.setPixelColor(2, 255,0,0);
//                 strip.setPixelColor(3, 0,0,0);
//                 strip.show();
                
//                 // Particle.publish("2r", "",60,PRIVATE);
//                 stat2 = "2r";
//             }
//             else
//             {
//                 digitalWrite(rled2, LOW);
//                 strip.setPixelColor(2, 0,0,0);
//                 strip.setPixelColor(3, 0,0,0);
//                 strip.show();
//                 // Particle.publish("2x", "",60,PRIVATE);
//                 stat2 = "2x";
//             }
        
//     }
//     while(digitalRead(rbutt2) == 0);
//     delay(50);
    
//          //gbutt3
    
//     if(digitalRead(gbutt3) == 0)
//     {
//         gbool3 = !gbool3;
//         ybool3 = FALSE;
//         rbool3 = FALSE;
//         if(gbool3)
//             {
//                 digitalWrite(gled3, HIGH);
//                 digitalWrite(yled3, LOW);
//                 digitalWrite(rled3, LOW);
//                 strip.setPixelColor(4, 0,255,0);
//                 strip.setPixelColor(5, 255,255,255);
//                 strip.show();
//                 ybool3 = FALSE;
//                 rbool3 = FALSE;
//                 // Particle.publish("3g", "",60,PRIVATE);
//                 stat3 = "3g";
//             }
//             else
//             {
//                 digitalWrite(gled3, LOW);
//                 strip.setPixelColor(4, 0,0,0);
//                 strip.setPixelColor(5, 0,0,0);
//                 strip.show();
//                 // Particle.publish("3x", "",60,PRIVATE);
//                 stat3 = "3x";
//             }
        
//     }
//   while(digitalRead(gbutt3) == 0);
//     delay(50);
    
    
    
//      //ybutt3
    
//     if(digitalRead(ybutt3) == 0)
//     {
//         ybool3 = !ybool3;
//         gbool3 = FALSE;
//         rbool3 = FALSE;
//         if(ybool3)
//             {
//                 digitalWrite(gled3, LOW);
//                 digitalWrite(yled3, HIGH);
//                 digitalWrite(rled3, LOW);
//                 strip.setPixelColor(4, 255,255,0);
//                 strip.setPixelColor(5, 0,0,0);
//                 strip.show();
                
//                 // Particle.publish("3y", "",60,PRIVATE);
//                 stat3 = "3y";
//             }
//             else
//             {
//                 digitalWrite(yled3, LOW);
//                 strip.setPixelColor(4, 0,0,0);
//                 strip.setPixelColor(5, 0,0,0);
//                 strip.show();
//                 // Particle.publish("3x", "",60,PRIVATE);
//                 stat3 = "3x";
//             }
        
//     }
//     while(digitalRead(ybutt3) == 0);
//     delay(50);
    
    
//          //rbutt3
    
//     if(digitalRead(rbutt3) == 0)
//     {
//         rbool3 = !rbool3;
//         gbool3 = FALSE;
//         ybool3 = FALSE;
//         if(rbool3)
//             {
//                 digitalWrite(gled3, LOW);
//                 digitalWrite(yled3, LOW);
//                 digitalWrite(rled3, HIGH);
//                 strip.setPixelColor(4, 255,0,0);
//                 strip.setPixelColor(5, 0,0,0);
//                 strip.show();
                
//                 // Particle.publish("3r", "",60,PRIVATE);
//                 stat3 = "3r";
//             }
//             else
//             {
//                 digitalWrite(rled3, LOW);
//                 strip.setPixelColor(4, 0,0,0);
//                 strip.setPixelColor(5, 0,0,0);
//                 strip.show();
//                 // Particle.publish("3x", "",60,PRIVATE);
//                 stat3 = "3x";
//             }
        
//     }
//     while(digitalRead(rbutt3) == 0);
//     delay(50);
    
    
// //gbutt4
    
//     if(digitalRead(gbutt4) == 0)
//     {
//         gbool4 = !gbool4;
//         ybool4 = FALSE;
//         rbool4 = FALSE;
//         if(gbool4)
//             {
//                 digitalWrite(gled4, HIGH);
//                 digitalWrite(yled4, LOW);
//                 digitalWrite(rled4, LOW);
//                 strip.setPixelColor(6, 0,255,0);
//                 strip.setPixelColor(7, 255,255,255);
//                 strip.show();
                
//                 // Particle.publish("4g", "",60,PRIVATE);
//                 stat4 = "4g";
//             }
//             else
//             {
//                 digitalWrite(gled4, LOW);
//                 strip.setPixelColor(6, 0,0,0);
//                 strip.setPixelColor(7, 0,0,0);
//                 strip.show();
//                 // Particle.publish("4x", "",60,PRIVATE);
//                 stat4 = "4x";
//             }
        
//     }
//   while(digitalRead(gbutt4) == 0);
//     delay(50);
    
    
    
//      //ybutt4
    
//     if(digitalRead(ybutt4) == 0)
//     {
//         ybool4 = !ybool4;
//         gbool4 = FALSE;
//         rbool4 = FALSE;
//         if(ybool4)
//             {
//                 digitalWrite(gled4, LOW);
//                 digitalWrite(yled4, HIGH);
//                 digitalWrite(rled4, LOW);
//                 strip.setPixelColor(6, 255,255,0);
//                 strip.setPixelColor(7, 0,0,0);
//                 strip.show();
                
//                 // Particle.publish("4y", "",60,PRIVATE);
//                 stat4 = "4y";
//             }
//             else
//             {
//                 digitalWrite(yled4, LOW);
//                 strip.setPixelColor(6, 0,0,0);
//                 strip.setPixelColor(7, 0,0,0);
//                 strip.show();
//                 // Particle.publish("4x", "",60,PRIVATE);
//                 stat4 = "4x";
//             }
        
//     }
//     while(digitalRead(ybutt4) == 0);
//     delay(50);
    
    
//          //rbutt4
    
//     if(digitalRead(rbutt4) == 0)
//     {
//         rbool4 = !rbool4;
//         gbool4 = FALSE;
//         ybool4 = FALSE;
//         if(rbool4)
//             {
//                 digitalWrite(gled4, LOW);
//                 digitalWrite(yled4, LOW);
//                 digitalWrite(rled4, HIGH);
//                 strip.setPixelColor(6, 255,0,0);
//                 strip.setPixelColor(7, 0,0,0);
//                 strip.show();
                
//                 // Particle.publish("4r", "",60,PRIVATE);
//                 stat4 = "4r";
//             }
//             else
//             {
//                 digitalWrite(rled4, LOW);
//                 strip.setPixelColor(6, 0,0,0);
//                 strip.setPixelColor(7, 0,0,0);
//                 strip.show();
//                 // Particle.publish("4x", "",60,PRIVATE);
//                 stat4 = "4x";
//             }
        
//     }
//     while(digitalRead(rbutt4) == 0);
//     delay(50);
    
}






int pwr1 (String command){
    if (command == "pwr1"){
            gbool1 = !gbool1;
            ybool1 = FALSE;
            rbool1 = FALSE;
            if(gbool1)
                {
                    digitalWrite(gled1, HIGH);
                    digitalWrite(yled1, LOW);
                    digitalWrite(rled1, LOW);
                    strip.setPixelColor(0, 0,255,0);
                    strip.setPixelColor(1, 255,255,255);
                    strip.show();
                    
                    // Particle.publish("1g", "",60,PRIVATE);
                    stat1 = "1g";
                }
                else
                {
                    digitalWrite(gled1, LOW);
                    digitalWrite(yled1, LOW);
                    digitalWrite(rled1, LOW);
                    strip.setPixelColor(0, 0,0,0);
                    strip.setPixelColor(1, 0,0,0);
                    strip.show();
                    // Particle.publish("1x", "",60,PRIVATE);
                    stat1 = "1x";
                }
        }
    }
    
int pwr2 (String command){  
    if (command == "pwr2"){
        gbool2 = !gbool2;
        ybool2 = FALSE;
        rbool2 = FALSE;
        if(gbool2)
            {
                digitalWrite(gled2, HIGH);
                digitalWrite(yled2, LOW);
                digitalWrite(rled2, LOW);
                strip.setPixelColor(2, 0,255,0);
                strip.setPixelColor(3, 255,255,255);
                strip.show();
                
                // Particle.publish("2g", "",60,PRIVATE);
                stat2 = "2g";
            }
            else
            {
                digitalWrite(gled2, LOW);
                digitalWrite(yled2, LOW);
                digitalWrite(rled2, LOW);
                strip.setPixelColor(2, 0,0,0);
                strip.setPixelColor(3, 0,0,0);
                strip.show();
                // Particle.publish("2x", "",60,PRIVATE);
                stat2 = "2x";
            }
    }
}

int pwr3 (String command){ 
    if (command == "pwr3"){
        gbool3 = !gbool3;
        ybool3 = FALSE;
        rbool3 = FALSE;
        if(gbool3)
            {
                digitalWrite(gled3, HIGH);
                digitalWrite(yled3, LOW);
                digitalWrite(rled3, LOW);
                strip.setPixelColor(4, 0,255,0);
                strip.setPixelColor(5, 255,255,255);
                strip.show();
               
                // Particle.publish("3g", "",60,PRIVATE);
                stat3 = "3g";
            }
            else
            {
                digitalWrite(gled3, LOW);
                digitalWrite(yled3, LOW);
                digitalWrite(rled3, LOW);
                strip.setPixelColor(4, 0,0,0);
                strip.setPixelColor(5, 0,0,0);
                strip.show();
                // Particle.publish("3x", "",60,PRIVATE);
                stat3 = "3x";
            }
    }
}

int pwr4 (String command){
    if (command == "pwr4"){
        gbool4 = !gbool4;
        ybool4 = FALSE;
        rbool4 = FALSE;
        if(gbool4)
            {
                digitalWrite(gled4, HIGH);
                digitalWrite(yled4, LOW);
                digitalWrite(rled4, LOW);
                strip.setPixelColor(6, 0,255,0);
                strip.setPixelColor(7, 255,255,255);
                strip.show();
                
                // Particle.publish("4g", "",60,PRIVATE);
                stat4 = "4g";
            }
            else
            {
                digitalWrite(gled4, LOW);
                digitalWrite(yled4, LOW);
                digitalWrite(rled4, LOW);
                strip.setPixelColor(6, 0,0,0);
                strip.setPixelColor(7, 0,0,0);
                strip.show();
                // Particle.publish("4x", "",60,PRIVATE);
                stat4 = "4x";
            }
    }
}

When you request the value of a Particle.variable you are not requesting it from the cloud but from the device itself - the cloud only relays your request.
Particle.variable() is not pushing the value of the local variable to the cloud to be stored there for later use but exposes that local variable to be requested on demand.
So when checking the value every 500ms your device has to send the value to the cloud to be relayed to you.

That's also what the docs mean when stating this
https://docs.particle.io/reference/device-os/firmware/electron/#particle-variable-

3 Likes

Great info! I did not know this either...

1 Like

Thank you for the response. I guess I will have to change the way I am going about doing this. I suppose publishing events is the way to go. Can anyone point me to a resource for explaining how to push a published event directly to a web app? I’ve done this via the rules engine, but wanted to experiment with getting the data from the device, figuring it would improve any latency issues. I’m looking to make a web app that would be pretty responsive, making real time updates to the status on the ground. Maybe just doing through the rules engine as I have done, is the best way??

You’d want to subscribe to the SSE stream in your web app
https://docs.particle.io/reference/SDKs/javascript/#geteventstream

Thanks ScruffR,

I will take a look at the link. Forgot to mention, the way I got this to work through the rules engine was to incorporate mqtt nodes. This works great, but it is a bit complicated. You have to sign up with a mqtt broker, install their libraries into your JavaScript and figure out how to pass an array of data to your web app, parse the data etc. It’s actually works great, but I had hoped calling variables would have been less complicated, which it is. …just uses way too much data.

If anyone is interested, I can share how I’ve done that.