Advertisement
Guest User

Untitled

a guest
Oct 29th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.96 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <WiFiClient.h>
  3. #include <ESP8266WebServer.h>
  4. #include <DNSServer.h>
  5. #include <WiFiManager.h>
  6. WiFiManager wifiManager;
  7. #include <PubSubClient.h>
  8. ESP8266WebServer httpServer(80);
  9.  
  10. const char* mqtt_server = "192.168.1.103"; //INDIQUER ICI L'URL DU BROKER
  11. const char* mqtt_user = "monuser";
  12. const char* mqtt_pass = "monpass";
  13. const char* ID = "ESP_teleinfo";
  14. const char* willTopic= "ESP_teleinfo/status";
  15.  
  16. WiFiClient espClient;
  17. PubSubClient client(espClient);
  18.  
  19. long readInterval=60*1000;
  20.  
  21. long lastRead = 60000;
  22.  
  23. String trame="init.";
  24. void setup() {
  25. Serial.begin(1200,SERIAL_7E1); //config pour lire la TI
  26. wifiManager.setDebugOutput(false);
  27. wifiManager.autoConnect(ID);
  28. client.setServer(mqtt_server, 1883);
  29.  
  30. httpServer.on("/", [](){httpServer.send(200, "text/html", "it's works!");Serial.println("http");});
  31. httpServer.on("/info", [](){displayInfo();Serial.println("httpinfo");});
  32. httpServer.begin();
  33. delay(1000);
  34.  
  35. }
  36. void reconnect() {
  37. // Loop until we're reconnected
  38. while (!client.connected()) {
  39. Serial.print("Attempting MQTT connection...");
  40. // Attempt to connect
  41. if (client.connect(ID,willTopic,mqtt_user,mqtt_pass,0,true,"Offline")) { //connection avec un LWT
  42. Serial.println("connected");
  43. } else {
  44. Serial.print("failed, rc=");
  45. Serial.print(client.state());
  46. Serial.println(" try again in 5 seconds");
  47. // Wait 5 seconds before retrying
  48. delay(5000);
  49. }
  50. }
  51. }
  52. void loop() {
  53. if (!client.connected()) {
  54. reconnect();
  55. }
  56. client.loop();
  57. httpServer.handleClient();
  58. if(millis()-lastRead > readInterval || millis()-lastRead < 0){
  59. boolean erreur=false;
  60. lastRead=millis();
  61. //Serial.println("reading..");
  62. trame="";
  63. //client.publish("maison/compteurEDF/debug","debut mesure");
  64. while(Serial.available()){Serial.read();} //Vide le buffer : indispensable !
  65. int i=0;
  66. while(Serial.read()!=0x02){//On attend le debut (et on arrete si ca dure plus de 5 secondes)
  67. client.loop();
  68. yield();
  69. if(millis() - lastRead > 5000){
  70. erreur=true;
  71. client.publish(willTopic,"Erreur compteur",true);
  72. trame="Erreur de lecteur sur le compteur";
  73. break;
  74. }
  75. }
  76. if(erreur==false){
  77. while(true){ //boucle pour récuperer le contenu du port serie
  78. while(!Serial.available()){client.loop();yield();} //On attent le buffer, (!)
  79. char c=Serial.read();
  80. if(c==0X03){break;}//fin de la trame
  81. else{
  82. trame+=c;
  83. }
  84. yield();
  85. }
  86. int start=-1;
  87. int stop=-1;
  88. i=0;
  89. if(trame.length()>0){
  90. while(true){
  91. start=trame.indexOf(0x0A,stop+1); //le stop précédent
  92. if(start==-1){break;} //fini !
  93. stop=trame.indexOf(0x0D,start+1);
  94. int sep1=trame.indexOf(0x20,start+1);
  95. int sep2=trame.indexOf(0x20,sep1+1);
  96.  
  97. String libel=trame.substring(start+1,sep1);
  98. String val=trame.substring(sep1+1,sep2);
  99. String crc=trame.substring(sep2+1,stop);
  100. sendMQTT(libel,val); //if checksum ok
  101. i++;
  102. if(i>50){break;Serial.println("Erreur");}
  103. client.loop();
  104. yield();
  105. }
  106. client.publish(willTopic,"OK",true);
  107. }
  108. }
  109.  
  110. }
  111. }
  112.  
  113. String checksum(String libel, String val){
  114. String str=libel+' '+val;
  115. char sum=0;
  116. for (int i = 0 ; i<=str.length() ; i++){
  117. sum=sum+str[i];
  118. }
  119. sum=(sum & 0x3F)+0x20 ;//garder que les 6bits de fin
  120. String sumS = String(sum);
  121. return sumS;
  122.  
  123. }
  124. void sendMQTT(String libel, String val){
  125. String topic = ID+String("/");
  126. topic += libel;
  127.  
  128. char topicChar[50];
  129. char valChar[35];
  130. topic.toCharArray(topicChar,50);
  131. val.toCharArray(valChar,35);
  132. client.publish(topicChar,valChar);
  133. }
  134. void displayInfo(){
  135. String html="<pre>";
  136. html+=trame;
  137. html+="</pre>";
  138. httpServer.send(200, "text/html", html);
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement