Hello i need help with my django project . I want to run program on ESP 8266 from django website. To test I create simple program with blink diode on ESP and I made button on my django website and it works but I use "webbrowser()" in views.py and it is not looks perfect (I need to stay on this webiste, a webiste made in django, not open a new one). I need help to create something what will work like: I click the button on my django website, program in "views.py" send something to esp what runs program on it or change something in the ESP code, which is only going to run the code, and it won't open web browser.
my views.py:
def espON(request):
on = On.objects.all()
off = Off.objects.all()
menu = menu.objects.all()
webbrowser.open("http://my_ip/turnOn")
d = {'on':on, 'off':off, 'menu':menu,}
return render(request, 'on/index.html', d)
ESP code:
#include <ESP8266WiFi.h>
#include <Ticker.h>
#include <Math.h>
const char* ssid = "my_ssid"; //nazwa ssid sieci
const char* password = "my_password"; //haslo
#define LED 5
WiFiServer server(80);
void setup() {
Serial.begin(115200);
delay(10);
pinMode(LED, OUTPUT);
//Laczenie z siecia wifi
Serial.println();
Serial.println();
Serial.print("Laczenie z: ");
Serial.println(ssid);
IPAddress ip(my_ip); //ip
IPAddress gateway(my_ip2);
IPAddress subnet(my_ip3);
WiFi.config(ip, gateway, subnet);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi polaczone");
// Start
server.begin();
Serial.println("Serwer uruchomiony");
}
void loop() {
while (WiFi.status() == WL_CONNECTED){
WiFiClient client = server.available();
if (!client) {
return;
}
String request = client.readStringUntil('\r');
client.flush();
//condition
if (request.indexOf("/turnOn") > 0)
{
digitalWrite(LED, HIGH); //work
}
if (request.indexOf("/turnOff") > 0) //not
{
digitalWrite(LED, LOW);
}
}
}