Skip to main content
Mod Moved Comments To Chat
added 12 characters in body
Source Link

EDIT2 with @Juraj code.

EDIT2 with

EDIT2 with @Juraj code.

added 2868 characters in body
Source Link

EDIT2 with

    #include <SoftwareSerial.h>

SoftwareSerial Serial1(10, 11);
#define Trash "Trash3"
#define DHTTYPE DHT11
#define DHTPIN 2
#define TRIGGER_PIN 4
#define ECHO_PIN 3



float humidity, temp_f;
int distance;
long duration;


String buff(64);
String getStr(128);

void setup() {

Serial.begin(9600);

Serial1.begin(9600);
//Serial1.resetESP();
delay(2000);
Serial1.setTimeout(5000);


if (!connectWiFi()) {
Serial.println("Can not connect to the WiFi.");
while (true)
; // do nothing
}
Serial.println("OK, Connected to WiFi.");

sendCommand("AT+CIPSTA?");
sendCommand("AT+CIPDNS_CUR?");
sendCommand("AT+CIPSTAMAC?");
}

void loop() {

temp_f = 11;


// connect to server
if (sendCommand("AT+CIPSTART=\"TCP\",\"www.skeetty.com\",80")) {
Serial.println("connected to Cloud");

// build HTTP request
getStr = "GET /km/db.php?id=";
getStr += temp_f;
getStr += " HTTP/1.1\r\n";
getStr += "Host: expository-spoke.000webhostapp.com\r\n\r\n";

// open send buffer
buff = "AT+CIPSEND=";
buff += getStr.length();
if (sendCommand(buff.c_str()) && Serial1.find(">")) { // AT firmware is ready to accept data

// send HTTP request
Serial.println(getStr);
Serial1.print(getStr);

// print HTTP response
if (Serial1.find("+IPD,")) { // response received
int l = Serial1.parseInt();
while (l > 0) {
if (Serial1.available()) {
Serial.write(Serial1.read());
l--;
}
}
Serial.println("--------------");
} else {
Serial.println("no response");
}
} else {
Serial.println("send error");
}
sendCommand("AT+CIPCLOSE");
} else {
Serial.println("Error connecting");
}
}

bool connectWiFi() {

if (!sendCommand("ATE0")) // echo off
return false;
if (!sendCommand("AT+CIPMUX=0")) // set single connection mode
return false;
if (!sendCommand("AT+CWMODE=1")) // set STA mode
return false;
return sendCommand("AT+CWJAP=\"NETGEAR80\",\"calmgrasshopper675\"");
}

bool sendCommand(const char* cmd) {
Serial.println(cmd);
Serial1.println(cmd);
while (true) {
buff = Serial1.readStringUntil('\n');
buff.trim();
if (buff.length() > 0) {
Serial.println(buff);
if (buff == "OK" || buff == "SEND OK" || buff == "ALREADY CONNECTED")
return true;
if (buff == "ERROR" || buff == "FAIL" || buff == "SEND FAIL")
return false;
}
}
}

it never connects to the Wi-Fi network because sendCommand() always return false.


EDIT2 with

    #include <SoftwareSerial.h>

SoftwareSerial Serial1(10, 11);
#define Trash "Trash3"
#define DHTTYPE DHT11
#define DHTPIN 2
#define TRIGGER_PIN 4
#define ECHO_PIN 3



float humidity, temp_f;
int distance;
long duration;


String buff(64);
String getStr(128);

void setup() {

Serial.begin(9600);

Serial1.begin(9600);
//Serial1.resetESP();
delay(2000);
Serial1.setTimeout(5000);


if (!connectWiFi()) {
Serial.println("Can not connect to the WiFi.");
while (true)
; // do nothing
}
Serial.println("OK, Connected to WiFi.");

sendCommand("AT+CIPSTA?");
sendCommand("AT+CIPDNS_CUR?");
sendCommand("AT+CIPSTAMAC?");
}

void loop() {

temp_f = 11;


// connect to server
if (sendCommand("AT+CIPSTART=\"TCP\",\"www.skeetty.com\",80")) {
Serial.println("connected to Cloud");

// build HTTP request
getStr = "GET /km/db.php?id=";
getStr += temp_f;
getStr += " HTTP/1.1\r\n";
getStr += "Host: expository-spoke.000webhostapp.com\r\n\r\n";

// open send buffer
buff = "AT+CIPSEND=";
buff += getStr.length();
if (sendCommand(buff.c_str()) && Serial1.find(">")) { // AT firmware is ready to accept data

// send HTTP request
Serial.println(getStr);
Serial1.print(getStr);

// print HTTP response
if (Serial1.find("+IPD,")) { // response received
int l = Serial1.parseInt();
while (l > 0) {
if (Serial1.available()) {
Serial.write(Serial1.read());
l--;
}
}
Serial.println("--------------");
} else {
Serial.println("no response");
}
} else {
Serial.println("send error");
}
sendCommand("AT+CIPCLOSE");
} else {
Serial.println("Error connecting");
}
}

bool connectWiFi() {

if (!sendCommand("ATE0")) // echo off
return false;
if (!sendCommand("AT+CIPMUX=0")) // set single connection mode
return false;
if (!sendCommand("AT+CWMODE=1")) // set STA mode
return false;
return sendCommand("AT+CWJAP=\"NETGEAR80\",\"calmgrasshopper675\"");
}

bool sendCommand(const char* cmd) {
Serial.println(cmd);
Serial1.println(cmd);
while (true) {
buff = Serial1.readStringUntil('\n');
buff.trim();
if (buff.length() > 0) {
Serial.println(buff);
if (buff == "OK" || buff == "SEND OK" || buff == "ALREADY CONNECTED")
return true;
if (buff == "ERROR" || buff == "FAIL" || buff == "SEND FAIL")
return false;
}
}
}

it never connects to the Wi-Fi network because sendCommand() always return false.

deleted 87 characters in body
Source Link
Juraj
  • 18.3k
  • 4
  • 32
  • 50
AT+CWJAP="NETGEAR80","my_password"

WIFI CONNECTED
WIFI GOT IP

OK
AT+CIPMUX=1


OK
AT+CIPSTART=1,”TCP","www.server.com",80

Link type ERROR

ERROR
AT+CIPSTART=1,"TCP","www.server.com",80

1,CONNECT

OK
AT+CWJAP="NETGEAR80","my_password"

WIFI CONNECTED
WIFI GOT IP

OK
AT+CIPMUX=1


OK
AT+CIPSTART=1,”TCP","www.server.com",80

Link type ERROR

ERROR
AT+CIPSTART=1,"TCP","www.server.com",80

1,CONNECT

OK
AT+CWJAP="NETGEAR80","my_password"

WIFI CONNECTED
WIFI GOT IP

OK
AT+CIPMUX=1


OK

AT+CIPSTART=1,"TCP","www.server.com",80

1,CONNECT

OK
added the serial monitor output
Source Link
Loading
added the output on serial terminal
Source Link
Loading
edited body
Source Link
Loading
Source Link
Loading