Skip to content

Instantly share code, notes, and snippets.

@chaeplin
Created March 4, 2016 10:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chaeplin/7f7727c690a00b616bdf to your computer and use it in GitHub Desktop.
Save chaeplin/7f7727c690a00b616bdf to your computer and use it in GitHub Desktop.
dht_test2.ino
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include "PietteTech_DHT.h"
// ap setting
#include "/usr/local/src/ap_setting.h"
extern "C" {
#include "user_interface.h"
}
#define DHT_DEBUG_TIMING
// to check battery voltage using internal adc
ADC_MODE(ADC_VCC);
#define IPSET_STATIC { 192, 168, 10, 26 }
#define IPSET_GATEWAY { 192, 168, 10, 1 }
#define IPSET_SUBNET { 255, 255, 255, 0 }
#define IPSET_DNS { 192, 168, 10, 10 }
IPAddress ip_static = IPSET_STATIC;
IPAddress ip_gateway = IPSET_GATEWAY;
IPAddress ip_subnet = IPSET_SUBNET;
IPAddress ip_dns = IPSET_DNS;
//
IPAddress influxdbudp = MQTT_SERVER;
// wifi
const char* ssid = WIFI_SSID;
const char* password = WIFI_PASSWORD;
//
int vdd;
unsigned int localPort = 2390;
WiFiClient wifiClient;
WiFiUDP udp;
// system defines
#define DHTTYPE DHT22 // Sensor type DHT11/21/22/AM2301/AM2302
#define DHTPIN 2 // Digital pin for communications
#define REPORT_INTERVAL 10 // in sec
// to check dht
unsigned long startMills;
float t, h;
int acquireresult;
//declaration
void dht_wrapper(); // must be declared before the lib initialization
// Lib instantiate
PietteTech_DHT DHT(DHTPIN, DHTTYPE, dht_wrapper);
// globals
bool bDHTstarted; // flag to indicate we started acquisition
// This wrapper is in charge of calling
// must be defined like this for the lib work
void dht_wrapper() {
DHT.isrCallback();
}
void sendUdpmsg(String msgtosend) {
unsigned int msg_length = msgtosend.length();
byte* p = (byte*)malloc(msg_length);
memcpy(p, (char*) msgtosend.c_str(), msg_length);
udp.beginPacket(influxdbudp, 8089);
udp.write(p, msg_length);
udp.endPacket();
free(p);
}
void printEdgeTiming(class PietteTech_DHT *_d) {
byte n;
#if defined(DHT_DEBUG_TIMING)
volatile uint8_t *_e = &_d->_edges[0];
#endif
String udppayload = "edges2,device=esp-12-N3,debug=on,DHTLIB_ONE_TIMING=110 ";
for (n = 0; n < 41; n++) {
char buf[2];
udppayload += "e";
sprintf(buf, "%02d", n);
udppayload += buf;
udppayload += "=";
#if defined(DHT_DEBUG_TIMING)
udppayload += *_e++;
#endif
udppayload += "i,";
}
udppayload += "Freq=";
udppayload += ESP.getCpuFreqMHz();
udppayload += "i,H=";
udppayload += _d->getHumidity();
udppayload += ",T=";
udppayload += _d->getCelsius();
udppayload += ",vdd=";
udppayload += vdd;
udppayload += "i";
sendUdpmsg(udppayload);
}
void goingToSleep()
{
Serial.println(" -----> goingToSleep");
delay(100);
ESP.deepSleep((REPORT_INTERVAL * 1000 * 1000 ));
delay(250);
}
void wifi_connect() {
//wifi_set_phy_mode(PHY_MODE_11N);
//system_phy_set_rfoption(0);
//WiFi.config(ip_static, ip_gateway, ip_subnet, ip_dns);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
WiFi.hostname("esp-dht22-deepsleeptest");
int Attempt = 0;
while (WiFi.status() != WL_CONNECTED) {
Serial.print(". ");
Serial.print(Attempt);
delay(100);
Attempt++;
if (Attempt == 300)
{
// if fail
ESP.reset();;
}
}
}
void setup()
{
startMills = millis();
//wifi_status_led_uninstall();
Serial.begin(115200);
Serial.println("");
vdd = ESP.getVcc() * 0.96;
Serial.println(vdd);
//vdd = 0;
delay(2000);
acquireresult = DHT.acquireAndWait(500);
Serial.println(acquireresult);
if (acquireresult == 0) {
t = DHT.getCelsius();
h = DHT.getHumidity();
Serial.println(t);
Serial.println(h);
} else {
ESP.reset();
}
delay(2000);
acquireresult = DHT.acquireAndWait(500);
Serial.println(acquireresult);
if (acquireresult == 0) {
t = DHT.getCelsius();
h = DHT.getHumidity();
Serial.println(t);
Serial.println(h);
} else {
ESP.reset();
}
Serial.println("wifi on");
wifi_connect();
Serial.println("wifi conn done");
if (WiFi.status() == WL_CONNECTED) {
Serial.println("");
Serial.println("reporting using udp ---> : ");
// report
#if defined(DHT_DEBUG_TIMING)
printEdgeTiming(&DHT);
#endif
goingToSleep();
} else {
goingToSleep();
}
}
void loop()
{
goingToSleep();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment