domingo, 9 de outubro de 2016

Uma grande novidade na tecnologia: O nodemcu

Salve salve galerinha que acompanha o meu maravilhindo blog, toda postagem é uma novidade aqui, então vim trazzervos mais uma linda novidade, O node mcu:


Ahe voces me perguntam, o que é o nodemcu?Bem a resposta é simples, é uma plataforma de desenvolvimento baseada no chip esp8266, que começou como modulo wifi pro arduino e acabou se tornando auto-suficiente, desbancando o seu progenitor^^
Uma fotinha do esp sozinho:
E pra mexer com o danado pessoal, aconselho a comprar um node, pois ele ja vem com tudo prontinho, drivers, bootloader, linguagem lua, etc. É so plug-and-play, se voce quiser se virar com o ESP é um cambalacho pra fazer ele funcionar, alem de procurar drivers, terá de usar um conversor USB-SERIAL ou o própio arduino para programa-lo via comandos AT X_X

Bom pessoal, feita as apresentações, hora de falar desse danado, o que ele faz?
Bem basicamente ele vem com um firmware dentro de si, pronto para rodar linguagem LUA,que é uma linguagem desenvolvida por um brasileiro na decada de 90 que ganhou grandes proporções e hoje é bastante usada a nível mundial.
Mas nada impede que voce troque o firmware dele e o programe em uma linguagem a qual está mais acostumado como a do arduino^^
Bem pessoal irei ensinar a programar a do arduino e indicarei um tuto para programar em LUA, em vez de chupinhar o tuto do cara para cá mais fácil indicar o tuto dele^^
Se quiser programar em Lua, baixe o programa Esplorer aqui
E depois não tem muita novidade, ele não é instalável, é so clicar e executar^^
Depois voce pode testar esse código aqui do esplorer:
-----------------------------------------------------------------
-- Programa: Web Server com ESP8266 Nodemcu
-- Autor: FILIPEFLOP
 --Auterado por Luciano
-- Conexao na rede Wifi
wifi.setmode(wifi.STATION)
wifi.sta.config("NOME_DA_REDE","SENHA_DA_REDE")
print(wifi.sta.getip())
-- Definicoes do pino do led
led1 = 1
led2 = 2
led3 = 3
gpio.mode(led1, gpio.OUTPUT)
gpio.mode(led2, gpio.OUTPUT)
gpio.mode(led3, gpio.OUTPUT)
-- Definicoes do Web Server
srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
    conn:on("receive", function(client,request)
        local buf = "";
        local _, _, method, path, vars = string.find(request, "([A-Z]+) (.+)?(.+) HTTP");
        if(method == nil)then
            _, _, method, path = string.find(request, "([A-Z]+) (.+) HTTP");
        end
        local _GET = {}
        if (vars ~= nil)then
            for k, v in string.gmatch(vars, "(%w+)=(%w+)&*") do
                _GET[k] = v
            end
        end
        buf = buf.."<h1><u>FILIPEFLOP</u></h1>";
        buf = buf.."<h2><i>ESP8266 Web Server</i></h2>";
        buf = buf.."<p><a href=\"?pin=LIGA1\"><button><b>LED 1 LIG</b></button></a> <br/><br/><a href=\"?pin=DESLIGA1\"><button><b>LED 1 DES</b></button></a></p>";
        buf = buf.."<p><a href=\"?pin=LIGA2\"><button><b>LED 2 LIG</b></button></a> <br/><br/><a href=\"?pin=DESLIGA2\"><button><b>LED 2 DES</b></button></a></p>"; 
buf = buf.."<p><a href=\"?pin=LIGA3\"><button><b>LED 3 LIG</b></button></a> <br/><br/><a href=\"?pin=DESLIGA3\"><button><b>LED 3 DES</b></button></a></p>";        
local _on,_off = "",""
        if(_GET.pin == "LIGA1")then
              gpio.write(led1, gpio.HIGH);
        elseif(_GET.pin == "DESLIGA1")then
              gpio.write(led1, gpio.LOW);
        end


 if(_GET.pin == "LIGA2")then

              gpio.write(led2, gpio.HIGH);
        elseif(_GET.pin == "DESLIGA2")then
              gpio.write(led2, gpio.LOW);
        end

if(_GET.pin == "LIGA3")then

              gpio.write(led3, gpio.HIGH);
        elseif(_GET.pin == "DESLIGA3")then
              gpio.write(led3, gpio.LOW);
        end
        client:send(buf);
        client:close();
        collectgarbage();
    end)
end)
-----------------------
Originalmente do filipeflop, pode conferir na integra aqui, mas alterado por mim pra
incluir mais
2 botões...

Bahh confessor que não curti a linguagem LUA, preferi programar no arduino,
sendo assim segue o tuto de verdade:)
1° configure seu arduino para rodar o node, siga esse tutorial aqui:
Depois tenha ciencia dos pinos no arduino e sua contraparte no nodemcu:
NodeMCU – Arduino
D0 = 16;
D1 = 5;
D2 = 4;
D3 = 0;
D4 = 2;
D5 = 14;
D6 = 12;
D7 = 13;
D8 = 15;
D9 = 3;
D10 = 1;

Agora vem o chachacham^^

Esse código eu adaptei do Pedro Minatel, um excelente blog que aqui vos deixo o link
coloquei mais leds e também adicionei um sensor analogico, para voce ler a captura de sensor
que desejar
Sem frescura, segue o código:
-------------------
/*

The MIT License (MIT)

Copyright (c) 2015 Pedro Minatel

Permission is hereby granted, free of charge, to any person obtaining a copy

of this software and associated documentation files (the "Software"), to deal

in the Software without restriction, including without limitation the rights

to use, copy, modify, merge, publish, distribute, sublicense, and/or sell

copies of the Software, and to permit persons to whom the Software is

furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all

copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR

IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,

FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE

AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER

LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,

OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE

SOFTWARE.

*/



#include <ESP8266WiFi.h>





const char* ssid = "Sua rede";

const char* password = "Sua senha";

int valor;



WiFiServer server(80);



void setup() {

  

  Serial.begin(115200);

  delay(10);

pinMode(0,OUTPUT);

digitalWrite(0,0);

  // prepare GPIO2

  pinMode(4, OUTPUT);

  digitalWrite(4, 0);



  pinMode(5, OUTPUT);

  digitalWrite(5, 0);

  

  pinMode(12, OUTPUT);

  digitalWrite(12, 0);


  pinMode(13, OUTPUT);

  digitalWrite(13, 0);

  

  // Connect to WiFi network

  Serial.println();

  Serial.println();

  Serial.print("Connecting to ");

  Serial.println(ssid);

  

  WiFi.begin(ssid, password);

  

  while (WiFi.status() != WL_CONNECTED) {

    delay(500);

    Serial.print(".");

  }

  

  Serial.println("");

  Serial.println("WiFi connected");

  

  // Start the server

  server.begin();

  Serial.println("Server started");

  Serial.println(WiFi.localIP());

}



void loop() {

  

  WiFiClient client = server.available();

  if (!client) {

    return;

  }

  

  Serial.println("new client");

  while(!client.available()){

    delay(1);

  }

  

  String req = client.readStringUntil('\r');

  Serial.println(req);

  client.flush();

valor =analogRead(0);

  String buf = "";



  buf += "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\n";

  buf += "<center>";

  buf += "<h1> Eletronica e utilidades</h1>";

  buf += "<h1> ESP8266 Web Server teste</h1>";

  

  buf += "<p> <a href=\"?function=led4_on\"><button>ligar a sala1</button></a></p> <a href=\"?function=led4_off\"><button>apagar a sala1</button></a></p>";

  buf += "<p> <a href=\"?function=led5_on\"><button>ligar o quarto1</button></a></p> <a href=\"?function=led5_off\"><button>apagar o quarto1</button></a></p>";

   buf += "<p> <a href=\"?function=led6_on\"><button>ligar a cozinha</button></a></p> <a href=\"?function=led6_off\"><button>apagar o cozinha</button></a></p>";

    buf += "<p> <a href=\"?function=led7_on\"><button>ligar o corredor</button></a></p> <a href=\"?function=led7_off\"><button>apagar o corredor</button></a></p>";

     buf += "<p> <a href=\"?function=led8_on\"><button>ligar o banheiro</button></a></p> <a href=\"?function=led8_off\"><button>apagar o banheiro</button></a></p>";

 buf += "</html>\n";



  client.print(buf);

  client.println(valor);

  client.flush();

  

  if (req.indexOf("led5_on") != -1)

    digitalWrite(5, 1);

  else if (req.indexOf("led5_off") != -1)

    digitalWrite(5, 0);

  else if (req.indexOf("led4_on") != -1)

    digitalWrite(4, 1);

  else if (req.indexOf("led4_off") != -1)

    digitalWrite(4, 0);

    else if (req.indexOf("led6_on") != -1)

    digitalWrite(0, 1);

  else if (req.indexOf("led6_off") != -1)

  digitalWrite(0, 0);

  else if (req.indexOf("led7_on") != -1)

    digitalWrite(12, 1);

  else if (req.indexOf("led7_off") != -1)

   digitalWrite(12, 0);

   else if (req.indexOf("led8_on") != -1)

    digitalWrite(13, 1);

  else if (req.indexOf("led8_off") != -1)

   digitalWrite(13, 0);

    

    

  else {

    Serial.println("invalid request");

    client.stop();

  }

  Serial.println("Client disonnected");

}
----------------
Não terá vídeo desse projeto que ja dei provas mais que suficientes para voces confiarem em mim:)
Além disso não é nada de assim tão novo, é o ethernet shield só que com o nodemcu.
Então pessoal até a próxima^^

Nenhum comentário: