Difference between revisions of "ESP8266-DS18S20"

From Hackerspace ACKspace
Jump to: navigation, search
m (fixed link (outdated bookmark))
(set project picture)
Line 3: Line 3:
 
|Members=xopr
 
|Members=xopr
 
|Description=Creating a wireless SpaceAPI-like sensor
 
|Description=Creating a wireless SpaceAPI-like sensor
 +
|Picture=ESP8166-DS18S20-PCB.png
 
}}
 
}}
 
 
This code is '''deprecated''' and had been replaced by the [[SpaceAPI#the_firmware|new SpaceAPI's]] space state
 
This code is '''deprecated''' and had been replaced by the [[SpaceAPI#the_firmware|new SpaceAPI's]] space state
  
 
Also, the code is somewhat ugly, but it worked..
 
Also, the code is somewhat ugly, but it worked..
[[Image:ESP8166-DS18S20-PCB.png|right|350px|thumb|the PCB that powers the ESP8266]]
 
  
 
=== synopsis ===
 
=== synopsis ===

Revision as of 21:52, 3 November 2015

Project: ESP8266-DS18S20
Featured:
State Completed
Members xopr
GitHub No GitHub project defined. Add your project here.
Description Creating a wireless SpaceAPI-like sensor
Picture
ESP8166-DS18S20-PCB.png

This code is deprecated and had been replaced by the new SpaceAPI's space state

Also, the code is somewhat ugly, but it worked..

synopsis

This sketch provides a webserver-like interface and provides a json file (somewhat compatible with SpaceAPI).

The json will provide the DS18S20 temperature sensor's address and temperature in °C, and will look like:

{
  "sensors":{
    "ext_temp_count":2,
    "temperature":[
      {
        "ext_index":0,
        "value":-127.0,
        "unit":"°C",
        "ext_address":"00112233445566"
      },
      {
        "ext_index":0,
        "value":85.0,
        "unit":"°C",
        "ext_address":"99AABBCCDDEEFF"
      }
    ]
  }
}

Note: the ext_temp_count and ext_index fields are only visible in debug mode (GPIO0 pulled to ground)

code

ESP8266-DS18S20.ino

Click here to view the source code
/*
  2015-07-04: Created by xopr
  
  Code based on http://iot-playground.com/2-uncategorised/41-esp8266-ds18b20-temperature-sensor-arduino-ide by Igor Jarc
 
  External libraries:
  - https://github.com/milesburton/Arduino-Temperature-Control-Library
 
  This program is free software; you can redistribute it and/or
  modify it under the terms of the GNU General Public License
  version 2 as published by the Free Software Foundation.
*/

#include <ESP8266WiFi.h>
#include <OneWire.h>
#include <DallasTemperature.h>

// AP definitions
#define AP_SSID "ACKspaceWifi"
#define AP_PASSWORD "nospacenet"

#define ONE_WIRE_BUS 2  // DS18B20 pin
OneWire oneWire( ONE_WIRE_BUS );
DallasTemperature DS18B20( &oneWire );
WiFiServer server( 80 );

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

  // Make sure GPIO doesn't float if we try and read it
  pinMode( 0, INPUT_PULLUP );
  wifiConnect( );
}

const char* strIndent = "  ";
bool bDebug;

void loop()
{
  float temp;

  // Check if a client has connected
  WiFiClient client = server.available( );
  if ( !client )
    return;

  // Wait until the client sends some data
  Serial.println( "new client" );
  while(!client.available( ))
    delay(1);

  DS18B20.begin();

  // Read the first line of the request
  String req = client.readStringUntil( '\r' );
  Serial.println( req );
  client.flush( );

  bDebug = !digitalRead( 0 );

  DS18B20.requestTemperatures( );
  //delay(1);
  uint8_t nDevicecount = DS18B20.getDeviceCount( );

  // Output JSON, keep the spaceAPI (v0.13) standard.
  // NOTE: proper json decoders handle utf-8 at least. Also, you need this for the degree symbol
  client.print( "HTTP/1.1 200 OK\r\nContent-Type: application/json;charset=utf8\r\n\r\n" );

  client.print( "{\n" );

  client.print( strIndent );
  client.print( "\"sensors\":{\n" );

  if ( bDebug )
  {
    client.print( strIndent );
    client.print( strIndent );
    client.print( "\"ext_temp_count\":" );
    client.print( nDevicecount );
    client.print( ",\n" );
  }

  client.print( strIndent );
  client.print( strIndent );
  client.print( "\"temperature\":[\n" );

  for ( uint8_t nDevice = 0; nDevice < nDevicecount; nDevice++ )
  {
    DeviceAddress address;
    byte nTry = 10;
    boolean bFail = true;

    // Try and retrieve the sensor's value
    while ( nTry-- && bFail )
    {
      if ( DS18B20.getAddress( address, nDevice ) )
      {
        temp = DS18B20.getTempCByIndex( nDevice );

        if ( temp == 85.0 || temp == ( -127.0 ) )
          bFail = true;
        else
          bFail = false;
      }

      delay(1);
    }

    if ( nTry )
    {


      // Sensor
      client.print( strIndent );
      client.print( strIndent );
      client.print( strIndent );
      client.print( "{\n" );

      // sensor index (in debug only)
      if ( bDebug )
      {
        client.print( strIndent );
        client.print( strIndent );
        client.print( strIndent );
        client.print( strIndent );
        client.print( "\"ext_index\":" );
        client.print( nDevice );
        client.print( ",\n" );
      }

      // Temperature value
      client.print( strIndent );
      client.print( strIndent );
      client.print( strIndent );
      client.print( strIndent );
      client.print( "\"value\":" );

      if ( temp == ( -127.0 ) )
        client.print( "null," );
      else if ( temp == 85.0 )
        client.print( "false," );
      else
        client.print( temp );

      client.print( ",\n" );

      // Temperature unit
      client.print( strIndent );
      client.print( strIndent );
      client.print( strIndent );
      client.print( strIndent );
      client.print( "\"unit\":\"°C\",\n" );

      // Sensor address
      client.print( strIndent );
      client.print( strIndent );
      client.print( strIndent );
      client.print( strIndent );
      client.print( "\"ext_address\":" );
      client.print( "\"" );
      client.print( address[1], HEX );
      client.print( address[2], HEX );
      client.print( address[3], HEX );
      client.print( address[4], HEX );
      client.print( address[5], HEX );
      client.print( address[6], HEX );
      client.print( address[7], HEX );
      client.print( "\"\n" );


      client.print( strIndent );
      client.print( strIndent );
      client.print( strIndent );
      client.print( "}" );

      if ( nDevice + 1 < nDevicecount )
        client.print( "," );

      client.print( "\n" );

    }

  // /Sensor
  }
  
  client.print( strIndent );
  client.print( strIndent );
  client.print( "]\n" );
  
  client.print( strIndent );
  client.print( "}\n}" );

  delay(1);
  Serial.println("Client disonnected");

  // The client will actually be disconnected 
  // when the function returns and 'client' object is detroyed
}

void wifiConnect()
{
    Serial.print( "Connecting to " );
    Serial.print( AP_SSID );
    WiFi.begin( AP_SSID, AP_PASSWORD );
    while ( WiFi.status() != WL_CONNECTED )
    {
        delay( 1000 );
        Serial.print( "." );
    }
  
    Serial.println( "" );
    Serial.println( "WiFi connected" );

    server.begin();
    Serial.println( "Server started" );

    // Print the IP address
    Serial.println(WiFi.localIP( ) );
}