About thingspeak read and write delay problem | Arduino | Forum

Avatar

Please consider registering
Guest

sp_LogInOut Log In sp_Registration Register

Register | Lost password?
Advanced Search

— Forum Scope —






— Match —





— Forum Options —





Minimum search word length is 3 characters - maximum search word length is 84 characters

sp_Feed sp_TopicIcon
About thingspeak read and write delay problem
No permission to create posts
July 3, 2018
6:03 am
Avatar
pantani
New Member
Members
Forum Posts: 2
Member Since:
May 18, 2018
sp_UserOfflineSmall Offline

Hello,
I have a question about the delay of reading or writing on THINGSPEAK.
The code is about turning on the LED by reading the value on THINGSPEAK. After 10 seconds, the LED is turned off, but it takes about 5 seconds to read the value on the thingspeak, and then the value on the thingspeak is also rewritten also took 5 seconds.
Is this delay normal? Because the official indication is that the transmission time is 1 second.
Here is my code
********************************
#include
#include
#include

//Replace your wifi credentials here

const char* ssid = "xxxxxxxxxxxxxxxx";
const char* password = "xxxxxxxxxxxxxxxxxx";

//change your channel number here
unsigned long myChannelNumber =xxxxxxxxxxxx;
const char * myWriteAPIKey = "xxxxxxxxxxxxxxxxxx";

WiFiClient client;

void setup() {
Serial.begin(115200);
delay(100);
pinMode (D1, OUTPUT);
digitalWrite(D1, LOW);

// We start by connecting to a WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
Serial.print("Netmask: ");
Serial.println(WiFi.subnetMask());
Serial.print("Gateway: ");
Serial.println(WiFi.gatewayIP());
ThingSpeak.begin(client);
}
void loop() {
//get the last data of the fields
bool led_1 = ThingSpeak.readFloatField(myChannelNumber,1,myWriteAPIKey);

Serial.print("led1=");
Serial.println(led_1);

if(led_1 == 1){
digitalWrite(D1,HIGH);

Serial.println("D1 is On..!");
delay(10000); //delay 10sec
ThingSpeak.writeField(myChannelNumber, 1, 0,myWriteAPIKey);
}
if(led_1==0){
digitalWrite(D1,LOW);
}
}

July 3, 2018
8:57 am
Avatar
cstapels
Moderator
Members


Moderators
Forum Posts: 861
Member Since:
March 7, 2017
sp_UserOfflineSmall Offline

If you are using a free user account, you are limited to updates only every 15 seconds. Your code would violate that limit every other time through the loop.
Also, you have a mandated delay in the loop before the channel read is performed. See the examples in the ThingSpeak documentation for a more efficient solution. For example,
https://www.mathworks.com/help/thingspeak/rapid-prototyping-with-thingspeak.html
in section d, you can see how the time is checked (millis() )each time through the loop and then a post happens only when the appropriate time has elapsed.
You can put the read request in a loop with a smaller timeout. Then you will have faster response for your reads and the writes wont be too fast.
The connection delays will depend on your local environment more than ThingSpeak response time. 5 seconds is longer than I would expect, assuming you are not violating the update rate. But the 5 seconds or longer makes sense with the code above.

Another, solution is to use MQTT subscribe. The performance will likely be a good deal faster since the broker will push updates to the device every time the channel is updated. I have acheived 100-200 ms with MQTT subscribe on a device (faster with a desktop subscription program)

Here is particle photon code for subscribe, it is similar to arduino:
https://www.mathworks.com/help/thingspeak/use-particle-photon-client-to-subscribe-to-channel-updates.html

Here is publish code for srduino, you can use the same connection methods and then use the subscribe parts from the photon example.
https://www.mathworks.com/help/thingspeak/use-arduino-client-to-publish-to-a-channel.html

and here is the MQTT doc:
https://www.mathworks.com/help/thingspeak/mqtt-api.html

July 4, 2018
4:19 am
Avatar
pantani
New Member
Members
Forum Posts: 2
Member Since:
May 18, 2018
sp_UserOfflineSmall Offline

Thanks for cstapels reply.
I am using home license , so the update limit is 1 second. Now I try to use millis() to modify and measure the time of reading time interval .The time is still about 5~6 seconds.
As you said, my code will cause a long delay. How do I change the code to reduce this delay?

Below is the modified code,

#include
#include
#include

unsigned long deltaInterval;

unsigned long time1;
unsigned long time2;

//Replace your wifi credentials here
const char* ssid = "xxxxxx";
const char* password = "xxxxxxx";

//change your channel number here
unsigned long myChannelNumber =xxxxxxx;
const char * myWriteAPIKey = "xxxxxxxxxx";
WiFiClient client;

void setup() {
Serial.begin(115200);
delay(100);
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}

ThingSpeak.begin(client);
}
void loop() {
time1=millis();
bool led_1 = ThingSpeak.readFloatField(myChannelNumber,1,myWriteAPIKey);
time2=millis();
deltaInterval=time2-time1;
Serial.print("deltaInterval=");
Serial. println(deltaInterval);
Serial.print("led1=");
Serial.println(led_1);
}
****************Result**************************
deltaInterval=5724
led1=0
deltaInterval=5948
led1=0
deltaInterval=6040
led1=0
deltaInterval=5810
led1=0
deltaInterval=5866
led1=1
deltaInterval=5826
led1=1
deltaInterval=5948
.
.
.
**********************************************

July 6, 2018
3:34 pm
Avatar
cstapels
Moderator
Members


Moderators
Forum Posts: 861
Member Since:
March 7, 2017
sp_UserOfflineSmall Offline

Have a look at the examples I listed above. They show a way to make a more efficient loop using the timer to check the elapsed time. Did you see the code in section d for the proximity sensor?

Forum Timezone: America/New_York

Most Users Ever Online: 166

Currently Online:
13 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

rw950431: 272

Vinod: 240

piajola: 95

turgo: 70

vespapierre: 63

Adarsh_Murthy: 62

Member Stats:

Guest Posters: 1

Members: 8665

Moderators: 1

Admins: 2

Forum Stats:

Groups: 3

Forums: 14

Topics: 1600

Posts: 5760

Newest Members:

qkpham, SANGAMNATH B, swatiiot, kalyanigaware16@gmail.com, Romi, Drakan01

Moderators: cstapels: 460

Administrators: Hans: 405, lee: 457