Skip to content

Commit

Permalink
simplify esp8266 example
Browse files Browse the repository at this point in the history
  • Loading branch information
chaeplin committed Feb 19, 2016
1 parent 3f6bc82 commit a5db21f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 215 deletions.
238 changes: 32 additions & 206 deletions examples/DHT_esp8266/DHT_esp8266.ino
Original file line number Diff line number Diff line change
@@ -1,49 +1,15 @@
// 80M CPU / esp-test-test
// with #define DHT_DEBUG_TIMING on PietteTech_DHT-8266
#include <ESP8266WiFi.h>
// https://github.com/knolleary/pubsubclient
#include <PubSubClient.h>
#include <WiFiUdp.h>
#include <ESP8266mDNS.h>
#include <ArduinoOTA.h>
#include <WiFiUdp.h>
#include "PietteTech_DHT.h"

#define _IS_MY_HOME
// wifi
#ifdef _IS_MY_HOME
#include "/usr/local/src/ap_setting.h"
#else
#include "ap_setting.h"
#endif

// system defines
#define DHTTYPE DHT22 // Sensor type DHT11/21/22/AM2301/AM2302
#define DHTPIN 2 // Digital pin for communications
#define DHT_SAMPLE_INTERVAL 2000

#define REPORT_INTERVAL 5000 // in msec

String macToStr(const uint8_t* mac);
void sendmqttMsg(char* topictosend, String payload);

const char* ssid = WIFI_SSID;
const char* password = WIFI_PASSWORD;
const char* otapassword = OTA_PASSWORD;

IPAddress mqtt_server = MQTT_SERVER;

char* topic = "pubtest";

String clientName;
long lastReconnectAttempt = 0;
// to check dht
unsigned long startMills;
float t, h;
float t, h, d;
int acquireresult;
unsigned int DHTnextSampleTime;

WiFiClient wifiClient;
PubSubClient client(mqtt_server, 1883, wifiClient);

//declaration
void dht_wrapper(); // must be declared before the lib initialization
Expand All @@ -60,198 +26,58 @@ void dht_wrapper() {
DHT.isrCallback();
}

boolean reconnect()
{
if (!client.connected()) {
if (client.connect((char*) clientName.c_str())) {
Serial.println("===> mqtt connected");
} else {
Serial.print("---> mqtt failed, rc=");
Serial.println(client.state());
}
}
return client.connected();
}

void wifi_connect()
{
if (WiFi.status() != WL_CONNECTED) {
// WIFI
Serial.println();
Serial.print("===> WIFI ---> Connecting to ");
Serial.println(ssid);
delay(10);

WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);

int Attempt = 0;
while (WiFi.status() != WL_CONNECTED) {
Serial.print(". ");
Serial.print(Attempt);
delay(100);
Attempt++;
if (Attempt == 250)
{
Serial.println();
Serial.println("-----> Could not connect to WIFI");
ESP.restart();
delay(200);
}

}
Serial.println();
Serial.print("===> WiFi connected");
Serial.print(" ------> IP address: ");
Serial.println(WiFi.localIP());
}
}


void setup()
{
startMills = millis();
Serial.begin(115200);

wifi_connect();
Serial.println("");
Serial.println("DHT Example program using DHT.acquire and DHT.acquireAndWait");
Serial.println("");

clientName += "esp8266-";
uint8_t mac[6];
WiFi.macAddress(mac);
clientName += macToStr(mac);
clientName += "-";
clientName += String(micros() & 0xff, 16);

Serial.println(clientName);

//OTA
// Port defaults to 8266
//ArduinoOTA.setPort(8266);

// Hostname defaults to esp8266-[ChipID]
ArduinoOTA.setHostname("esp-test-test");

// No authentication by default
ArduinoOTA.setPassword(otapassword);

ArduinoOTA.onStart([]() {
//Serial.println("Start");
});
ArduinoOTA.onEnd([]() {
//Serial.println("\nEnd");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
//Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
});
ArduinoOTA.onError([](ota_error_t error) {
//ESP.restart();
if (error == OTA_AUTH_ERROR) abort();
else if (error == OTA_BEGIN_ERROR) abort();
else if (error == OTA_CONNECT_ERROR) abort();
else if (error == OTA_RECEIVE_ERROR) abort();
else if (error == OTA_END_ERROR) abort();

});

ArduinoOTA.begin();
// blocking method
acquireresult = DHT.acquireAndWait(1000);
if ( acquireresult == 0 ) {
t = DHT.getCelsius();
h = DHT.getHumidity();
d = DHT.getDewPoint();
} else {
t = h = 0;
t = h = d = 0;
}
}

void loop()
{
if (WiFi.status() == WL_CONNECTED) {
if (!client.connected()) {
long now = millis();
if (now - lastReconnectAttempt > 200) {
lastReconnectAttempt = now;
if (reconnect()) {
lastReconnectAttempt = 0;
}
}
} else {

if (bDHTstarted) {
if (!DHT.acquiring()) {
acquireresult = DHT.getStatus();
if ( acquireresult == 0 ) {
t = DHT.getCelsius();
h = DHT.getHumidity();
}
bDHTstarted = false;
}
{
if (bDHTstarted) {
if (!DHT.acquiring()) {
acquireresult = DHT.getStatus();
if ( acquireresult == 0 ) {
t = DHT.getCelsius();
h = DHT.getHumidity();
d = DHT.getDewPoint();
}

if ((millis() - startMills) > REPORT_INTERVAL) {
String payload ;
payload += "{\"startMills\":";
payload += millis();
payload += ",\"Temperature\":";
payload += t;
payload += ",\"Humidity\":";
payload += h;
payload += ",\"acquireresult\":";
payload += acquireresult;
payload += ",\"FreeHeap\":";
payload += ESP.getFreeHeap();
payload += ",\"RSSI\":";
payload += WiFi.RSSI();
payload += "}";

sendmqttMsg(topic, payload);
startMills = millis();

if (!bDHTstarted) {
DHT.acquire();
bDHTstarted = true;
}
}
client.loop();
bDHTstarted = false;
}
ArduinoOTA.handle();
} else {
wifi_connect();
}
}

void sendmqttMsg(char* topictosend, String payload)
{
if (client.connected()) {
Serial.print("Sending payload: ");
Serial.print(payload);

unsigned int msg_length = payload.length();
if ((millis() - startMills) > REPORT_INTERVAL) {
Serial.println("");

Serial.print("Humidity (%): ");
Serial.println(h);

Serial.print(" length: ");
Serial.println(msg_length);
Serial.print("Temperature (oC): ");
Serial.println(t);

byte* p = (byte*)malloc(msg_length);
memcpy(p, (char*) payload.c_str(), msg_length);
Serial.print("Dew Point (oC): ");
Serial.println(d);

startMills = millis();

if ( client.publish(topictosend, p, msg_length)) {
Serial.println("Publish ok");
free(p);
//return 1;
} else {
Serial.println("Publish failed");
free(p);
//return 0;
if (!bDHTstarted) {
// non blocking method
DHT.acquire();
bDHTstarted = true;
}
}
}

String macToStr(const uint8_t* mac)
{
String result;
for (int i = 0; i < 6; ++i) {
result += String(mac[i], 16);
if (i < 5)
result += ':';
}
return result;
}
9 changes: 0 additions & 9 deletions examples/DHT_esp8266/ap_setting.h

This file was deleted.

0 comments on commit a5db21f

Please sign in to comment.