Étape 4: Copier le code et l’exécuter !
Copiez ce code dans votre arduino IDE, puis exécuter et vous êtes prêt à partir !
N’oubliez pas d’ajouter vos propres clés wifi et api
#include
#include #include #include « M2XStreamClient.h » #include #define WATER_SENSOR 2 #define BUZZER 12 #define FLAME_SENSOR 5 //connect capteur numérique pin3 int a ; température du flotteur ; int B = 3975 ; B valeur de la résistance de flotteur thermistance ;
broche int = 8 ; unsigned longue durée ; unsigned long starttime ; unsigned long sampletime_ms = 30000 ; //sampe 30 s ; unsigned long lowpulseoccupancy = 0 ; ratio de flotteur = 0 ; concentration de flotter = 0 ; Char [] ssid = "Intel 2,4 GHz" ; votre char SSID (nom) du réseau pass [] = "wifipass ici" ; État int = WL_IDLE_STATUS ; Char [] Id_flux = "your feed here" ; Char [] deviceId = "votre appareil ici" ; Dispositif d’à que vouloir pousser
char m2xKey [] = "votre clé ici" ; Votre M2X accès clés const int temperaturePin = 0 ;
WiFiClient client ; M2XStreamClient m2xClient (& client, m2xKey) ;
void setup() {Serial.begin(115200) ; pinMode(8,INPUT) ; starttime = millis (); //get l’heure actuelle ; if (WiFi.status() == WL_NO_SHIELD) {Serial.println ("WiFi bouclier n’existe pas"); while (true);} tandis que (statut! = WL_CONNECTED) {Serial.print ("se connecter au SSID:") ; Serial.println(SSID) ; Connectez-vous au réseau WPA/WPA2. Changez cette ligne si vous utilisez open ou réseau WEP : statut = WiFi.begin (ssid, pass) ; Attendez 10 secondes pour le raccordement : delay(10000) ; } Serial.println("Connected to wifi") ; printWifiStatus() ; }
void loop() {}
EarthQuake() ;
}
void GasSensor() {float vol ; int sensorValue = analogRead(A0) ; vol = (float) sensorValue/1024 ; Serial.Print ("la densité de gaz est") ; Serial.println(vol) ; Delay(100) ; réponse d’int = m2xClient.updateStreamValue (deviceId, « gaz », vol) ; Serial.Print ("le code de réponse client M2x:") ; Serial.println(Response) ; }
void tempSensor() {a=analogRead(1) ; resistance=(float)(1023-a) * 10000/a ; //get la résistance du capteur ; température = 1 / (Journal (résistance/10000) /B+1/298.15)-273.15;//convert à température via datasheet ; delay(1000) ; Serial.Print ("température actuelle est") ; Serial.println(temperature*1.8+32) ; réponse d’int = m2xClient.updateStreamValue(deviceId, "temp", temperature*1.8+32) ; Serial.Print ("le code de réponse client M2x:") ; Serial.println(Response) ; }
void DustSensor() {durée = pulseIn (broches, LOW); lowpulseoccupancy = lowpulseoccupancy + durée ;
Si ((millis()-starttime) > sampletime_ms) //if le temps sampel == 30 s {ratio = lowpulseoccupancy/(sampletime_ms*10.0); / / pourcentage entier 0 = > concentration 100 = 1,1 * pow (rapport, 3)-3.8 * pow (ratio, 2) +520 * ratio + 0,62; / / à l’aide de la courbe de la fiche technique / / Serial.print(lowpulseoccupancy); / / Serial.print(","); / / Serial.print(ratio) ; Serial.Print ("concentration de poussières est:") ; Serial.println(concentration) ; lowpulseoccupancy = 0 ; StartTime = millis() ; réponse d’int = m2xClient.updateStreamValue (deviceId, « poussière », concentration) ; Serial.Print ("le code de réponse client M2x:") ; Serial.println(Response) ; } }
void myFlame() {if(isFlameDetected()) {int réponse = m2xClient.updateStreamValue (deviceId, « Fire », « 1 ») ; Serial.Print ("le code de réponse client M2x:") ; Serial.println(Response) ; turnOnLED() ; } else turnOffLED() ; réponse d’int = m2xClient.updateStreamValue (deviceId, « Fire », « 0 ») ; Serial.Print ("le code de réponse client M2x:") ; Serial.println(Response) ; }
void pinsInit() {pinMode (entrée, FLAME_SENSOR); pinMode (entrée, WATER_SENSOR); pinMode (BUZZER, OUTPUT);} void turnOnLED() {/ / digitalWrite(LED,HIGH) ; Serial.println ("Flame détecté!") ; } void turnOffLED() {/ / digitalWrite(LED,LOW) ; Serial.println ("pas de flamme détectée") ; } isFlameDetected() boolean {if(digitalRead(FLAME_SENSOR)) return false ; else return true;}
Boolean isExposedToWater() {if(digitalRead(WATER_SENSOR) == faible) return true ; sinon retourne false;}
void MyWater() {if(isExposedToWater()) {Serial.println ("l’eau est détectée!!!"); int réponse = m2xClient.updateStreamValue (deviceId, "l’eau", "1") ; Serial.Print ("le code de réponse client M2x:") ; Serial.println(Response);} else {Serial.println ("aucun eau n’est détecté."); int réponse = m2xClient.updateStreamValue (deviceId, "l’eau", "0") ; Serial.Print ("le code de réponse client M2x:") ; Serial.println(Response);} }
void soundAlarm() {pour (uint8_t j’ai = 0; j’ai < 20; i ++) {digitalWrite (sonnerie, élevée); digitalWrite (sonnerie, basse), delay(50) ; delay(50);}}
void EarthQuake() {{int sensorValue = analogRead(A3) ; Serial.println(sensorValue) ; Delay(1000) ; Si (sensorValue > = 1008) {Serial.println ("tremblement détecté!") ; GasSensor() ; tempSensor() ; DustSensor() ; myFlame() ; isExposedToWater() ; MyWater() ; {}} else {}}} void printWifiStatus() {/ / print le SSID du réseau vous êtes attaché à: Serial.print ("SSID:") ; Serial.println(WiFi.SSID()) ; imprimer l’adresse IP de votre bouclier WiFi : ip IPAddress = WiFi.localIP() ; Serial.Print ("adresse IP:") ; Serial.println(IP) ; imprimer l’intensité du signal reçu : rssi long = WiFi.RSSI() ; Serial.Print ("force (RSSI) du signal:") ; Serial.Print(RSSI) ; Serial.println ("dBm") ; } //float getVoltage (int code pin) / / {//return (analogRead(pin) * 0.004882814); / /} void waitMicros(int val) {unsigned long un = micros() ; unsigned long b = micros() ; while((b-a) < val) {b = micros() ; if(a>b) {break;}}}