-->
Page 1 of 1

Arduino Sketch for command relay: is all ok?

PostPosted: Wed Nov 30, 2016 10:50 am
by sineverba
Hi to all!
I did edit a sketch for Arduino to on / off a relay via local wifi. My Board is a NODEMCU.

E.g. if you type 192.168.1.101/5/1 relay on GPIO 5 on and with 192.168.1.101/5/0 goes OFF. Another, if you call 192.168.1.101/5

you get the state of PIN 5. If you edit at top, you can add others PIN so render sketch many abstract.

My only doubt is that with a model of Relay Module this works very well. 0 == OFF and 1 == ON. With another type, relay is ON at the bootup.

1) I think that I need to add PULL DOWN resistor to my circuit?
2) If I did understand very well, I cannot set the PULL DOWN via software?

This is the module that works as expected: Image


And module that works "otherwise": Image


My simple wiring is:

VIN to + (1st module) // VCC (2nd module)
GND to - (1st module) // gnd (2nd module)
GPIO5 to S (1st module) // IN (2nd module)

No transistor, no resistor. Maybe I need a resistor between GND (nodemcu) and GPIO5 (nodemcu) as PULL DOWN?


This is the code:

Code: Select all/*
 *  This sketch sends data via HTTP GET requests to data.sparkfun.com service.
 *
 *  You need to get streamId and privateKey at data.sparkfun.com and paste them
 *  below. Or just customize this script to talk to other HTTP servers.
 *
 */

#include <ESP8266WiFi.h>

IPAddress ip(192, 168, 1, 101);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);

const char* ssid     = "HIC_SUNT_LEONES";
const char* password = "***";

WiFiServer server(80);

void setup() {
 
  Serial.begin(115200);
  delay(10);

  // We start by connecting to a WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
 
  /* Explicitly set the ESP8266 to be a WiFi-client, otherwise, it by default,
     would try to act as both a client and an access-point and could cause
     network-issues with your other WiFi-devices on your WiFi-network. */
  WiFi.mode(WIFI_STA);

  WiFi.config(ip, gateway, subnet);
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected"); 
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  server.begin();

  // add here all pin need to be used
  pinMode(5, OUTPUT);
  digitalWrite(5, LOW);
 
}

int getHowManyOccurence(String url) {
  int i = 0;
  int occurence = 0;
  int string_length = url.length();
  for (i=0;i<string_length;i++) {
    if (url[i]=='/') {
      occurence++;
    }
  }
  return occurence;
}

String getNewGpioStateFromUrl(String url) {

  if (url.lastIndexOf("/")!=-1) {
    int last_index = url.lastIndexOf("/");
    String new_pin_state = url.substring(last_index+1);
    return new_pin_state;
  } else {
    return "";
  }
 
}

String getGpioFromUrl(String url) {

  String gpio;
  if (url.lastIndexOf("/")!=-1) {
    int last_index = url.lastIndexOf("/");
    int first_index = url.indexOf("/");

    if (last_index==first_index) {
      gpio = url.substring(1,url.length());
    } else {
      gpio = url.substring(1,last_index);
    }

    Serial.println("------ GETTING GPIO");
    Serial.println(gpio);

    return gpio;
   
  } else {
    Serial.println("LAST INDEX -1");
    return "";
  }
 
}

String getCleanUrl(String url) {
  url.replace("GET ", "");
  url.replace(" HTTP/1.1", "");
  return url;
}

String getGpio(String gpio) {
  int state;
  String return_state;
  int pin = atoi (gpio.substring(0,gpio.length()).c_str ());
  state = digitalRead(pin);

  if (state==HIGH) {
    return_state = "1";
  } else {
    return_state = "0";
  }
 
  return return_state;
 
}

void setGpio(String gpio, String state) {

  int new_state;
  int length_of_string = gpio.length();

  int pin = atoi (gpio.substring(0,length_of_string).c_str ());

  if (state=="1") {
    new_state = HIGH;
  } else {
    new_state = LOW;
  }

  digitalWrite(pin,new_state);
 
}

void printJsonAnswer(WiFiClient client, String state) {

  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: application/json");
  client.println("Access-Control-Allow-Origin: *");
  client.println("Connection: close");
  client.println("");

  String response = "\"" + state + "\"";

  client.println(response);

 
}

void printPage(WiFiClient client) {

  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html");
  client.println(""); //  do not forget this one
  client.println("<!DOCTYPE HTML>");
  client.println("<html>");
  client.println("<body>");
  client.println("<h1>");
  client.println("Benvenuto in ESP 8266!");
  client.println("</h1>");
  client.println("</body>");
  client.println("</html>");
 
}


void loop() {

  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client) {
    return;
  }
 
  // Wait until the client sends some data
  Serial.println("new client");
  while(!client.available()){
    delay(1);
  }
 
  // Read the first line of the request
  String raw_url = client.readStringUntil('\r');
  Serial.println(raw_url);
  client.flush();

  String url = getCleanUrl(raw_url);
  Serial.println("La url chiamata pulita è ");
  Serial.println(url);

  // Match the request

  // it's a get!
  String gpio = getGpioFromUrl(url);
  if (gpio!="") {
     Serial.println("La GPIO è ");
     Serial.println(gpio);
  }

  int occurence = getHowManyOccurence(url);
  Serial.println("Abbiamo trovato questo numero di slash ");
  Serial.println(occurence);

  if (occurence==2) {

    String new_pin_state = getNewGpioStateFromUrl(url);
    if (new_pin_state!="") {
      Serial.println("Il nuovo stato richiesto è ");
      Serial.println(new_pin_state);

      setGpio(gpio,new_pin_state);
    }
   
   
  }

  String state = getGpio(gpio);

  //printPage(client);
  printJsonAnswer(client,state);
 
  delay(1);
  Serial.println("Client disconnected");
  Serial.println("");
 
  }


Thank you to all