/
main.cpp
85 lines (65 loc) · 2.1 KB
/
main.cpp
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
#include <Arduino.h>
// Include the libraries we need
#include <OneWire.h>
#include <DallasTemperature.h>
#include <ESP8266WiFi.h>
#include <Thing.h>
#include <WebThingAdapter.h>
// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 5
const char *ssid = "_________";
const char *password = "_________";
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
// arrays to hold device address
DeviceAddress insideThermometer;
WebThingAdapter *adapter;
const char *sensorTypes[] = {"MultiLevelSensor", "Sensor", nullptr};
ThingDevice weather("ds18b20", "DS18B20 Temp Sensor", sensorTypes);
ThingProperty weatherTemp("temperature", "The temperature measured by the sensor", NUMBER, "LevelProperty");
void readData(DeviceAddress deviceAddress)
{
float temp(NAN);
temp = sensors.getTempC(deviceAddress);
ThingPropertyValue value;
value.number = temp;
weatherTemp.setValue(value);
}
/*
* Setup function. Here we do the basics
*/
void setup(void)
{
// start serial port
Serial.begin(9600);
sensors.begin();
if (!sensors.getAddress(insideThermometer, 0))
Serial.println("Unable to find address for Device 0");
sensors.setResolution(insideThermometer, 9);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
adapter = new WebThingAdapter("weathersensor", WiFi.localIP());
weather.addProperty(&weatherTemp);
adapter->addDevice(&weather);
adapter->begin();
}
/*
* Main function. It will request the tempC from the sensors and display on Serial.
*/
void loop(void)
{
sensors.requestTemperatures();
// It responds almost immediately. Let's print out the data
readData(insideThermometer); // Use a simple function to print out the data
adapter->update();
}