Saturday, June 23, 2018

make ESP8266 RELAY V3(LC Technology) useable , no longer a toy !

0X00 Background:
    ESP8266 RELAY V3(LC Technology) come with ESP8266-01 module + baseboard (with ssr), but, there is always a but, when power down and reboot it, the configuration is gone, should use AT command to config it again, eg: WIFI essid password can not store on chip.

0X01 GOAL:
     Http-GET control ssr -- ESP8266 RELAY V3(LC Technology)

0X02 Attention:
    This tutorial will erase every thing on ESP8266-01 module, your module will *NOT* support AT command anymore.

0XFF Tutorial here:
1. connect and prepare to flashing
REFERENCE:
esp8266_series_modules_user_manual_v1.1.pdf : 2.2 Boot Mode
How to use the ESP8266-01 pins

Get ESP8266-01 module down from baseboard, connect to *3.3V* USB-TTL as follow:
ESP8266-01 | USB-TTL
TX - RX
EN - resistance(560R works) - VCC3.3
RST - resistance(560R works) - VCC3.3
3V3 - VCC3.3
GND - GND
IO2 - VCC3.3
IO0 - GND
RX -TX

2. connect USB-TTL to pc usb port(OS arch linux here, windows will  be OK)
bash: ls /dev/ttyUSB*; sudo chmod 666 /dev/ttyUSB*

3. download Arduino IDE, and configuration
3.1 File -> Preferences: fill Additional board manager URLs as:
http://arduino.esp8266.com/stable/package_esp8266com_index.json
3.2 Tool -> board -> Boards Manager -> install esp8266 by ESP8266 Community (version 2.4.1 here)

4. write code and flashing
REFERENCE:
wifi connection example
esp8266wifi library reference
arduino code reference

Tool -> Board -> Generic ESP8266 Module
Tool -> Flash Size -> 8M
Tool -> Upoad Speed -> 115200
Tool -> Port -> /dev/ttyUSBx

code as following, change #define suitable to your network, and compile & upload
#include
#include
#include

#define MDNSNAME "testlight"
#define ESSID "<your wifi  essid>"
#define WIFIPASS "<your wifi password>"

byte openstr[] = {0xA0, 0x01, 0x01, 0xA2};
byte closestr[] = {0xA0, 0x01, 0x00, 0xA1};

ESP8266WebServer server(80);

void ssr(bool of=false) {
  if(of)
    Serial.write(openstr, sizeof(openstr));
  else
    Serial.write(closestr, sizeof(closestr));
}

void handleRoot() {
  server.send(200, "text/plain", "/?ssr=on/off");
  if (server.hasArg("ssr")) {
    if (server.arg("ssr") == "on") {
      ssr(true);
    }
    else if (server.arg("ssr") == "off") {
      ssr(false);
    }
    server.send(200, "text/plain", "ssr="+server.arg("ssr"));//this line  not works, fix it later
  } else {
    server.send(200, "text/plain", "/?ssr=on/off");
  }
}

void handleNotFound(){
  handleRoot();
}

void setup() {
  Serial.begin(9600);
  WiFi.begin(ESSID, WIFIPASS);
  Serial.print("Connecting");
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println();

  Serial.print("Connected, IP address: ");
  Serial.println(WiFi.localIP());

  if (!MDNS.begin(MDNSNAME)) {
  Serial.println("Error setting up MDNS responder!");
  }
  Serial.println("mDNS responder started");

  server.on("/", handleRoot);
  server.onNotFound(handleNotFound);
  server.begin();
  Serial.println("HTTP server started");
}

void loop() {
  server.handleClient();
}

5. Test
bash: ping testlight.local
eg: 192.168.1.123
http://192.168.1.123/?ssr=on
http://192.168.1.123/?ssr=off


No comments: