-->
Page 1 of 1

ESP-01S

PostPosted: Thu Feb 08, 2018 6:11 pm
by liderbug
Image
ESP8266 5V WiFi relay module Things smart home remote control switch phone APP ESP-01
$2.70

Two things must be done for this to work: a jumper on the back of the pcb, vcc->en & 10K vcc->gpio0

( to tempt fate - it's been a good day :D
)
Code: Select all#include <ESP8266WiFi.h>

const char* ssid = "YourSSid";
const char* password = "y0urpa55w0rd";
IPAddress ip( 192, 168, 0, 141 );  // unique IP for each board
IPAddress gateway( 192, 168, 0, 1 );
IPAddress subnet( 255, 255, 255, 0 );
WiFiServer server(80);

void setup() {
  pinMode(0, OUTPUT);
  delay(10);

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

  server.begin();
  delay(50); 
}

void loop() {

  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client) {
    return;
  }
 
  while(!client.available()){
    delay(10);
  }
 
  // Read the first line of the request
  String req = client.readStringUntil('\r');

  // Match the request
  int val;
  if (req.indexOf("/on") != -1) {     /http://192.168.0.141/on   or /off
     digitalWrite(0, 1);
     val = 1;                           // if you want feedback see below
  } else if (req.indexOf("/off") != -1) {
     digitalWrite(0, 0);
     val = 0;
  } else {
    client.stop();
    return;
  }

  client.flush();

// for feedback (or remove) see above
  String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\nGPIO is now ";
  s += (val)?"on":"off";
  s += "</html>\n";

  client.print(s);
  delay(10);
}



Re: ESP-01S

PostPosted: Fri Feb 09, 2018 1:52 am
by schufti
add a decent power supply, a protective case and your over a sonoff ...
and instead of setting val to 0/1 reading back the gpio would be more reliable.