Leonardo Gonçalves
Published

Control your Things by Messenger

Control and manage your things by Facebook.

BeginnerProtip1 hour6,626
Control your Things by Messenger

Things used in this project

Hardware components

NodeMCU ESP8266 Breakout Board
NodeMCU ESP8266 Breakout Board
×1

Software apps and online services

Messenger
Taurus Platform
Das Coisas Taurus Platform

Story

Read more

Code

NodeMCU code

Arduino
/**
 * Leonardo Gonçalves - Das Coisas IoT Solutions
 *
 *
 *
 */

#include <Arduino.h>

#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>

#include <ESP8266HTTPClient.h>

#define USE_SERIAL Serial

ESP8266WiFiMulti WiFiMulti;
String ID = "ACTUATOR_ID";
int VAR = 0;
String GET, GETCREATE;
String V;
void setup() {

  USE_SERIAL.begin(115200);
  // USE_SERIAL.setDebugOutput(true);
  pinMode(BUILTIN_LED, OUTPUT);     // Initialize the BUILTIN_LED pin as an output
  USE_SERIAL.println();
  USE_SERIAL.println();
  USE_SERIAL.println();
  digitalWrite(BUILTIN_LED, HIGH);
  for (uint8_t t = 4; t > 0; t--) {
    USE_SERIAL.printf("[SETUP] WAIT %d...\n", t);
    USE_SERIAL.flush();
    delay(1000);
  }

  WiFiMulti.addAP("SSID", "PASSWORD");

}

void loop() {
  // wait for WiFi connection
  if ((WiFiMulti.run() == WL_CONNECTED)) {
    HTTPClient http;
    URL();
    USE_SERIAL.print("[HTTP] begin...\n");
    // configure traged server and url
    //http.begin("192.168.1.12", 443, "/test.html", true, "7a 9c f4 db 40 d3 62 5a 6e 21 bc 5c cc 66 c8 3e a1 45 59 38"); //HTTPS
    http.begin("104.131.189.228", 80, GET); //HTTP
    USE_SERIAL.print("[HTTP] GET...\n");
    // start connection and send HTTP header
    int httpCode = http.GET();
    if (httpCode) {
      // HTTP header has been send and Server response header has been handled
      USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode);

      // file found at server
      //if(httpCode == 200) {
      String payload = http.getString();
      int posicao = payload.indexOf(ID);
      if (posicao == -1 && httpCode == 200)
      {
        //CREATE();
      }
      String dado = payload.substring(posicao + 5, posicao + 6);
      USE_SERIAL.print("Dado: ");
      USE_SERIAL.println(dado);
      if (dado == "1")
      {
        digitalWrite(BUILTIN_LED, LOW);//ANODO COMUM, ACENDE COM LOW
      }
      if (dado == "0")
      {
        digitalWrite(BUILTIN_LED, HIGH);
      }
      USE_SERIAL.println(payload);
      //}
    } else {
      USE_SERIAL.print("[HTTP] GET... failed, no connection or no HTTP server\n");
    }
  }
  delay(1000);
}

void URL(void)
{
  GET = "http://104.131.189.228/ws/webresources/GetDigitais?key=SERIAL"; // Put your Taurus Serial Here!
  String estado = ",0)";
  GETCREATE = "http://104.131.189.228/ws/webresources/Create?key=SERIAL&&load=(";// Put your Taurus Serial Here!
  GETCREATE = GETCREATE + ID + estado;
  /*
  VAR++;
  V = String(VAR);
  USE_SERIAL.println(V);
  GET=GET+V;*/
}
void CREATE(void)
{
  URL();
  HTTPClient http;
  USE_SERIAL.println("Componente não existente, criando ...");
  http.begin("104.131.189.228", 80, GETCREATE); //HTTP
  USE_SERIAL.print("[HTTP] GET...\n");
  // start connection and send HTTP header
  int httpCode = http.GET();
  if (httpCode) {
    // HTTP header has been send and Server response header has been handled
    USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode);

    // file found at server
    //if(httpCode == 200) {
    String payload = http.getString();
    int posicao = payload.indexOf(ID);
  }
}

Credits

Leonardo Gonçalves

Leonardo Gonçalves

23 projects • 92 followers
Master Degree Engineer at Federal University of Itajubá - Brazil / Telecommunications Engineer graduated at Inatel - Minas Gerais - Brazil

Comments