/* EmonTx CT123 Voltage Serial Only example Part of the openenergymonitor.org project Licence: GNU GPL V3 Author: Trystan Lea Added WiFi support with ESP8266 Added EEPROM config Author: Dimitris Tassopoulos */ #include #include #include "EmonLib.h" #define AP_SSID "linksys" // use your AP SSID name #define AP_PASSWD "password" // use your AP wpa/wpa2 password #define AP_SERVER_IP "10.10.1.10" // use your own ip #define AP_SERVER_PORT 7171 //#define DEBUG_WIFI #ifdef DEBUG_WIFI #define DEBUG(X) Serial.print(X) #define DEBUGL(X) Serial.println(X) #define DEBUGF(X) Serial.print(F(X)) #define DEBUGFL(X) Serial.println(F(X)) #else #define DEBUG(X) {} #define DEBUGL(X) {} #define DEBUGF(X) {} #define DEBUGFL(X) {} #endif // EEPROM config structure // typedef struct { uint16_t eeprom_header; char wifi_ssid[40]; char wifi_passwd[32]; char udp_ip[16]; uint16_t port; uint16_t eeprom_cs; } tp_conf; tp_conf conf; // UDP packet // typedef struct { double irms; double realPower; double aparentPower; double powerFactor; } tp_ct_val; typedef struct { double vrms; tp_ct_val ct1; tp_ct_val ct2; tp_ct_val ct3; } tp_udp_packet; tp_udp_packet glb_udp_packet; EnergyMonitor ct1,ct2,ct3; const int LEDpin = 9; // Software serial port (leave Serial0 for debuging) SoftwareSerial wifi(2, 13); // RX,TX void setup() { CONF_Read(); // Read config from EEPROM wifi.begin(9600); // Start soft/wifi uart Serial.begin(9600); Serial.println("emonTX Shield CT123 Voltage Serial Only example"); Serial.println("OpenEnergyMonitor.org"); ct1.current(1, 59.206); ct2.current(2, 59.206); ct3.current(3, 59.206); ct1.voltage(0, 215.2, 1.25); ct2.voltage(0, 215.2, 1.35); ct3.voltage(0, 215.2, 1.45); ESP8266_Init(); // Initialize ESP8266 pinMode(LEDpin, OUTPUT); // Heartbeat LED digitalWrite(LEDpin, HIGH); } void loop() { // Measure ct1.calcVI(11,2000); ct2.calcVI(11,2000); ct3.calcVI(11,2000); // Load values PrepareUDPdata(); // Just copy ctx values to udp struct // Print Values PrintValues(); // Comment out if it's not needed // If connection status is connected then send data if (ESP8266_CIPSTATUS()) { ESP8266_CIPSENDSingle((uint8_t*) &glb_udp_packet, sizeof(tp_udp_packet)); } // else try reconnect and re-bind to the UDP port else { ESP8266_Init(); } delay(2000); } void PrintValues(void) { Serial.print("Vrms:"); Serial.println(ct1.Vrms); Serial.print("CT1:"); Serial.print(ct1.Irms); Serial.print(" - "); Serial.print(ct1.realPower); Serial.print(" - "); Serial.print(ct1.apparentPower); Serial.print(" - "); Serial.println(ct1.powerFactor); Serial.print("CT2: "); Serial.print(ct2.Irms); Serial.print(" - "); Serial.print(ct2.realPower); Serial.print(" - "); Serial.print(ct2.apparentPower); Serial.print(" - "); Serial.println(ct2.powerFactor); Serial.print("CT3: "); Serial.print(ct3.Irms); Serial.print(" - "); Serial.print(ct3.realPower); Serial.print(" - "); Serial.print(ct3.apparentPower); Serial.print(" - "); Serial.println(ct3.powerFactor); Serial.println(); } void PrepareUDPdata(void) { glb_udp_packet.vrms = ct1.Vrms; // CT1 glb_udp_packet.ct1.irms = ct1.Irms; glb_udp_packet.ct1.realPower = ct1.realPower; glb_udp_packet.ct1.aparentPower = ct1.apparentPower; glb_udp_packet.ct1.powerFactor = ct1.powerFactor; // CT2 glb_udp_packet.ct2.irms = ct2.Irms; glb_udp_packet.ct2.realPower = ct2.realPower; glb_udp_packet.ct2.aparentPower = ct2.apparentPower; glb_udp_packet.ct2.powerFactor = ct2.powerFactor; // CT3 glb_udp_packet.ct3.irms = ct3.Irms; glb_udp_packet.ct3.realPower = ct3.realPower; glb_udp_packet.ct3.aparentPower = ct3.apparentPower; glb_udp_packet.ct3.powerFactor = ct3.powerFactor; } /** * Read configuration data from EEPROM and if they don't exists then * load defaults. */ void CONF_Read(void) { uint8_t i; uint8_t * p = (uint8_t*) &conf; uint8_t valid_data = false; Serial.print(F("EEPROM content:")); for (i=0; i 0) { wifi.read(); } } String recvString(String target, uint32_t timeout = 1000) { String data; char a; unsigned long start = millis(); while (millis() - start < timeout) { while(wifi.available() > 0) { a = wifi.read(); if(a == '\0') continue; data += a; } if (data.indexOf(target) != -1) { break; } } // Debug print received data DEBUGF("R:"); DEBUGL(data); return data; } bool recvFind(String target, uint32_t timeout) { String data_tmp; data_tmp = recvString(target, timeout); if (data_tmp.indexOf(target) != -1) { return true; } return false; } bool ESP8266_AT(void) { rx_empty(); wifi.println("AT"); return recvFind("OK", 1000); } bool ESP8266_RST(void) { rx_empty(); wifi.println("AT+RST"); return recvFind("Ready", 10000); } bool ESP8266_CWMODE(int mode) { String data; rx_empty(); wifi.print("AT+CWMODE="); wifi.println(mode); data = recvString("OK", 3000); if (data.indexOf("OK") != -1) { return true; } return false; } bool ESP8266_CWJAP(String ssid, String pwd) { String data; rx_empty(); wifi.print("AT+CWJAP=\""); wifi.print(ssid); wifi.print("\",\""); wifi.print(pwd); wifi.println("\""); data = recvString("OK", 10000); if (data.indexOf("OK") != -1) { return true; } return false; } bool ESP8266_CIPMUX(int mux) { String data; rx_empty(); wifi.print("AT+CIPMUX="); wifi.println(mux); data = recvString("OK", 3000); if (data.indexOf("OK") != -1) { return true; } return false; } bool ESP8266_CIPSTART(String ip, int port) { String data; rx_empty(); wifi.print("AT+CIPSTART=\"UDP\",\""); wifi.print(ip); wifi.print("\","); wifi.println(port); data = recvString("OK", 3000); if (data.indexOf("OK") != -1) { return true; } return false; } bool ESP8266_CIPSTATUS(void) { String data; bool result = false; delay(100); rx_empty(); wifi.println("AT+CIPSTATUS"); data = recvString("\r\n\r\nOK"); int index = data.indexOf("+CIPSTATUS:"); DEBUGF("index = "); DEBUGL(index); index = data.indexOf("STATUS:"); DEBUGF("index = "); DEBUGL(index); if (index) { int status = data.substring(index+7, index+7+1).toInt(); DEBUGF("UDP status = "); DEBUGL(status); if (status == 5) { result = true; } } return(result); } bool ESP8266_CIPSENDSingle(const uint8_t *buffer, uint32_t len) { rx_empty(); wifi.print("AT+CIPSEND="); wifi.println(len); if (recvFind(">", 5000)) { rx_empty(); for (uint32_t i = 0; i < len; i++) { wifi.write(buffer[i]); } return recvFind("SEND OK", 10000); } return false; }