SML Smartmeter Code

Hier der aktuelle Code. Die Zugangsdaten des WiFi habe ich aber absichtlich "falsch" eingetragen. Es lohnt sich also nicht in der Nähe zu Parken um auf "Free Internet" zu hoffen.

/*
 * ESP8266 and D=-Smart Metering
 * 
 * Simple Code to collect the SML-Data from a IR-LED and send it using UDP to the local subnet
 * Configuration : Enter the ssid and password of your Wifi AP. Enter the port number your server is listening on.
 *
 *Serial Port RxD is used to read data from Smartmeter. Phototransistor is pulling down the RxD-Line. Should work even with PC connected
 *Serial Port TxD is used to send debug output
 *
 * 20160501 frank@carius.de  initial Version  
 * Portions from  http://www.esp8266.com/viewtopic.php?f=29&t=2222
 */

// Every serial.write or serial.writeln will take long at 9600 baud. So remove them in production

#include <ESP8266WiFi.h>
#include <WiFiUDP.h>
extern "C" {  //required for system_get_chip_id()
#include "user_interface.h"
  // uint16 readvdd33(void);
}

// Global constants for WiFi connections
int status = WL_IDLE_STATUS;
const char* ssid = "FCHAUS";             //  your network SSID (name)  Case sensible !
const char* pass = "wifi4haus";       // your network password
IPAddress udpip(192,168,178,255);       // specify target IP
unsigned int udpport = 12345;           // Specify Source and Target Port

// Buffer for serial reading 
int  serIn;             // var that will hold the bytes-in read from the serialBuffer
byte datagram[1000];    // array that will hold the different bytes 
int  serindex = 0;    // index of serInString[] in which to insert the next incoming byte

int  serialbyte;        // Byte to store serial input data
// Create a UDP instance to send and receive packets over UDP
WiFiUDP Udp;            // Instantiate UDP-Class

//
//  Blink Function for Feedback of Status, takes about 1000ms
//
void blink(int count, int durationon, int durationoff, int delayafter) {
  for (int i=0; i < count; i++) {
    digitalWrite(LED_BUILTIN, LOW);   // Turn the LED on 
    delay(durationon);
    digitalWrite(LED_BUILTIN, HIGH);  // Turn the LED off by making the voltage HIGH
    delay(durationoff);
  }
  delay(delayafter);
}

// ------------------------------------------------
//   SETUP running once at the beginning
// ------------------------------------------------
//   Initialize  Serial, WIFi and UDP
void setup() {
  pinMode(LED_BUILTIN, OUTPUT);     // Initialize the LED_BUILTIN pin as an output
  blink(1,5000,500,1000);   // Signal startup
  Serial.begin(9600);   // Open serial communications and wait for port to open:
  while (!Serial) {
    ; // wait for serial port to connect. 
  }
  Serial.println("ESP8266-DO-Logger init");
  blink(4,100,500,2000);      // Signal Serial OK
  
  //
  // Wait for connect to AP
  //
  Serial.print("Start WiFi to SSID: ");
  Serial.println(ssid);
  while (WiFi.status() != WL_CONNECTED) {
    blink(5,50,100,2000);
    WiFi.begin(ssid, pass);
  }

  // Report Data to Serial Port. 
  Serial.print("Connect with IP:");
  Serial.println(WiFi.localIP());
  blink(6,100,500,2000);   
  
  // Start UDP Object
  Serial.print("Start UDP-Service on port:");
  Serial.println(udpport);
  Udp.begin(udpport);
  // Serial.setTimeout(1000);  // Set Timeout for Serial.readBytesUntil()
  blink(7,100,500,2000);   
  Serial.print("INIT Done");
}


// ------------------------------------------------
//   MAIN LOOP RUNNING all the time
// ------------------------------------------------
void loop() {
  Serial.print("LOOP: ");          // Send some startup data to the console
  Serial.print("SSID: ");               // SSID of Network
  Serial.print(WiFi.SSID());            // SSID ausgeben
  Serial.print("     IP Address: ");    // assigned IP-Address
  IPAddress ip = WiFi.localIP();        // IP Adresse auslesen
  Serial.println(ip);                   // IP Adresse ausgeben
  blink(2,400,100,2000);                // Show status

  //
  // Clear Serial Data
  //
  Serial.println("C"); 
  while (Serial.available()) {
    while (Serial.available()) {
      serialbyte = Serial.read(); //Read Buffer to clear
    }
    //Serial.print("F");
    delay(10);  // wait approx 10 bytes at 9600 Baud to clear bytes in transmission
  }

  //
  // assume, that there is now a pause. Wait for start of transmission
  // 
  Serial.println("W"); 
  int flashcount = 0;
  while (!Serial.available()){
    flashcount++;
    // Serial.println(flashcount);
    if (flashcount == 400) { 
      digitalWrite(LED_BUILTIN, LOW);       // Turn the LED on 
    }
    else if (flashcount > 500) {
      digitalWrite(LED_BUILTIN, HIGH);      // Turn the LED off 
      flashcount=0;
    }
    else {
      delay(5);  // wait 5 ms for new packets
    }
  }

  // We got some bytes. read until next pause
  Serial.println("R"); 
  // Serial.println("Reading serial data"); 
  Serial.setTimeout(500);   // Set Timeout to 500ms.
  serindex = Serial.readBytes(datagram,1000);
  // serindex = Serial.readBytesUntil('',datagram,1000);  // read all serial data and end with timeout.. How to read without looking for stop character 
  //serindex = 0;
  //while (Serial.available() && (serindex < 1000)){
  //  while (Serial.available() && (serindex < 1000)){
  //    serialbyte = Serial.read();   // Read Data with a 1000ms timeout 
  //    datagram[serindex] = serialbyte;
  //    serindex++;
  //  }
  //  delay(10);  // wait 10ms for more bytes
  //}

  if (serindex < 1000) {
    Serial.println("D"); 
    blink(5,100,100,0);
    Serial.print("Datagram received. Total Bytes:");Serial.println(serindex);
  
    //Serial.println("Sending UDP-Paket  XML");
    //Udp.beginPacket(udpip, udpport);  // Start new paket
    //Udp.write("<SMLR  eader>");
    //Udp.write("  <chipid>");
    //Udp.print(system_get_chip_id());
    // Udp.write("#IP of ESP8266#");
    //Udp.write("  </chipid>");
    //Udp.write("  <ipaddress>");
    //Udp.println(WiFi.localIP());
    //Udp.write("  </ipaddress>");
    //Udp.write("  <datagram>");
    //for (int i=0; i < serindex ; i++) {
      //char zeichen = String(datagram[i], HEX)[0];
      //Udp.write(zeichen);
    //  Udp.write(datagram[i]);
    // }
    //Udp.write("  </datagram>");
    //Udp.write("</SMLReader>");
    //Udp.endPacket();   // Send paket
  
    Serial.println("Sending UDP-Paket  RAW");
    Udp.beginPacket(udpip, udpport);  // Start new paket
    Udp.write(datagram,serindex);
    Udp.endPacket();   // Send paket
  }
  else {       // Error out of bounds during reading bytes from serial.
    blink(10,100,100,2000);
  } 
  
  // Serial.println("Sleep 10 Seconds");
  blink(100,30,70,0);
}