MATLAB: First implementation of ThingSpeak, not every write is registered, pls look at the code

setfieldThingSpeak

Hi,
This is my first post here 🙂
I've implemented ThingSpeak in my code and see that some of my channel fields are not updated frequently. My code runs where one field updates based on motion (counting and uploading the count per count) and the others update per 30 seconds. As some are not updated as they should, I would like to know if I'm using the implementation correctly. Could anybody please look over and verify my usage?
void loop() {
/************************************************************************************************************
*** MOTION DELAY TURN OF LED/MOTOR DELAY ***
***********************************************************************************************************/
if (webCheckedTrue != "1") {
if (delayMotionRunning && ((millis() - delayMotionStart) > (timeMotionSeconds*1000))) {
static uint32_t hourTimer = millis();
if (millis() - hourTimer >= millisPerHour) {
hourTimer = millis();
motionCounter = 0;
}
motionCounter = motionCounter + 1;
digitalWrite(ledMotor, LOW);
digitalWrite(onMotor, LOW);
delayMotionRunning = false;
ThingSpeak.setField(3, motionCounter);
if (client.connect(thingspeakServer,80)) {
int x = ThingSpeak.writeFields(myChannelNumber, apiKey);
}
client.stop();
}
}
else {
if (delayMotionRunning && ((millis() - delayMotionStart) > (timeMotionSeconds*1000))) {
Serial.println("Motion IDLE...");
static uint32_t hourTimer = millis();
if (millis() - hourTimer >= millisPerHour) {
hourTimer = millis();
motionCounter = 0;
}
motionCounter = motionCounter + 1;
digitalWrite(onMotor, LOW);
delayMotionRunning = false;
ThingSpeak.setField(3, motionCounter);
Serial.println(motionCounter);
if (client.connect(thingspeakServer,80)) {
int x = ThingSpeak.writeFields(myChannelNumber, apiKey);
}
client.stop();
}
}
/************************************************************************************************************
*** AMOUNT DELAY TO CHCK BATTERY STATUS ***
***********************************************************************************************************/
if (voltDelay.justFinished()) {
getVolt();
voltDelay.repeat();
int memHeap = (ESP.getFreeHeap());
ThingSpeak.setField(1, voltage_V);
ThingSpeak.setField(4, memHeap);
if (client.connect(thingspeakServer,80)) {
int x = ThingSpeak.writeFields(myChannelNumber, apiKey);
}
client.stop();
}
/************************************************************************************************************
*** CHECK WEIGHT IF BELOW THRESHOLD_AMOUNT AND REPORT ***
***********************************************************************************************************/
if (amountDelay.justFinished()) {
scale.power_up();
float weight0 = scale.get_units(10);
ThingSpeak.setField(2, weight0);
if (weight0 > THRESHOLD_AMOUNT) {
sendSMTP();
}
scale.power_down();
amountDelay.repeat();
if (client.connect(thingspeakServer,80)) {
int x = ThingSpeak.writeFields(myChannelNumber, apiKey);
}
client.stop();
}
}

Best Answer

The free license is limited to one update per 15 seconds in a given channel. If you are updating different fields in the same channel asynchronously, there will be collisions where the write frequency requested is faster than the allowed frequency, so your request will be rejected. I reccomend using multiple channels for devices or processes.writing asynchronously.
Related Question