0

I am developing a project based in Arduino Nano and 2 sensors. In this projects is very important the energy because of the prototype is going to be powered by batteries or solar power. In order to save energy in the

void loop(){....}

I am only getting the value of a temperature sensor continuously in a method that returns true / false. When the method returns true, one relay is activated by Arduino, and it provides energy to the ESP8266-01 module and other sensor. At this moment I need to connect the ESP8266 to WiFi network, and after the WiFi connection is successfully, I need to read other sensor value 50 times and make a connection with

client.connect(server, 80)

and send sensor data with GET method to upload every sensor value to server. In this way I am able to send the first read, but for further sensor reads the connection to server gives error

My code resumed:

void setup(){
  // Setup sensors...
}

void initWifi(){
  // Open ESP8266 connection to WiFi network
}

boolean tempetarure_read(){
  // read continuously the temp sensor...
}

void loop(){
  if(tempetarure_read()){
    digitalWrite(relayPin, HIGH); //Open relay to give energy to ESP8266 module
    read_other_sensor_and_send_to_server();
    digitalWrite(relayPin, LOW); // Switch off the relay
  }
}

void read_other_sensor_and_send_to_server(){
  for (int x = 0; x < 50; x++) {
    string sensor_data = get_sensor_data();

    Serial.println();
    Serial.println("Starting connection to server...");
    // if you get a connection, report back via serial
    if (client.connect(server, 80)) {
        Serial.println("Connected to server");
        // Make a HTTP request
        //client.println("GET /asciilogo.txt HTTP/1.1");
        client.print("GET /sensor/store?");
        client.print(sensor_data);
        client.println(" HTTP/1.1");
        client.println("Host: my.server.com");
        client.println("Connection: close");
        client.println();
    }

    String res="";
    while (client.available()) {
        char c = client.read();
        res += c;
    }

    client.flush();
    client.stop();
    delay(1000);

  } // end for
}

I get

[WiFiEsp] Connecting to my.server.com
[WiFiEsp] >>> TIMEOUT >>>
[WiFiEsp] Disconnecting  3

but the module is connected to wifi and my.serever.com is online. Can anybody help me to solve this issue?

Thanks in advance

3
  • There is a better approach than using a relay, you may want to read my blog on how I use an ESP-01 as a wifi shield and how an Arduino as a sensor node can "wake" it up when it is time to send data. – hcheung Jul 30 '20 at 6:48
  • BTW, the problem of your code is that when you switch on the ESP-01, ESP-01 by default sent out some boot msg at baud rate of 74880, so what you received on Serial is not what you want. Secondly, it take several seconds for ESP-01 to established a wifi connection. – hcheung Jul 30 '20 at 7:03
  • this is a case for troubleshooting not for an answer. turn on more logging. if you have an USB-to-TTL adapter, you can use it to sniff the communication between Arduino and esp8266, to see the exchanged AT commands and responses. do you use SoftwareSerial? is it at 9600 baud? if you have AT firmware 1.7+ (SDK3), try my WiFiEspAT library. – Juraj Jul 30 '20 at 9:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.