Skip to content
Snippets Groups Projects
Commit cd7cad0c authored by Marc Kühne's avatar Marc Kühne
Browse files

tested

parent fffff39d
Branches master
No related tags found
No related merge requests found
/**
* @file SimulatedESP8266.cpp
* @author Samuel Helbling (samuel.helbling@ost.ch)
* @brief
* @version 0.1
* @date 2021-04-29
*
* @copyright Copyright (c) 2021
*
*/
#include "Arduino.h"
#include "SimulatedESP8266.h"
#define DEBUG false // set to TRUE to get more informations during runtime
uint8_t m_responseBuffer[300] = {0};
SimulatedESP8266::SimulatedESP8266(int rxPin, int txPin) : rxPin{rxPin}, txPin{txPin}
{
}
void SimulatedESP8266::setup()
{
espSerial.begin(115200);
espData("AT+RST", 10, DEBUG); //Reset the ESP8266 module
espData("AT+CWMODE=1", 10, DEBUG); //Set the ESP mode as station mode
espData("AT+CWJAP=\"VBB\",\"12345\"", 10, DEBUG); //Connect to WiFi network
Serial.println("ESP ready");
}
String SimulatedESP8266::httpGet(String host, String endpoint, String protocol = "80")
{
Serial.println("Sending HTTP Get-Request");
String sendData = "GET " + endpoint;
espData("AT+CIPMUX=1", 10, DEBUG); //Allow multiple connections
espData("AT+CIPSTART=0,\"TCP\",\"" + host + "\"," + protocol, 10, DEBUG);
espData("AT+CIPSEND=0," + String(sendData.length() + 4), 10, DEBUG);
espSerial.find('>');
String response = espData(sendData, 10, DEBUG);
espData("AT+CIPCLOSE=0", 10, DEBUG);
return response;
}
String SimulatedESP8266::httpPost(String host, String endpoint, String payload, String protocol = "80")
{
Serial.println("Sending HTTP Post-Request");
String sendData = "POST " + endpoint +" HTTP/1.1\r"
+ "Host: " + host + "\r"
+ "Content-Type: text/plain\r"
+ "Content-Length: " + payload.length() + "\r\r"
+ payload;
espData("AT+CIPMUX=1", 10, DEBUG); //Allow multiple connections
espData("AT+CIPSTART=0,\"TCP\",\"" + host + "\"," + protocol, 10, DEBUG);
espData("AT+CIPSEND=0," + String(sendData.length() + 4), 10, DEBUG);
espSerial.find('>');
String response = espData(sendData, 10, DEBUG);
espData("AT+CIPCLOSE=0", 10, DEBUG);
return response;
}
String SimulatedESP8266::espData(String command, long timeout, boolean debug)
{
String response = "";
espSerial.println(command);
long time = millis();
while ((time + timeout) > millis())
{
while (espSerial.available())
{
char c = espSerial.read();
response += c;
}
}
return response;
}
/**
* @file SimulatedESP8266.h
* @author Samuel Helbling (samuel.helbling@ost.ch)
* @brief
* @version 0.1
* @date 2021-04-29
*
* @copyright Copyright (c) 2021
**/
#include "Arduino.h"
#include <SoftwareSerial.h> //Software Serial library
#ifndef SIMULATEDESP8266
#define SIMULATEDESP8266
class SimulatedESP8266 {
public:
SimulatedESP8266(int rxPin, int txPin);
void setup();
String httpGet(String host, String endpoint, String protocol = "80");
String httpPost(String host, String endpoint, String payload, String protocol = "80");
private:
int txPin;
int rxPin;
String espData(String command, long timeout, boolean debug);
SoftwareSerial espSerial = SoftwareSerial(rxPin,txPin);
};
#endif
#include <Arduino.h>
#include "AirSensor.h"
#include "SimulatedESP8266.h"
int ventilatorPinIN1 {7};
int ventilatorPinIN2 {6};
......@@ -11,6 +12,15 @@
const float thresholdVentilatorMax{0.6};
float airQuality;
const int RX_PIN{11};
const int TX_PIN{12};
String myHost = "104.248.41.20:1890";
SimulatedESP8266 simulatedESP8266{RX_PIN, TX_PIN};
void setup() {
pinMode(ventilatorPinEN, OUTPUT);
pinMode(ventilatorPinIN1, OUTPUT);
......@@ -22,9 +32,13 @@ void setup() {
digitalWrite(ventilatorPinIN2, LOW);
Serial.println(thresholdVentilatorMax);
Serial.println(thresholdVentilatorStart);
simulatedESP8266.setup();
;
}
void loop() {
airQuality = airSensor.getAirQuality();
......@@ -46,4 +60,9 @@ void loop() {
Serial.println(ventilatorSpeed);
delay(500);
String payloadAirQuality = "{\"Luftqualität\":" + String(airQuality);
payloadAirQuality = payloadAirQuality +"}";
simulatedESP8266.httpPost(myHost, "/airQuality", payloadAirQuality);
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment