Monday, 23 March 2015

wifi temperature sensor DS18B20

After I integrated ESP8266, I made a wifi temperature sensor which post temperature to my server every minute.



I didn't use any IoT platform to host my data because I will later use this data for big data analysis.

To decouple my small system with any other system, I made my server to store data and my front-end to display data.

The temperature sensor I am using is DS18B20




For ESP8266 with Arduino you can check my other posts:
http://baiduhix.blogspot.co.uk/2015/03/arduino-dirt-cheap-wifi-solution_1.html
http://baiduhix.blogspot.co.uk/2015/03/arduino-dirt-cheap-wifi-solution.html
http://baiduhix.blogspot.co.uk/2015/02/arduino-cheap-wifi-solution-esp8266.html



The code i am using for temperature sensor(DS18B20)  is from :
http://www.tweaking4all.com/hardware/arduino/arduino-ethernet-data-pull/

you need download "onwire" library and install into your arduino IDE, (you can search how to do this on google.com)

Code is also provided here:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include <UIPEthernet.h> // Used for Ethernet
#include <OneWire.h>     // Used for temperature sensor(s)

// **** ETHERNET SETTING ****
// Ethernet MAC address - must be unique on your network
byte mac[] = { 0x540x340x410x300x300x31 };                                    
// ethernet interface IP address (unique in your network)
IPAddress ip(1921681179);                      
// ethernet interface IP port (80 = http)
EthernetServer server(80);

EthernetClient client;

// **** TEMPERATURE SETTINGS ****
// Sensor(s) data pin is connected to Arduino pin 2 in non-parasite mode!
OneWire  ds(2);

void setup() {
  // Open serial communications:
  Serial.begin(9600);

  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  server.begin();

  Serial.println("Tweaking4All.com - Temperature Drone - v1.0");
  Serial.println("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\n");
  Serial.print("IP Address: ");
  Serial.println(Ethernet.localIP());
  Serial.println();
}

void loop() {
  // listen for incoming clients
  client = server.available();

  if (client)
  {
    Serial.println("-> New Connection\n");

    // a http request ends with a blank line
    boolean currentLineIsBlank = true;

    while (client.connected())
    {
      if (client.available()) {
        char c = client.read();

        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (== '\n' && currentLineIsBlank)
        {
          client.println("<?xml version='1.0'?>\n<sensordata>");
          Serial.println("   Collecting Sensor Data:");
          TemperaturesToXML();
          client.println("</sensordata>");
          Serial.println("\n   Done Collecting Sensor Data");
          break;
        }

        if (== '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        }
        else if (!= '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }

    // give the web browser time to receive the data
    delay(10);

    // close the connection:
    client.stop();
    Serial.println("   Disconnected\n");
  }
}

void TemperaturesToXML(void) {
  byte counter;
  byte present = 0;
  byte sensor_type;
  byte data[12];
  byte addr[8];
  float celsius, fahrenheit;

  ds.reset_search();

  while ( ds.search(addr)) {
    client.println("<sensor>");

    // Get Serial number
    client.print("  <serial>");
    Serial.print("\n   Sensor     : ");

    for( counter = 0; counter < 8; counter++)
    {
      if (addr[counter]<10) client.print("0");
      client.print(String(addr[counter], HEX));
      client.print(" ");

      if (addr[counter]<10) Serial.print("0");
      Serial.print(String(addr[counter], HEX));
      Serial.print(" ");
    }
    Serial.println();

    client.println("</serial>");

    // Check CRC
    if (OneWire::crc8(addr, 7) != addr[7])
    {
        client.println("  <status>Invalid CRC</status>\n</sensor>");
        Serial.println("   ERROR\n");
        return;
    }

    client.println("  <status>OK</status>");

    // Get Chip type (the first ROM byte indicates which chip)
    client.print("  <chip>");

    switch (addr[0])
    {
      case 0x10:
        client.println("DS18S20");
        sensor_type = 1;
        break;
      case 0x28:
        client.println("DS18B20");
        sensor_type = 0;
        break;
      case 0x22:
        client.println("DS1822");
        sensor_type = 0;
        break;
      default:
        client.println("undefined</chip>");
        return;
    }
    client.println("</chip>");

    ds.reset();
    ds.select(addr);
    ds.write(0x44);  // start conversion, with regular (non-parasite!) power

    delay(1000);     // maybe 750ms is enough, maybe not

    present = ds.reset();
    ds.select(addr);  
    ds.write(0xBE);  // Read Scratchpad

    // Get Raw Temp Data, we need 9 bytes
    for ( counter = 0; counter < 9; counter++)
    {        
      data[counter] = ds.read();
    }

    // Convert the data to actual temperature
    // because the result is a 16 bit signed integer, it should
    // be stored to an "int16_t" type
    int16_t raw = (data[1] << 8) | data[0];

    if (sensor_type)
    {
      raw = raw << 3; // 9 bit resolution default
      if (data[7] == 0x10) {
        // "count remain" gives full 12 bit resolution
        raw = (raw & 0xFFF0) + 12 - data[6];
      }
    }
    else
    {
      // at lower res, the low bits are undefined, so let's zero them
      byte cfg = (data[4] & 0x60);

      //// default is 12 bit resolution, 750 ms conversion time
      if (cfg == 0x00) raw = raw & ~7;  // 9 bit resolution, 93.75 ms
      else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
      else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
    }

    celsius = (float)raw / 16.0;
    fahrenheit = celsius * 1.8 + 32.0;

    client.print("  <celsius>");
    client.print(celsius);
    client.print("</celsius>\n  <fahrenheit>");
    client.print(fahrenheit);
    client.println("</fahrenheit>");

    Serial.print("   Temperature: ");
    Serial.print(celsius);
    Serial.print(" C  (");
    Serial.print(fahrenheit);
    Serial.println(" F)");

    client.println("</sensor>");
  }

  return;
}


Issues:


1, wifi issues,
    this issue half fixed by my new code, half fixed by replacing some wires....

2, temperature sensor,
    sometime it give me very extremely big data or 0. if you have the same issue, we can discuss.






No comments:

Post a comment