Prof. J. Walter - Informationstechnik, Mikrocomputertechnik, Digitale Medien Quellcode
Hochschule Karlsruhe Logo Informationstechnik
3D-Drucker-Platine
Wintersemester 2018/2019
Andy Eichmann
Kai Klotz

Quellcode

-
#include <Arduino.h>
#include <WiFi.h>
#include <Wire.h>
#include <WiFiMulti.h>
#include <SparkFun_RHT03.h>

void requestEvent();
int connect_WiFi();
void RHT03_read(RHT03, float*, float*); ////Temp u. Feuchtesensor auslesen
float getPressure(); //Druck einlesen
void Send_Data_Channel1(float, float, float , float , float, float, float , float);
void Send_Data_Channel2(float,float,bool);
//Daten an Thingspeak senden

const char* ssid = "HIT-FRITZBOX-7490"; // Your SSID (Name of your WiFi)
const char* password = "63601430989011937932"; //Your Wifi password

const char* host = "api.thingspeak.com"; //Thingspeak Host
String api_key1 = "RAAR3P14LNCLJBZ8"; // Your API Key provied by thingspeak
String api_key2 = "GZBPQL4RPDZLABPS"; // Your API Key provied by thingspeak

#define analogPin1 34 //Differentielle Spannungsmessung Druck +
#define analogPin2 35 //Differentielle Spannungsmessung Druck -
#define wait_Time 500

const int RHT03_DATA_PIN1 = 32; // RHT03 data pin Sensor 1
const int RHT03_DATA_PIN2 = 33; // RHT03 data pin Sensor 2
const int RHT03_DATA_PIN3 = 25; // RHT03 data pin Sensor 3
const int RHT03_DATA_PIN4 = 26; // RHT03 data pin Sensor 4

RHT03 temp1; //Temp u. Feuchtesensor 1
RHT03 temp2; //Temp u. Feuchtesensor 2
RHT03 temp3; //Temp u. Feuchtesensor 3
RHT03 temp4; //Temp u. Feuchtesensor 4

float t1; //Temperatur 1
float h1; //Humidity 1
float t2;
float h2;
float t3;
float h3;
float t4;
float h4;
float p; //Druck
float et;
bool es;

void setup()
{
 
Serial.begin(115200); //Seriellen Monitor starten //Baud 115200
temp1.begin(RHT03_DATA_PIN1); //Temp u. Feuchtesensor 1 starten
temp2.begin(RHT03_DATA_PIN2); //Temp u. Feuchtesensor 2 starten
temp3.begin(RHT03_DATA_PIN3); //Temp u. Feuchtesensor 3 starten
temp4.begin(RHT03_DATA_PIN4); //Temp u. Feuchtesensor 4 starten

connect_WiFi();

// Wire.begin(8); // join i2c bus (address optional for master)
// Wire.onRequest(requestEvent);
//derzeit seitens Softwaregruppe noch nicht implementiert
}

void loop()
{
RHT03_read(temp1,&t1,&h1); //Temp u. Feuchtesensor 1 auslesen
RHT03_read(temp2,&t2,&h2); //Temp u. Feuchtesensor 2 auslesen
RHT03_read(temp3,&t3,&h3); //Temp u. Feuchtesensor 3 auslesen
RHT03_read(temp4,&t4,&h4); //Temp u. Feuchtesensor 4 auslesen
p = getPressure();

et=40;
es=0;

if(connect_WiFi() == WL_CONNECTED) //Wenn Wifi connected
{
Send_Data_Channel1(t1,t2,t3,t4,h1,h2,h3,h4); //Sende Daten an Thingspeak
Send_Data_Channel2(et,p,es);
}

delay(15000);
}

void RHT03_read(RHT03 temp, float* t, float* h) //Temp u. Feuchtesensor auslesen
{
int updateRet = temp.update();
// If successful, the update() function will return 1.
// If update fails, it will return a value <0
if (updateRet == 1)
{
// The humidity(), tempC(), and tempF() functions can be called -- after
// a successful update() -- to get the last humidity and temperature value
*h = temp.humidity();
*t = temp.tempC();
 
// Now print the values:
Serial.print("Humidity: " + String(*h, 1) + " % ");
Serial.println("Temp (C): " + String(*t, 1) + " deg C");
}
else
{
// If the update failed, try delaying for RHT_READ_INTERVAL_MS ms before trying again.
delay(RHT_READ_INTERVAL_MS);
}
}

// void requestEvent() //I2C derzeit seitens Softwaregruppe nicht implementiert
// {
// String data_to_send = String(t1);
// data_to_send += String(h1);
// data_to_send += String(t2);
// data_to_send += String(h2);
// data_to_send += String(t3);
// data_to_send += String(h3);
// data_to_send += String(t4);
// data_to_send += String(h4);
// data_to_send += String(p);

// Wire.write(data_to_send.c_str());
// }

int connect_WiFi() //Mit Wifi verbinden
{
if(WiFi.status() != WL_CONNECTED)
{
WiFi.begin(ssid, password);
 
for(int i;i>10;i++)
{
if(WiFi.status() != WL_CONNECTED)
{
delay(wait_Time);
Serial.println("Connecting to WiFi..");
}

if(WiFi.status() == WL_CONNECTED)
{
break;
}
}
 
Serial.println("Connected to the WiFi network");
}
return WiFi.status();
}


float getPressure() //Druck einlesen
{
int pSensorVal = analogRead(analogPin1); //ADC Messung Differentiell Druck+
int nSensorVal = analogRead(analogPin2); //ADC Messung Differentiell Druck -

int gSensorVal = pSensorVal-nSensorVal; //Differenz berechnen

float Voltage = (gSensorVal/4096.0)*3.0; //Wert in Spannung umrechnen
float Pressure = (Voltage-0.18)*4.65; //Wert in Druck umrechnen

Serial.print("Pressure: " + String(Pressure, 1) + " Bar " + "\n");

return Pressure;

}

void Send_Data_Channel1(float t1, float t2, float t3, float t4, float h1, float h2, float h3, float h4)
{

Serial.println("Prepare to send data");

// Use WiFiClient class to create TCP connections
WiFiClient client;

const int httpPort = 80;

if (!client.connect(host, httpPort))
{
Serial.println("connection failed");
return;
}
else
{
String data_to_send = api_key1;
data_to_send += "&field1=";
data_to_send += String(t1);
data_to_send += "&field2=";
data_to_send += String(h1);
data_to_send += "&field3=";
data_to_send += String(t2);
data_to_send += "&field4=";
data_to_send += String(h2);
data_to_send += "&field5=";
data_to_send += String(t3);
data_to_send += "&field6=";
data_to_send += String(h3);
data_to_send += "&field7=";
data_to_send += String(t4);
data_to_send += "&field8=";
data_to_send += String(h4);
data_to_send += "\r\n\r\n";

client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: " + api_key1 + "\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(data_to_send.length());
client.print("\n\n");
client.print(data_to_send);
Serial.println("Data send Channel1");
}

client.stop();
 
}

void Send_Data_Channel2(float et,float p,bool es)
{

Serial.println("Prepare to send data");

// Use WiFiClient class to create TCP connections
WiFiClient client;

const int httpPort = 80;

if (!client.connect(host, httpPort))
{
Serial.println("connection failed");
return;
}
else
{
String data_to_send = api_key2;
data_to_send += "&field1=";
data_to_send += String(et);
data_to_send += "&field2=";
data_to_send += String(p);
data_to_send += "&field3=";
data_to_send += String(es);
data_to_send += "\r\n\r\n";

client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: " + api_key2 + "\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(data_to_send.length());
client.print("\n\n");
client.print(data_to_send);
Serial.println("Data send Channel2");
}

client.stop();

Hier gibt es das Projekt als .zip-File: ESP32_Prozessauswertung.zip


  Mit Unterstützung von Prof. J. Walter Wintersemester 2018/2019