/*
* 29 May 2015
* This sketch displays a DS18B20 temp as a UDP packet coming from an UDP client.
*
* Configuration : Enter the ssid and password of your Wifi AP. Enter the port number your server is listening on.
*
*/
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <OneWire.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
int status = WL_IDLE_STATUS;
const char* ssid = "yourssid"; // your network SSID (name)
const char* pass = "yourpasswd"; // your network password
uint16_t localPort = 2652; // local port to listen for UDP packets
char packetBuffer[512]; //buffer to hold incoming and outgoing packets
int16_t fahrenheit, celsius, packetCnt;
// A UDP instance to let us send and receive packets over UDP
WiFiUDP Udp;
// A OneWire instance to test a DS18B20
OneWire ds(2); // on pin 2 (a 4.7K resistor is necessary)
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// setting up Station AP
WiFi.begin(ssid, pass);
// Wait for connect to AP
Serial.print("[Connecting]");
Serial.print(ssid);
int tries=0;
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
tries++;
if (tries > 30){
break;
}
}
Serial.println();
printWifiStatus();
Serial.println("Connected to wifi");
Serial.print("Udp server started at port ");
Serial.println(localPort);
Udp.begin(localPort);
}
void getTemp(void) {
byte i;
byte present = 0;
byte type_s;
byte data[12];
byte addr[8];
if ( !ds.search(addr)) {
Serial.println("No more addresses.");
Serial.println();
ds.reset_search();
delay(250);
return;
}
Serial.print("ROM =");
for( i = 0; i < 8; i++) {
Serial.write(' ');
Serial.print(addr[i], HEX);
}
if (OneWire::crc8(addr, 7) != addr[7]) {
Serial.println("CRC is not valid!");
return;
}
Serial.println();
// the first ROM byte indicates which chip
switch (addr[0]) {
case 0x28:
Serial.println(" Chip = DS18B20");
type_s = 0;
break;
default:
Serial.println("Device is not a DS18x20 family device.");
return;
}
ds.reset();
ds.select(addr);
ds.write(0x44, 1); // start conversion, with parasite power on at the end
delay(1000); // maybe 750ms is enough, maybe not
// we might do a ds.depower() here, but the reset will take care of it.
present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad
Serial.print(" Data = ");
Serial.print(present, HEX);
Serial.print(" ");
for ( i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
Serial.print(data[i], HEX);
Serial.print(" ");
}
Serial.print(" CRC=");
Serial.print(OneWire::crc8(data, 8), HEX);
Serial.println();
// Convert the data to actual temperature
// because the result is a 16 bit signed integer, it should
// be stored to an "int16_t" type, which is always 16 bits
// even when compiled on a 32 bit processor.
int16_t raw = (data[1] << 8) | data[0];
byte cfg = (data[4] & 0x60);
// at lower res, the low bits are undefined, so let's zero them
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
//// default is 12 bit resolution, 750 ms conversion time
celsius = raw / 16;
fahrenheit = ((celsius * 9)/5) + 32.0;
Serial.print(" Temperature = ");
Serial.print(celsius);
Serial.print(" Celsius, ");
Serial.print(fahrenheit);
Serial.println(" Fahrenheit");
}
void loop()
{
int noBytes = Udp.parsePacket();
if ( noBytes )
{
Serial.print(millis() / 1000);
Serial.print(":Packet of ");
Serial.print(noBytes);
Serial.print(" received from ");
Serial.print(Udp.remoteIP());
Serial.print(":");
Serial.println(Udp.remotePort());
// We've received a packet, read the data from it
Udp.read(packetBuffer,noBytes); // read the packet into the buffer
// display the packet contents in HEX
for (int i=1;i<=noBytes;i++)
{
Serial.print(packetBuffer[i-1],HEX);
if (i % 32 == 0)
{
Serial.println();
}else Serial.print(' ');
} // end for
Serial.println("Getting Temp");
getTemp();
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
packetCnt = sprintf(packetBuffer, "Temperature is %d degrees C, %d degrees F\n", celsius, fahrenheit);
Udp.write(packetBuffer, packetCnt);
Udp.endPacket();
} // end if
}
void printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
}