Attachment 'WiFiLoggerOneWire.c'

Download

   1 /*
   2  Program: WiFiLoggerOneWire.ino
   3  UDP NTP Client + Real Time Clock (no backup, just NTP sync)
   4 
   5  Get the time from a Network Time Protocol (NTP) time server
   6  Demonstrates use of UDP sendPacket and ReceivePacket
   7  For more on NTP time servers and the messages needed to communicate with them,
   8  see http://en.wikipedia.org/wiki/Network_Time_Protocol
   9 
  10  created 4 Sep 2010
  11  by Michael Margolis
  12  modified 9 Apr 2012
  13  by Tom Igoe
  14  updated for the ESP8266 12 Apr 2015 
  15  by Ivan Grokhotkov
  16 
  17  This code is in the public domain.
  18 
  19  2015-12-14 Real Time Clock added by Rudolf Reuter
  20  2015-12-15 Logger One Wire, send temperature to volkszaehler.org
  21  2015-12-17 version 1.1,  chane from POST with time to GET without time
  22  2015-12-21 version 1.2, if rooter access does not succeed, start Access Point
  23             and fetch the WiFi data from the user. Then restart.
  24 
  25  https://www.aritso.net/mehr-informationen/tools/timestampconverter.htm
  26  */
  27 
  28 #include <ESP8266WiFi.h>
  29 #include <WiFiUdp.h>
  30 #include <DNSServer.h>
  31 #include <ESP8266mDNS.h>
  32 #include <ESP8266WebServer.h>
  33 #include <OneWire.h>
  34 #include <EEPROM.h>
  35 #include "WiFiLoggerOneWire.h"
  36 
  37 // LED is needed for failure signalling
  38 const short int BUILTIN_LED2 = 16;  //GPIO16 on NodeMCU (ESP-12)
  39 
  40 String versionP = "1.2";  //  Version of the program
  41 #define GETmethod  // else POST method with time
  42 
  43 // Set these to your desired softAP credentials. They are not configurable at runtime 
  44 const char *softAP_ssid = "esp_ap";
  45 const char *softAP_password = "12345678";
  46 
  47 // hostname for mDNS. Try http://logger_one_wire.local 
  48 const char *myHostname = "logger_one_wire";
  49 
  50 // Don't set this wifi credentials. They are configurated at runtime and stored on EEPROM 
  51 char ssid[32] = "";
  52 char password[32] = "";
  53 
  54 //-----------------------
  55 
  56 // DNS server
  57 const byte DNS_PORT = 53;
  58 DNSServer serverDNS;
  59 /* Soft Access Point network parameters */
  60 IPAddress apIP(192, 168, 4, 1);
  61 IPAddress netMsk(255, 255, 255, 0);
  62 //-------------------------
  63 
  64 boolean debug = false;  // true = more messages
  65 //boolean debug = true;
  66 
  67 /** Current WLAN status */
  68 int status = WL_IDLE_STATUS;
  69 
  70 // Web server for credentials query
  71 ESP8266WebServer server(80);
  72 
  73 // for volkszaehler.org access
  74 const char* serverVZ = "volkszaehler";
  75 String UUID_tempWW = "e1131330-a27e-11e5-94d4-ed7230fae87c"; 
  76 
  77 WiFiClient client;
  78 
  79 // for OneWire
  80 OneWire  ds(2);  // on pin 2 (a 4.7K resistor is necessary)
  81 byte b_idx;
  82 byte b_present = 0;
  83 byte b_type_s;
  84 byte b_data[12];
  85 byte b_addr[8];
  86 float celsius;
  87 String strCelsius;
  88 // show temperature after conversion time
  89 boolean showTemp = false;
  90 unsigned long convTime = millis(); // conversion time about 1000 ms
  91 
  92 // cycle time in loop()
  93 const unsigned long cycleTime=60000;  // ms
  94 #ifndef GETmethod
  95 const unsigned long sec24h=24 * 3600; // one day in seconds
  96 boolean startSync=false;
  97 #endif
  98 
  99 unsigned long timeStart;
 100 unsigned long timeRel;
 101 
 102 // for NTP time server
 103 unsigned int localPort = 2390;      // local port to listen for UDP packets
 104 unsigned long epoch;
 105 unsigned long timeNTPstart;
 106 unsigned long timeUnix;
 107 
 108 /* Don't hardwire the IP address or we won't get the benefits of the pool.
 109  *  Lookup the IP address for the host name instead */
 110 //IPAddress timeServer(129, 6, 15, 28); // time.nist.gov NTP server
 111 IPAddress timeServerIP; // time.nist.gov NTP server address
 112 const char* ntpServerName = "time.nist.gov";
 113 
 114 const int NTP_PACKET_SIZE = 48; // NTP time stamp is in the first 48 bytes of the message
 115 
 116 byte packetBuffer[ NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets
 117 
 118 // A UDP instance to let us send and receive packets over UDP
 119 WiFiUDP udp;
 120 // End of NTP time server
 121 
 122 // provide text for the WiFi status
 123 const char *str_status[]= {
 124   "WL_IDLE_STATUS",
 125   "WL_NO_SSID_AVAIL",
 126   "WL_SCAN_COMPLETED",
 127   "WL_CONNECTED",
 128   "WL_CONNECT_FAILED",
 129   "WL_CONNECTION_LOST",
 130   "WL_DISCONNECTED"
 131 };
 132 
 133 // provide text for the WiFi mode
 134 const char *str_mode[]= { "WIFI_OFF", "WIFI_STA", "WIFI_AP", "WIFI_AP_STA" };
 135 
 136 
 137 //----------------------- WiFi handling
 138 void connectWifi() {
 139   Serial.print("Connecting as wifi client to SSID: ");
 140   Serial.println(ssid);
 141 
 142   // use in case of mode problem
 143   WiFi.disconnect();
 144   // switch to Station mode
 145   if (WiFi.getMode() != WIFI_STA) {
 146     WiFi.mode(WIFI_STA);
 147   }
 148 
 149   WiFi.begin ( ssid, password );
 150 
 151   if (debug ) WiFi.printDiag(Serial);
 152 
 153   // ... Give ESP 10 seconds to connect to station.
 154   unsigned long startTime = millis();
 155   while (WiFi.status() != WL_CONNECTED && millis() - startTime < 10000) {
 156     delay(500);
 157     Serial.print(".");
 158   }
 159   Serial.println("");
 160   // Check connection
 161   if (WiFi.status() == WL_CONNECTED) {
 162     // print the received signal strength:
 163     long rssi = WiFi.RSSI();
 164     Serial.print("signal strength (RSSI):");
 165     Serial.print(rssi);
 166     Serial.println(" dBm");
 167     Serial.print("WiFi connected; IP address: ");
 168     Serial.println(WiFi.localIP());
 169     // signal WiFi connect
 170     digitalWrite(BUILTIN_LED2, LOW);
 171     delay(300); // ms
 172     digitalWrite(BUILTIN_LED2, HIGH); 
 173   } else {
 174     Serial.print("WiFi connect failed to ssid: ");
 175     Serial.println(ssid);
 176     Serial.print("WiFi password <");
 177     Serial.print(password);
 178     Serial.println(">");
 179     Serial.println("Check for wrong typing!");
 180   }
 181 }  // connectWiFi()
 182 
 183 void signalError() {  // loop endless with LED blinking in case of error
 184   while(1) {
 185       digitalWrite(BUILTIN_LED2, LOW);
 186       delay(300); // ms
 187       digitalWrite(BUILTIN_LED2, HIGH);
 188       delay(300); // ms
 189   }
 190 }
 191 
 192 // declare telnet server (do NOT put in setup()), from example WiFiTelnetToSerial.ino
 193 WiFiServer telnetServer(23);
 194 WiFiClient serverClient;
 195 
 196 
 197 void setup() 
 198 {
 199   delay(1000);
 200   Serial.begin(115200);
 201   delay(1000);
 202   Serial.println("Sync,Sync,Sync,Sync,Sync");
 203   delay(500);
 204   Serial.println();
 205   // signal start
 206   pinMode(BUILTIN_LED2, OUTPUT);
 207   digitalWrite(BUILTIN_LED2, LOW);
 208   delay(100); // ms
 209   digitalWrite(BUILTIN_LED2, HIGH);
 210   delay(300); // ms
 211 
 212   Serial.print("Program Version: ");
 213   Serial.println(versionP);
 214   Serial.print("Chip ID: 0x");
 215   Serial.println(ESP.getChipId(), HEX);
 216   Serial.print("MAC address Station: ");
 217   String macAdrSta = WiFi.macAddress();
 218   Serial.println(macAdrSta);
 219 
 220   loadCredentials(); // Load WLAN credentials from EEPROM
 221 
 222   Serial.println ( "Connect to Router requested" );
 223   connectWifi();
 224   if (WiFi.status() != WL_CONNECTED) {  // switch to Access Point mode
 225     // setup Web Sever handles for HTTP access
 226     server.onNotFound ( handleNotFound );
 227     server.on("/", handleRoot);
 228     server.on("/wifi", handleWifi);
 229     server.on("/wifisave", handleWifiSave);
 230     Serial.println("");
 231     Serial.println("WiFi connect failed, try Access Point mode for configuration");
 232     Serial.print("Configuring as access point, SSID: ");
 233     Serial.println(softAP_ssid);
 234     /* You can remove the password parameter if you want the AP to be open. */
 235     WiFi.mode(WIFI_AP_STA);  // Access Point & Station mode
 236     delay(10);
 237     WiFi.softAPConfig(apIP, apIP, netMsk);
 238     WiFi.softAP(softAP_ssid, softAP_password);
 239     WiFi.begin();  // for test
 240     delay(500); // Without delay I've seen the IP address blank
 241     Serial.print("AP IP address: ");
 242     Serial.println(WiFi.softAPIP());
 243     delay(1000);
 244     Serial.print ( "Status: " );
 245     Serial.println (str_status[WiFi.status()]);
 246     
 247     server.begin(); // Web server start
 248     Serial.println("HTTP server started");
 249     digitalWrite(BUILTIN_LED2, LOW);
 250     delay(300); // ms
 251     digitalWrite(BUILTIN_LED2, HIGH);
 252     delay(300); // ms
 253     digitalWrite(BUILTIN_LED2, LOW);
 254     delay(300); // ms
 255     digitalWrite(BUILTIN_LED2, HIGH);     
 256   } 
 257   
 258 #ifndef GETmethod
 259   Serial.println("Starting UDP");
 260   udp.begin(localPort);
 261   Serial.print("Local port: ");
 262   Serial.println(udp.localPort());
 263 
 264  // ... Give NTP 10 seconds to get the time.
 265   unsigned long startTime = millis();
 266   while (epoch == 0 && millis() - startTime < 10000) {
 267     delay(500);
 268     Serial.print(".");
 269     getNTPtime();
 270   }
 271   Serial.println("");
 272   if (epoch == 0) {
 273     Serial.println("");
 274     Serial.println("NTP access failed, push RESET button.");
 275     signalError();
 276   }
 277   timeNTPstart = epoch;
 278 #endif
 279 
 280   timeStart = millis();
 281   // http://playground.arduino.cc/Code/TimingRollover
 282   timeRel = millis() + cycleTime; // avoid unsigned long rollover after 40 days
 283 
 284   // search for DS18B20 sensors
 285   initSensors();
 286 
 287   telnetServer.begin();
 288   telnetServer.setNoDelay(true);
 289   Serial.println("Now start Telnet client");
 290 
 291   Serial.print("Free Heap[B]: ");
 292   Serial.println(ESP.getFreeHeap());
 293 
 294   // Setup the DNS server redirecting all the domains to the apIP 
 295   serverDNS.setErrorReplyCode(DNSReplyCode::NoError);
 296   serverDNS.start(DNS_PORT, "*", apIP);
 297   // Setup MDNS responder
 298   if (!MDNS.begin(myHostname)) {
 299     Serial.println("Error setting up MDNS responder!");
 300   } else {
 301     Serial.println("mDNS responder started");
 302     // Add service to MDNS-SD
 303     MDNS.addService("http", "tcp", 80);
 304   }
 305 }  // setup()
 306 
 307 void loop()
 308 {
 309   if( (long)( millis() - timeRel ) >= 0) {  // run every cycleTime
 310     timeRel = millis() + cycleTime;
 311 #ifndef GETmethod
 312     if (startSync) {
 313       getNTPtime();
 314       if (epoch > 0) {
 315         timeNTPstart = epoch;
 316         timeStart = millis();
 317         startSync = false;
 318       }
 319     }
 320     if (timeUnix > 0) {
 321       Serial.print("Unix timestamp: ");
 322       Serial.println(timeUnix);
 323       unixToUTC(timeUnix);
 324     }
 325 #endif
 326     // start Temperature capture
 327     ds.reset();
 328     ds.select(b_addr);
 329     ds.write(0x44, 1);   // start conversion, with parasite power on at the end 
 330     showTemp = true;
 331     convTime = millis();
 332   }
 333   if (showTemp && millis() - convTime > 1000) {  // 1000 ms conversion time
 334     showTemp = false;
 335     readTemp();
 336     strCelsius = String(celsius, 1); // 1 decimal place only
 337     Serial.print(strCelsius);
 338     Serial.println(" °C ");
 339     // send temperature to volkszaehler.org
 340     // use Firefox Add-on HttpRequester for debug
 341     if (client.connect(serverVZ, 80)) {
 342       //String cmd = "POST /volkszaehler.org/htdocs/middleware.php/data/";
 343 #ifndef GETmethod
 344       String cmd = "POST /middleware.php/data/"; // depends on Apache2 virtualhost settings.
 345       cmd += UUID_tempWW;
 346       cmd += ".json?ts=";
 347       cmd += String(timeUnix);  // must be in ms -> "000"
 348       cmd += "000&value=";
 349 #else  // GET method
 350       String cmd = "GET /middleware.php/data/"; // depends on Apache2 virtualhost settings.
 351       cmd += UUID_tempWW;
 352       cmd += ".json?operation=add&value=";
 353 #endif
 354       cmd += strCelsius;
 355       cmd += " HTTP/1.1\r\nHost:";
 356       cmd += serverVZ;
 357       cmd += "\r\n";
 358       cmd += "Content-Type: application/json\r\n";
 359       cmd += "Connection: keep-alive\r\nAccept: */*\r\n\r\n";
 360       client.print(cmd);
 361       Serial.print(cmd);
 362       delay(800);  // otherwise you will not see the acknowledge
 363     } else Serial.println("Connect to volkszaehler server failed");
 364     // Print Update Response to Serial Monitor
 365     while (client.available()) {
 366       char c = client.read();
 367       Serial.print(c);
 368     }
 369     Serial.println();
 370     client.stop();
 371     
 372     // Test for Telnet as Serial Monitor - Connect at PGM Start
 373     if (serverClient && serverClient.connected()) {
 374       serverClient.print(strCelsius);
 375       serverClient.print(" °C, sec.: ");
 376       serverClient.println(millis()/1000);
 377       serverClient.print("Free Heap[B]: ");
 378       serverClient.println(ESP.getFreeHeap());
 379     }
 380   }
 381   
 382   //check if there is any new serverClient on the telnetServer
 383   if (telnetServer.hasClient()) {
 384     if (!serverClient || !serverClient.connected()) {
 385       if (serverClient) {
 386         serverClient.stop();
 387       }
 388       serverClient = telnetServer.available();
 389       Serial.println("New Telnet client"); 
 390       serverClient.flush();  // clear input buffer, else you get strange characters
 391     } 
 392   }
 393   while(serverClient.available()) {  // get data from Client
 394       Serial.write(serverClient.read());
 395   }
 396   
 397 #ifndef GETmethod
 398   timeUnix = (millis() - timeStart) / 1000 + timeNTPstart;
 399   // synch clock with NTP every 24h, must run every second
 400   if ((timeUnix % sec24h) == 0) {
 401     startSync = true;
 402   }
 403 #endif
 404   delay(10);  // to avoid strange characters in buffer
 405 
 406   // DNS
 407   serverDNS.processNextRequest();
 408   // HTTP
 409   server.handleClient();
 410 }

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2015-12-20 15:34:00, 3.3 KB) [[attachment:GetTemp.c]]
  • [get | view] (2015-12-18 19:00:00, 79.9 KB) [[attachment:Thingspeak_WarmWasser.png]]
  • [get | view] (2015-12-21 15:53:00, 11.9 KB) [[attachment:WiFiLoggerOneWire.c]]
  • [get | view] (2015-12-20 15:35:00, 1.0 KB) [[attachment:WiFiLoggerOneWire.h]]
  • [get | view] (2015-12-15 21:02:00, 77.5 KB) [[attachment:WiFiLoggerOneWireVZ.png]]
  • [get | view] (2015-12-20 15:05:00, 103.1 KB) [[attachment:WiFiLoggerOneWire_config.png]]
  • [get | view] (2015-12-18 19:51:00, 89.0 KB) [[attachment:WiFiLogger_DSC06322.jpg]]
  • [get | view] (2016-05-08 10:40:00, 36.5 KB) [[attachment:WiFiLogger_screenshot.png]]
  • [get | view] (2016-06-20 05:37:00, 35.1 KB) [[attachment:WiFiLogger_screenshot2.png]]
  • [get | view] (2015-12-15 14:38:00, 100.5 KB) [[attachment:WiFi_Logger_DSC06319.jpg]]
  • [get | view] (2015-12-20 15:33:00, 0.7 KB) [[attachment:credentials.c]]
  • [get | view] (2015-12-20 15:33:00, 3.9 KB) [[attachment:getNTPtime.c]]
  • [get | view] (2015-12-20 15:34:00, 5.4 KB) [[attachment:handleHTTP.c]]
  • [get | view] (2015-12-20 15:35:00, 0.4 KB) [[attachment:tools.c]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.