So I am stuck with my project. I am creating gas sensor with arduino nano, led, MQ-2 gas sensor and piezo buzzer. All data will be transfered to thingspeak, but I have a problem. My ESP - 01 can’t connect to Wifi. So I need some help. Putting code below.
#include <SoftwareSerial.h>
SoftwareSerial espSerial = SoftwareSerial(8,9); // arduino RX pin=2 arduino TX pin=3 connect the arduino RX pin to esp8266 module TX pin - connect the arduino TX pin to esp8266 module RX pin
float sensor=A0;
float gas_value;
String apiKey = "IUW8M8C5PQDFX7LA"; // replace with your channel's thingspeak WRITE API key
String ssid="calculator"; // Wifi network SSID
String password ="calculator"; // Wifi network password
boolean DEBUG=true;
#define VOLTAGE_MAX 5.0
#define VOLTAGE_MAXCOUNTS 1023.0
//======================================================================== showResponse
void showResponse(int waitTime){
long t=millis();
char c;
while (t+waitTime>millis()){
if (espSerial.available()){
c=espSerial.read();
if (DEBUG) Serial.print(c);
}
}
}
//=======================connection to thinkspeak.com=================================================
boolean thingSpeakWrite(float value1){
String cmd = "AT+CIPSTART=\"TCP\",\""; // TCP connection
cmd += "184.106.153.149"; // api.thingspeak.com
cmd += "\",80";
espSerial.println(cmd);
if (DEBUG) Serial.println(cmd);
if(espSerial.find("Error")){
if (DEBUG) Serial.println("AT+CIPSTART error");
return false;
}
String getStr = "GET /update?api_key="; // prepare GET string
getStr += apiKey;
getStr +="&field1=";
getStr += String(value1);
//getStr +="&field2=";
//getStr += String(value2);
// ...
getStr += "\r\n";
// send data length
cmd = "AT+CIPSEND=";
cmd += String(getStr.length());
espSerial.println(cmd);
if (DEBUG) Serial.println(cmd);
delay(100);
if(espSerial.find(">")){
espSerial.print(getStr);
if (DEBUG) Serial.print(getStr);
}
else{
espSerial.println("AT+CIPCLOSE");
// alert user
if (DEBUG) Serial.println("AT+CIPCLOSE");
return false;
}
return true;
}
//================================================================================ setup
void setup() {
DEBUG=true; // enable debug serial
//----------my sensor code-----------
pinMode(LED_BUILTIN, OUTPUT);
pinMode(sensor,INPUT);
Serial.begin(9600);
espSerial.begin(38000); // enable software serial
// Your esp8266 module's speed is probably at 115200.
// For this reason the first time set the speed to 115200 or to your esp8266 configured speed
// and upload. Then change to 9600 and upload again
//espSerial.println("AT+RST"); // Enable this line to reset the module;
//showResponse(1000);
//espSerial.println("AT+UART_CUR=9600,8,1,0,0"); // Enable this line to set esp8266 serial speed to 9600 bps
//showResponse(1000);
espSerial.println("AT+CWMODE=1"); // set esp8266 as client
showResponse(1000);
espSerial.println("AT+CWJAP=\""+ssid+"\",\""+password+"\""); // set your home router SSID and password
showResponse(5000);
if (DEBUG) Serial.println("Setup completed");
}
// ======================================================================= loop
void loop() {
//---------my gas sensor value reading-------------------
gas_value=analogRead(sensor);
Serial.println(gas_value);
delay(1); //delay in between reads for stability
if(gas_value>150){
// initialize digital pin LED_BUILTIN as an output.
digitalWrite(LED_BUILTIN, HIGH);
//delay(250);
// digitalWrite(LED_BUILTIN, LOW);
//delay(250);
float t = gas_value*(VOLTAGE_MAX / VOLTAGE_MAXCOUNTS); //convert gas value to voltages
if (isnan(t)) {
if (DEBUG) Serial.println("Failed to read from MQ5");
}
else{
if (DEBUG) Serial.println("Voltage="+String(t));
thingSpeakWrite(t); // Write values to thingspeak
}
// thingspeak needs 15 sec delay between updates, */
delay(20000);
}
else{
digitalWrite(LED_BUILTIN, LOW);
}
}
So I am stuck with my project. I am creating gas sensos that transfers data to thingspeak. I am usig ESP - 01, ARDUINO NANO, PIEZO BUZZER, MQ2 GAS SENSOR, LED. So my only problems is that my esp-01 can’t connect to WIFI, and I need help to solve this problem. Posting code below
#include <SoftwareSerial.h>
SoftwareSerial espSerial = SoftwareSerial(2,3); // arduino RX pin=2 arduino TX pin=3 connect the arduino RX pin to esp8266 module TX pin - connect the arduino TX pin to esp8266 module RX pin
float sensor=A0;
float gas_value;
String apiKey = "IUW8M8C5PQDFX7LA"; // replace with your channel's thingspeak WRITE API key
String ssid="calculator"; // Wifi network SSID
String password ="calculator"; // Wifi network password
boolean DEBUG=true;
#define VOLTAGE_MAX 5.0
#define VOLTAGE_MAXCOUNTS 1023.0
//======================================================================== showResponse
void showResponse(int waitTime){
long t=millis();
char c;
while (t+waitTime>millis()){
if (espSerial.available()){
c=espSerial.read();
if (DEBUG) Serial.print(c);
}
}
}
//=======================connection to thinkspeak.com=================================================
boolean thingSpeakWrite(float value1){
String cmd = "AT+CIPSTART=\"TCP\",\""; // TCP connection
cmd += "184.106.153.149"; // api.thingspeak.com
cmd += "\",80";
espSerial.println(cmd);
if (DEBUG) Serial.println(cmd);
if(espSerial.find("Error")){
if (DEBUG) Serial.println("AT+CIPSTART error");
return false;
}
String getStr = "GET /update?api_key="; // prepare GET string
getStr += apiKey;
getStr +="&field1=";
getStr += String(value1);
//getStr +="&field2=";
//getStr += String(value2);
// ...
getStr += "\r\n";
// send data length
cmd = "AT+CIPSEND=";
cmd += String(getStr.length());
espSerial.println(cmd);
if (DEBUG) Serial.println(cmd);
delay(100);
if(espSerial.find(">")){
espSerial.print(getStr);
if (DEBUG) Serial.print(getStr);
}
else{
espSerial.println("AT+CIPCLOSE");
// alert user
if (DEBUG) Serial.println("AT+CIPCLOSE");
return false;
}
return true;
}
//================================================================================ setup
void setup() {
DEBUG=true; // enable debug serial
//----------my sensor code-----------
pinMode(LED_BUILTIN, OUTPUT);
pinMode(sensor,INPUT);
Serial.begin(9600);
espSerial.begin(115200); // enable software serial
// Your esp8266 module's speed is probably at 115200.
// For this reason the first time set the speed to 115200 or to your esp8266 configured speed
// and upload. Then change to 9600 and upload again
//espSerial.println("AT+RST"); // Enable this line to reset the module;
//showResponse(1000);
//espSerial.println("AT+UART_CUR=9600,8,1,0,0"); // Enable this line to set esp8266 serial speed to 9600 bps
//showResponse(1000);
espSerial.println("AT+CWMODE=1"); // set esp8266 as client
showResponse(1000);
espSerial.println("AT+CWJAP=\""+ssid+"\",\""+password+"\""); // set your home router SSID and password
showResponse(5000);
if (DEBUG) Serial.println("Setup completed");
}
// ======================================================================= loop
void loop() {
//---------my gas sensor value reading-------------------
gas_value=analogRead(sensor);
Serial.println(gas_value);
delay(1); //delay in between reads for stability
if(gas_value>250){
// initialize digital pin LED_BUILTIN as an output.
digitalWrite(LED_BUILTIN, HIGH);
//delay(250);
// digitalWrite(LED_BUILTIN, LOW);
//delay(250);
float t = gas_value*(VOLTAGE_MAX / VOLTAGE_MAXCOUNTS); //convert gas value to voltages
if (isnan(t)) {
if (DEBUG) Serial.println("Failed to read from MQ2");
}
else{
if (DEBUG) Serial.println("Voltage="+String(t));
thingSpeakWrite(t); // Write values to thingspeak
}
// thingspeak needs 15 sec delay between updates, */
delay(20000);
}
else{
digitalWrite(LED_BUILTIN, LOW);
}
}
The ESP-01 is power hungry.
You need it to be connected to its own 3.3V power supply; not the Arduino's.
Otherwise, it won't work reliably.
Once you have that.
You need to manually use Putty or some sort of terminal program to communicated with the ESP-01 to make sure you have the AT commands right and you are able to get onto your router.
Once that it done, put those AT commands into your Arduino sketch.
If you hadn't started the project, I would have suggested that you buy a ESP8266 module such as the Wemo D1 mini pro or something like that, and save yourself a lot of headache.
U cant speak to esp at 38000 baud rate. Depending on version of firmware its either 9600 or 115200 or 78800(im not sure exactly but around 7xK).
Open softwareserial example change your pins change speeds to the ones i wrote then send at command if no response send at+rst wait for ready. Then send command at+uart_def=The baud rate you want. Then go from there