Last active
August 29, 2015 14:22
-
-
Save xseignard/bcb1f612f0a2cc57bd17 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <SoftwareSerial.h> | |
| char inChar; | |
| String data = ""; | |
| SoftwareSerial teleInfo(2, 3); | |
| int watts; | |
| void setup() { | |
| Serial.begin(9600); | |
| teleInfo.begin(1200); | |
| delay(1000); | |
| } | |
| void loop() { | |
| if (teleInfo.available()) { | |
| inChar = teleInfo.read() & 0x7F; | |
| if (inChar == '\n') { | |
| int current = getIINST(); | |
| if (current > 0) { | |
| watts = current * 230; | |
| Serial.print("Watts: "); | |
| Serial.println(watts); | |
| } | |
| } | |
| else { | |
| data += inChar; | |
| } | |
| } | |
| } | |
| int getIINST() { | |
| int iinst = -1; | |
| // check if line is at least 39 chars long | |
| if(data.indexOf("IINST1") > 0 && data.indexOf("IINST2") > 0 && data.indexOf("IINST3") > 0) { | |
| String line = data.substring(data.indexOf("IINST1"), data.indexOf("IINST3")+10); | |
| Serial.print("Ligne recue du compteur: "); | |
| Serial.println(line); | |
| // should print something like this | |
| //IINST1 002 3IINST2 012 6IINST3 006 | |
| int iinst1 = line.substring(7,10).toInt(); | |
| int iinst2 = line.substring(21,24).toInt(); | |
| int iinst3 = line.substring(35,39).toInt(); | |
| Serial.print("IINST1: "); | |
| Serial.println(iinst1); | |
| Serial.print("IINST2: "); | |
| Serial.println(iinst2); | |
| Serial.print("IINST3: "); | |
| Serial.println(iinst3); | |
| iinst = iinst1 + iinst2 + iinst3; | |
| Serial.print("Total: "); | |
| Serial.println(iinst); | |
| } | |
| else { | |
| Serial.println("No iinst found"); | |
| } | |
| data = ""; | |
| return iinst; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment