read all channel data with thingspeak.h | ThingSpeak API | 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
read all channel data with thingspeak.h
No permission to create posts
October 10, 2019
8:31 pm
Avatar
EputraP
New Member
Members
Forum Posts: 1
Member Since:
October 10, 2019
sp_UserOfflineSmall Offline

please help me, i am new in thingspeak Smile
I use ESP 32 and DHT 11.
When the code is run, it can run normally and get the value from the channel. but the value obtained is always same. Even though the value on the channel is different.

#include "ThingSpeak.h"
#include "WiFi.h"

const char ssid[] = "Aww"; // your network SSID (name)
const char pass[] = "12345678"; // your network password
WiFiClient client;

unsigned long counterChannelNumber = 00000; // Channel ID
const char * myCounterReadAPIKey = "V27W8VX5IWxxxxx"; // Read API Key
const int FieldNumber1 = 1; // The field you wish to read
const int FieldNumber2 = 2; // The field you wish to read
//-------------------------------//
int n = 1;

void setup()
{
Serial.begin(115200);
delay(10);
Serial.println("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
ThingSpeak.begin(client);
}

void loop()
{
int statusCode = 0;

long temp = ThingSpeak.readLongField(counterChannelNumber, FieldNumber1, myCounterReadAPIKey);
statusCode = ThingSpeak.getLastReadStatus();
if (statusCode == 200)
{
Serial.print(n);
Serial.print(") Temperature: ");
Serial.print(temp);
}
else
{
Serial.println("Unable to read channel / No internet connection");
}
delay(100);

long humidity = ThingSpeak.readLongField(counterChannelNumber, FieldNumber2, myCounterReadAPIKey);
statusCode = ThingSpeak.getLastReadStatus();
if (statusCode == 200)
{
Serial.print(" || Humidity: ");
Serial.println(humidity);
}
else
{
Serial.println("Unable to read channel / No internet connection");
}
delay(100);

n = n+1;
}

October 11, 2019
12:32 am
Avatar
teatree
New Member
Members
Forum Posts: 4
Member Since:
October 9, 2019
sp_UserOfflineSmall Offline

Try putting the wifi connect lines into the loop rather than setup.

Looks like your current code is making only a single connection, hence the static values.

You should also probably increase delay(100) at end of loop to 1000 or more.

October 11, 2019
9:18 am
Avatar
cstapels
Moderator
Members


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

I agree with teatree on increasing the delay. Hopefully you don't even need to read the value every second.
If you look at the example code that comes with the library, the connection is in the loop, but I'm pretty sure you shouldn't need to reconnect for each read, only if your wifi connection is really bad and it drops.

Are you updating all the fields in the channel? If you only update field 6, for example, fields one and two would get null.
Let us know if reducing the read frequency helps, then we can try other things.

FYI, MATLAB Answers is now the preferred location for online help regarding ThingSpeak topics.

https://www.mathworks.com/matlabcentral/answers/?product_base_code

Forum Timezone: America/New_York

Most Users Ever Online: 166

Currently Online:
15 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:

titan83, Svetoslav, abubakar, shindepavan551@gmail.com, fet61-fakohse, Mario Marquez

Moderators: cstapels: 460

Administrators: Hans: 405, lee: 457