Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <WiFi.h>
- #include <PubSubClient.h>
- WiFiServer server ( 80 );
- const char* ssid = "xxxxxx";
- const char* password = "xxxxxxxxxxxxxx";
- const char* mqttServer = "xxxxxxxxxxxx";
- const int mqttPort = 18897;
- const char* mqttUser = "xxxxxxxxxxx";
- const char* mqttPassword = "xxxxxxxxxxxx";
- WiFiClient espClient1;
- PubSubClient client(espClient1);
- long lastMsg = 0;
- char msg[50];
- int value = 0;
- int buzzer = 12;
- int sensoreA = 16;
- long tempo;
- long tempo1;
- int h=0;
- int Q=0;
- int p=0;
- int i;
- int tempo1min=0;
- int g1=0;
- int minuti;
- void callback(char* topic, byte* payload, unsigned int length) {
- char messaggio = (char)payload[0];
- if (String(topic) == "start"){
- if(messaggio == 'a'){
- minuti =0;
- g1=0;
- Buz();
- Q=1;
- }
- }
- Serial.println (h);
- }
- void Mqtt (){
- while (!client.connected()) {
- Serial.println("Connecting to MQTT...");
- if (client.connect("ESP32Client1", mqttUser, mqttPassword )) {
- Serial.println("connected");
- } else {
- Serial.print("failed with state ");
- Serial.print(client.state());
- delay(2000);
- }
- }
- }
- void setup() {
- Serial.begin (115200);
- pinMode (sensoreA, INPUT);
- pinMode(buzzer,OUTPUT);
- Serial.print("Connecting to ");
- Serial.println(ssid);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- // Print local IP address and start web server
- Serial.println("");
- Serial.println("WiFi connected.");
- Serial.println("IP address: ");
- Serial.println(WiFi.localIP());
- client.setServer(mqttServer, mqttPort);
- client.setCallback(callback);
- Mqtt();
- client.subscribe("start");
- }
- void Buz (){
- digitalWrite(buzzer,HIGH);
- delay(200);
- digitalWrite(buzzer,LOW);
- delay(100);
- }
- void loop() {
- client.loop();
- if (h == 1) {
- Serial.println("inizio if");
- minuti =0;
- g1=0;
- Q=1;
- Buz();
- Serial.println("start avviato ");
- }
- while (Q==1){
- if (p==0){
- delay (5000);
- Serial.println("primo delay");
- Buz ();
- delay (5000);
- Serial.println("secondo delay");
- Buz ();
- delay (5000);
- Serial.println("START");
- digitalWrite(buzzer,HIGH);
- tempo= millis();
- p=1;
- }
- if ((millis()-tempo)== 1000){
- digitalWrite(buzzer,LOW);
- }
- if (((millis()-tempo)> 59995)&&((millis()-tempo)> 60005)){
- minuti= minuti+1;
- tempo= millis();
- }
- if ((digitalRead (sensoreA)== HIGH)&&(g1==0)){
- tempo1 = millis()- tempo;
- tempo1min = minuti;
- Serial.println("primo intertempo");
- Mqtt();
- char temp1[8];
- dtostrf(tempo1, 1, 0, temp1);
- client.publish("sensore_1", temp1);
- Serial.print (temp1);
- Q=0;
- g1=1;
- h=0;
- p=0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement