diff --git a/Arduino-ESP/ArduinoTX/ArduinoTX.ino b/Arduino-ESP/ArduinoTX/ArduinoTX.ino new file mode 100644 index 0000000..d509c52 --- /dev/null +++ b/Arduino-ESP/ArduinoTX/ArduinoTX.ino @@ -0,0 +1,93 @@ +/* + * Arduino with THO2(Temperature/Humidity) and VOC sensors. + * + * This example show how to send the Name and Value of each Sensor + * to ESP module through Serial Port. + * + * Created by Lidia Pocero. + */ + +//Libraries for I2C comunication with the THO +#include +#include + +//Estructure for each Sensor +struct Sensor { + String Name; + float Value; +}; + +//Definde your Sensor number +const int VarNumber = 3; + +//Array of Sensors +Sensor MySensors[VarNumber]; + +void setup() +{ + Wire.begin(); + + //Digital Sensors + /* Power up,delay 150ms,until voltage is stable */ + delay(150); + /* Reset HP20x_dev */ + TH02.begin(); + + delay(150); +} + +void loop() +{ + //TH02 sensor + + //Temperature value + float t = TH02.ReadTemperature(); + //Sensor name: The name of the sensor on Sensorflare + MySensors[0].Name = "mydev1/temperature"; + //Sensor value + MySensors[0].Value = t; + + delay(150); + + //Humidity value + float h = TH02.ReadHumidity(); + //Sensor name: The name of the sensor on Sensorflare + MySensors[1].Name = "mydev1/humidity"; + //Sensor value + MySensors[1].Value = h; + delay(150); + + //VOC sensor + + int gas = analogRead(A2); + float voc = gas * 4.9; + //Sensor name: The name of the sensor on Sensorflare + MySensors[2].Name = "mydev1/voc"; + //Sensor value + MySensors[2].Value = voc; + + //Send all the Sensor Values to ESP + SendToESP(); + + +} +//Funcion that send the variable names and the variable values to the ESP +void SendToESP() +{ + int i = 0; + char Val[10]; + Serial.begin(9600); + while (i < VarNumber) + { + dtostrf(MySensors[i].Value, 4, 2, Val); + Serial.print(MySensors[i].Name); + Serial.print(":"); + Serial.println(Val); + i++; + } + Serial.end(); + //Later of each send to ESP wait half minets before to go back + delay(30000); + +} + diff --git a/Arduino-ESP/espRX/espRX.ino b/Arduino-ESP/espRX/espRX.ino new file mode 100644 index 0000000..65e3437 --- /dev/null +++ b/Arduino-ESP/espRX/espRX.ino @@ -0,0 +1,136 @@ +/* + * ESP board Arduino program to receive through Serial Port the sensor names + * and values to be uploaded on Sensorflare + * + * Created by Lidia Pocero. + */ + +//Include the 2 necessary library for HTTPS Wifi comunication +#include +#include + +//Your Sensorflare acreditation (username:password) on base64 +#define CredBase64 "**************************************" + +// Unser name and password of you WiFi conection +const char* ssid = "********"; +const char* password = "***********"; + + +void setup() +{ + //The serial port will comunicate with th Arduino Module + Serial.begin(9600); + + //Connect the device to the WiFi + Serial.println(ssid); + WiFi.begin(ssid, password); + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + } + Serial.println(""); + Serial.println("WiFi connected"); + Serial.println("IP address: "); + Serial.println(WiFi.localIP()); + delay (1000); +} + +void loop() +{ + //Serial Comunication with the Arduino to take the sensor values to upload it + int incoming = Serial.available(); + + if (incoming > 0) + { + + //Necessary variables + int indexL,indexH; + String element,value; + float val; + String variable=Serial.readString(); + int len=variable.length(); + + Serial.println(variable); + int in=0; + if (variable!=0) + { + Serial.println("NEW MESSEGE"); + do{ + Serial.print("NEW VARIABLE:"); + + indexL=variable.indexOf(':',in); + element=variable.substring(in,indexL); + indexH=variable.indexOf('\n',indexL); + value=variable.substring(indexL+1,indexH); + val=value.toFloat(); + + + Serial.print(element); + Serial.print(":"); + Serial.println(val); + + //Upload on Sensorflare each receive Sensor value + SendToSensorFlare(val,element); + in=indexH+1; + }while(in