my project needs 4 analogue inputs (2 x MQ-2 & 2 x MQ-7 gas sensors) but this wont work with my ESP8266-12E board.
so naively i hooked up an ESP8266-12 as a shield for a Nano and got it working (learned some AT commands, yay!), but then i tried to upload my code (that i had originally written for my ESP8266) but now it says the code is too big for the Nano! how rude!!!
so instead? can i run the ESP with the main code and digital sensors connected, and have a Nano hanging off it (like a shield?) with only the code relating to the analogue sensors and sending this to the ESPās Tx/Rx connections?
it is an interesting idea - replace functions arduino and esp8266.
ESP as a wifi connection with the installed BLYNK and arduino as an extension ports for ESP. Both connected by serial hardware.
Very interesting - I have to check it in practice
Thank you for the idea
Iāve got somewhere a small piece of code that allows to read analog inputs or write digital outputs using serial com on demand. This code with @Costas link could work togetherā¦ I will share it with you
#define debug 1 // this is the debugging tag - change to 0 to turn off debugging serial prints
#include <SimpleTimer.h>
#include "Wire.h"
const int fireRoomSmokePin = A1; //SMOKE GAS sensor output pin for upstairs plenum MQ-2 sensor
const int fireRoomCO2Pin = A2; //CO GAS sensor output pin for upstairs plenum MQ-7 sensor
int fireRoomSmoke;
//int fireRoomCO2;
SimpleTimer timer;
#include "SoftwareSerial.h"
SoftwareSerial EspSerial(2, 3); // RX, TX
void runGasSensorTest()
{
#ifdef debug
Serial.println(F("Starting to read gas sensors...."));
#endif
fireRoomSmoke = analogRead(fireRoomSmokePin); //Read Gas value from analog 1
EspSerial.write("<"); // start marker
EspSerial.write(fireRoomSmoke); // variable to send
EspSerial.write(">"); // end marker
#ifdef debug
Serial.println(F("smoke sensor reading:"));
Serial.println(fireRoomSmoke);
#endif
// fireRoomCO2 = analogRead(fireRoomCO2Pin); //Read Gas value from analog 2
// EspSerial.write("<"); // start marker
// EspSerial.write(fireRoomCO2); // variable to send
// EspSerial.write(">"); // end marker
//#ifdef debug
// Serial.println(F("carbon monoxide sensor reading:"));
// Serial.println(fireRoomCO2);
//#endif
}
void setup()
{
//Serial.begin(115200);
Serial.println(F(""));
Serial.println(F("FIRE ROOM ENVIRO MONITOR - Gas sensor sending"));
Serial.print(F("File name: "));
Serial.println(__FILE__);
Serial.begin(9600);
delay(10);
EspSerial.begin(9600);
delay(10);
timer.setInterval(6L * 1000L, runGasSensorTest);
Serial.println(F("SimpleTimer begins runGasSensorTest() at 6 second intervals..."));
}
void loop()
{
timer.run(); // Initiates SimpleTimer
}
and here is my ESP8266 sketch:
#define DEBUG 1 // this is the debugging tag - change to 0 to turn off debugging serial prints
#define BLYNK_PRINT Serial //this is the debugging for Blynk
//#define BLYNK_DEBUG // Optional, this enables 'full' debugging with detailed prints
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include "Wire.h"
#include <TimeLib.h>
//this is the stuff for espserial receive from Nano
const byte numChars = 32;
char fireRoomSmoke[numChars];
char fireRoomCO2[numChars];
boolean newData = false;
int dataNumber = 0;
float fireRoomSmokeC;
//end nano stuff
WiFiClient client;
const char* ssid = "eyeHeartBlynk";
const char* password = "CostasRulz";
char authBlynk[] = "numberletternumbernumberletteretcetc"; //insert token generated by Blynk
SimpleTimer timer;
void sendGasSensorReadings()
{
fireRoomSmokeC = atof(fireRoomSmoke); // array to float conversion
Blynk.virtualWrite(V7, fireRoomSmokeC);
}
void recvWithStartEndMarkersSmoke() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (recvInProgress == true)
{
if (rc != endMarker)
{
fireRoomSmoke[ndx] = rc;
ndx++;
if (ndx >= numChars)
{
ndx = numChars - 1;
}
}
else
{
fireRoomSmoke[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker)
{
recvInProgress = true;
}
}
}
void showNewData() {
if (newData == true) {
Serial.print(F("This just in ... "));
Serial.println(fireRoomSmoke);
newData = false;
}
else {
Serial.println(F("nothing from Nano yet................ ... "));
}
}
void setup()
{
Serial.begin(9600);
Blynk.begin(authBlynk, ssid, password);
while (!Blynk.connect())
{
//Wait until connected
delay(50);
Serial.print(F(". "));
}
Serial.println(F(""));
Serial.println(F("Found some WiFi!"));
long rssi = WiFi.RSSI();
Serial.print(F("WiFi signal strength (RSSI): "));
Serial.print(rssi);
Serial.println(F(" dBm"));
Serial.println("");
Serial.println(F("------------"));
bool isFirstConnect = true; // Keep this flag not to re-sync on every reconnection
BLYNK_CONNECTED(); // This function will run every time Blynk initial connection is established
{
if (isFirstConnect) {
Blynk.syncAll(); // where is the sync.All function supposed to go?
isFirstConnect = false;
}
}
Serial.println(F("Blynk syncAll done! We operate under <SimpleTimer.h> now:"));
timer.setInterval(70L * 1000L, sendGasSensorReadings);
Serial.println(F("SimpleTimer begins sendGasSensorReadings() at 70 second intervals..."));
timer.setInterval(25L * 1000L, recvWithStartEndMarkersSmoke);
Serial.println(F("SimpleTimer begins recvWithStartEndMarkers() at 25 sec intervals..."));
timer.setInterval(26L * 1000L, showNewData);
Serial.println(F("SimpleTimer begins showNewData() at 26 sec intervals..."));
}
void loop()
{
Blynk.run(); // Initiates Blynk
timer.run(); // Initiates SimpleTimer
}
and this is my output:
> [5001] Connecting to blynk-cloud.com:8442
> [5695] Ready (ping: 0ms).
> Found some WiFi!
> WiFi signal strength (RSSI): -40 dBm
> ------------
> Blynk syncAll done! We operate under <SimpleTimer.h> now:
> SimpleTimer begins sendGasSensorReadings() at 70 second intervals...
> SimpleTimer begins recvWithStartEndMarkers() at 25 sec intervals...
> SimpleTimer begins showNewData() at 26 sec intervals...
> This just in ... /
> This just in ... ƶ
> This just in ... e
> This just in ... n
> This just in ... ĀŖ
> This just in ... y
> This just in ... {
> This just in ... Ć·
> This just in ... "
> This just in ... A
> This just in ... Ā½
the sensor is an analogue MQ2 gas module, and puts out 0 to 999 i think,
like this:
Starting to read gas sensors....
smoke sensor reading:
978
Starting to read gas sensors....
smoke sensor reading:
523
Starting to read gas sensors....
smoke sensor reading:
480
Starting to read gas sensors....
smoke sensor reading:
819
Starting to read gas sensors....
smoke sensor reading:
201
Starting to read gas sensors....
smoke sensor reading:
726
Starting to read gas sensors....
smoke sensor reading:
637
Starting to read gas sensors....
smoke sensor reading:
162
Starting to read gas sensors....
smoke sensor reading:
530
Starting to read gas sensors....
smoke sensor reading:
824
Starting to read gas sensors....
smoke sensor reading:
395
Starting to read gas sensors....
smoke sensor reading:
224
Starting to read gas sensors....
smoke sensor reading:
742
Starting to read gas sensors....
smoke sensor reading:
894
Starting to read gas sensors....
smoke sensor reading:
443
Starting to read gas sensors....
smoke sensor reading:
260
Starting to read gas sensors....
smoke sensor reading:
924
Starting to read gas sensors....
smoke sensor reading:
880
so i know i have messed my variable types up somewhere, but not sure whereā¦?
is it where it ESPwrite it in the Nano? or where it process it in the ESP8266?
@Dave1829,
I know that you are pointing at other direction, but, as you said you need 4 analog inputs the ADS1115 came to my mind. Itās a 16-Bit ADC - 4 Channel with Programmable Gain Amplifier. The ADS1115 provides 16-bit precision at 860 samples/second over I2C.
it DOES change the raw numbers from 200 to 800 range to figures between 80 and 130 range - but it is a relative reading, not absolute, so itās fineā¦
What data type are the original figures of 200 to 800 and how many decimal places?
I think you might need to change the 5, 1.
In my actual project I check how many digits there are in the number and also if it is plus or minus (temperature) and alter the 5 ,1 accordingly.
The other way to do all this is with division, modulus and adding ā0ā to each digit which converts them to characters before you send them to the ESP.
I thought it would be easier to open up one of our boxes to look at the pin connections rather than look in the two corresponding sketches.
I was rather shocked when I opened the box that I thought was shown in post 2 of this thread. Rather than a WeMos and a Nano all sitting neatly with barely a wire in sight it was a mass of wires and an ESP07 with a Nano. I think my business partner currently has the nice neat one.
Anyway you can pretty much use any pins you like. You define an rx and tx in the SoftwareSerial of the Arduino. You then need to ensure you put a voltage divider between the Arduino tx pin and the rx of the ESP.
I have just looked at the 2 sketches and for the Nano I have this line:
I can confirm on the box I have opened I only have pin 7 on the Nano connected to the ESP (via a voltage divider). Basically what this is saying is that as I was just sending from Nano to ESP then you only need the TX line from the Arduino connected to the RX line of ESP (via a voltage divider).
A couple of notes in the ESP sketch:
// ENSURE COMMON GROUND FROM NANO TO ESP
#include <SoftwareSerial.h>
// ONLY D5 RX IS NEEDED ON ESP, FROM PIN 7 (TX) OF NANO
// USE NANO GND NEAR RST PIN AND VITAL A VOLTAGE DIVIDER IS USED
SoftwareSerial ESPserial(14, 13); //RX and TX D5 and D7 WeMos, TX not connected
I made my nodemcu work with Uno, success connect to Blynk. I connect directly Unoās TX RX to RX TX of nodeMCU, donāt use voltage divider. After flashed right AT firmware, it worked. You know how to use AT firmware with GPIO pin of esp-12? Searched from Espressif forum, but nothing talk about that
At least we will all know why your system suddenly stops working.
I know the basics of the AT command set as I was using them 30+ years ago but I wouldnāt recommend anyone uses them with an ESP when there are much better ways to do it.
@Dave1829,
I have been playing with the ADS1115 doing a small Shield for the Wemos Mini D1, it works like a charm!
Itās really easy to use with Blynk and you can achieve 4 extra analog inputs.