A place users can post their projects. If you have a small project and would like your own dedicated place to post and have others chat about it then this is your spot.

User avatar
By froggy
#18952 OK so I'm new to ESP8266, and decided to try what i considered a very simple project, but guess what its not working, so any help would be much appriciated. Thanks in advance

I've got an ESP8266 201 (http://smarpl.com/sites/default/files/i ... ce-v01.png)

i am trying to connect a DS18B20 to it and send the inforamation to thingsspeak.com (any other IOT platform information is gladly received if you think there are better ones.)

The code below was uploaded using LUA Loader, but i would really prefer to use the arduino IDE if possible, I guess it would mean writing an Arduino Sketch.

My problems are:-
1. No data is geting to thingsspeak.com
2. I dont know how to see if any data is being collected by the DS18B20

The DS18B20 is connected as follows
Pin 1 GND
Pin 2 ESP Pin 9 (IO14)
Pin 3 VCC 3.3V

The following is the code i uploaded to the ESP
-- Measure temperature and post data to thingspeak.com
-- 2014 OK1CDJ
--- Tem sensor DS18B20 is conntected to GPIO0
--- 2015.01.21 sza2 temperature value concatenation bug correction

pin = 9
ow.setup(pin)

counter=0
lasttemp=-999

function bxor(a,b)
local r = 0
for i = 0, 31 do
if ( a % 2 + b % 2 == 1 ) then
r = r + 2^i
end
a = a / 2
b = b / 2
end
return r
end

--- Get temperature from DS18B20
function getTemp()
addr = ow.reset_search(pin)
repeat
tmr.wdclr()

if (addr ~= nil) then
crc = ow.crc8(string.sub(addr,1,7))
if (crc == addr:byte(8)) then
if ((addr:byte(1) == 0x10) or (addr:byte(1) == 0x28)) then
ow.reset(pin)
ow.select(pin, addr)
ow.write(pin, 0x44, 1)
tmr.delay(1000000)
present = ow.reset(pin)
ow.select(pin, addr)
ow.write(pin,0xBE, 1)
data = nil
data = string.char(ow.read(pin))
for i = 1, 8 do
data = data .. string.char(ow.read(pin))
end
crc = ow.crc8(string.sub(data,1,8))
if (crc == data:byte(9)) then
t = (data:byte(1) + data:byte(2) * 256)
if (t > 32768) then
t = (bxor(t, 0xffff)) + 1
t = (-1) * t
end
t = t * 625
lasttemp = t
print("Last temp: " .. lasttemp)
end
tmr.wdclr()
end
end
end
addr = ow.search(pin)
until(addr == nil)
end

--- Get temp and send data to thingspeak.com
function sendData()
getTemp()
t1 = lasttemp / 10000
t2 = (lasttemp >= 0 and lasttemp % 10000) or (10000 - lasttemp % 10000)
print("Temp:"..t1 .. "."..string.format("%04d", t2).." C\n")
-- conection to thingspeak.com
print("Sending data to thingspeak.com")
conn=net.createConnection(net.TCP, 0)
conn:on("receive", function(conn, payload) print(payload) end)
-- api.thingspeak.com 184.106.153.149
conn:connect(80,'184.106.153.149')
conn:send("GET /update?key=*****MYKEYGOESHERE*****&field1="..t1.."."..string.format("%04d", t2).." HTTP/1.1\r\n")
conn:send("Host: api.thingspeak.com\r\n")
conn:send("Accept: */*\r\n")
conn:send("User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n")
conn:send("\r\n")
conn:on("sent",function(conn)
print("Closing connection")
conn:close()
end)
conn:on("disconnection", function(conn)
print("Got disconnection...")
end)
end

-- send data every X ms to thing speak
tmr.alarm(0, 60000, 1, function() sendData() end )
User avatar
By torntrousers
#19014 Here's a sample Arduino sketch for sending ds18b20 readings to data.sparkfun:
Code: Select all
#include <ESP8266WiFi.h>
#include <OneWire.h>
#include <DallasTemperature.h>

extern "C" {
  #include "user_interface.h"
  uint16 readvdd33(void);
}

const char* ssid = "BTHub5-72W5";
const char* password = "xxxxxxxx";

// https://data.sparkfun.com/input/<streamid>?private_key=<privateKey>&vdd=33&temp=20.3
const char* host = "data.sparkfun.com";
const char* streamId   = "8drL95XOz2iwYJ7bWvYa";
const char* privateKey = "pzeJnoKvRVCbgerN8WgJ";

const unsigned long SLEEP_INFTERVAL = 15 * 60 * 1000 * 1000; // 15 minutes

// oneWire sensor DS18B20 on pin 2
OneWire oneWire(2);
DallasTemperature sensors(&oneWire);

void setup() {
  float vdd = readvdd33() / 1000.0;

  Serial.begin(115200);
  Serial.println();

  Serial.print("Vdd: ");
  Serial.println(vdd);

  initWifi();

  sensors.begin();
  sensors.requestTemperatures();
  float temp = sensors.getTempCByIndex(0);
  Serial.print("Temp: ");
  Serial.println(temp); 

  sendReadings(vdd, temp);

  Serial.print("up time: ");
  Serial.print(millis());

  Serial.print(", deep sleep for ");
  Serial.print(SLEEP_INFTERVAL / 1000000);
  Serial.println(" secs...");
  system_deep_sleep_set_option(1);
  system_deep_sleep(SLEEP_INFTERVAL - micros()); // deep sleep for 15 minutes minus how long this reading was up for
}

void loop() {
   // should never get here 
}

void sendReadings(float vdd, float temp) {
  Serial.print("connecting to ");
  Serial.println(host);
 
  WiFiClient client;
  while (!client.connect(host, 80)) {
    Serial.println("connection failed, retrying...");
  }

  String url = "/input/";
  url += streamId;
  url += "?private_key=";
  url += privateKey;
 
  Serial.print("Requesting URL: ");
  Serial.println(url);
 
  client.print(String("GET ") + url);
  client.print("&temp=");
  client.print(temp, 1);
  client.print("&vdd=");
  client.print(vdd, 1);
  client.print(String(" HTTP/1.1\r\n") +
               "Host: " + host + "\r\n" +
               "Connection: close\r\n\r\n");
 
  // wait for response
  while(!!!client.available()) {
     delay(10);
  }
 
  // Read all the lines of the reply from server and print them to Serial
  while(client.available()){
    String line = client.readStringUntil('\r');
    Serial.print(line);
  }
 
  Serial.println();
  Serial.println("closing connection");
}

void initWifi() {
   Serial.print("current connect: ");
   Serial.println(WiFi.SSID());
   if (strcmp (WiFi.SSID(),ssid) != 0) {
      Serial.print("Connecting to ");
      Serial.println(ssid);
 
      WiFi.begin(ssid, password);
      wifi_station_set_auto_connect(true);
 
      while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
      }
  }

  Serial.println("");
  Serial.print("WiFi connected in: ");   
  Serial.println(millis());

  Serial.println();
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
}