String ssid="xxxx"; // key element: Wifi network SSID
String password ="zzz"; // key element: Wifi network password
String str;
boolean DEBUG=true;
void showResponse(int waitTime){
long t=millis();
char c;
while (t+waitTime>millis()){
if (Serial3.available()){
c=Serial3.read();
if (DEBUG) Serial.print(c);
}
}
}
void setup() {
DEBUG=true; // enable debug serial
Serial.begin(9600); // key element: enable serial to see program action in serial monitor
Serial3.begin(115200); // key element: enable serial3; printing to Serial3 sends information directly to ESP8266
//Serial3.println("AT+RST"); // if needed, Enable this AT command line to reset the module;
//showResponse(1000);
//Serial3.println("AT+UART_CUR=9600,8,1,0,0"); // if needed, Enable this AT command line to set esp8266 serial speed to 9600 bps
//showResponse(1000);
Serial3.println("AT+CWMODE=1");
showResponse(5000);
if (DEBUG) Serial.println("AT+CWMODE=1");
Serial3.println("AT+CWJAP=\""+ssid+"\",\""+password+"\""); // key element: use this AT command to join network using SSID and password
showResponse(1000);
if (DEBUG) Serial.println("AT+CWJAP=\""+ssid+"\",\""+password+"\"");
if (DEBUG) Serial.println("Setup completed");
}
void loop() {
int sensorValue = analogRead(A0);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage = sensorValue * (5.0 / 1023.0);
// print out the value you read:
// start TCP connection
String cmd = "AT+CIPSTART=\"TCP\",\""; //key element: set up this AT command to start connection
cmd += "172.18.20.27"; // api.thingspeak.com
cmd += "\",80"; // port number
Serial3.println(cmd); //Key element: print AT command to ESP8266, which is on Serial3
if (DEBUG) Serial.println(cmd);
if(Serial3.find("Error")){
if (DEBUG) Serial.println("AT+CIPSTART error");
return false;
}
String getStr = "GET /--/--/--/----.php";
getStr +="?c=";
getStr += String(voltage);
getStr +="&h=";
getStr += String(sensorValue);
getStr +=" HTTP/1.1\r\n";
getStr +="Host: 172.18.20.27\r\n";
getStr +="Connection: close\r\n\r\n";
cmd = "AT+CIPSEND="; //key element: setup for sending string length
cmd += String(getStr.length());
Serial3.println(cmd); //key element: send AT string length command to ESP8266 on Serial3
if (DEBUG) Serial.println(cmd);
delay(100);
// Write sensor values to thingspeak
// if(Serial3.find(">")){ //this if statement didn't work for me; manually sending precceding AT commands in serial monitor yielded the ">", but this if statement didn't read it
Serial3.print(getStr); //key element: send GET command (which includes data) to ESP8266 on Serial3
delay(1000);
if (DEBUG) Serial.print(getStr);
// thingspeak needs 15+ sec delay between updates,
delay(1000);
}
with the above code im not able to connect to my local server. Im attaching serial monitor output, Please let me know where im going wrong
I downloaded "ESP8266 NonOS AT Bin V1.7.4"version firmware and uploaded using FDT as per the settings suggested. But still Serial monitor response is not proper. Im attaching the screenshot Pls have a look
reva23:
I downloaded "ESP8266 NonOS AT Bin V1.7.4"version firmware and uploaded using FDT as per the settings suggested. But still Serial monitor response is not proper. Im attaching the screenshot Pls have a look
this is the correct output. the garbage is bootlog at different baud rate. ignore it.
"ready" is from AT firmware.
now set the newline characters in the bottom right corner of Serial Monitor to "Both CR&LF" and send AT
String ssid="xxxx"; // key element: Wifi network SSID
String password ="xxxx"; // key element: Wifi network password
boolean DEBUG=true;
//======================================================================== showResponse
// solarsev doesn't know if this is a key element in this form (vs delay(1000))...
void showResponse(int waitTime){
long t=millis();
char c;
while (t+waitTime>millis()){
if (Serial3.available()){
c=Serial3.read();
if (DEBUG) Serial.print(c);
}
}
}
//================================================================================ setup
void setup() {
DEBUG=true; // enable debug serial
Serial.begin(9600); // key element: enable serial to see program action in serial monitor
Serial3.begin(115200); // key element: enable serial3; printing to Serial3 sends information directly to ESP8266
// showResponse(100);
Serial.println("Next");
Serial3.println("AT");
Serial3.println("AT+RST"); // if needed, Enable this AT command line to reset the module;
showResponse(1000);
// set esp8266 as client
Serial3.println("AT+CWMODE=1");
showResponse(5000);
//
// // key element: use this AT command to put ESP8266 into client mode
// showResponse(1000);
if (DEBUG) Serial.println("AT+CWMODE=1");
//
//
//// connect to wifi
Serial3.println("AT+CWJAP=\""+ssid+"\",\""+password+"\""); // key element: use this AT command to join network using SSID and password
if (DEBUG) Serial.println("AT+CWJAP=\""+ssid+"\",\""+password+"\"");
if (DEBUG) Serial.println("Setup completed");
}
//void loop(){}
// ====================================================================== loop
void loop() {
int sensorValue = analogRead(A0);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage = sensorValue * (5.0 / 1023.0);
// print out the value you read:
//str = String (voltage) + String(",")+ String (sensorValue)+ String(",")+ String ("1");
// start TCP connection
String cmd = "AT+CIPSTART\"TCP\",\""; //key element: set up this AT command to start connection
// String cmd = "30";
cmd += "172.18.20.27"; // api.thingspeak.com
cmd += "\",80"; // port number
Serial3.println(cmd); //Key element: print AT command to ESP8266, which is on Serial3
showResponse(1000);
if (DEBUG) Serial.println(cmd);
if(Serial3.find("Error")){
if (DEBUG) Serial.println("AT+CIPSTART error");
return false;
}
//
// prepare GET string for data upload to Thingspeak //key element: string must match Thingspeak specified format
String getStr = "GET /xxx.php";
//getStr += apiKey;
getStr +="?c=";
getStr += String(voltage);
getStr +="&h=";
getStr += String(sensorValue);
getStr +=" HTTP/1.1\r\n";
getStr +="Host: 172.18.20.27\r\n";
getStr +="Connection: close\r\n\r\n";
// send data length to Thingspeak
cmd = "AT+CIPSEND="; //key element: setup for sending string length
cmd += String(getStr.length());
Serial3.println(cmd); //key element: send AT string length command to ESP8266 on Serial3
if (DEBUG) Serial.println(cmd);
delay(100);
// Write sensor values to thingspeak
// if(Serial3.find(">")){ //this if statement didn't work for me; manually sending precceding AT commands in serial monitor yielded the ">", but this if statement didn't read it
Serial3.print(getStr); //key element: send GET command (which includes data) to ESP8266 on Serial3
delay(1000);
if (DEBUG) Serial.print(getStr);
// thingspeak needs 15+ sec delay between updates,
delay(1000);
}
I loaded the above code in special mode i.e 1=2=3=4=ON and remaining in OFF condition and set RXD/TXD switch to 3 and selected board as Ärduino Mega.
Serial output shows the esp8266 is connected wifi but it is not sending data to local server.
I tried Your "check firmware.ino" sketch but the serial monitor prints "Communication with WiFi module failed!"
DIP switch status are 1=2=3=4=ON and rest are OFF and in arduino IDE, I selected Arduino Mega.
Im attaching modified code:
/*
This sketch checks the version of the AT firmware in attached esp8266.
created in Jul 2019 for WiFiEspAT library
by Juraj Andrassy https://github.com/jandrassy
*/
#include <WiFiEspAT.h>
// Emulate Serial1 on pins 6/7 if not present
#if defined(ARDUINO_ARCH_AVR) && !defined(HAVE_HWSERIAL3)
//#include <SoftwareSerial.h>
//SoftwareSerial Serial1(6, 7); // RX, TX
#define AT_BAUD_RATE 115200
#else
#define AT_BAUD_RATE 115200
#endif
void setup() {
Serial.begin(115200);
while (!Serial);
Serial3.begin(AT_BAUD_RATE);
WiFi.init(Serial3);
if (WiFi.status() == WL_NO_MODULE) {
Serial.println("Communication with WiFi module failed!");
// don't continue
while (true);
}
char ver[10];
int major = 0;
int minor = 0;
if (WiFi.firmwareVersion(ver)) {
Serial.print("AT firmware version ");
Serial.println(ver);
char* tok = strtok(ver, ".");
major = atoi(tok);
tok = strtok(NULL, ".");
minor = atoi(tok);
if (major == 2 && minor == 0) {
Serial.println("AT firmware version 2.0 doesn't support passive receive mode and can't be used with the WiFiEspAt library");
} else if (major < 1 || (major == 1 && minor < 7)) {
Serial.println("WiWiEspAT library requires at least version 1.7.0 of AT firmware (but not 2.0)");
} else {
Serial.println("AT firmware is OK for the WiFiEspAT library.");
#ifdef WIFIESPAT1
if (major > 1) {
Serial.println("For AT firmware version 2 comment out #define WIFIESPAT1 in EspAtDrvTypes.h");
}
#else
if (major == 1) {
Serial.println("For AT firmware version 1 add #define WIFIESPAT1 in EspAtDrvTypes.h");
}
#endif
}
} else {
Serial.println("Error getting AT firmware version");
}
}
void loop() {
}