Hello, I would need your help to combine these two codes into one to work at the same time.
The first one works by switching the relay off at "http://ipadress/relay=OFF" and switching the relay on at "http://ipadress/relay=ON".
The code is here:
/*
* Web-Controlled LED - https://steve.fi/Hardware/
*
* This is a simple program which uses WiFi to allow the on-board
* LED to be controlled.
*
* http://192.168.10.51/relay=OFF
* -> Turns off the on-board LED.
*
* http://192.168.10.51/relay=ON
* -> Turns on the on-board LED.
*
*/
#include <ESP8266WiFi.h>
//
// WiFi details.
//
const char* ssid = "wifi_name";
const char* password = "wifi_pass";
//
// The on-board status-relay pin, and starting state (on).
//
int state = HIGH;
//
// We'll be running a HTTP-server on port 80.
//
WiFiServer server(80);
//
// Called once to set things up.
//
void setup()
{
// Setup serial-output
Serial.begin(115200);
delay(10);
// Configure the pin.
pinMode(D1, OUTPUT);
digitalWrite(D1, state);
// Connect to WiFi network
WiFi.mode(WIFI_STA);
WiFi.hostname("web-relay");
WiFi.begin(ssid, password);
// Wait until we're connected.
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("WiFi connected");
// Start the server
server.begin();
Serial.println("Server started");
// Print the IP address
Serial.print("Use this URL to connect: ");
Serial.print("http://");
Serial.print(WiFi.localIP());
Serial.println("/");
}
//
// Called constantly to respond to things.
//
void loop()
{
// Check if a client has connected
WiFiClient client = server.available();
if (!client)
return;
// Wait until the client sends some data
while (client.connected() && !client.available())
delay(1);
// Read the first line of the request
String request = client.readStringUntil('\r');
Serial.println(request);
client.flush();
if (request.indexOf("/relay=OFF") != -1)
state = LOW;
if (request.indexOf("/relay=ON") != -1)
state = HIGH;
// Change the state.
digitalWrite(D1, state);
// Return the response
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><head><title>Web-relay</title></head><body>");
if (state == LOW)
client.print("<p>relay pin is now off.</p>");
if (state == HIGH)
client.print("<p>relay pin is now on.</p>");
// Show the output
client.println("<p><a href=\"/relay=OFF\">Turn OFF</a></p>");
client.println("<p><a href=\"/relay=ON\">Turn ON</a></p>");
client.println("</body></html>");
delay(1);
Serial.println("Client disconnected");
}
and then the code from sinric pro to control the relay via google home is here:
/*
* Simple example for how to use multiple SinricPro Switch device:
* - setup 4 switch devices
* - handle request using multiple callbacks
*
* If you encounter any issues:
* - check the readme.md at https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/README.md
* - ensure all dependent libraries are installed
* - see https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/README.md#arduinoide
* - see https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/README.md#dependencies
* - open serial monitor and check whats happening
* - check full user documentation at https://sinricpro.github.io/esp8266-esp32-sdk
* - visit https://github.com/sinricpro/esp8266-esp32-sdk/issues and check for existing issues or open a new one
*/
#ifdef ENABLE_DEBUG
#define DEBUG_ESP_PORT Serial
#define NODEBUG_WEBSOCKETS
#define NDEBUG
#endif
#include <Arduino.h>
#ifdef ESP8266
#include <ESP8266WiFi.h>
#endif
#ifdef ESP32
#include <WiFi.h>
#endif
#include "SinricPro.h"
#include "SinricProSwitch.h"
#define WIFI_SSID "wifi_name"
#define WIFI_PASS "wifi_pass"
#define APP_KEY "APP_KEY"
#define APP_SECRET "APP_SECRET"
#define SWITCH_ID_1 "SWITCH_ID"
#define RELAYPIN_1 D1
#define BAUD_RATE 115200 // Change baudrate to your need
bool onPowerState1(const String &deviceId, bool &state) {
Serial.printf("Device 1 turned %s", state?"on":"off");
digitalWrite(RELAYPIN_1, state ? HIGH:LOW);
return true; // request handled properly
}
// setup function for WiFi connection
void setupWiFi() {
Serial.printf("\r\n[Wifi]: Connecting");
WiFi.begin(WIFI_SSID, WIFI_PASS);
while (WiFi.status() != WL_CONNECTED) {
Serial.printf(".");
delay(250);
}
Serial.printf("connected!\r\n[WiFi]: IP-Address is %s\r\n", WiFi.localIP().toString().c_str());
}
// setup function for SinricPro
void setupSinricPro() {
// add devices and callbacks to SinricPro
pinMode(RELAYPIN_1, OUTPUT);
SinricProSwitch& mySwitch1 = SinricPro[SWITCH_ID_1];
mySwitch1.onPowerState(onPowerState1);
// setup SinricPro
SinricPro.onConnected([](){ Serial.printf("Connected to SinricPro\r\n"); });
SinricPro.onDisconnected([](){ Serial.printf("Disconnected from SinricPro\r\n"); });
SinricPro.begin(APP_KEY, APP_SECRET);
}
// main setup function
void setup() {
Serial.begin(BAUD_RATE); Serial.printf("\r\n\r\n");
setupWiFi();
setupSinricPro();
}
void loop() {
SinricPro.handle();
}
I think some of you will be able to do it, I tried but always with an error, so I submit here, I hope I picked a good category.