AT commands and downloaded sketch ?
Posted: Mon May 01, 2017 6:15 pm
Hi to all !
i'de like to know if it's possible to have an ESP8266-01 module with AT commands answer possibility with a sketch uploaded, like autoconnect...
i want to be able to use this sketch for wifi connection simplicity and able to receive AT commands from an arduino plugged on 2, 3.
here is the autoconnect software:
#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
// ce script permet a l'ESP8266 de se connecter a un wifi en spécifiant par interface web que reseau choisir...
//needed for library
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
//WiFiManager
//Local intialization. Once its business is done, there is no need to keep it around
WiFiManager wifiManager;
//reset saved settings
//wifiManager.resetSettings();
//set custom ip for portal
//wifiManager.setAPStaticIPConfig(IPAddress(10,0,1,1), IPAddress(10,0,1,1), IPAddress(255,255,255,0));
//fetches ssid and pass from eeprom and tries to connect
//if it does not connect it starts an access point with the specified name
//here "AutoConnectAP"
//and goes into a blocking loop awaiting configuration
wifiManager.autoConnect("AutoConnectAP");
//or use this for auto generated name ESP + ChipID
//wifiManager.autoConnect();
//if you get here you have connected to the WiFi
Serial.println("connected...yeey ");
}
void loop() {
// put your main code here, to run repeatedly:
}
and here is the arduino sketch, with AT commands:
#include <Filters.h>
#include <SoftwareSerial.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
#define DEBUG 0
#define IP "184.106.153.149" // IP address of thingspeak.com
#define DHTPIN 5 // Pin which is connected to the DHT sensor.
// Uncomment the type of sensor in use:
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302)
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
DHT_Unified dht(DHTPIN, DHTTYPE);
//uint32_t delayMS;
String network = "xxxxx"; // your access point SSID
String password = "xxxxx"; // your wifi Access Point password
String GET = "GET /update?key=xxxxxxx"; // replace with your channel key
String apiKey = "xxxxxx"; // replace with your channel's thingspeak API key
const int postingInterval = 20 * 1000; // post data every 20 seconds
SoftwareSerial esp8266Module(2, 3); // RX, TX
// this runs once
void setup()
{
Serial.begin(9600); // enable debug serial
esp8266Module.begin(9600);
setupEsp8266();
dht.begin();
sensor_t sensor;
dht.temperature().getSensor(&sensor);
dht.humidity().getSensor(&sensor);
}
// the loop
void loop()
{
// Get temperature event and print its value.
sensors_event_t event;
//sensors_event_h event;
dht.temperature().getEvent(&event);
float temperature = (event.temperature);
dht.humidity().getEvent(&event);
float humidity = (event.relative_humidity);
// setupEsp8266();
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" *C");
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.println("%");
updateInput(String (temperature) , String(humidity));
delay(postingInterval);
}
//-------------------------------------------------------------------
// Following function setup the esp8266, put it in station made and
// connect to wifi access point.
//------------------------------------------------------------------
void setupEsp8266()
{
//Serial.println("initialisation du esp8266");
if(DEBUG){
Serial.println("Reseting esp8266");
}
esp8266Module.flush();
esp8266Module.println(F("AT+RST"));
Serial.write(esp8266Module.read() );
delay(2000);
//delay(7000);
Serial.write(esp8266Module.read() );
if (esp8266Module.find("OK"))
{
if(DEBUG){
Serial.println("Found OK");
Serial.println("Changing espmode");
}
esp8266Module.flush();
changingMode();
delay(2000);
//delay(5000);
esp8266Module.flush();
connectToWiFi();
}
else
{
if(DEBUG){
Serial.println("OK not found");
}
}
}
//-------------------------------------------------------------------
// Following function sets esp8266 to station mode
//-------------------------------------------------------------------
bool changingMode()
{
esp8266Module.println(F("AT+CWMODE=1"));
Serial.write(esp8266Module.read() );
if (esp8266Module.find("OK"))
{
if(DEBUG){
Serial.println("Mode changed");
}
return true;
}
else if(esp8266Module.find("NO CHANGE")){
if(DEBUG){
Serial.println("Already in mode 1");
}
return true;
}
else
{
if(DEBUG){
Serial.println("Error while changing mode");
}
return false;
}
}
//-------------------------------------------------------------------
// Following function connects esp8266 to wifi access point
//-------------------------------------------------------------------
bool connectToWiFi()
{
if(DEBUG){
Serial.println("inside connectToWiFi");
}
String cmd = F("AT+CWJAP=\"");
cmd += network;
cmd += F("\",\"");
cmd += password;
cmd += F("\"");
esp8266Module.println(cmd);
//Serial.println(cmd);
delay(5000);
//delay(5000);
if (esp8266Module.find("OK"))
{
Serial.println("Connected to Access Point");
if(DEBUG){
Serial.println("Connected to Access Point");
}
return true;
}
else
{
Serial.println("Could not connect to Access Point");
if(DEBUG){
Serial.println("Could not connect to Access Point");
}
return false;
}
}
//-------------------------------------------------------------------
// Following function sends sensor data to thingspeak.com
//-------------------------------------------------------------------
void updateInput(String voltage1,String voltage2)
{
String cmd = "AT+CIPSTART=\"TCP\",\"";
cmd += IP;
cmd += "\",80";
esp8266Module.println(cmd);
//Serial.println(cmd);
delay(5000);
//delay(5000);
if(esp8266Module.find("Error")){
if(DEBUG){
Serial.println("ERROR while SENDING");
}
return;
}
cmd = GET + "&field1=" + voltage1 + "&field2=" + voltage2 + "\r\n";
esp8266Module.print("AT+CIPSEND=");
esp8266Module.println(cmd.length());
//Serial.println(cmd);
delay(5000);
//delay(15000);
if(esp8266Module.find(">"))
{
esp8266Module.print(cmd);
if(DEBUG){
Serial.println("Data sent");
}
}else
{
esp8266Module.println("AT+CIPCLOSE");
if(DEBUG){
Serial.println("Connection closed");
}
}
}
i'de like to know if it's possible to have an ESP8266-01 module with AT commands answer possibility with a sketch uploaded, like autoconnect...
i want to be able to use this sketch for wifi connection simplicity and able to receive AT commands from an arduino plugged on 2, 3.
here is the autoconnect software:
#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
// ce script permet a l'ESP8266 de se connecter a un wifi en spécifiant par interface web que reseau choisir...
//needed for library
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
//WiFiManager
//Local intialization. Once its business is done, there is no need to keep it around
WiFiManager wifiManager;
//reset saved settings
//wifiManager.resetSettings();
//set custom ip for portal
//wifiManager.setAPStaticIPConfig(IPAddress(10,0,1,1), IPAddress(10,0,1,1), IPAddress(255,255,255,0));
//fetches ssid and pass from eeprom and tries to connect
//if it does not connect it starts an access point with the specified name
//here "AutoConnectAP"
//and goes into a blocking loop awaiting configuration
wifiManager.autoConnect("AutoConnectAP");
//or use this for auto generated name ESP + ChipID
//wifiManager.autoConnect();
//if you get here you have connected to the WiFi
Serial.println("connected...yeey ");
}
void loop() {
// put your main code here, to run repeatedly:
}
and here is the arduino sketch, with AT commands:
#include <Filters.h>
#include <SoftwareSerial.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
#define DEBUG 0
#define IP "184.106.153.149" // IP address of thingspeak.com
#define DHTPIN 5 // Pin which is connected to the DHT sensor.
// Uncomment the type of sensor in use:
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302)
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
DHT_Unified dht(DHTPIN, DHTTYPE);
//uint32_t delayMS;
String network = "xxxxx"; // your access point SSID
String password = "xxxxx"; // your wifi Access Point password
String GET = "GET /update?key=xxxxxxx"; // replace with your channel key
String apiKey = "xxxxxx"; // replace with your channel's thingspeak API key
const int postingInterval = 20 * 1000; // post data every 20 seconds
SoftwareSerial esp8266Module(2, 3); // RX, TX
// this runs once
void setup()
{
Serial.begin(9600); // enable debug serial
esp8266Module.begin(9600);
setupEsp8266();
dht.begin();
sensor_t sensor;
dht.temperature().getSensor(&sensor);
dht.humidity().getSensor(&sensor);
}
// the loop
void loop()
{
// Get temperature event and print its value.
sensors_event_t event;
//sensors_event_h event;
dht.temperature().getEvent(&event);
float temperature = (event.temperature);
dht.humidity().getEvent(&event);
float humidity = (event.relative_humidity);
// setupEsp8266();
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" *C");
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.println("%");
updateInput(String (temperature) , String(humidity));
delay(postingInterval);
}
//-------------------------------------------------------------------
// Following function setup the esp8266, put it in station made and
// connect to wifi access point.
//------------------------------------------------------------------
void setupEsp8266()
{
//Serial.println("initialisation du esp8266");
if(DEBUG){
Serial.println("Reseting esp8266");
}
esp8266Module.flush();
esp8266Module.println(F("AT+RST"));
Serial.write(esp8266Module.read() );
delay(2000);
//delay(7000);
Serial.write(esp8266Module.read() );
if (esp8266Module.find("OK"))
{
if(DEBUG){
Serial.println("Found OK");
Serial.println("Changing espmode");
}
esp8266Module.flush();
changingMode();
delay(2000);
//delay(5000);
esp8266Module.flush();
connectToWiFi();
}
else
{
if(DEBUG){
Serial.println("OK not found");
}
}
}
//-------------------------------------------------------------------
// Following function sets esp8266 to station mode
//-------------------------------------------------------------------
bool changingMode()
{
esp8266Module.println(F("AT+CWMODE=1"));
Serial.write(esp8266Module.read() );
if (esp8266Module.find("OK"))
{
if(DEBUG){
Serial.println("Mode changed");
}
return true;
}
else if(esp8266Module.find("NO CHANGE")){
if(DEBUG){
Serial.println("Already in mode 1");
}
return true;
}
else
{
if(DEBUG){
Serial.println("Error while changing mode");
}
return false;
}
}
//-------------------------------------------------------------------
// Following function connects esp8266 to wifi access point
//-------------------------------------------------------------------
bool connectToWiFi()
{
if(DEBUG){
Serial.println("inside connectToWiFi");
}
String cmd = F("AT+CWJAP=\"");
cmd += network;
cmd += F("\",\"");
cmd += password;
cmd += F("\"");
esp8266Module.println(cmd);
//Serial.println(cmd);
delay(5000);
//delay(5000);
if (esp8266Module.find("OK"))
{
Serial.println("Connected to Access Point");
if(DEBUG){
Serial.println("Connected to Access Point");
}
return true;
}
else
{
Serial.println("Could not connect to Access Point");
if(DEBUG){
Serial.println("Could not connect to Access Point");
}
return false;
}
}
//-------------------------------------------------------------------
// Following function sends sensor data to thingspeak.com
//-------------------------------------------------------------------
void updateInput(String voltage1,String voltage2)
{
String cmd = "AT+CIPSTART=\"TCP\",\"";
cmd += IP;
cmd += "\",80";
esp8266Module.println(cmd);
//Serial.println(cmd);
delay(5000);
//delay(5000);
if(esp8266Module.find("Error")){
if(DEBUG){
Serial.println("ERROR while SENDING");
}
return;
}
cmd = GET + "&field1=" + voltage1 + "&field2=" + voltage2 + "\r\n";
esp8266Module.print("AT+CIPSEND=");
esp8266Module.println(cmd.length());
//Serial.println(cmd);
delay(5000);
//delay(15000);
if(esp8266Module.find(">"))
{
esp8266Module.print(cmd);
if(DEBUG){
Serial.println("Data sent");
}
}else
{
esp8266Module.println("AT+CIPCLOSE");
if(DEBUG){
Serial.println("Connection closed");
}
}
}