uploading multiple fields to thingspeak from arduino | 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
uploading multiple fields to thingspeak from arduino
No permission to create posts
January 30, 2018
8:59 am
Avatar
Bird
New Member
Members
Forum Posts: 4
Member Since:
January 30, 2018
sp_UserOfflineSmall Offline

Hi all,

 

i've come slightly unstuck attempting to send data to thingspeak, and was hoping someone with a better knowledge my be able to point me in the right direction.

I'm attempting to send pressure and temperature information collected using a MS5803-14(and later electrical conductivity), and I have been using the write multiple voltage example as a basis.

I was trying to use ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey); to update multiple fields at once. However, when multiple fields have been set using ThingSpeak.setField(1,pressure); nothing is written to thing speak.

The strange element is that if I comment out one of the setField lines, the data will upload normally. This works if I comment out either of the factors i'm trying to upload.

 

Additionally if I use ThingSpeak.writeField(myChannelNumber, 1, pressure, myWriteAPIKey); with a delay followed by

ThingSpeak.writeField(myChannelNumber, 2, temperature, myWriteAPIKey);

the data will also upload.

the arduino is obviously calculating the data correctly as well, as values will print to serial.

its only in the combination of the data that thinspeak stops receiving the data.

 

Many thanks

 

#include "ThingSpeak.h"

#include <OneWire.h>
#include <Wire.h>
#include <MS5803_14.h>

 

#define USE_ETHERNET_SHIELD

 

#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
EthernetClient client;

 

MS_5803 sensor = MS_5803(4096);
float pressure;
float temperature;

 

unsigned long myChannelNumber = XXXXXXXX;
const char * myWriteAPIKey = "XXXXXXXXXXX";

void setup() {
Serial.begin (9600);

Ethernet.begin(mac);

 

ThingSpeak.begin(client);

sensor.initializeMS_5803();

delay(3000);

}

void loop() {

 

sensor.readSensor();
pressure = sensor.pressure();
temperature = sensor.temperature();

 

ThingSpeak.setField( 1, (float) pressure);
ThingSpeak.setField( 2, (float) temperature);
ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);

delay(20000); // ThingSpeak will only accept updates every 15 seconds.
}

January 31, 2018
9:52 am
Avatar
cstapels
Moderator
Members


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

Have you downloaded the channel data to make sure its not a scaling problem and your data it updating but you don't see it?  Does the number of entries change after a post, even if you don't see that data?

Your code looks fine to me, but you might want to remove the cast to float in the .setField command.  

ThingSpeak.setField( 1, (float) pressure);

to

ThingSpeak.setField( 1, pressure);

If that still doesn't work, you could consider converting your variables to a string before uploading it.  ThingSpeak will still interpret the string as a float for plotting as long at there aren't any other characters in the string (like # or ') 

ThingSpeak.setField( 1, String(pressure));

But that method has all the extra problems of bringing along the string class.

One other thing to try is to output pressure and temperature variables to the serial monitor before you write to ThingSpeak just to make sure they read correctly.

Let us know if any that works.  If it doesn't, you can update multiple fields without the library, though its a bit harder.

January 31, 2018
11:51 am
Avatar
Bird
New Member
Members
Forum Posts: 4
Member Since:
January 30, 2018
sp_UserOfflineSmall Offline

Thanks for looking at it, I'm not going to be able to have a look for a couple of days but I'll update then.

Off the top of my head, I think that nothing being added, they  number of entries was not increasing (but i'll double check this) and I don't think the float setfield command would be causing the issue, as if one of the two lines of setfield was commented out the data would then upload for the other field. 

 

Anyway, i'll update soon.

 

thanks again for your help

February 2, 2018
7:09 am
Avatar
Bird
New Member
Members
Forum Posts: 4
Member Since:
January 30, 2018
sp_UserOfflineSmall Offline

just to update:

 

It was posting, just with no values.

Converting to a string as suggested worked.

 

Thanks very much!

March 15, 2018
4:55 am
Avatar
MrFrames
New Member
Members
Forum Posts: 2
Member Since:
March 15, 2018
sp_UserOfflineSmall Offline

Hi everybody,

I'm completely noob to arduino (Thingspeak also) and I have tried to follow this tutorial: 

https://create.arduino.cc/projecthub/arduino/plant-communicator-7ea06f?ref=part&ref_id=54617&offset=1

As other users I have seen in this forum, I can't upload the data to Thingspeak. Everything works fine until the command 

ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);

then nothing happens. Communications are OK, due to 

<span class="hljs-string">"WiFi connected" </span>

message. And also I can see the data in the console with 

<span class="hljs-built_in">Serial</span>.<span class="hljs-built_in">println.</span>

I have tried all this things:

  • Instead of 

    ThingSpeak.setField(<span class="hljs-number">1</span>, get_light()); --&gt; ThingSpeak.setField(<span class="hljs-number">1</span>, String(get_light()));
  • Assign to a variable the return of the function 

    get_light()

    and after this

     ThingSpeak.setField(<span class="hljs-number">1</span>, variable)

    or

    String(variable).

    Nothing happens again.

  • I have succesfully checked that the channel can be written, with https://api.thingspeak.com/update?api_key=XXXXXXXXXX&field1=400&field2=123. Works without problem, I can see the data in the plot. 
  • Also I have compared that the y-axis of the plot with my data units.

I'm completely stucked...any idea?

This is the code I have copied/pasted directly from the tutorial. Thanks a lot!

<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;WiFi101.h&gt; </span></span> <span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string">&lt;WiFiSSLClient.h&gt; </span></span> <span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">"ThingSpeak.h"</span> </span> <span class="hljs-keyword">const</span> <span class="hljs-keyword">char</span>* ssid = SECRET_SSID; <span class="hljs-comment">// your network SSID (name) </span> <span class="hljs-keyword">const</span> <span class="hljs-keyword">char</span>* password = SECRET_PSWD; <span class="hljs-comment">// your network password </span> <span class="hljs-built_in">WiFiClient</span> ThingSpeakClient; <span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">long</span> myChannelNumber = <span class="hljs-number">356392</span>; <span class="hljs-keyword">const</span> <span class="hljs-keyword">char</span> * myWriteAPIKey = SECRET_WRITE_API; <span class="hljs-keyword">int</span> lightPin = A0; <span class="hljs-comment">//the analog pin the light sensor is connected to </span> <span class="hljs-keyword">int</span> tempPin = A1; <span class="hljs-comment">//the analog pin the TMP36's Vout (sense) pin is connected to </span> <span class="hljs-keyword">int</span> moisturePin = A2; <span class="hljs-keyword">void</span> <span class="hljs-built_in">setup</span>() { <span class="hljs-built_in">Serial</span>.<span class="hljs-built_in">begin</span>(<span class="hljs-number">9600</span>); <span class="hljs-built_in">while</span> (!<span class="hljs-built_in">Serial</span>); <span class="hljs-built_in">delay</span>(<span class="hljs-number">2000</span>); <span class="hljs-built_in">Serial</span>.<span class="hljs-built_in">print</span>(<span class="hljs-string">"Connecting Wifi: "</span>); <span class="hljs-built_in">Serial</span>.<span class="hljs-built_in">println</span>(ssid); <span class="hljs-built_in">while</span> (<span class="hljs-built_in">WiFi</span>.<span class="hljs-built_in">begin</span>(ssid, password) != WL_CONNECTED) { <span class="hljs-built_in">Serial</span>.<span class="hljs-built_in">print</span>(<span class="hljs-string">"."</span>); <span class="hljs-built_in">delay</span>(<span class="hljs-number">500</span>); } <span class="hljs-built_in">Serial</span>.<span class="hljs-built_in">println</span>(<span class="hljs-string">""</span>); <span class="hljs-built_in">Serial</span>.<span class="hljs-built_in">println</span>(<span class="hljs-string">"WiFi connected"</span>); } <span class="hljs-keyword">void</span> <span class="hljs-built_in">loop</span>() { ThingSpeak.setField(<span class="hljs-number">1</span>, get_light()); ThingSpeak.setField(<span class="hljs-number">2</span>, get_temperature()); ThingSpeak.setField(<span class="hljs-number">3</span>, get_average_moisture()); ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey); <span class="hljs-built_in">Serial</span>.<span class="hljs-built_in">println</span>(<span class="hljs-string">"message sent to cloud"</span>); <span class="hljs-built_in">delay</span>(<span class="hljs-number">60000</span>); <span class="hljs-comment">// send values every 1 minute </span> } <span class="hljs-keyword">float</span> get_temperature() { <span class="hljs-keyword">int</span> reading = <span class="hljs-built_in">analogRead</span>(tempPin); <span class="hljs-keyword">float</span> voltage = reading * <span class="hljs-number">3.3</span>; voltage /= <span class="hljs-number">1024.0</span>; <span class="hljs-comment">// Print tempeature in Celsius </span> <span class="hljs-keyword">float</span> temperatureC = (voltage - <span class="hljs-number">0.5</span>) * <span class="hljs-number">100</span> ; <span class="hljs-comment">//converting from 10 mv per degree wit 500 mV offset </span> <span class="hljs-comment">// Convert to Fahrenheit </span> <span class="hljs-keyword">float</span> temperatureF = (temperatureC * <span class="hljs-number">9.0</span> / <span class="hljs-number">5.0</span>) + <span class="hljs-number">32.0</span>; <span class="hljs-built_in">return</span> temperatureC; } <span class="hljs-keyword">int</span> get_average_moisture() { <span class="hljs-comment">// make an average of 10 values to be more accurate </span> <span class="hljs-keyword">int</span> tempValue = <span class="hljs-number">0</span>; <span class="hljs-comment">// variable to temporarly store moisture value </span> <span class="hljs-built_in">for</span> (<span class="hljs-keyword">int</span> a = <span class="hljs-number">0</span>; a &lt; <span class="hljs-number">10</span>; a++) { tempValue += <span class="hljs-built_in">analogRead</span>(moisturePin); <span class="hljs-built_in">delay</span>(<span class="hljs-number">10</span>); } <span class="hljs-built_in">return</span> tempValue / <span class="hljs-number">10</span>; } <span class="hljs-keyword">int</span> get_light() { <span class="hljs-keyword">int</span> light_value = <span class="hljs-built_in">analogRead</span>(A0); <span class="hljs-built_in">return</span> light_value; }
March 15, 2018
5:03 am
Avatar
MrFrames
New Member
Members
Forum Posts: 2
Member Since:
March 15, 2018
sp_UserOfflineSmall Offline

MrFrames said

Hi everybody,

I'm completely noob to arduino (Thingspeak also) and I have tried to follow this tutorial: 

https://create.arduino.cc/projecthub/arduino/plant-communicator-7ea06f?ref=part&ref_id=54617&offset=1

As other users I have seen in this forum, I can't upload the data to Thingspeak. Everything works fine until the command 

ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);

then nothing happens. Communications are OK, due to 

<span class="hljs-string">"WiFi connected" </span>

message. And also I can see the data in the console with 

<span class="hljs-built_in">Serial</span>.<span class="hljs-built_in">println.</span>

I have tried all this things:

    • Instead of 

      ThingSpeak.setField(<span class="hljs-number">1</span>, get_light()); --&gt; ThingSpeak.setField(<span class="hljs-number">1</span>, String(get_light()));
    • Assign to a variable the return of the function 

      get_light()

      and after this

       ThingSpeak.setField(<span class="hljs-number">1</span>, variable)

      or

      String(variable).

      Nothing happens again.

    • Also I have compared that the y-axis of the plot with my data units.

I'm completely stucked...any idea?

This is the code I have copied/pasted directly from the tutorial. Thanks a lot!

<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> </span><br /> <span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> </span><br /> <span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">"ThingSpeak.h"</span> </span><br /> <span class="hljs-keyword">const</span> <span class="hljs-keyword">char</span>* ssid = SECRET_SSID; <span class="hljs-comment">// your network SSID (name) </span><br /> <span class="hljs-keyword">const</span> <span class="hljs-keyword">char</span>* password = SECRET_PSWD; <span class="hljs-comment">// your network password </span><br /> <span class="hljs-built_in">WiFiClient</span> ThingSpeakClient; <br /> <span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">long</span> myChannelNumber = <span class="hljs-number">356392</span>; <br /> <span class="hljs-keyword">const</span> <span class="hljs-keyword">char</span> * myWriteAPIKey = SECRET_WRITE_API; <br /> <span class="hljs-keyword">int</span> lightPin = A0; <span class="hljs-comment">//the analog pin the light sensor is connected to </span><br /> <span class="hljs-keyword">int</span> tempPin = A1; <span class="hljs-comment">//the analog pin the TMP36's Vout (sense) pin is connected to </span><br /> <span class="hljs-keyword">int</span> moisturePin = A2; <br /> <span class="hljs-keyword">void</span> <span class="hljs-built_in">setup</span>() { <br /> <span class="hljs-built_in">Serial</span>.<span class="hljs-built_in">begin</span>(<span class="hljs-number">9600</span>); <br /> <span class="hljs-built_in">while</span> (!<span class="hljs-built_in">Serial</span>); <br /> <span class="hljs-built_in">delay</span>(<span class="hljs-number">2000</span>); <br /> <span class="hljs-built_in">Serial</span>.<span class="hljs-built_in">print</span>(<span class="hljs-string">"Connecting Wifi: "</span>); <br /> <span class="hljs-built_in">Serial</span>.<span class="hljs-built_in">println</span>(ssid); <br /> <span class="hljs-built_in">while</span> (<span class="hljs-built_in">WiFi</span>.<span class="hljs-built_in">begin</span>(ssid, password) != WL_CONNECTED) { <br /> <span class="hljs-built_in">Serial</span>.<span class="hljs-built_in">print</span>(<span class="hljs-string">"."</span>); <br /> <span class="hljs-built_in">delay</span>(<span class="hljs-number">500</span>); <br /> } <br /> <span class="hljs-built_in">Serial</span>.<span class="hljs-built_in">println</span>(<span class="hljs-string">""</span>); <br /> <span class="hljs-built_in">Serial</span>.<span class="hljs-built_in">println</span>(<span class="hljs-string">"WiFi connected"</span>); <br /> } <br /> <span class="hljs-keyword">void</span> <span class="hljs-built_in">loop</span>() { <br /> ThingSpeak.setField(<span class="hljs-number">1</span>, get_light()); <br /> ThingSpeak.setField(<span class="hljs-number">2</span>, get_temperature()); <br /> ThingSpeak.setField(<span class="hljs-number">3</span>, get_average_moisture()); <br /> ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey); <br /> <span class="hljs-built_in">Serial</span>.<span class="hljs-built_in">println</span>(<span class="hljs-string">"message sent to cloud"</span>); <br /> <span class="hljs-built_in">delay</span>(<span class="hljs-number">60000</span>); <span class="hljs-comment">// send values every 1 minute </span><br /> } <br /> <span class="hljs-keyword">float</span> get_temperature() { <br /> <span class="hljs-keyword">int</span> reading = <span class="hljs-built_in">analogRead</span>(tempPin); <br /> <span class="hljs-keyword">float</span> voltage = reading * <span class="hljs-number">3.3</span>; <br /> voltage /= <span class="hljs-number">1024.0</span>; <br /> <span class="hljs-comment">// Print tempeature in Celsius </span><br /> <span class="hljs-keyword">float</span> temperatureC = (voltage - <span class="hljs-number">0.5</span>) * <span class="hljs-number">100</span> ; <span class="hljs-comment">//converting from 10 mv per degree wit 500 mV offset </span><br /> <span class="hljs-comment">// Convert to Fahrenheit </span><br /> <span class="hljs-keyword">float</span> temperatureF = (temperatureC * <span class="hljs-number">9.0</span> / <span class="hljs-number">5.0</span>) + <span class="hljs-number">32.0</span>; <br /> <span class="hljs-built_in">return</span> temperatureC; <br /> } <br /> <span class="hljs-keyword">int</span> get_average_moisture() { <span class="hljs-comment">// make an average of 10 values to be more accurate </span><br /> <span class="hljs-keyword">int</span> tempValue = <span class="hljs-number">0</span>; <span class="hljs-comment">// variable to temporarly store moisture value </span><br /> <span class="hljs-built_in">for</span> (<span class="hljs-keyword">int</span> a = <span class="hljs-number">0</span>; a &lt; <span class="hljs-number">10</span>; a++) { <br /> tempValue += <span class="hljs-built_in">analogRead</span>(moisturePin); <br /> <span class="hljs-built_in">delay</span>(<span class="hljs-number">10</span>); <br /> } <br /> <span class="hljs-built_in">return</span> tempValue / <span class="hljs-number">10</span>; <br /> } <br /> <span class="hljs-keyword">int</span> get_light() { <br /> <span class="hljs-keyword">int</span> light_value = <span class="hljs-built_in">analogRead</span>(A0); <br /> <span class="hljs-built_in">return</span> light_value; <br /> }

  

Sorry for the format ConfusedConfusedConfused, I'm not able to edit the post again!

Forum Timezone: America/New_York

Most Users Ever Online: 114

Currently Online:
21 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

rw950431: 261

Vinod: 196

piajola: 85

turgo: 70

vespapierre: 63

Adarsh_Murthy: 62

Member Stats:

Guest Posters: 1

Members: 5703

Moderators: 0

Admins: 2

Forum Stats:

Groups: 4

Forums: 17

Topics: 1313

Posts: 4565

Newest Members:

terranceqh2, DianeEmids, Alanawaype, uqovitay, ufijifode, manish01

Administrators: Hans: 387, lee: 457