Internet de les coses amb ESP32 i ESP8266

Exemples Referència Plaques   Recursos CITCEA
Projectes Programació Perifèrics   Inici

Sonòmetre

Programa del grup 2

En aquest cas s'ha dissenyat un dispositiu per monitoritzar el son d'un infant, amb dos modes de funcionament. En el mode nocturn mesura el nivell de soroll i si el considera moderat fa sonar una cançó de bressol per intentar que l'infant es torni a dormir. Si el nivell de soroll és alt, suposa que l'infant està plorant i fa sonar el brunzidor per despertar als familiars. En el mode diurn, monittoritza el soroll i el classifica en tres nivells: baix, moderat i alt. Cada cop que canvia el nivell, es fuarda el canvi en un full de càlcul de Google. El microcontrolador treballa com a client. A més, el color del LED indica el nivell de soroll actual. Un applet d'IFTTT envia un correu electrònic quan detecta que el nivell de so alt dura més de l'esperat. El programa del microcontrolador és el següent:

#include <Adafruit_NeoPixel.h>
#include <SPI.h>    // Carreguem la biblioteca SPI
#include <WiFiNINA.h>    // Carreguem la biblioteca WiFiNINA
#include "iftttWebhook.h"
#define N_Do0   33  // Definim totes les notes amb la seva freqüència corresponent
#define N_ReB0  35
#define N_Re0   37
#define N_MiB0  39
#define N_Mi0   41
#define N_Fa0   44
#define N_SolB0   46
#define N_Sol0  49
#define N_LaB0  52
#define N_La0   55
#define N_SiB0  58
#define N_Si0   62
#define N_Do1   65
#define N_ReB1  69
#define N_Re1   73
#define N_MiB1  78
#define N_Mi1   82
#define N_Fa1   87
#define N_SolB1   93
#define N_Sol1  98
#define N_LaB1   104
#define N_La1 110
#define N_SiB1   117
#define N_Si1 123
#define N_Do2 131
#define N_ReB2   139
#define N_Re2 147
#define N_MiB2   156
#define N_Mi2 165
#define N_Fa2 175
#define N_SolB2  185
#define N_Sol2   196
#define N_LaB2   208
#define N_La2 220
#define N_SiB2   233
#define N_Si2 247
#define N_Do3 262
#define N_ReB3   277
#define N_Re3 294
#define N_MiB3   311
#define N_Mi3 330
#define N_Fa3 349
#define N_SolB3  370
#define N_Sol3   392
#define N_LaB3   415
#define N_La3 440
#define N_SiB3   466
#define N_Si3 494
#define N_Do4 523
#define N_ReB4   554
#define N_Re4 587
#define N_MiB4   622
#define N_Mi4 659
#define N_Fa4 698
#define N_SolB4  740
#define N_Sol4   784
#define N_LaB4   831
#define N_La4 880
#define N_SiB4   932
#define N_Si4 988
#define N_Do5   1047
#define N_ReB5  1109
#define N_Re5   1175
#define N_MiB5  1245
#define N_Mi5   1319
#define N_Fa5   1397
#define N_SolB5 1480
#define N_Sol5  1568
#define N_LaB5  1661
#define N_La5   1760
#define N_SiB5  1865
#define N_Si5   1976
#define N_Do6   2093
#define N_ReB6  2217
#define N_Re6   2349
#define N_MiB6  2489
#define N_Mi6   2637
#define N_Fa6   2794
#define N_SolB6 2960
#define N_Sol6  3136
#define N_LaB6  3322
#define N_La6   3520
#define N_SiB6  3729
#define N_Si6   3951
#define N_Do7   4186
Adafruit_NeoPixel cadena = Adafruit_NeoPixel(3, 1, NEO_GRB + NEO_KHZ800);
WiFiSSLClient client;
const char idXarxa[] = "xarxa";		// Nom del punt d'accés 
const char contrasenya[] = "contrasenya"	;		// Contrasenya de connexió 
const char formulari[] = "1CXzpNtrKvOQ_EcmLyfZcI048t9qLpylYRoYMBsLYLe8"; 
const char adrePost[] = "https://docs.google.com/forms/u/0/d/e/1FAIpQLSepN9yFbyFrjOWW9QZrORrfSoxnGGu5M-PtkPeoXfSHKaePNw/formResponse";
String camp1 = "entry.1116552742";
String camp2 = "entry.412337382";
char server[] = "docs.google.com";
float estat,c1,c2,c3;
String data,valor,intensitat;
int status = WL_IDLE_STATUS;
bool CanviEstat = false;
void setup() {    // Inicialització
	Serial.begin(9600);
	cadena.begin();	// Inicialitza els NeoPixels			
	cadena.show();
	pinMode(3, OUTPUT);	// Brunzidor a la pota 3
	pinMode(7, INPUT_PULLUP);
	pinMode(6, OUTPUT);
	digitalWrite(6, LOW);
	if (WiFi.status() == WL_NO_MODULE) {
		Serial.println("No s'ha trobat el dispositiu Wi-Fi");
		while (true);		// Bloquegem el programa
	}
	String versio = WiFi.firmwareVersion();
	if (versio < "1.0.0") {
		Serial.println("Convindria actualitzar el firmware");
	}
	while (status != WL_CONNECTED) {
		Serial.print("Connectant a la xarxa ");
		Serial.println(idXarxa);
		status = WiFi.begin(idXarxa, contrasenya);
		delay(10000);		// Ho tornarem a intentar passats 10 s
	}
	Serial.print("Connectat a "); 
	Serial.println(WiFi.SSID());
	Serial.print("Estat de la connexió: ");
	Serial.println(WiFi.status()); 
	Serial.print("Adreça IP del dispositiu: ");
	Serial.println(WiFi.localIP()); 
	Serial.print("Intensitat del senyal: ");
	Serial.print(WiFi.RSSI()); 
	Serial.println(" dBm");
	Serial.println(); 
}
void loop() {
	int sen = digitalRead(7);	// PALANCA DE CANVI DE MODE
	int Regulador = analogRead(A5)/2	+500;	// valor dictat pel potenciometre
	int sensorValue = analogRead(A6);	// SONÒMETRE
	Serial.println(sensorValue);
	Serial.println(Regulador);
	Serial.println(sen);
	if (sen==0){	// MODE NIT
		static int c=0;	// iniciem els contadors del programa
		static int h=-10000;	// h ens dira quan hem de tocar cada nota, començem amb un numero negatiu
					// per no arribar al maxim int d'arduino (+-32768)
		int sensorValue = analogRead(A6);	// variable que guarda el volum del exterior
		// A partir d'aquí, depenent del volum mesurat el programa farà una cosa a un altre
		if ((sensorValue>=Regulador*0.600)&(sensorValue<Regulador*0.700)){	//Per intensitat suau, posa el neopixel verd
			cadena.setPixelColor(0, 0, 100, 0);	// verd
			c=1;
		}
		if ((sensorValue>=Regulador*0.700)&(sensorValue<Regulador)){
			//Per intensitat moderada, toca una nana i posa el neopixel groc
			c=0;
			h=-10000;
			cadena.setPixelColor(0, 255, 112, 40);	// taronja
			cadena.show();
			while (sensorValue<Regulador){	// inicia el loop de la melodia
				h=h+1;
				if (h==1-10000){
					tone(3, N_Do4, 500);	
				}
				if (h==551-10000){
					tone(3, N_Do4, 500);	
				}
				if (h==1101-10000){
					tone(3, N_Sol4, 500); 
				}
				if (h==1+550*3-10000){
					tone(3, N_Sol4, 500);	
				}
				if (h==1+550*4-10000 ){
					tone(3, N_La4, 500);	
				}
				if (h==1+550*5-10000){
					tone(3, N_La4, 500);	
				}
				if (h==1+550*6-10000){
					tone(3, N_Sol4, 1000);	
				}
				if (h==1+1000+550*6-10000){
					tone(3, N_Fa4, 500);	
				}
				if (h==1+1000+550*7-10000){
					tone(3, N_Fa4, 500);	
				}
				if (h==1+1000+550*8-10000){
					tone(3, N_Mi4, 500);	
				}
				if (h==1+1000+550*9-10000){
					tone(3, N_Mi4, 500); 
				}
				if (h==1+1000+550*10-10000){
					tone(3, N_Re4, 500);	
				}
				if (h==1+1000+550*11-10000){
					tone(3, N_Re4, 500);	
				}
				if (h==1+1000+550*12-10000){
					tone(3, N_Do4, 1000);	
				}
				if (h==1+2100+550*12-10000){
					tone(3, N_Sol4, 500); 
				}
				if (h==1+2100+550*13-10000){
					tone(3, N_Sol4, 500);	
				}
				if (h==1+2100+550*14-10000){
					tone(3, N_Fa4, 500); 
				}
				if (h==1+2100+550*15-10000){
					tone(3, N_Fa4, 500); 
				}
				if (h==1+2100+550*16-10000){
					tone(3, N_Mi4, 500);	
				}
				if (h==1+2100+550*17-10000){
					tone(3, N_Mi4, 500); 
				}
				if (h==1+2100+550*18-10000){
					tone(3, N_Re4, 500);	
				}
				if (h==1+3100+550*18-10000){
					tone(3, N_Sol4, 500); 
				}
				if (h==1+3100+550*19-10000){
					tone(3, N_Sol4, 500);	
				}
				if (h==1+3100+550*20-10000){
					tone(3, N_Fa4, 500); 
				}
				if (h==1+3100+550*21-10000){
					tone(3, N_Fa4, 500);	
				}
				if (h==1+3100+550*22-10000){
					tone(3, N_Mi4, 500);	
				}
				if (h==1+3100+550*23-10000){
					tone(3, N_Mi4, 500);	
				}
				if (h==1+3100+550*24-10000){
					tone(3, N_Re4, 1000);	
				}
				if (h==1+4100+550*24-10000){
					tone(3, N_Do4, 500);	
				}
				if (h==1+4100+550*25-10000){
					tone(3, N_Do4, 500); 
				}
				if (h==1+4100+550*26-10000){
					tone(3, N_Sol4, 500);	
				}
				if (h==1+4100+550*27-10000){
					tone(3, N_Sol4, 500);	
				}
				if (h==1+4100+550*28-10000){
					tone(3, N_La4, 500);	
				}
				if (h==1+4100+550*29-10000){
					tone(3, N_La4, 500);	
				}
				if (h==1+4100+550*30-10000){
					tone(3, N_Sol4, 500);	
				}
				if (h==1+5100+550*30-10000){
					tone(3, N_Do4, 500);	
				}
				if (h==1+5100+550*31-10000){
					tone(3, N_Fa4, 500); 
				}
				if (h==1+5100+550*32-10000){
					tone(3, N_Mi4, 500); 
				}
				if (h==1+5100+550*33-10000){
					tone(3, N_Mi4, 500); 
				}
				if (h==1+5100+550*34-10000){
					tone(3, N_Re4, 500);	
				}
				if (h==1+5100+550*35-10000){
					tone(3, N_Re4, 500);	
				}
				if (h==1+5100+550*36-10000){
					tone(3, N_Do4, 1000); 
				}
				if (h==1+6200+550*36-10000){
					tone(3, N_Do4, 100); 
				}
				if (h==1+6200+550*36+100-10000){
					cadena.setPixelColor(0, 0, 0,0);
					cadena.show();
					break;	
				}
				sensorValue = analogRead(A6);
				Regulador = analogRead(A5)/2	+500;	// valor dictat pel potenciometre
			}
		}
		if (sensorValue>=1000){	// Per intensitat alta, fa sonar l'alarma i posa el noepixel vermell
			cadena.setPixelColor(0, 255, 0,0);	// vermell
			cadena.show();
			tone(3, N_Do5, 400); 
			delay(600);
			tone(3, N_Do5, 400); 
			delay(600);
			tone(3, N_Do5, 400); 
			delay(600);
			tone(3, N_Do5, 400); 
			delay(600);
			tone(3, N_Do5, 400); 
			delay(600);
			tone(3, N_Do5, 400); 
			delay(600);
			tone(3, N_Do5, 400); 
			delay(600);
			tone(3, N_Do5, 400); 
			delay(600);
			tone(3, N_Do5, 400); 
			delay(600);
			tone(3, N_Do5, 400); 
			delay(600);
			cadena.setPixelColor(0, 0, 0,0);
		}
		if(c!=0){
			c=c+1;
		}
		if(c==1000){
			cadena.setPixelColor(0, 0, 0,0);
			c=0;
		}
		cadena.show();
		delay(1);	// s
	}
	if (sen==1){	// MODE CANGUR
		int sensorValue = analogRead(A6);
		static float c1=0;
		static float c2=0;
		if ((sensorValue<Regulador*0.800)&(estat!=1)&(c1==0)&(c2==0)){	// Per intensitat suau, posa el neopixel verd
			cadena.setPixelColor(0, 0, 100, 0);	// verd
			CanviEstat=true;
			valor="0";
			intensitat="BAIX";
			estat=1;
		}
		if ((sensorValue>=Regulador*0.800)&(sensorValue<Regulador)&(estat!=2)&(c2==0)){
			// Per intensitat moderada, posa el neopixel taronja
			cadena.setPixelColor(0, 255, 112, 40);	// taronja
			CanviEstat=true;
			valor="1";
			intensitat="MODERAT";
			estat=2;
			c1=10000;
		}
		if (sensorValue>=Regulador){	// Per intensitat alta, posa el neopixel vermell
			if (estat!=3){
				cadena.setPixelColor(0, 255, 0, 0);	// vermell
				CanviEstat=true;
				valor="2";
				intensitat="ALT";
				estat=3;
			}
			c2=10000;
		}
		data = "";
		data += camp1;
		data += "=";
		data += intensitat;
		data += "&";
		data += camp2;
		data += "=";
		data += valor;
		data += "&submit=Submit";
		if (CanviEstat) {
			client.connect(server, 443);
			Serial.println("Connectat");
			client.print("POST ");
			client.print(adrePost);
			client.print("?formkey=");
			client.print(formulari);
			client.println("&ifq HTTP/1.1");
			client.print("Host: ");
			client.println(server);
			client.println("Content-Type: application/x-www-form-urlencoded");
			client.println("Connection: close");
			client.print("Content-Length: ");
			client.println(data.length());
			client.println();
			client.print(data);
			client.println();
			Serial.print("Enviat		T = ");
			Serial.print(valor);
			Serial.print("		H = ");
			Serial.println(intensitat);
			CanviEstat= false;
			c3=0;
		}
		if (c1!=0){
			c1=c1-1;
		}
		if (c2!=0){
			c2=c2-1;
		}
		if (estat==3) {
			c3=c3+1;
			if (c3==10000){
				SendIFTTTWebhookRequest(client, 0, 0, "El_micròfon_ha_detectat_so_elevat_durant_un_llarg_període_de_temps");	// envia l'email
			}
		}
		cadena.show();
	}
}

 

 

Llicència de Creative Commons
Aquesta obra d'Oriol Boix està llicenciada sota una llicència no importada Reconeixement-NoComercial-SenseObraDerivada 3.0.