0

I have connected ESP8266 to my Raspberry Pi's WiFi network and assigned a static IP.

My Arduino code

#include "SoftwareSerial.h"String ssid = "Rpi";String password = "raspberry";SoftwareSerial esp(3, 2);// RX, TXString server = "192.168.50.1"; //my Hostvoid setup() {  esp.begin(115200);  Serial.begin(115200);  connectWifi();  httpget();  delay(1000);}void connectWifi() {  String cmd = "AT+CWJAP=\"" + ssid + "\",\"" + password + "\"";  esp.println(cmd);  delay(4000);  if (esp.find("OK")) {    Serial.println("Connected!");  }  else {    Serial.println("Cannot connect to wifi ! Connecting again...");  }  connectWifi();}/////////////////////////////GET METHOD///////////////////////////////void httpget() {  esp.println("AT+CIPSTART=\"TCP\",\"" + server + "\",1234");//start a TCP connection.  if ( esp.find("OK")) {    Serial.println("TCP connection ready");  }  delay(1000);  /*    String getRequest =    "GET " + uri + " HTTP/1.0\r\n" +    "Host: " + server + "\r\n" +    "Accept: *" + "/" + "*\r\n" +    "Content-Type: application/json\r\n" +    "\r\n";  */  String sendCmd = "AT+CIPSEND=";  esp.print(sendCmd);  //esp.println(getRequest.length() );  delay(500);  if (esp.find(">")) {    Serial.println("Sending..");    //  esp.print(getRequest);    if ( esp.find("SEND OK")) {      Serial.println("Packet sent");      while (esp.available()) {        String response = esp.readString();      }      esp.println("AT+CIPCLOSE");    }  }}void loop() {  httpget();}

I get this message

Cannot connect to wifi ! Connecting again...

In my Raspberry Pi I am constantly sending a signal to this IP address.

import socketTCP_IP = '192.168.50.1' #Local HostTCP_PORT = 1234            #Listening PortBUFFER_SIZE = 12SERVER_IP = '192.168.50.18'  #Arduino IPSERVER_PORT = 3007            #Server Ports = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)s.bind((TCP_IP,TCP_PORT))#s.listen(1)#(conn,addr) = s.accept()#print 'Connection address: ',addrwhile True:    print 'Sending message'    s.sendto('1',(SERVER_IP,SERVER_PORT))    print 'Waiting for response'    data,addr = s.recvfrom(BUFFER_SIZE)    if not data: break    print 'received data: ',data    print 'data recieved from: ',addr    conn.close()s.close()

But I can not see it in my Serial Monitor. Do I need to create a separate program for getting from WiFi?

per1234's user avatar
per1234
4,2982 gold badges24 silver badges44 bronze badges
askedDec 19, 2017 at 6:55
Sachith Muhandiram's user avatar
3
  • you didn't start a server on esp8266 so nothing listens on esp for signals from raspberryCommentedDec 23, 2017 at 19:21
  • did you change the wifi mode in esp? it is in AP mode at defaultCommentedDec 23, 2017 at 19:22
  • softwareserial at 115200 has errorsCommentedDec 23, 2017 at 19:23

1 Answer1

1

You are recursively callingconnectWifi() and therefore your setup function results in an infinite loop.You must not implement loops and for processes, which take some time, you should set and ask a flag.Btw. why don't you use the official ESP WiFi library?

answeredDec 22, 2017 at 14:57
Sim Son's user avatar
1
  • What is official ESP WiFi library?CommentedDec 23, 2017 at 19:18

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.