Hello guys!
I'm developing an arduino using SIM800L, DHT11 sensor, Soil Moisture Sensor, Ethernet Shield HR911105A.
The goal of this project is get the sensors by SMS and Local Web Server.
My SIM800L is working, i can receive and send SMS normally, also get sensors values.
But when i start the local server, the SIM800L stops working and only the Web Server works.
I would like to know how can i use them at the same time.
This is how my code are:
#include <SoftwareSerial.h>
#include <Wire.h>
#include <SPI.h>
#include <Ethernet.h> // biblioteca para efetuar conexão com a internet
#include <dht.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
dht DHT;
#define rxPin 10
#define txPin 11
#define pinSensorA A0
#define DHT11_PIN 2
short DHT_OK = -1;
SoftwareSerial sim(10, 11);
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 0, 9);
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
char Received_SMS;
String Data_SMS;
int _timeout;
int umidade;
String _buffer;
String number = "xxxxxxxxxxx"; //-> change with your number
String mensagem;
unsigned long delay1 = 3600000;
unsigned long delay2 = 0;
struct Sensors {
int chk;
float temperature;
float humidity;
float ground_humidity;
};
void setup() {
//Inicializando Serial
delay(7000); //delay de 7 segundos para garantir que o módulo GSM consiga sinal
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.println("Ethernet WebServer Example");
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
// start the server
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
Serial.println(F("Iniciando sistema..."));
sim.begin(9600);
_buffer.reserve(50);
delay(1000);
ReceiveMode();
}
Sensors *getSensors() {
Sensors *sensors;
sensors = (Sensors*)malloc(sizeof(Sensors));
sensors->chk = DHT.read11(DHT11_PIN);
sensors->temperature = DHT.temperature;
sensors->humidity = DHT.humidity;
//delay(5000);
sensors->ground_humidity = map(analogRead(pinSensorA), 1023, 0, 0, 200);
return sensors;
}
void loop() {
Sensors *sensors;
String RSMS;
if (millis() >= 4233600000) { // reseta o millis quando atinge o valor máximo
noInterrupts();
delay1 = 0;
delay2 = 0;
interrupts();
delay(1000);
}
if (millis() - delay2 >= 300000) { // a cada 30 minutos "reinicia" o arduino para que a função receber/enviar SMS se mantenha funcionando
noInterrupts();
interrupts();
delay(1000);
delay2 = millis();
RestartArduino();
}
sensors = getSensors();
if (sensors->ground_humidity > 20 || sensors->temperature > 20 || sensors->humidity > 20) { // verifica se há alguma anormalidade em qualquer 1 dos 3 sensores
if ((millis() - delay1) > 10800000) { // após notificar uma vez, ele demora 3 horas para notificar novamente.
sensors = getSensors();
Data_SMS = "ANORMALIDADE DETECTADA!"; // Define a mensagem do alerta a ser enviado
delay(4000);
Send_Data(); //envia a mensagem designada à variável Data_SMS
delay(4000);
Data_SMS = "\nRelatorio \nUmidade do solo (Umidade max: 20%): "+String(sensors->ground_humidity, 1)+ "%" + "\nTemperatura (Temp max: 25C): " +String(sensors->temperature, 1) + "C" + "\nUmidade do ar (Umidade max: 20%): " + String(sensors->humidity, 1) + "%";
delay(4000);
Send_Data(); //envia a mensagem designada à variável Data_SMS
delay1 = millis();
delay(2000);
RestartArduino(); //Prepara o arduino para receber/enviar SMS novamente, sem isso em algum momento o arduino para de enviar/receber SMS
}
}
while (sim.available() > 0) { // verifica se há novas mensagens
Received_SMS = sim.read(); // Designa a mensagem recebida a uma variável
Serial.print(Received_SMS); // Printa a mensagem recebida
RSMS.concat(Received_SMS); // Concatena a variável RSMS que está vazia com a variável Received_SMS
DHT_OK = RSMS.indexOf("Relatorio"); //verifica se a palavra Relatorio está presente no sms recebido
}
//verifica se a variável DHT_OK é diferente de -1 (Caso a variável seja -1 quer dizer que não há mensagem recebida)
if (DHT_OK != -1) {
Serial.println(F("\nfound DHT request"));//indica que a mensagem Relatorio foi recebida
delay(2000);
Serial.print(F("Umidade do solo = "));
Serial.print(sensors->ground_humidity);
Serial.print(F("%"));
Serial.print(F("\nTemperatura = "));
Serial.print(sensors->temperature);
Serial.print(F("C"));
Serial.print(F("\nUmidade relativa do ar = "));
Serial.print(sensors->humidity);
Serial.print(F("%"));
Data_SMS = "\nRelatorio \nUmidade do solo (Umidade max: 20%): " + String(sensors->ground_humidity, 1) + "%" + "\nTemperatura (Temp max: 25C): " + String(sensors->temperature, 1) + "C" + "\nUmidade do ar (Umidade max: 20%): " + String(sensors->humidity, 1) + "%";
Send_Data();
// pollSms();
DHT_OK = -1;
delay(2000);
RestartArduino(); //Prepara o arduino para receber/enviar SMS novamente, sem isso em algum momento o arduino para de enviar/receber SMS
}
free(sensors);
EthernetClient client = server.available();
if (client) {
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close"); // the connection will be closed after completion of the response
client.println("Refresh: 5"); // refresh the page automatically every 5 sec
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
// output the value of each analog input pin
for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
int sensorReading = analogRead(analogChannel);
client.print("analog input ");
client.print(analogChannel);
client.print(" is ");
client.print(sensorReading);
client.println("<br />");
}
client.println("</html>");
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
} else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
}
}// ~~~~~~~~~~~~Final do Loop~~~~~~~~~~~~
void pollSms() { // Prepara o GSM
delay(500);
while (Serial.available())
{
sim.write(Serial.read());
}
while(sim.available())
{
Serial.write(sim.read());
}
}
void ReceiveMode() { //prepara o arduino para receber SMS
sim.println(F("AT"));
pollSms();
sim.println(F("AT+CMGF=1"));
pollSms();
sim.println(F("AT+CNMI=2,2,0,0,0"));
pollSms();
}
void Send_Data(){
Serial.println("\nEnviando dados..."); // Displays on the serial monitor
sim.print(F("AT+CMGF=1\r")); //Prepara o GSM para o modo de SMS
delay(100);
sim.print(F("AT+CMGS=\"xxxxxxxxxxxx\"\r")); //phone number
delay(500);
sim.print(Data_SMS); //Essa string é enviada como SMS
delay(500);
sim.print((char)26); //Finaliza o comando AT
delay(500);
sim.println();
Serial.println(F("Dados enviados."));
delay(500);
}
void RestartArduino() {
delay(7000); //Delay de 7 segundos para garantir que o módulo consiga sinal.
_buffer.reserve(50);
Serial.println("Reiniciando sistema...");
sim.begin(9600);
ReceiveMode();// Chama a função ReceiveMode
}