Customise Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorised as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyse the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customised advertisements based on the pages you visited previously and to analyse the effectiveness of the ad campaigns.

No cookies to display.

Wysyłanie danych przez WiFi ESP i Python

//Arduino
#include <ESP8266WiFi.h>
const char* ssid=" "; // podaj swoje ssid
const char* password=" "; // podaj swoje haslo wifi
const int led= 2;
WiFiServer server(80);

void setup() {
  pinMode(led,OUTPUT);
  Serial.begin(115200);
  Serial.print("Connecting to.");
  Serial.println(ssid);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid,password);

  while (WiFi.status() != WL_CONNECTED){
    delay(500);
    Serial.print("..");
  }
  Serial.println("Nodemcu(esp8266) is connected to the ssid");
  Serial.println(WiFi.localIP());
  server.begin();
  delay(1000);
}

void loop() {
  WiFiClient client;
  client=server.available();
  if (client ==1){
    String request=client.readStringUntil('\n');
    client.flush();
    Serial.println(request);
    if (request.indexOf("ledon") != -1){
      digitalWrite(led,HIGH);
      Serial.println("LED IS ON NOW");}
    else if (request.indexOf("ledoff") != -1){
      digitalWrite(led,LOW);
      client.println("HTTP/1.1 200 OK");
      Serial.println("LED IS OFF NOW");}
      Serial.print("Client Disconnected");
      Serial.println("===========================");
      Serial.println("                              ");
  }
}
#Python
import tkinter
import urllib.request # funkcje I klasy pomagajace w obsłudze adresow url
root_url = http://192.168.0.24 # wpisz adres ip ESP8266, który sprawdzisz w Serial monitorze po wgraniu programu

window=tkinter.Tk()
window.title("Python with ESP8266 wifi")

def sendRequest(url):
    n = urllib.request.urlopen(url)

def set_button_on():
    btn_state_label['text'] = "LED ON"
    sendRequest(root_url+"/ledon")

def set_button_off():
    btn_state_label['text'] = "LED OFF"
    sendRequest(root_url+"/ledoff")

window.geometry('300x300')
btn_state_label = tkinter.Label(text = "___")
btn_state_label.pack()

button1state = tkinter.Button(window,
    text="ON",
    command=set_button_on,
    height = 4,
    fg = "black",
    width = 8,
    bd = 5,
    background = "green"
)
button1state.pack(side='top', ipadx=10, padx=10, pady=15)

button2state = tkinter.Button(window,
    text="OFF",
    command=set_button_off,
    height = 4,
    fg = "black",
    width = 8,
    bd = 5,
    background='red'
)
button2state.pack(side='top', ipadx=10, padx=10, pady=15)

window.mainloop()

Leave a Comment