publish multiple values to thingspeak using MQTT | ThingSpeak Projects | 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
publish multiple values to thingspeak using MQTT
No permission to create posts
March 8, 2018
2:29 pm
Avatar
digimation
New Member
Members
Forum Posts: 2
Member Since:
March 8, 2018
sp_UserOfflineSmall Offline
I am trying to publish a multiple random data using mqtt to send data from raspberry pi to thingspeaks. 
i want to publish the 10 values of temp to thingspeaks field but it limits me to one value every 15 seconds.
 so is there anyway to send a list of values every 15 second to graph it with time in thingspeaks channels??
 

<span class="pln">temp </span><span class="pun">=</span> <span class="pun">[]</span><span class="pln"> current </span><span class="pun">=</span> <span class="pun">[]</span> <span class="kwd">while</span><span class="pun">(</span><span class="lit">1</span><span class="pun">):</span> <span class="com"># get the system performance data over 20 seconds.</span> <span class="kwd">for</span><span class="pln"> i </span><span class="kwd">in</span><span class="pln"> range</span><span class="pun">(</span><span class="lit">10</span><span class="pun">):</span><span class="pln"> temp</span><span class="pun">.</span><span class="pln">append</span><span class="pun">(</span><span class="pln">random</span><span class="pun">.</span><span class="pln">randint</span><span class="pun">(</span><span class="lit">0</span><span class="pun">,</span> <span class="lit">100</span><span class="pun">))</span><span class="pln"> current</span><span class="pun">.</span><span class="pln">append</span><span class="pun">(</span><span class="pln">random</span><span class="pun">.</span><span class="pln">randint</span><span class="pun">(</span><span class="lit">0</span><span class="pun">,</span> <span class="lit">100</span><span class="pun">))</span> <span class="com"># build the payload string.</span><span class="pln"> payload </span><span class="pun">=</span> <span class="str">"field1="</span> <span class="pun">+</span><span class="pln"> str</span><span class="pun">(</span><span class="pln">temp</span><span class="pun">)</span> <span class="pun">+</span> <span class="str">"&amp;field2="</span> <span class="pun">+</span><span class="pln"> str</span><span class="pun">(</span><span class="pln">current</span><span class="pun">)</span> <span class="com"># attempt to publish this data to the topic.</span> <span class="kwd">try</span><span class="pun">:</span><span class="pln"> publish</span><span class="pun">.</span><span class="pln">single</span><span class="pun">(</span><span class="pln">topic</span><span class="pun">,</span><span class="pln"> payload</span><span class="pun">,</span><span class="pln"> hostname</span><span class="pun">=</span><span class="pln">mqttHost</span><span class="pun">,</span><span class="pln"> transport</span><span class="pun">=</span><span class="pln">tTransport</span><span class="pun">,</span><span class="pln"> port</span><span class="pun">=</span><span class="pln">tPort</span><span class="pun">,</span><span class="pln">auth</span><span class="pun">={</span><span class="str">'username'</span><span class="pun">:</span><span class="pln">mqttUsername</span><span class="pun">,</span><span class="str">'password'</span><span class="pun">:</span><span class="pln">mqttAPIKey</span><span class="pun">})</span> <span class="kwd">print</span> <span class="pun">(</span><span class="str">" Published temp ="</span><span class="pun">,</span><span class="pln">temp</span><span class="pun">,</span><span class="str">" current ="</span><span class="pun">,</span><span class="pln"> current</span><span class="pun">,</span><span class="str">" to host: "</span> <span class="pun">,</span><span class="pln"> mqttHost </span><span class="pun">,</span> <span class="str">" clientID= "</span> <span class="pun">,</span><span class="pln"> clientID</span><span class="pun">)</span> <span class="kwd">except</span> <span class="pun">(</span><span class="typ">KeyboardInterrupt</span><span class="pun">):</span> <span class="kwd">break</span> <span class="kwd">except</span><span class="pun">:</span> <span class="kwd">print</span> <span class="pun">(</span><span class="str">"There was an error while publishing the data."</span><span class="pun">)</span><span class="pln"> time</span><span class="pun">.</span><span class="pln">sleep</span><span class="pun">(</span><span class="lit">15</span><span class="pun">)</span>
March 14, 2018
3:17 pm
Avatar
cstapels
Moderator
Members


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

 Have a look at the documentation for MQTT Publish to a Channel Feed.  You can definitely send all 8 fields in a single publish, and you can do this every 15 seconds.  If you needed to, you could also use the location and status fields, but I think you would be better served to put 5 measurements in each channel, or even use a separate channel for each value.  

To publish to multiple fields at once, your publish message should have the format field1=xx&field2=yy&field3=zz...

Forum Timezone: America/New_York

Most Users Ever Online: 114

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

JudestAboth, concettazx4, lyndarf60, caseybo16, iodiree, emiliacz4

Administrators: Hans: 387, lee: 457