this is my code, after upload in serial monitor i see esp is connect but this is not in network in reality ! this code is ok for esp8266 but can not work with esp32 !
in below is my code and my output:
Code: Select all
#include <WiFi.h>
#include <ESPmDNS.h>
#include <Bounce2.h>
IPAddress ip(192, 168, 1, 252); //*****************************
IPAddress gateway(192, 168, 1, 1); //**************************
IPAddress subnet(255, 255, 255, 0); //*************************
IPAddress DNS(192, 168, 1, 1); //******************************
const char* ssid = "Reza";
const char* password = "rayan1369";
const int buttonPinA = 4;
const int relayPinA = 22;
Bounce debouncerA = Bounce();
int trigger = 0;
int oldValueA;
WiFiClient client;
WiFiServer server(80);
void setup() {
pinMode(buttonPinA, INPUT_PULLUP);
debouncerA.attach(buttonPinA);
debouncerA.interval(50);
pinMode(relayPinA, OUTPUT);
digitalWrite(relayPinA, HIGH);
Serial.begin(115200);
delay(10);
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.config(ip, gateway, subnet, DNS);//**********************
delay(100);//**********************************
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
if (WiFi.status() != WL_CONNECTED) {
for (int i = 0; i <= 20; i++) {
delay(500);
Serial.print(".");
}
}
if (WiFi.status() == WL_CONNECTED) {
Serial.println("");
Serial.println("WiFi connected");
}
// Start the server
server.begin();
Serial.println("Server started");
// Print the IP address
Serial.print("Use this URL : ");
Serial.print("http://");
Serial.print(WiFi.localIP());
Serial.println("/");
debouncerA.update();
oldValueA = debouncerA.read();
}
void loop() {
{
if (WiFi.status() != WL_CONNECTED) {
WiFi.config(ip, gateway, subnet, DNS);
Serial.print("Connecting to ");
delay(10);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
server.begin();
}
delay(100);
}
{
if (trigger == 0) {
if (client.connect("192.168.1.201", 8080)) {
client.print("GET /json.htm?type=command¶m=udevice&idx=55&nvalue=0");
client.println(" HTTP/1.1");
client.print("Host: ");
client.print("192.168.1.201");
client.print(":");
client.println(8080);
client.println();
client.stop();
}
if (client.connect("192.168.1.201", 8080)) {
client.print("GET /json.htm?type=command¶m=addlogmessage&message=OFF");
client.println(" HTTP/1.1");
client.print("Host: ");
client.print("192.168.1.201");
client.print(":");
client.println(8080);
client.println();
client.stop();
}
trigger = 1;
}
}
{
debouncerA.update();
int valueA = debouncerA.read();
if (valueA != oldValueA) {
int state = digitalRead(relayPinA);
digitalWrite(relayPinA, !state);
if ( state == 0) {
if (client.connect("192.168.1.201", 8080)) {
client.print("GET /json.htm?type=command¶m=udevice&idx=55&nvalue=0");
client.println(" HTTP/1.1");
client.print("Host: ");
client.print("192.168.1.201");
client.print(":");
client.println(8080);
client.println();
client.stop();
}
if (client.connect("192.168.1.201", 8080)) {
client.print("GET /json.htm?type=command¶m=addlogmessage&message=OFF");
client.println(" HTTP/1.1");
client.print("Host: ");
client.print("192.168.1.201");
client.print(":");
client.println(8080);
client.println();
client.stop();
}
}
if ( state == 1) {
if (client.connect("192.168.1.201", 8080)) {
client.print("GET /json.htm?type=command¶m=udevice&idx=55&nvalue=1");
client.println(" HTTP/1.1");
client.print("Host: ");
client.print("192.168.1.201");
client.print(":");
client.println(8080);
client.println();
client.stop();
}
if (client.connect("192.168.1.201", 8080)) {
client.print("GET /json.htm?type=command¶m=addlogmessage&message=ON");
client.println(" HTTP/1.1");
client.print("Host: ");
client.print("192.168.1.201");
client.print(":");
client.println(8080);
client.println();
client.stop();
}
}
}
oldValueA = valueA;
delay(50);
}
{
WiFiClient client = server.available();
if (!client) {
return;
}
Serial.println("new client");
while (!client.available()) {
delay(1);
}
String request = client.readStringUntil('\r');
Serial.println(request);
client.flush();
if (request.indexOf("/relayon") != -1) {
digitalWrite(relayPinA, LOW);
}
if (request.indexOf("/relayoff") != -1) {
digitalWrite(relayPinA, HIGH);
}
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>");
}
}
Code: Select all
ets Jun 8 2016 00:22:57
rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
ets Jun 8 2016 00:22:57
rst:0x10 (RTCWDT_RTC_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0010,len:4
load:0x3fff0014,len:588
load:0x40078000,len:0
load:0x40078000,len:10472
entry 0x40078a28
Connecting to Reza
.....................
WiFi connected
Server started
Use this URL : http://192.168.1.252/