Data Usage is too high

Hi guys

I have a problem: I want to post data on the website wunderground.com
There you can send your weather data. So here is my code:

# include "cellular_hal.h"
STARTUP(cellular_credentials_set("gprs.swisscom.ch", "", "", NULL));

char server [] = "weatherstation.wunderground.com";  
char WEBPAGE [] = "GET /weatherstation/updateweatherstation.php?";
char ID [] = "xxxxxx";
char PASSWORD [] = "xxxxxx";

TCPClient client;
SYSTEM_MODE(SEMI_AUTOMATIC);

void setup()
{
  Serial.begin(115200);
  Cellular.connect();
  delay(1000);
 
}

void loop(){  
  float tempf =  29;
  float humidity = 83;
  float inches = 28.31;
  //check sensor data
  Serial.println("+++++++++++++++++++++++++");
  Serial.print("tempf= ");
  Serial.print(tempf);
  Serial.println(" *F");
  Serial.print("humidity= ");
  Serial.print(humidity);
  Serial.println("%");
  Serial.print("&baromin=");
  Serial.println(inches);

  client.connect(server, 80);
  //Send data to Weather Underground
  Serial.print("connecting to ");
  Serial.println(server);
    client.print(WEBPAGE); 
    client.print("ID=");
    client.print(ID);
    client.print("&PASSWORD=");
    client.print(PASSWORD);
    client.print("&dateutc=");
    client.print("now");    
    client.print("&tempf=");
    client.print(tempf);
    client.print("&humidity=");
    client.print(humidity);
    client.print("&baromin=");
  client.print(inches);
    client.println();
    delay(60000); 
    client.stop();  
}

So the theorie is that this TCP use around 200 bytes per message. So I send one message per minutes:200 bytes x 60 x 24 x 30 = 8 640 000 bytes = 8.64 Mb. But in the first three days it has already used over 5 Mb. So why the Electron is using so much data and how I can reduce it?

Best regards

Mod edit (@harrisonhjones): Formatting

Additional Information: I use a 3rd party sim.

What if you use a webhook? This would decrease the data usage a lot.

But I think it isn’t the TCP connection which uses so much data. I think that the particle sends some other information. So how I can turn off all other data which the particle sends?

Since you’re not using it, you could probably disconnect the cloud to make sure no data gets ‘lost’ on that end. Keep in mind there’s some overhead to just keeping the connection alive though. Doing nothing in code will probably still use up data, since you need to send keep-alive messages to the cellular tower to keep the connection alive.

So I think I have already disconnected my connection with the cloud with: SYSTEM_MODE(SEMI_AUTOMATIC);
Or is there a better method?

Hmm, I’m not really with you there. Since you wrap each single client.print() statement in its own TCP packet with all the overhead (possible retries, ack, …) you’ll definetly see more data up and down stream than your pure payload.

Try putting all your data in one string and only client.print() once.

1 Like

But how I can do this? Because the values change every loop.

You build up a string with sprintf() on each visit to loop().
http://www.cplusplus.com/reference/cstdio/sprintf/

But I have also some information which doesn’t change (for example the password and the id)

@electronweather Here is an example of how I created a string with various data types,

char Org[] = "My_ORGANIZATION";
char Disp[] = "Particle Photon";
char Locn[] = "Fishers, IN";

const char* cityLocation = "Fishers"; //City for my Photon
const char* stateLocation = "IN"; // State for my Photon

int SOC = 95;
float Voltage = 12.80;
float Current = 5.25;
float TTG = 35.4;
float Temperature = 81.3;
float SolarVin = 24.2;
float SolarVmp = 19.2;
float SolarW = 56.2;


// The on-board LED
int led = D7;

void setup() {
  pinMode(led, OUTPUT);
}
void loop() {




  char payload[255];
  // Turn the LED ON
 digitalWrite(led, HIGH);

  snprintf(payload, sizeof(payload), "{ \"s\": %d, \"v\": %.2f, \"i\": %.2f, \"t\": %.2f,\"f\": %.2f, \"s\": %.2f, \"m\": %.2f, \"w\": %.2f,\"l\":\"%s\" }", SOC, Voltage, Current, TTG, Temperature, SolarVin, SolarVmp, SolarW, Locn);
   //Serial.println(payload);
  //   Spark.publish("AzureElectron", payload, NO_ACK);   //The NO_ACK is currently only working with RC 0.6.0 rc1 firmware. This is just a test. For Electron
   Particle.publish("Azure_IOT_Almost", payload, PRIVATE);


  // Turn the LED off
  digitalWrite(led, LOW);
  delay(10000);
}

Could you give me the string which you can see on your server?

Not sure what you mean by that?

I think that you’re sending the data to a server. So could you send me a piture of the data which your server receives.

I’m sending that data to the Particle Cloud via a Particle.publish command webhook.

@ScruffR May be able to help guide you in how to send that string via client.print()

I still dont know how I should do it… :frowning:

I’d think the reference to sprintf() should have answered that too :wink:

But I can overcome my lazyness and type it out :sunglasses:

  char msg[128];
  snprintf(msg, sizeof(msg)
          , "ID=%s&PASSWORD=%s&dateutc=now&tempf=%.1f&humidity=%.1f&baromin=%.1f"
          ,  ID,   PASSWORD,               tempf,     humidity,     inches
          );
  Serial.println(msg);
  client.println(msg); 
2 Likes

Thank you very much :wink:

What does the .1f mean?

The format string directive “%.1f” means print a double precision number with one place after the decimal. So pi=3.14158 becomes “3.1”.

Google search for printf directives for more info.

3 Likes