Hello,
This is my first post in here.
I've been struggling for a couple of days now with the module ESP8266.
I have tested it already with another arduino and I could send some test commands and receive the corresponding response. However, I am currently facing a issue where the ESP8266 receives the data but it seems that I not reading out properly the incoming data (Rx).
The code consists of 3 files:
water-monitoring.ino // master
wifi.cpp
wifi.h
(I am creating some customized wifi files to be reused in another project)
here is the code (still being developed):
water-monitoring.ino
#include<SoftwareSerial.h>
#include "wifi.h"
#define WIFI_RX PD2
#define WIFI_TX PD3
const String url = "jsonplaceholder.typicode.com";
const String endpoint = "/posts/42";
const int yellowLed = PD6;
const int redLed = PD7;
const int motorPWM = PB1;
const int lWaterLevel = PC0;
const int sWaterLevel = PC1;
unsigned long timerOne = 0;
Wifi wifi(WIFI_RX, WIFI_TX);
void setup() {
//Setting up pin inputs and outputs
DDRD=0x00;
DDRB=0x00;
DDRC=0x00;
DDRD|=(1 << yellowLed) | (1 << redLed);
DDRB|=(1 << motorPWM);
DDRC&=~((1 << lWaterLevel) | (1 << sWaterLevel));
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.println("Serial communication started...");
// set the data rate for the SoftwareSerial port
wifi.init();
wifi.getRequest(url, endpoint);
}
void loop() {
// put your main code here, to run repeatedly:
PORTD^=(1 << yellowLed);
PORTD^=(1 << redLed);
}
wifi.cpp
#include <Arduino.h>
#include<SoftwareSerial.h>
#include "wifi.h"
void Wifi::init() {
//ESP8266.begin(BAUD_RATE);
Serial.println("WIFI INIT");
sendCommand("AT");
// AT+CWMODE (1: Station mode (client), 2 = AP mode (host), 3 : AP + Station mode)
sendCommand("AT+CWMODE=1");
checkFirmware();
connect();
connectionStatus();
};
void Wifi::setBaudRate() {
//set baud rate
sendCommand("AT+UART_DEF=9600,8,1,0,0");
};
void Wifi::checkFirmware () {
//AT+GMR check firmware
sendCommand("AT+GMR");
};
bool Wifi::connect() {
//AT+CIPSTATUS check whether it is connected
sendCommand("AT+CIPSTATUS");
// AT+CWJAP connect to wifi
sendCommand("AT+CWJAP=\"" + String(WIFI_SSID) + "\",\"" + String(WIFI_PASS) + "\"");
// AT+CIFSR check ip of wifi module
sendCommand("AT+CIFSR");
};
bool Wifi::disconnect() {
// AT+CWQAP disconnect from current WIFI
sendCommand("AT+CWQAP");
};
String Wifi::connectionStatus () {
// AT+CIPSTATUS status from the connection
sendCommand("AT+CIPSTATUS");
};
String Wifi::getReq(String url, String endpoint) {
};
String Wifi::postReq(String url, String endpoint, String data) {
};
bool Wifi::sendCommand(String command){
//printing command in the debug window
delay(30);
Serial.print("=> ");
Serial.print(command);
Serial.print(" ");
countSendAttempts = 0;
successfulResponse = false;
response = "";
while(countSendAttempts < SEND_ATTEMPTS) {
countSendAttempts++;
Serial.print(".");
ESP8266.println(command);
delay(100);
if(ESP8266.find("OK")){
Serial.print("OK");
Serial.println(" ");
successfulResponse = true;
printResponse();
break;
};
}
if(!successfulResponse) {
Serial.print("Fail");
}
};
void Wifi::printResponse() {
delay(50);
Serial.print("RES:");
Serial.print(getResponse());
Serial.print("\n");
};
String Wifi::getResponse() {
delay(50);
if (ESP8266.available()){
return ESP8266.readStringUntil('\n');
}
};
void Wifi::htmlRequest(String url, String endpoint, String reqType, String data = "") {
// AT+CIPMUX (0: single connection, 1: multiple connection)
sendCommand("AT+CIPMUX=1");
// AT+CWMODE (1: Station mode (client), 2 = AP mode (host), 3 : AP + Station mode)
sendCommand("AT+CWMODE=1");
// AT+CIPSTART establishes TCP connection
sendCommand("AT+CIPSTART=4,\"TCP\",\"" + url + "\",80");
String cmd = "";
if(reqType == "GET") {
cmd = "GET " + endpoint + " HTTP/1.1 \r\nHost: " + url + "\r\n\r\n";
} else if (reqType == "POST") {
//TODO
cmd = "POST " + endpoint + " HTTP/1.1 \r\nHost: " + url + "\r\n\r\n";
}
// AT+CIPSTART set the command size
sendCommand("AT+CIPSEND=4," + String(cmd.length() + 4));
// Send the command
sendCommand(cmd);
};
String Wifi::getRequest(String url, String endpoint){
Serial.println("ENDPOINT:" + endpoint);
htmlRequest(url, endpoint, "GET");
};
String Wifi::postRequest(String url, String endpoint, String data) {
};
wifi.h
#ifndef Wifi_h
#define Wifi_h
#include <Arduino.h>
#include<SoftwareSerial.h>
#define WIFI_SSID "XXXXXX"
#define WIFI_PASS "XXXXXX"
#define BAUD_RATE 9600
#define SEND_ATTEMPTS 5
#define DEBUG true
class Wifi
{ private:
uint8_t rx;
uint8_t tx;
uint8_t countSendAttempts;
bool successfulResponse;
String response;
public:
void init();
bool sendCommand(String command);
String getResponse();
void setBaudRate();
void checkFirmware();
bool connect();
bool disconnect();
void printResponse();
String connectionStatus();
void htmlRequest(String url, String endpoint, String reqType, String data = "");
String getRequest(String url, String endpoint);
String postRequest(String url, String endpoint, String data);
String getReq(String url, String endpoint);
String postReq(String url, String endpoint, String data);
SoftwareSerial ESP8266;
Wifi(uint8_t rx, uint8_t tx) : ESP8266 (rx, tx) {
ESP8266.begin(BAUD_RATE);
}
};
#endif
and here is the serial response:
Serial communication started...
WIFI INIT
=> AT .OK
RES:
=> AT+CWMODE=1 .OK
RES:
=> AT+GMR .OK
RES:
=> AT+CIPSTATUS .OK
RES:
=> AT+CWJAP="FlixRouter","12345678!" .....OK
RES:
=> AT+CIFSR .....Fail=> AT+CIPSTATUS .OK
RES:
ENDPOINT:jsonplace
=> AT+CIPMUX=1 .OK
RES:
=> AT+CWMODE=1 .OK
RES:
=> AT+CIPSTART=4,"TCP","jsonplaceholder.typicode.com",80 .OK
RES:
=> AT+CIPSEND=4,67 .OK
RES:
=> GET jsonplace HTTP/1.1
Host: jsonplaceholder.typicode.com
..OK
RES:
Another problem I noticed is that the variable "endpoint" is mutating and changing its value for some unknown reason.
If anyone could shed some light on my problem, it would be really appreciated.
Thanks!
Victor