Ir ao conteúdo
  • Cadastre-se

Ardunio + esp8266 + sensor


Posts recomendados

Hey pessoal,
Estou tentando fazer um projeto onde o arduino coleta informações de um sensor de umidade e envia para uma webpage .
Eu tentei de duas formas, uma enviando para o thingspeak e outra por um web server. nenhum dos dois aparenta estar funcionando.
O sensor está ligado na porta A0, coloquei também um botão na porta 8 do digital para testar.
O wifi usando o esp8266, está conectando e consegue um ip.
A programação nao dá nenhum erro ao compilar.
Minha pergunta é se eu cometi algum erro na programação aqui? Deveria estar funcionando?

   

  

   #include <SoftwareSerial.h>
    int sensor_humid = 8; //entrada do botao, mudar para A0 para o sensor.
    int value_humid;
    
    //-- Hardware Serial
    //#define _baudrate 9600
     
    //RX pino 2, TX pino 3
    SoftwareSerial esp8266(2, 3);
     
    #define DEBUG true
    //*-- IoT Information
    #define SSID "coloco a ssid aqui"
    #define PASS "coloco a senha aqui"
    #define IP "184.106.153.149" // ThingSpeak IP Address: 184.106.153.149
    
    // GET /update?key=[THINGSPEAK_KEY]&field1=[data 1]&field2=[data 2]...;
    String GET = "GET /update?key=UTY2RG15WLM53teste";
     
    void setup()
    {
      Serial.begin(9600);
      esp8266.begin(19200);
     
      sendData("AT+RST\r\n", 2000, DEBUG); // rst
      // Conecta a rede wireless
      sendData("AT+CWJAP=\"teste\",\"qwertyuiop\"\r\n", 2000, DEBUG); //nesse campo eu defino a senha de conexao
      delay(3000);
      sendData("AT+CWMODE=1\r\n", 1000, DEBUG);
      // Mostra o endereco IP
      sendData("AT+CIFSR\r\n", 1000, DEBUG);
      // Configura para multiplas conexoes
      sendData("AT+CIPMUX=1\r\n", 1000, DEBUG);
      // Inicia o web server na porta 80
      //sendData("AT+CIPSERVER=1,80\r\n", 1000, DEBUG);
    }
    
    void loop() {
      value_humid = digitalRead(sensor_humid); //digital para botao e analogRead para o sensor
      String humid=String(value_humid);// turn integer to string
      updateTS(humid);
      delay(3000); //
    }
    //----- update the  Thingspeak string with 3 values
    void updateTS(String H)
    {
      // ESP8266 Client
      String cmd = "AT+CIPSTART=\"TCP\",\"";// Setup TCP connection
      cmd += IP;
      cmd += "\",80";
      sendDebug(cmd);
      delay(2000);
      if( Serial.find( "Error" ) )
      {
        esp8266.print( "RECEIVED: Error\nExit1" );
        return;
      }
    
      cmd = GET + "&field1=" + H +"\r\n";
      Serial.print( "AT+CIPSEND=" );
      Serial.println( cmd.length() );
      if(Serial.find( ">" ) )
      {
        esp8266.print(">");
        esp8266.print(cmd);
        Serial.print(cmd);
      }
      else
      {
        sendDebug( "AT+CIPCLOSE" );//close TCP connection
      }
      if( Serial.find("OK") )
      {
        esp8266.println( "RECEIVED: OK" );
      }
      else
      {
        esp8266.println( "RECEIVED: Error\nExit2" );
      }
    }
    
     
    String sendData(String command, const int timeout, boolean debug)
    {
      // Envio dos comandos AT para o modulo
      String response = "";
      esp8266.print(command);
      long int time = millis();
      while ( (time + timeout) > millis())
      {
        while (esp8266.available())
        {
          // The esp has data so display its output to the serial window
          char c = esp8266.read(); // read the next character.
          response += c;
        }
      }
      if (debug)
      {
        Serial.print(response);
      }
      return response;
    }
    
    void sendDebug(String cmd)
    {
      esp8266.print("SEND: ");
      esp8266.println(cmd);
      Serial.println(cmd);
    }

 

Link para o comentário
Compartilhar em outros sites

Visitante
Este tópico está impedido de receber novas respostas.

Sobre o Clube do Hardware

No ar desde 1996, o Clube do Hardware é uma das maiores, mais antigas e mais respeitadas comunidades sobre tecnologia do Brasil. Leia mais

Direitos autorais

Não permitimos a cópia ou reprodução do conteúdo do nosso site, fórum, newsletters e redes sociais, mesmo citando-se a fonte. Leia mais

×
×
  • Criar novo...