New to particle electron but interested in sending fixed amount of data at a fixed interval to a server(or even particles server).
Seems to be easy program but having some trouble and maybe someone has good resources where to start from.
I have came up with something like this:
SYSTEM_MODE(MANUAL); /*do not autoconnect to particle cloud*/
STARTUP(cellular_credentials_set("APN", "", "", NULL));
/*my APN has no username and no password. adjust accordingly*/
/*TCP configuration*/
TCPClient client;
String server = "194.204.x.xx"; /*your server IP address or domain name*/
void setup() {
Serial.begin(9600);
Cellular.on();
Cellular.connect();
while(!Cellular.ready()); //wait until condition is true
Serial.println("Connected to cellular network. ...");
Serial.println("Connecting to server...");
if (client.connect(server, 8080))
{
Serial.println("Connected to server");
}
Serial.println("Delay for 5sec");
delay(5000);
Serial.println("Send to server");
int val = 100;
client.write(val);
client.flush();
client.stop();
Cellular.off();
Serial.println("Cellular link disconnected.");
}
void loop() {
// nothing happens after setup
}
But how could I make it send a fixed amount ow packet, doesn’t matter what the content is.
Do you mean the actual TCP packet size or a fixed payload size?
What size are you thinking of?
If you want fixed size packets, you may be out of luck since packet splitting may occure.
When talking about the playload, you can use client.write(buffer, sizeof(buffer)) where buffer is your fixed size buffer.
IIRC, on Electrons the maximum that can be transferred in a single packet is 512 byte.
Yes I read that. I'm not worried about sending it in chunks.
The main goal would be that the device would just send whatever data in on session, not in one packet.
Still can't figure out the code thought
SYSTEM_MODE(MANUAL); /*do not autoconnect to particle cloud*/
STARTUP(cellular_credentials_set("APN", "", "", NULL));
/*my APN has no username and no password. adjust accordingly*/
/*TCP configuration*/
TCPClient client;
String server = "194.204.x.xx"; /*your server IP address or domain name*/
void setup() {
Serial.begin(9600);
Cellular.on();
Cellular.connect();
while(!Cellular.ready());
//waitFor(Cellular.ready());
Serial.println("Connected to cellular network. ...");
Serial.println("Connecting to server...");
if (client.connect(server, 8080))
{
Serial.println("Connected to server");
}
Serial.println("Delay for 5sec");
delay(5000);
uint16_t buffer[10240]; // 10KB buffer
client.write(buffer, sizeof(buffer));
client.flush();
client.stop();
Cellular.off();
Serial.println("Cellular link disconnected.");
}
void loop() {
// nothing happens after setup
}
You don't want that 10KB buffer to be a local variable since these would be put on the stack which is not big enough for that.
You'd either have a global buffer or just a pointer and dynamically allocate the buffer in setup().
That's a not very specific statement, is it? What do you not understand? What do you want it to do?
What do you expect from the code you have written, what does it do and how does that not match up?
But seeing that you define the buffer, never put any data into it but still try to write it to your TCPClient raises the question what your programming background is irrespective of platform.