Need a unique gift idea?
A Pastebin account makes a great Christmas gift
SHARE
TWEET

Untitled

a guest Sep 19th, 2018 59 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
 
  1. #include <ESP8266WiFi.h>
  2. #include <PubSubClient.h>
  3. #include <OneWire.h>
  4. #include <DallasTemperature.h>
  5.  
  6. unsigned long start = millis();
  7.  
  8. #define RETRIES 5
  9.  
  10. OneWire oneWire1(D1);
  11. DallasTemperature DS18B20_1(&oneWire1);
  12.  
  13. WiFiClient espClient;
  14.  
  15. void slp(int ms) {
  16.   ESP.deepSleep(ms * 1000);
  17. }
  18.  
  19. void setup() {
  20.   Serial.begin(115200);
  21.  
  22.   // WiFi.disconnect(true);
  23.  
  24.   DS18B20_1.begin();
  25.  
  26.   Serial.println(F("Go!"));
  27. }
  28.  
  29. bool sendMQTT(const char *const topic, const float value) {
  30.   PubSubClient client(espClient);
  31.  
  32.   client.setServer("192.168.64.1", 1883);
  33.  
  34.   unsigned long int startM = millis();
  35.  
  36.   for (int i = 0; i < RETRIES; i++) {
  37.     while (!client.connected()) {
  38.       client.connect("watertemp");
  39.       Serial.print(',');
  40.       delay(100);
  41.  
  42.       if (millis() - startM > 5000)
  43.         return false;
  44.     }
  45.  
  46.     char buffer[32] = { 0 };
  47.  
  48.     dtostrf(value, 8, 8, buffer);
  49.     if (client.publish(topic, buffer))
  50.       break;
  51.  
  52.     client.loop();
  53.   }
  54.  
  55.   client.disconnect();
  56.  
  57.   return true;
  58. }
  59.  
  60. void loop() {
  61.   float temp1 = 127.0, temp2 = 127.0, temp3 = 127.0;;
  62.  
  63.   for (int i = 0; i < RETRIES; i++) {
  64.     DS18B20_1.requestTemperatures();
  65.     temp1 = DS18B20_1.getTempCByIndex(0);
  66.     if (temp1 < 80 && temp1 > -120)
  67.       break;
  68.     delay(100);
  69.   }
  70.  
  71.   for (int i = 0; i < RETRIES; i++) {
  72.     temp2 = DS18B20_1.getTempCByIndex(1);
  73.     if (temp2 < 80 && temp2 > -120)
  74.       break;
  75.     delay(100);
  76.     DS18B20_1.requestTemperatures();
  77.   }
  78.  
  79.   for (int i = 0; i < RETRIES; i++) {
  80.     temp3 = DS18B20_1.getTempCByIndex(2);
  81.     if (temp3 < 80 && temp3 > -120)
  82.       break;
  83.     delay(100);
  84.     DS18B20_1.requestTemperatures();
  85.   }
  86.  
  87.   Serial.print(temp1);
  88.   Serial.print(' ');
  89.   Serial.print(temp2);
  90.   Serial.print(' ');
  91.   Serial.print(temp3);
  92.   Serial.println(F(""));
  93.  
  94.   float volt = analogRead(A0) / 0.902 / 1024.0 * 4.2;
  95.   Serial.println(volt);
  96.  
  97.   if (temp1 < 80 && temp2 < 80 && temp3 < 80 && temp1 > -120 && temp2 > -120 && temp3 > - 120) { // 85 degrees == error, -127 == disconnected
  98.     const unsigned long wifiStart = millis();
  99.  
  100.     WiFi.persistent(false);
  101.     WiFi.mode(WIFI_OFF);
  102.     WiFi.mode(WIFI_STA);
  103.  
  104.     WiFi.begin("www.vanheusden.com", "");
  105.  
  106.     for (;;) {
  107.       int rc = WiFi.status();
  108.  
  109.       if (rc == WL_CONNECTED)
  110.         break;
  111.  
  112.       if (rc != WL_DISCONNECTED) {
  113.         WiFi.begin("www.vanheusden.com", "");
  114.         delay(250);
  115.       }
  116.       else {
  117.         delay(100);
  118.       }
  119.  
  120.       Serial.print('[');
  121.       Serial.print(rc);
  122.       Serial.print(']');
  123.  
  124.       if (millis() - start > 60000l) {
  125.         Serial.print("FAIL");
  126.         goto fail;
  127.       }
  128.     }
  129.  
  130.     unsigned long wifiConnectTook = millis() - wifiStart;
  131.  
  132.     sendMQTT("watertmp2-1", temp1); // ignore rc for now
  133.     sendMQTT("watertmp2-2", temp2); // ignore rc for now
  134.     sendMQTT("watertmp2-3", temp3); // ignore rc for now
  135.  
  136.     sendMQTT("watertmp2-batV", volt);
  137.  
  138.     sendMQTT("watertmp2-WRT", wifiConnectTook / 1000.0); // wifi ready time
  139.   }
  140. /*  else {
  141.     char buffer[256];
  142.     snprintf(buffer, sizeof buffer, "failed acquire sensors %d", millis());
  143.     sendString("watertmp2-meta", buffer);
  144.   } */
  145.  
  146.   Serial.print(F("Took: "));
  147.   Serial.println(millis() - start);
  148.   Serial.flush();
  149.  
  150. fail:
  151.   slp(300 * 1000);
  152. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top