Skip to content

Instantly share code, notes, and snippets.

@molda
Created February 28, 2018 08:03
Show Gist options
  • Save molda/2ea8b6c0904d6bbd5c27d20d7019526c to your computer and use it in GitHub Desktop.
Save molda/2ea8b6c0904d6bbd5c27d20d7019526c to your computer and use it in GitHub Desktop.
#include <ESP8266WiFi.h>
#include <OneWire.h>
#include <DallasTemperature.h>
//AP definitions
#define AP_SSID "xxxxx"
#define AP_PASSWORD "xxxxxxxxxxxx"
#define IP_ADDRESS "192.168.0.107"
#define PORT 80
#define NAME "living_room"
#define REPORT_INTERVAL 60 // in sec
#define ONE_WIRE_BUS 14 // DS18B20 pin
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature DS18B20(&oneWire);
unsigned long startTime = millis();
unsigned int raw = 0;
float volt = 0.0;
void setup() {
Serial.begin(115200);
//Serial.println("");
//Serial.println("Starting...");
pinMode(A0, INPUT);
wifiConnect();
float temp = 0;
raw = analogRead(A0);
volt = raw / 1023.0;
volt = volt * 4.2;
do {
DS18B20.requestTemperatures();
delay(3);
temp = DS18B20.getTempCByIndex(0);
//Serial.print("Temperature: ");
//Serial.println(temp);
} while (temp == 85.0 || temp == (-127.0));
send(temp, volt);
Serial.println(String("Uptime ") + (millis() - startTime) + "ms");
//1,000,000 = 1 second
ESP.deepSleep(5000000);
}
void loop() {
}
void wifiConnect()
{
//Serial.print("Connecting to AP");
WiFi.begin(AP_SSID, AP_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
//Serial.print(".");
}
//Serial.println("");
//Serial.println("WiFi connected");
/Serial.println("IP address: ");
//Serial.println(WiFi.localIP());
}
void send(float temp, float adcvalue)
{
WiFiClient client;
while(!client.connect(IP_ADDRESS, PORT)) {
//Serial.println("connection failed");
wifiConnect();
}
String url = "";
url += "/report?device=" + String(NAME) + "&val=" + String(temp) + "&ip=" + WiFi.localIP().toString() + "&voltage=" + String(adcvalue);
//Serial.print("POST data to URL: ");
//Serial.println(String(IP_ADDRESS) + url);
client.print(String("POST ") + url + " HTTP/1.1\r\n" +
"Host: " + String(IP_ADDRESS) + "\r\n" +
"Connection: close\r\n" +
"Content-Length: 0\r\n" +
"\r\n");
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//delay(100);
//while(client.available()){
//String line = client.readStringUntil('\r');
//Serial.print(String("RESPONSE ") + line);
//}
client.flush();
//Serial.println();
//Serial.println("Connection closed");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment