<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki2.sitebuilt.net/index.php?action=history&amp;feed=atom&amp;title=DS18B20_readings_to_JSON</id>
	<title>DS18B20 readings to JSON - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki2.sitebuilt.net/index.php?action=history&amp;feed=atom&amp;title=DS18B20_readings_to_JSON"/>
	<link rel="alternate" type="text/html" href="https://wiki2.sitebuilt.net/index.php?title=DS18B20_readings_to_JSON&amp;action=history"/>
	<updated>2026-05-06T15:13:50Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.37.0</generator>
	<entry>
		<id>https://wiki2.sitebuilt.net/index.php?title=DS18B20_readings_to_JSON&amp;diff=5944&amp;oldid=prev</id>
		<title>Tim: Created page with &quot;=====DS18B20 readings to JSON (in serial monitor)===== ::Base code from http://www.pjrc.com/teensy/td_libs_OneWire.html This code in../electronics/Arduino/[https://github.com/...&quot;</title>
		<link rel="alternate" type="text/html" href="https://wiki2.sitebuilt.net/index.php?title=DS18B20_readings_to_JSON&amp;diff=5944&amp;oldid=prev"/>
		<updated>2013-01-12T23:20:05Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;=====DS18B20 readings to JSON (in serial monitor)===== ::Base code from http://www.pjrc.com/teensy/td_libs_OneWire.html This code in../electronics/Arduino/[https://github.com/...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;=====DS18B20 readings to JSON (in serial monitor)=====&lt;br /&gt;
::Base code from http://www.pjrc.com/teensy/td_libs_OneWire.html This code in../electronics/Arduino/[https://github.com/mckennatim/arduino/blob/master/DS18B20Serial_json/DS18B20Serial_json.ino DS18B20Serial_json.ino] &lt;br /&gt;
:key bits:&lt;br /&gt;
You need to allocate a buffer for tempstr&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
 float celsius, fahrenheit;&lt;br /&gt;
 String jsn; &lt;br /&gt;
 char tempBuf[32] = {0};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
You end the json after you've gone through all the sensors and if you are just starting, create a new json string. Otherwise, Increment to next sensor.&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
  if ( !ds.search(addr)) {//this is where it loops to the next address&lt;br /&gt;
	Serial.println(&amp;quot;No more addresses.&amp;quot;);&lt;br /&gt;
	Serial.println();&lt;br /&gt;
  	jsn = jsn + &amp;quot;}&amp;quot;;//so end the json&lt;br /&gt;
  	Serial.println(jsn);//and print it out&lt;br /&gt;
  	ctr=0;&lt;br /&gt;
    ds.reset_search();//if there are no more devices go back to start of list&lt;br /&gt;
    delay(250);&lt;br /&gt;
    return;&lt;br /&gt;
  } else {&lt;br /&gt;
  	if (ctr==0){jsn=&amp;quot;{&amp;quot;;}//must be just beginning, start the json str&lt;br /&gt;
  	++ctr;//since we found a device, increment sensor&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
The first two bytes of data = ds.read() is the sensor raw temp which gets converted here&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
  // convert the data to actual temperature&lt;br /&gt;
  unsigned int raw = (data[1] &amp;lt;&amp;lt; 8) | data[0];&lt;br /&gt;
  if (type_s) {&lt;br /&gt;
    raw = raw &amp;lt;&amp;lt; 3; // 9 bit resolution default&lt;br /&gt;
    if (data[7] == 0x10) {&lt;br /&gt;
      // count remain gives full 12 bit resolution&lt;br /&gt;
      raw = (raw &amp;amp; 0xFFF0) + 12 - data[6];&lt;br /&gt;
    }&lt;br /&gt;
  } else {&lt;br /&gt;
    byte cfg = (data[4] &amp;amp; 0x60);&lt;br /&gt;
    if (cfg == 0x00) raw = raw &amp;lt;&amp;lt; 3;  // 9 bit resolution, 93.75 ms&lt;br /&gt;
    else if (cfg == 0x20) raw = raw &amp;lt;&amp;lt; 2; // 10 bit res, 187.5 ms&lt;br /&gt;
    else if (cfg == 0x40) raw = raw &amp;lt;&amp;lt; 1; // 11 bit res, 375 ms&lt;br /&gt;
    // default is 12 bit resolution, 750 ms conversion time&lt;br /&gt;
  }&lt;br /&gt;
  celsius = (float)raw / 16.0;&lt;br /&gt;
  fahrenheit = celsius * 1.8 + 32.0;&lt;br /&gt;
  dtostrf(fahrenheit, 5, 1, tempBuf); //turns float to string&lt;br /&gt;
  jsn= jsn + &amp;quot;\&amp;quot;sensor&amp;quot;+ ctr + &amp;quot;\&amp;quot;: &amp;quot; + tempBuf + &amp;quot;, &amp;quot;;//appends temp to json&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Tim</name></author>
	</entry>
</feed>