updating multiple fields to thingspeak | 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
updating multiple fields to thingspeak
No permission to create posts
March 12, 2016
2:05 am
Avatar
sushma

Silver
Members
Forum Posts: 5
Member Since:
February 2, 2016
sp_UserOfflineSmall Offline

hello,
actually i m using 4 fields in a channel. in that i have used first field for updating the moisture sensor data and i wann use fields 2,3,4 for updating dht11 sensor data i.e field 2 for temperature , field 3 for humidity, and field 4 for dew points. but i m not able update all the 4 fields at a time though i have multiple fields in channel setting, just the field 1 is getting updated.

March 12, 2016
7:34 am
Avatar
Hans
Natick, MA

Admin
Forum Posts: 411
Member Since:
January 18, 2011
sp_UserOfflineSmall Offline

What does your command look like?

March 23, 2016
1:42 am
Avatar
sushma

Silver
Members
Forum Posts: 5
Member Since:
February 2, 2016
sp_UserOfflineSmall Offline

Hans said

What does your command look like?

thank you.

the code is,

// This sketch uses a DHT11 sensor to report temperature, humidity and dew point data to http://www.thingspeak.com.
//
// Sketch tested with an Arduino Uno, a HanRun Ethernet shield and a DHT11 temperature and humidity sensor.
//
// See http://playground.arduino.cc/main/DHT11Lib for the origins of the temperature, humidity and dew point functions.

#include
#include
#include

// ThingSpeak Settings
char thingSpeakAddress[] = "api.thingspeak.com";
String writeAPIKey = "99E4E2IDC9Y8J59B"; // Add your Thingspeak API key here

EthernetClient client;

// Temperature sensor settings
dht DHT;
#define DHTPIN 6

const int ONE_MINUTE = 60 * 1000;

int status;
int failedConnectionAttempCounter;

//Rounds down (via intermediary integer conversion truncation)
//See : http://lordvon64.blogspot.co.uk/2012/01/simple-arduino-double-to-string.html
String dblToString(double input, int decimalPlaces)
{
if( decimalPlaces != 0)
{
String string = String((int)(input*pow(10,decimalPlaces)));

if(abs(input) 0)
{
string = "0" + string;
}
else if(input 3 )
{
Serial.println("Re-starting the ethernet connection...");
connectToInternet();
failedConnectionAttempCounter = 0;
}
}
}

March 23, 2016
12:57 pm
Avatar
wheato22

Silver
Members
Forum Posts: 8
Member Since:
February 23, 2016
sp_UserOfflineSmall Offline

sushma said

hello,
actually i m using 4 fields in a channel. in that i have used first field for updating the moisture sensor data and i wann use fields 2,3,4 for updating dht11 sensor data i.e field 2 for temperature , field 3 for humidity, and field 4 for dew points. but i m not able update all the 4 fields at a time though i have multiple fields in channel setting, just the field 1 is getting updated.

---------------------
Hi,
Here is sample code from a Kickstarter project that shows a way update multiple fields with one write. It worked for me on my water meter project.
Larry

// This #include statement was automatically added by the Particle IDE.
#include "ThingSpeak.h"

// This #include statement was automatically added by the Particle IDE.
#include "Adafruit_DHT/Adafruit_DHT.h"

// Sensor type
#define DHTTYPE DHT22 // DHT 22 (AM2302)

// DHT22 sensor pinout:
// Pin 1 (on the left): +3.3V
// Pin 2: output
// Pin 4 (on the right): GROUND
#define DHT_5V_PIN D1
#define DHT_SENSOR_PIN D2
#define DHT_GROUND_PIN D4

DHT dht(DHT_SENSOR_PIN, DHTTYPE);

/* Thingspeak */
TCPClient client;
unsigned long myChannelNumber = 89137;
const char * myWriteAPIKey = "3D23PQHQIJ3OZXNF";

void setup() {
// Connect to ThingSpeak
ThingSpeak.begin(client);

// Give power to the sensor
pinMode(DHT_5V_PIN, OUTPUT);
pinMode(DHT_GROUND_PIN, OUTPUT);
digitalWrite(DHT_5V_PIN, HIGH);
digitalWrite(DHT_GROUND_PIN, LOW);

// Wait for the sensor to stabilize
delay(1000);

// Initialize sensor
dht.begin();

// Read Sensor
double temperature = dht.getTempCelcius();
double humidity = dht.getHumidity();

// Update the 2 ThingSpeak fields with the new data
ThingSpeak.setField(1, (float)temperature);
ThingSpeak.setField(2, (float)humidity);

// Write the fields that you've set all at once.
ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);

// Give time for the message to reach ThingSpeak
delay(5000);

// Sleep for 15 minutes to save battery
System.sleep(SLEEP_MODE_DEEP, 15 * 60);
}

void loop() {
// This will run because the system is sleeping
}

March 25, 2016
5:54 am
Avatar
sushma

Silver
Members
Forum Posts: 5
Member Since:
February 2, 2016
sp_UserOfflineSmall Offline

thank you sir,
i will try it.

March 25, 2016
6:07 am
Avatar
sushma

Silver
Members
Forum Posts: 5
Member Since:
February 2, 2016
sp_UserOfflineSmall Offline

i tried the code given above but it is showing compilation error at command "Adafruit_DHT/Adafruit_dht.h"

April 14, 2019
7:24 am
Avatar
PaulEfford
New Member
Members
Forum Posts: 3
Member Since:
April 14, 2019
sp_UserOfflineSmall Offline

You can also use the following:

char post[100];
snprintf(post, sizeof(post), "field%d=%s&field%d=%s", fieldA, valueA, fieldB, valueB);
int res = ThingSpeak.writeRaw(CHANNEL_ID, post, WRITE_KEY);

April 14, 2019
7:28 am
Avatar
PaulEfford
New Member
Members
Forum Posts: 3
Member Since:
April 14, 2019
sp_UserOfflineSmall Offline

And as an example, use the following function:

bool writeMyField(unsigned int fieldA, const char *valueA, unsigned int fieldB, const char *valueB) {

if(lastWriteConnectionStamp == 0) lastWriteConnectionStamp = millis();
else if(millis() - lastWriteConnectionStamp < 1000*(MIN_WAIT + 1)) {
Serial.printf("please wait yet %ld ms...
", 1000*(MIN_WAIT + 1) - millis() + lastWriteConnectionStamp); // unsigned long = %lu, long = %ld
return false;
}
if( !client.connect(SERVER_IoT, PORT)) return false;

char post[100];
snprintf(post, sizeof(post), "field%d=%s&field%d=%s", fieldA, valueA, fieldB, valueB);
// if you have more arguments, just add them inbetween "&" like: snprintf(post, sizeof(post), "field%d=%s&field%d=%s&field%d=%s", fieldA, valueA, fieldB, valueB, fieldC, valueC);
int res = ThingSpeak.writeRaw(CHANNEL_ID, post, WRITE_KEY);
client.stop();
if(res != SUCCESS) return false;
lastWriteConnectionStamp = millis();
return true;
}

April 14, 2019
7:28 am
Avatar
PaulEfford
New Member
Members
Forum Posts: 3
Member Since:
April 14, 2019
sp_UserOfflineSmall Offline

And call it in your loop like:

void loop() {

float h1 = random(60,100);
char buff1[64];
snprintf(buff1, sizeof(buff1), "%f", h1); // "converts" float --> char*

float h2 = random(40,75);
char buff2[64];
snprintf(buff2, sizeof(buff2), "%f", h2);

int field1 = 3;
int field2 = 4;
if(writeMyField(field1, buff1, field2, buff2)) Serial.println("written = ok!");
else Serial.println("written = nok...");
delay(1*1000);
}

Hope this helps!
Paul

Forum Timezone: America/New_York

Most Users Ever Online: 166

Currently Online:
36 Guest(s)

Currently Browsing this Page:
2 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:

porosh2019, KellyBrise, sunainakhanna, flail, sathwik, samlewis02

Moderators: cstapels: 460

Administrators: Hans: 405, lee: 457