Hello,
I have been struggling for days on a matter that appears to be harder than i thought it would be.
I'm trying to create a personal air quality sensor manager arduino UNO as well as one or two more sensor and send its values to my domoticz server through an ESP8266-01.
I'm using arduino to get the sensor value. I managed to do the measure part without a single inch of trouble.
To be able to communicate with my domotic server (working on domoticz). I want to send a simple get or post request toward my local API.
To do so, i bought an ESP8266-01. I want to use this only to send the value and not to be programmed itself. (Basically i want to the code to be on my arduino because he has more pins than the esp8266, maybe this is where i'm wrong)
Most website indicate that i should download the esp8266 board and libraries and use the ESP8266WiFi.h library. I already used this library on another project with a standalone esp8266 and a single sensor linked to it without arduino involved, it worked quite nicely.
But as soon as i tried to put the esp8266 as a slave of my arduino, the problems began.
I followed some tutorials to plug the ESP8266
To sum it up, I found two ways where i could interact with my ESP8266
In both cases, I connected alimentation pins and CH_PD as documentations stated and I didn't plug RST pin (since i don't want the code to be on the ESP itself)
- Schema 1
RX and TX on arduino where linked to RX and TX on ESP8266 - Schema 2
Serial 2 and 3 on arduino where linked to RX and TX on ESP8266
With both schemas I couldn't use the ESP8266WiFi.h libraries since it appears to be only compatible with the ESP8266 processor.
Question 1 - First of all am I doing something wrong or out of sense there?
Question 2 - Is it even possible to use an ESP8266 as a slave emitter for Arduino UNO?
Question 3 - Anybody has an idea of how could ESP8266WiFi.h library in this kind of build?
I never managed to run the simple script BasicHttpClient.ino from ESP8266
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
ESP8266WiFiMulti WiFiMulti;
void setup() {
Serial.begin(115200);
// Serial.setDebugOutput(true);
Serial.println();
Serial.println();
Serial.println();
for (uint8_t t = 4; t > 0; t--) {
Serial.printf("[SETUP] WAIT %d...\n", t);
Serial.flush();
delay(1000);
}
WiFi.mode(WIFI_STA);
WiFiMulti.addAP("SSID", "PASSWORD");
}
void loop() {
// wait for WiFi connection
if ((WiFiMulti.run() == WL_CONNECTED)) {
WiFiClient client;
HTTPClient http;
Serial.print("[HTTP] begin...\n");
if (http.begin(client, "http://jigsaw.w3.org/HTTP/connection.html")) { // HTTP
Serial.print("[HTTP] GET...\n");
// start connection and send HTTP header
int httpCode = http.GET();
// httpCode will be negative on error
if (httpCode > 0) {
// HTTP header has been send and Server response header has been handled
Serial.printf("[HTTP] GET... code: %d\n", httpCode);
// file found at server
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
String payload = http.getString();
Serial.println(payload);
}
} else {
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
} else {
Serial.printf("[HTTP} Unable to connect\n");
}
}
delay(10000);
}
I managed to send a request through AT commands to the ESP8266-01 thanks to the following script. Sadly, the request i managed to send with my low ESP8266 AT skills was not recognised as a valid request by my domoticz server since it might lack headers.
It is an adaptation of a common serial code for ESP8266 in serial mode.
#include <SoftwareSerial.h>
#define RX 2
#define TX 3
String AP = "wifiSSID"; // CHANGE ME
String PASS = "password"; // CHANGE ME
String HOST = "hostOfDomoticz";
String PORT = "portOfDomoticz";
int countTrueCommand;
int countTimeCommand;
boolean found = false;
int valSensor = 1;
SoftwareSerial esp8266(RX,TX);
void setup() {
Serial.begin(9600);
esp8266.begin(115200);
sendCommand("AT",5,"OK");
sendCommand("AT+CWMODE=1",5,"OK");
sendCommand("AT+CWJAP=\""+ AP +"\",\""+ PASS +"\"",20,"OK");
}
void loop() {
valSensor = getSensorData();
//This request is a domoticZ request to add a value on a sensor
String getData = "GET /json.htm?type=command¶m=udevice&idx=666&nvalue="+String(valSensor);
sendCommand("AT+CIPMUX=1",5,"OK");
sendCommand("AT+CIPSTART=0,\"TCP\",\""+ HOST +"\","+ PORT,15,"OK");
sendCommand("AT+CIPSEND=0," +String(getData.length()+4),4,">");
esp8266.println(getData);
delay(200);
countTrueCommand++;
sendCommand("AT+CIPCLOSE=0",5,"OK");
}
int getSensorData(){
return random(1000); // Replace with
}
void sendCommand(String command, int maxTime, char readReplay[]) {
Serial.print(countTrueCommand);
Serial.print(". at command => ");
Serial.print(command);
Serial.print(" ");
while(countTimeCommand < (maxTime*1))
{
esp8266.println(command);//at+cipsend
if(esp8266.find(readReplay))//ok
{
found = true;
break;
}
countTimeCommand++;
}
if(found == true)
{
Serial.println("OK");
countTrueCommand++;
countTimeCommand = 0;
}else {
Serial.println("Fail");
countTrueCommand = 0;
countTimeCommand = 0;
}
found = false;
}
This code is working but it seems that the following part
sendCommand("AT+CIPSEND=0," +String(getData.length()+4),4,">");
esp8266.println(getData);
is received by the server but not formed like a GET request that a simple navigator would do.
Which make the whole code not working.
Thank you if anyone as any clue on how i could make it work in any way. And sorry for not giving schemas (i'm a new users and cannot upload file).
Matthieu