Advertisement
Guest User

crono_Mqtt

a guest
Apr 8th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. #include <WiFi.h>
  2. #include <PubSubClient.h>
  3.  
  4. WiFiServer server ( 80 );
  5. const char* ssid = "xxxxxx";
  6. const char* password = "xxxxxxxxxxxxxx";
  7.  
  8. const char* mqttServer = "xxxxxxxxxxxx";
  9. const int mqttPort = 18897;
  10. const char* mqttUser = "xxxxxxxxxxx";
  11. const char* mqttPassword = "xxxxxxxxxxxx";
  12.  
  13. WiFiClient espClient1;
  14. PubSubClient client(espClient1);
  15. long lastMsg = 0;
  16. char msg[50];
  17. int value = 0;
  18.  
  19. int buzzer = 12;
  20. int sensoreA = 16;
  21. long tempo;
  22. long tempo1;
  23.  
  24. int h=0;
  25. int Q=0;
  26. int p=0;
  27. int i;
  28. int tempo1min=0;
  29. int g1=0;
  30. int minuti;
  31.  
  32. void callback(char* topic, byte* payload, unsigned int length) {
  33. char messaggio = (char)payload[0];
  34. if (String(topic) == "start"){
  35. if(messaggio == 'a'){
  36. minuti =0;
  37. g1=0;
  38. Buz();
  39. Q=1;
  40.  
  41. }
  42. }
  43. Serial.println (h);
  44. }
  45.  
  46. void Mqtt (){
  47. while (!client.connected()) {
  48. Serial.println("Connecting to MQTT...");
  49.  
  50. if (client.connect("ESP32Client1", mqttUser, mqttPassword )) {
  51.  
  52. Serial.println("connected");
  53.  
  54. } else {
  55.  
  56. Serial.print("failed with state ");
  57. Serial.print(client.state());
  58. delay(2000);
  59. }
  60. }
  61. }
  62. void setup() {
  63. Serial.begin (115200);
  64. pinMode (sensoreA, INPUT);
  65. pinMode(buzzer,OUTPUT);
  66.  
  67.  
  68.  
  69. Serial.print("Connecting to ");
  70. Serial.println(ssid);
  71. WiFi.begin(ssid, password);
  72. while (WiFi.status() != WL_CONNECTED) {
  73. delay(500);
  74. Serial.print(".");
  75.  
  76. }
  77. // Print local IP address and start web server
  78.  
  79. Serial.println("");
  80. Serial.println("WiFi connected.");
  81. Serial.println("IP address: ");
  82. Serial.println(WiFi.localIP());
  83. client.setServer(mqttServer, mqttPort);
  84. client.setCallback(callback);
  85.  
  86.  
  87. Mqtt();
  88.  
  89. client.subscribe("start");
  90.  
  91.  
  92. }
  93.  
  94. void Buz (){
  95. digitalWrite(buzzer,HIGH);
  96. delay(200);
  97. digitalWrite(buzzer,LOW);
  98. delay(100);
  99.  
  100. }
  101.  
  102.  
  103. void loop() {
  104.  
  105. client.loop();
  106.  
  107.  
  108. if (h == 1) {
  109. Serial.println("inizio if");
  110. minuti =0;
  111. g1=0;
  112. Q=1;
  113. Buz();
  114. Serial.println("start avviato ");
  115. }
  116. while (Q==1){
  117. if (p==0){
  118. delay (5000);
  119. Serial.println("primo delay");
  120. Buz ();
  121. delay (5000);
  122. Serial.println("secondo delay");
  123. Buz ();
  124. delay (5000);
  125. Serial.println("START");
  126. digitalWrite(buzzer,HIGH);
  127. tempo= millis();
  128.  
  129. p=1;
  130.  
  131. }
  132. if ((millis()-tempo)== 1000){
  133. digitalWrite(buzzer,LOW);
  134.  
  135. }
  136. if (((millis()-tempo)> 59995)&&((millis()-tempo)> 60005)){
  137. minuti= minuti+1;
  138. tempo= millis();
  139. }
  140.  
  141. if ((digitalRead (sensoreA)== HIGH)&&(g1==0)){
  142. tempo1 = millis()- tempo;
  143. tempo1min = minuti;
  144. Serial.println("primo intertempo");
  145. Mqtt();
  146. char temp1[8];
  147. dtostrf(tempo1, 1, 0, temp1);
  148. client.publish("sensore_1", temp1);
  149. Serial.print (temp1);
  150. Q=0;
  151. g1=1;
  152. h=0;
  153. p=0;
  154. }
  155.  
  156. }
  157.  
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement