So you're a Noob? Post your questions here until you graduate! Don't be shy.

User avatar
By jeffas
#62106 Like I said, you can have only one sketch on the ESP at one time. That's either the AT interpreter that it came with, or a sketch that you have loaded onto it. If it's running your sketch, it is not running the AT interpreter. If you have already loaded a sketch and want to revert to AT, you have to get the AT firmware and load that back onto the ESP.

You're also likely to have a problem trying to drive SoftwareSerial at 115200. I have seen other posts that say that is too fast for SoftwareSerial. I had much the same problem. I had an ESP-05 and changed the speed to 9600. That worked briefly, but now I can't communicate with it at all (viewtopic.php?f=160&t=13082). No-one has replied to that, so I don't know any way to reliably change speed on these.
User avatar
By Marmachine
#62156
jeffas wrote:Like I said, you can have only one sketch on the ESP at one time. That's either the AT interpreter that it came with, or a sketch that you have loaded onto it. If it's running your sketch, it is not running the AT interpreter. If you have already loaded a sketch and want to revert to AT, you have to get the AT firmware and load that back onto the ESP.


Ahh, that will explain why (i have two ESP8266) one isn't responding to AT commands anymore... i (accidently) mixed them up so at one momement couldn't tell which i uploaded and which still is original. :oops: but anyway, what i've shared here is the response of the one that still is responding to AT commands.

I need to find the Ai-Thinker firmware to fix my other ESP module i guess. Thanks!

jeffas wrote:You're also likely to have a problem trying to drive SoftwareSerial at 115200. I have seen other posts that say that is too fast for SoftwareSerial. I had much the same problem. I had an ESP-05 and changed the speed to 9600. That worked briefly, but now I can't communicate with it at all (viewtopic.php?f=160&t=13082). No-one has replied to that, so I don't know any way to reliably change speed on these.


I think i have similar experience, entering AT commands manually in the serial monitor (at 115200) returns good response. If you look at the response i am getting to the AT commands from my Arduino sketch, there is some garbage, and it doesn't even seem to respond to some at 115200. I will re-try 9600 again, but i remember not getting any response either.

One thing i want to try is constructing the json url with a variable and then make the call using SoftwareSerial... assuming that is possible? I don't really care for the response when calling the URL will be successfull ;) so i want to try an see.

So my question is, how would i make that call to http://1.2.3.4:1234?some=var using SoftwareSerial?
User avatar
By jeffas
#62230 As you say in the subject that you got an IP, guess you already know how to use AT commands to connect to WiFi. Then you use AT+CIPSTART to connect to the IP address and port of your server, then construct an HTTP request and send that using AT+CIPSEND
So in your example, connect to 1.2.3.4, port 1234
Then the request to send is "GET /?some=var HTTP/1.0\r\n\r\n"
User avatar
By Marmachine
#62275
jeffas wrote:As you say in the subject that you got an IP, guess you already know how to use AT commands to connect to WiFi. Then you use AT+CIPSTART to connect to the IP address and port of your server, then construct an HTTP request and send that using AT+CIPSEND
So in your example, connect to 1.2.3.4, port 1234
Then the request to send is "GET /?some=var HTTP/1.0\r\n\r\n"


Thank you, that is very helpfull actually, it sends me in the right direction i guess!
I've found this example http://www.esp8266.com/viewtopic.php?f=8&t=1594 and i have tried it, but with no luck.

I guess that the "garbage" returned by ESP8266 as response to any AT command keep stopping this script in every step. Maybe i need to flash new AT software first? Setting bautrate to 9600 only makes things worse by the way.

The code that i have now
Code: Select all// SoftwareSerial library required for AT commands -> Wifi / ESP8266 module
#include <SoftwareSerial.h>

// include library's for sensors
#include <Wire.h>
#include <SPI.h>

#include <SensorTransmitter.h> // required for 433mhz transmitter

#include <BH1750.h> // lightmeter library
#include <dht.h> // dht library

// ************************** //
// Configuration/wiring setup //
// ************************** //

// ====== wifi module ESP8266 ======
const byte  ESP8266_RX_Pin = 2; // Wire this to Tx Pin of ESP8266
const byte  ESP8266_TX_Pin = 3; // Wire this to Rx Pin of ESP8266
SoftwareSerial ESP8266 (ESP8266_RX_Pin, ESP8266_TX_Pin); // create ESP8266 object
/* ESP8266 wifi module on base board ESP-01 connection:
   VCC to 5v+ | GND to GND | RX to ESP8266_RX_Pin | TX to ESP8266_TX_Pin */



// ==========================================================

void setup() {
  delay(5000); // Wait for the module self-initialize

  Serial.begin(115200);
  initialize_wifi();
  status_wifi();

}

void loop() {

  //... here's other stuff going on like measuring temp, humidity and lux... successfully though :-)

  //... now i want to send various data to Domoticz, the following URL is tested successfull in a browser
  //... example URL to set temperature to device with IDX 133
  //... http://192.168.2.100:8080/json.htm?type=command&param=udevice&idx=133&nvalue=0&svalue=20

  initialize_wifi();
  send_get_command();

  Serial.println("==================================================================");
  Serial.println("pause");
  delay(30000); // Wait a while
}

// ==================================================================
// =============== Wifi ESP8266 AT command functions ================
// ==================================================================

void initialize_wifi() {
  ESP8266.begin(115200); // Change this to the baudrate used by ESP8266
}

void status_wifi() {
  Serial.println("Sending 'AT' command [OK]");
  ESP8266.println("AT");
  // display output
  while (ESP8266.available()) {
    String inData = ESP8266.readStringUntil('\n');
    Serial.println("reponse ESP8266 -> " + inData);
  }
  // ===================
  Serial.println("Sending AT+CIFSR=? command [IP and MAC address]");
  ESP8266.println("AT+CIFSR=?");
  // display output
  while (ESP8266.available()) {
    String inData = ESP8266.readStringUntil('\n');
    Serial.println("reponse ESP8266 -> " + inData);
  }
  // ===================
  Serial.println("Sending AT+GMR command [Firmware version]");
  ESP8266.println("AT+GMR");
  // display output
  while (ESP8266.available()) {
    String inData = ESP8266.readStringUntil('\n');
    Serial.println("reponse ESP8266 -> " + inData);
  }
  // ===================
  Serial.println("Sending AT+CIPSTATUS command [connection status]");
  ESP8266.println("AT+CIPSTATUS");
  // display output
  while (ESP8266.available()) {
    String inData = ESP8266.readStringUntil('\n');
    Serial.println("reponse ESP8266 -> " + inData);
  }
}

void send_get_command(){
  // Establish TCP connection
  #define DEST_HOST "192.168.2.100" // set the target address
  #define DEST_PORT "8080"          // set the port
  #define CONTINUE    false
  #define TIMEOUT     10000 // mS
  String cmd = "AT+CIPSTART=\"TCP\",\""; cmd += DEST_HOST; cmd += "\","; cmd += DEST_PORT;
  // if (!echoCommand(cmd, "OK", CONTINUE)) return;
  delay(2000);
 
  // Get connection status
  // if (!echoCommand("AT+CIPSTATUS", "OK", CONTINUE)) return;
  // Build HTTP request.
  // cmd = "GET / HTTP/1.0\r\n\r\n";
  cmd = "GET /json.htm?type=command&param=udevice&idx=133&nvalue=0&svalue=13 HTTP/1.0\r\n\r\n";
  String cipstart = "AT+CIPSEND=";
  cipstart += cmd.length();
  delay(5000);

  if (!echoCommand(cipstart, ">", CONTINUE))
  {
    echoCommand("AT+CIPCLOSE", "", CONTINUE);
    Serial.println("Connection timeout.");
    return;
  }
  delay(500);
 
  // Send the raw HTTP request
  echoCommand(cmd, "OK", CONTINUE);  // GET
 
  // Loop forever echoing data received from destination server.
  while(true)
    while (Serial.available())
      Serial.write(Serial.read());
     
  errorHalt("ONCE ONLY");
}

// Send a command to the module and wait for acknowledgement string
// (or flush module output if no ack specified).
// Echoes all data received to the serial monitor.
boolean echoCommand(String cmd, String ack, boolean halt_on_fail)
{
  Serial.println(cmd);
  #ifdef ECHO_COMMANDS
    Serial.print("--"); Serial.println(cmd);
  #endif
 
  // If no ack response specified, skip all available module output.
  if (ack == "")
    echoSkip();
  else
    // Otherwise wait for ack.
    if (!echoFind(ack))          // timed out waiting for ack string
      if (halt_on_fail)
        errorHalt(cmd+" failed");// Critical failure halt.
      else
        return false;            // Let the caller handle it.
  return true;                   // ack blank or ack found
}

// Print error message and loop stop.
void errorHalt(String msg)
{
  Serial.println(msg);
  Serial.println("HALT");
  while(true){};
}
// Read characters from WiFi module and echo to serial until keyword occurs or timeout.
boolean echoFind(String keyword)
{
  byte current_char   = 0;
  byte keyword_length = keyword.length();
 
  // Fail if the target string has not been sent by deadline.
  long deadline = millis() + TIMEOUT;
  while(millis() < deadline)
  {
    if (Serial.available())
    {
      char ch = Serial.read();
      Serial.write(ch);
      if (ch == keyword[current_char])
        if (++current_char == keyword_length)
        {
          Serial.println();
          return true;
        }
    }
  }
  return false;  // Timed out
}

// Read and echo all available module output.
// (Used when we're indifferent to "OK" vs. "no change" responses or to get around firmware bugs.)
void echoFlush()
  {while(Serial.available()) Serial.write(Serial.read());}
 
// Echo module output until 3 newlines encountered.
// (Used when we're indifferent to "OK" vs. "no change" responses.)
void echoSkip()
{
  echoFind("\n");        // Search for nl at end of command echo
  echoFind("\n");        // Search for 2nd nl at end of response.
  echoFind("\n");        // Search for 3rd nl at end of blank line.
}

void connect_wifi(){
  // connect to the IP address and port of server/website using AT
  // AT+CIPSTART="TCP","192.168.2.100","8080"
  Serial.println("Sending AT+CIPSTART=4,\"TCP\",\"192.168.2.100\",8080");
  ESP8266.println("AT+CIPSTART=\"TCP\",\"192.168.2.100\",8080");
  // test AT+CIPSTART=?
  Serial.println("Sending AT+CIPSTART=? [result]");
  ESP8266.println("AT+CIPSTART=?");
  while (ESP8266.available()) {
    String inData = ESP8266.readStringUntil('\n');
    Serial.println("reponse ESP8266 -> " + inData);
  }
}

void http_get_wifi(){
  // construct an HTTP request and send it
  // AT+CIPSEND="GET /json.htm?type=command&param=udevice&idx=133&nvalue=0&svalue=13 HTTP/1.0\r\n\r\n"
  Serial.println("Sending AT+CIPSEND=\"GET /json.htm?type=command&param=udevice&idx=133&nvalue=0&svalue=13 HTTP/1.0\r\n\r\n\"");
  ESP8266.println("AT+CIPSEND=\"GET /json.htm?type=command&param=udevice&idx=133&nvalue=0&svalue=13 HTTP/1.0\r\n\r\n\"");
  while (ESP8266.available()) {
    String inData = ESP8266.readStringUntil('\n');
    Serial.println("reponse ESP8266 -> " + inData);
  }
}


The above code, results in the following response

Code: Select allSending 'AT' command [OK]
Sending AT+CIFSR=? command [IP and MAC address]
reponse ESP8266 -> AZCH¨Hèj•DüAZ¥¨RÔ*¥új
CèjµDü
Sending AT+GMR command [Firmware version]
Sending AT+CIPSTATUS command [connection status]
reponse ESP8266 -> AZ¥TJ5
AT v±qqiow=0.28'0(Kul  L’‚Š²™�у¢Ò¢ªeu
reponse ESP8266 -> *Ô)²•ÉÍ¥½¹éŠ9µAT+CS
====== lightmeter BH1750
BH1750 Light = 54612 lux
============ DHT11
DHT11 Temperature = 22.00 C
DHT11 Humidity = 40.00 %
============ DHT22
DHT22 Temperature = 19.10 C
DHT22 Humidity = 40.80 %
============ SYN115
Transmitted Temp / Hum values: 191 C 40 %
AT+CIPSEND=80
AT+CIPCLOSE
Connection timeout.
==================================================================
pause



The funny thing is that, when i enter the AT commands manually, they do return a GOOD response at the same bautrate (115200). I really don't have any idea what to do here!?