├── ESP_Wahaj.h ├── README.md ├── esp_python.py └── examples └── esp_wifi └── esp_wifi.ino /ESP_Wahaj.h: -------------------------------------------------------------------------------- 1 | /* ESP8266 TO PY: LOCAL 2 | * Written by Junicchi 3 | * https://github.com/Kebablord 4 | *Edited and modified to transfer full duplex 5 | *https://github.com/wahajmurtaza/ 6 | 7 | * MAP 8 | - start(ssid,password)---> Starts the connection with given username and password 9 | - waitUntilNewReq() -----> Waits until a request is received from python 10 | - returnThisInt(data) ---> sends your Integer data to localhost 11 | - returnThisStr(data) ---> sends your String data to localhost 12 | - getPath() -------------> gets the request's path as string, ex: https://192.113.133/here -> "/here" 13 | */ 14 | 15 | #include 16 | #include 17 | #include 18 | 19 | // OUR SERVER'S PORT, 80 FOR DEFAULT 20 | WiFiServer server(80); 21 | WiFiClient client; 22 | String rule; 23 | 24 | void start(String ssid, String pass){ 25 | WiFi.mode(WIFI_STA); 26 | WiFi.begin(ssid.c_str(),pass.c_str()); 27 | 28 | Serial.println(""); 29 | // Wait for connection 30 | while (WiFi.status() != WL_CONNECTED) { 31 | delay(500); 32 | Serial.print("."); 33 | } 34 | Serial.println(""); 35 | Serial.print("Connected to "); 36 | Serial.println(ssid); 37 | Serial.print("IP address: "); 38 | Serial.println(WiFi.localIP()); 39 | if (!MDNS.begin("esp8266")) { 40 | Serial.println("Error setting up MDNS responder!"); 41 | while (1) { 42 | delay(1000); 43 | } 44 | } 45 | Serial.println("mDNS responder started"); 46 | server.begin(); 47 | Serial.println("TCP server started"); 48 | MDNS.addService("http", "tcp", 80); 49 | } 50 | 51 | bool isReqCame = false; 52 | 53 | bool CheckNewReq(){ 54 | client = server.available(); 55 | if (!client) { 56 | return 0; 57 | } 58 | /* 59 | while (client.connected() && !client.available()) { 60 | delay(1); 61 | }*/ //to make data transfer fast 62 | String req = client.readStringUntil('\r'); 63 | int addr_start = req.indexOf(' '); 64 | int addr_end = req.indexOf(' ', addr_start + 1); 65 | if (addr_start == -1 || addr_end == -1) { 66 | Serial.print("Invalid request: "); 67 | Serial.println(req); 68 | return 0; 69 | } 70 | req = req.substring(addr_start + 1, addr_end); 71 | 72 | 73 | rule = req; 74 | isReqCame = true; 75 | 76 | client.flush(); 77 | return 1; 78 | } 79 | void waitUntilNewReq(){ 80 | do {CheckNewReq();} while (!isReqCame); 81 | isReqCame = false; 82 | } 83 | 84 | void returnThisStr(String final_data){ 85 | String s; 86 | client.print(final_data); 87 | } 88 | void returnThisInt(int final_data){ 89 | returnThisStr(String(final_data)); 90 | } 91 | 92 | String getPath(){ 93 | return rule; 94 | } 95 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Reference 2 | This is modifid library from https://github.com/KebabLord/esp_to_python . 3 | I have edited some functions to get data two ways. 4 | 5 | 6 | # NodeMCU-Python-Wifi 7 | This is project to communicate between node mcu and python through wifi and send data two-ways, without have to wait and 8 | perform other task like normal function in arduino and python. 9 | 10 | This example code is using url library to send and receive data. But must change IP in pyhton.You can also access the ip from browser. 11 | i.e 192.168.10.1/one in browser 12 | # Installation 13 | Create a folder in arduino/library name esp_wahaj and paste all the files there. 14 | -------------------------------------------------------------------------------- /esp_python.py: -------------------------------------------------------------------------------- 1 | import urllib.request 2 | import http 3 | 4 | base = "http://192.168.10.19/" 5 | 6 | 7 | def transfer(my_url): #use to send and receive data 8 | try: 9 | n = urllib.request.urlopen(base + my_url).read() 10 | n = n.decode("utf-8") 11 | return n 12 | 13 | except http.client.HTTPException as e: 14 | return e 15 | 16 | 17 | two = transfer("two") 18 | one = transfer("one") 19 | print(one,two) 20 | _ = transfer("45") #Send this data 21 | 22 | #you can send pwm255 and then decode it to get pwm or more varibales 23 | -------------------------------------------------------------------------------- /examples/esp_wifi/esp_wifi.ino: -------------------------------------------------------------------------------- 1 | 2 | /* This example is written for Nodemcu Modules */ 3 | 4 | #include "ESP_Wahaj.h" // importing our library 5 | 6 | # define led D4 7 | int pwm = 255; 8 | String path = "nothing"; 9 | void setup(){ 10 | Serial.begin(9600); 11 | start("ssid","password"); // Wifi details connect to 12 | 13 | 14 | } 15 | 16 | void loop(){ 17 | //waitUntilNewReq(); //Waits until a new request from python come 18 | 19 | if(CheckNewReq() == 1) 20 | { 21 | //Serial.println("new request"); 22 | if (getPath()=="/two"){ 23 | returnThisStr("two replied"); 24 | } 25 | else if (getPath()=="/one"){ 26 | returnThisStr("one replied"); 27 | } 28 | else if(getPath()=="/favicon.ico"){ //this happens for browsers only. 29 | returnThisStr("garbage"); 30 | } 31 | 32 | else //here we receive data. You can receive pwm255 and the decode it to 255 and also get more variables like this 33 | { 34 | path = getPath(); 35 | Serial.println(path); //String 36 | //returnThisStr("nothing"); 37 | path.remove(0,1); //Remove slash / 38 | Serial.println(path); 39 | pwm = path.toInt(); //convert to int you can use toFloat() 40 | Serial.println(pwm); 41 | } 42 | 43 | } 44 | 45 | //Serial.println("tesiting...."); 46 | //if(pwm == 255) Serial.println("highhhhh"); 47 | //if(pwm == 0) Serial.println("lowwwwsssh"); 48 | //analogWrite(led,pwm); 49 | 50 | } --------------------------------------------------------------------------------