Fabrication industrielle
Internet des objets industriel | Matériaux industriels | Entretien et réparation d'équipement | Programmation industrielle |
home  MfgRobots >> Fabrication industrielle >  >> Manufacturing Technology >> Processus de fabrication

Relevé de température DHT22 par SMS avec Arduino Mega 2560 et SIM800L

Composants nécessaires

À propos du projet

Ce projet fait partie d'un projet plus vaste. J'ai rencontré beaucoup de difficultés sur cette partie et ayant trouvé une solution, je la partage pour aider la communauté.

Description

Envoyez un SMS à l'Arduino et recevez la température actuelle à la maison.

Logiciel

Pour le module SIM800L, j'ai utilisé la bibliothèque « Seeeduino_GPRS-master ». J'ai dû modifier les lignes suivantes dans « sim800.h » pour fonctionner sur un Mega :

#define SIM800_TX_PIN 8
#define SIM800_RX_PIN 7

→ changés en TX_PIN 10 et RX_PIN 11 (utilisation de SoftwareSerial). Passer à un port série matériel (Serial3) était trop complexe, j'ai pris la solution simple.

Le problème

Mon code combine des exemples de deux bibliothèques : « DHT.h » et « Seeeduino_GPRS-master ». Le problème : l'exemple « gprs.h » ne montrait comment envoyer qu'un message prédéfini entre guillemets. Je n'arrivais pas à envoyer la valeur float variable du capteur de température. Même problème avec « GPRS_SIM900-master » et « SIM800L-master ».

J'ai passé des semaines à chercher sur le web sans trouver de solution. Des posts de forum sur le même sujet restaient sans réponse. J'espère que cette publication aidera ceux qui sont dans le même cas.

La solution

Convertir la valeur float en String, puis la String en tableau de caractères (char array) — c'est tout !

Extrait de code clé

MyString = String(temp, 0);           // Convertir float en String
MyString = (MyString + " Degrees C"); // Ajouter l'unité
// Conversion String → char array
int str_len = MyString.length() + 1;
char char_array[str_len];
MyString.toCharArray(char_array, str_len);
// Envoi SMS
gprs.sendSMS("04++++++++", char_array); // Remplacez par votre numéro

Câblage

J'ai utilisé une batterie ULTRAFIRE 18650 pour alimenter le module SIM. Un régulateur de tension sera ajouté plus tard. Voir ce tutoriel.

Références

Bibliothèques

Code complet : DHT_SIMPosting.ino

// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain
#include "DHT.h"
#define DHTPIN 2
#define DHTTYPE DHT22
//#define DHTTYPE DHT21

// Connexions DHT22 :
// Pin 1 (gauche) → +5V
// Pin 2 → DHTPIN (broche 2)
// Pin 4 (droite) → GND
// Résistance 10K entre pin 2 (data) et pin 1 (alimentation)

DHT dht(DHTPIN, DHTTYPE);

#include 
#include 
#define TIMEOUT 5000

GPRS gprs;
bool ITemp = false;
float temp;
String MyString;
#define LED_PIN 13
bool ledStatus;

char currentLine[500] = "";
int currentLineIndex = 0;
bool nextLineIsMessage = false;

void Reply() {
  Serial.println("GPRS - Send SMS Test ...");
  gprs.preInit();
  delay(1000);
  while (0 != gprs.init()) {
    delay(1000);
    Serial.print("init error\r\n");
  }
  Serial.println("Init success, start to send SMS message...");
  if (ITemp == true) {
    float t = dht.readTemperature();
    temp = t;
    ITemp = false;
  }
  MyString = String(temp, 0);
  MyString = (MyString + " Degrees C");
  int str_len = MyString.length() + 1;
  char char_array[str_len];
  MyString.toCharArray(char_array, str_len);
  gprs.sendSMS("04++++++++", char_array);
}

void setup() {
  Serial.begin(9600);
  Serial.println("DHTxx test!");
  dht.begin();
  gprs.preInit();
  delay(1000);
  while (0 != gprs.init()) {
    delay(1000);
    Serial.print("init error\r\n");
  }
  // Mode SMS ASCII
  if (0 != gprs.sendCmdAndWaitForResp("AT+CMGF=1\r\n", "OK", TIMEOUT)) {
    ERROR("ERROR:CNMI"); return;
  }
  // Notifications nouveaux SMS
  if (0 != gprs.sendCmdAndWaitForResp("AT+CNMI=1,2,0,0,0\r\n", "OK", TIMEOUT)) {
    ERROR("ERROR:CNMI"); return;
  }
  Serial.println("Init success");
}

void loop() {
  delay(2000);
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  Serial.print("Humidity: "); Serial.print(h); Serial.println(" %\t");
  Serial.print("Temperature: "); Serial.print(t); Serial.println(" *C ");
  SIM();
}

void SIM() {
  digitalWrite(LED_PIN, ledStatus);
  Serial.println("Looking for SMS");
  if (gprs.serialSIM800.available()) {
    char lastCharRead = gprs.serialSIM800.read();
    if (lastCharRead == '\r' || lastCharRead == '\n') {
      String lastLine = String(currentLine);
      if (lastLine.startsWith("+CMT:")) {
        Serial.println(lastLine);
        nextLineIsMessage = true;
      } else if (lastLine.length() > 0) {
        if (nextLineIsMessage) {
          Serial.println(lastLine);
          if (lastLine.indexOf("InTemp") >= 0) {
            Serial.println("InTemp");
            ITemp = true;
            Reply();
          }
          // Autres commandes possibles : "LED ON", "LED OFF"...
          nextLineIsMessage = false;
        }
      }
      // Nettoyer buffer
      for (int i = 0; i < sizeof(currentLine); ++i) currentLine[i] = 0;
      currentLineIndex = 0;
    } else {
      currentLine[currentLineIndex++] = lastCharRead;
    }
  }
}

Schéma de câblage

Relevé de température DHT22 par SMS avec Arduino Mega 2560 et SIM800L
Schéma de connexion Arduino Mega 2560, DHT22, SIM800L

Processus de fabrication

  1. Ventilateur à contrôle de température : système relais DIY pour refroidissement meuble multimédia
  2. Construire un enregistreur de température Raspberry Pi avec capteur I2C à 5 $
  3. Comment lire la température avec un DS18B20 sur Raspberry Pi 2
  4. Surveillance de température professionnelle sur Raspberry Pi avec DS18B20
  5. Mesure de température sur Raspberry Pi avec capteurs 1‑Wire Maxim et pont I2C DS2482
  6. Surveillance précise de la température dans un placard serveur avec Raspberry Pi
  7. Tutoriel IoT débutant : Diffusion de données de température depuis un Raspberry Pi
  8. Construire une station météo de laboratoire connectée en réseau avec Raspberry Pi
  9. Construire un moniteur de température domestique avec Raspberry Pi, MCP9808, InfluxDB et Grafana