MATLAB Answers

First implementation of ThingSpeak, not every write is registered, pls look at my code

3 views (last 30 days)
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();
}
}

  0 Comments

Sign in to comment.

Accepted Answer

Christopher Stapels
Christopher Stapels on 12 Oct 2020
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.

  1 Comment

Stigh Aarstein
Stigh Aarstein on 12 Oct 2020
Thank you :) I tested by upgrading the account to HOME and the issues was gone, meaning you are perfectly correct.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!