Skip to content

Arduino-ESP comunication for Sensorflare upload #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions Arduino-ESP/ArduinoTX/ArduinoTX.ino
Original file line number Diff line number Diff line change
@@ -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 <Wire.h>
#include <TH02_dev.h>

//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);

}

136 changes: 136 additions & 0 deletions Arduino-ESP/espRX/espRX.ino
Original file line number Diff line number Diff line change
@@ -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 <ESP8266WiFi.h>
#include <WiFiClientSecure.h>

//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<len);
}
}

}

//Fucntion to Upload a sensor value on Sensorflare
void SendToSensorFlare(float var,String SensorName)
{

String url="/api/resource/"+SensorName+"?value=";

//Sensorflare host
const char* host = "www.sensorflare.com";
const int httpsPort = 443;

WiFiClientSecure client;
//Connect to the server
Serial.print("connecting to ");
Serial.println(host);
if (!client.connect(host, httpsPort)) {
Serial.println("connection failed");
return;
}


//Connect to the server
Serial.print("requesting URL: ");
Serial.println(url);
// This will send the POST to the server
client.print("POST ");
client.print( url );
client.print(var,2);
client.println(" HTTP/1.0");
client.print("Host: ");
client.println(host);
client.print("Authorization: Basic ");
client.println(CredBase64);
client.println("User-Agent: espagent");
client.println();

delay(500);
Serial.println("request sent");
while (client.connected()) {
String line = client.readStringUntil('\n');
if (line == "\r") {
Serial.println("headers received");
break;
}
}
String line = client.readStringUntil('\n');
Serial.println("reply was:");
Serial.println("==========");
Serial.println(line);
Serial.println("==========");
Serial.println("closing connection");
client.stop();
Serial.println("Client close");
delay(500);
}