- Posts: 111
- Karma: 2
- Thank you received: 12
Please Log in or Create an account to join the conversation.
piman wrote: Hello is there anyone who can help, on how I refer to more than one sensor within IDE sketch i.e. temperature sensor and door sensor.
Thanks in advance Andy.
Please Log in or Create an account to join the conversation.
/*
Created by EasyioT
Created by piman
#include <ESP8266WiFi.h>
#include <Base64.h>
#include <Wire.h>
#include "HTU21D.h"
//Create an instance of the object
HTU21D myHumidity;
//AP definitions
#define AP_SSID "12345678"
#define AP_PASSWORD "87654321"
// EasyIoT server definitions
#define EIOT_USERNAME "admin"
#define EIOT_PASSWORD "test"
#define EIOT_IP_ADDRESS "192.168.1.999"
#define EIOT_PORT 80
#define EIOT_NODE "N1S0"
#define INPUT_PIN 2
//#define INPUT_PIN 5
#define USER_PWD_LEN 40
char unameenc[USER_PWD_LEN];
bool oldInputState;
void setup() {
Serial.begin(9600);
pinMode(INPUT_PIN, INPUT);
myHumidity.begin();
pinMode(4, INPUT);
pinMode(5, INPUT);
wifiConnect();
char uname[USER_PWD_LEN];
String str = String(EIOT_USERNAME)+":"+String(EIOT_PASSWORD);
str.toCharArray(uname, USER_PWD_LEN);
memset(unameenc,0,sizeof(unameenc));
base64_encode(unameenc, uname, strlen(uname));
// oldInputState = !digitalRead(INPUT_PIN);
}
void loop() {
float humd = myHumidity.readHumidity();
float temp = myHumidity.readTemperature();
delay(1000);
int inputState = digitalRead(temp);
if (inputState != oldInputState);
{
sendInputState(inputState);
oldInputState = inputState;
}
}
void wifiConnect()
{
Serial.print("Connecting to AP");
WiFi.begin(AP_SSID, AP_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
}
void sendInputState(bool inputState)
{
WiFiClient client;
while(!client.connect(EIOT_IP_ADDRESS, EIOT_PORT)) {
Serial.println("connection failed");
wifiConnect();
}
String url = "";
String command = "";
if (inputState)
command = " ControlOn";
else
command = "ControlOff"; //"temp""humd";
url = "/Api/EasyIoT/Control/Module/Virtual/"+ String(EIOT_NODE) + "/"+command; // generate EasIoT server node URL
Serial.print("POST data to URL: ");
Serial.println(url);
client.print(String("POST ") + url + " HTTP/1.1\r\n" +
"Host: " + String(EIOT_IP_ADDRESS) + "\r\n" +
"Connection: close\r\n" +
"Authorization: Basic " + unameenc + " \r\n" +
"Content-Length: 0\r\n" +
"\r\n");
delay(100);
while(client.available()){
String line = client.readStringUntil('\r');
Serial.print(line);
}
Serial.println();
Serial.println("Connection closed");
delay(2000);
}
Please Log in or Create an account to join the conversation.
/*
Created by Igor Jarc
See http://iot-playground.com for details
Please use community forum on website do not contact author directly
External libraries:
- https://github.com/adamvr/arduino-base64
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
*/
#include <ESP8266WiFi.h>
#include <Base64.h>
#include <Wire.h>
#include "HTU21D.h"
//AP definitions
#define AP_SSID "xxx"
#define AP_PASSWORD "xxx"
// EasyIoT server definitions
#define EIOT_USERNAME "admin"
#define EIOT_PASSWORD "test"
#define EIOT_IP_ADDRESS "192.168.1.xxx"
#define EIOT_PORT 80
#define EIOT_NODE "N1S0"
#define INPUT_PIN 2
#define OUTPUT_PIN 4
//#define INPUT_PIN 5
#define USER_PWD_LEN 40
char unameenc[USER_PWD_LEN];
bool oldInputState;
bool getEvent(HTU21D myHumidity);
//Create an instance of the object
HTU21D myHumidity;
void setup() {
Serial.begin(9600);
// pinMode(2, INPUT);
// pinMode(4, OUTPUT);
// pinMode(5, INPUT);
myHumidity.begin();
//Wire.begin(4, 5);
wifiConnect();
char uname[USER_PWD_LEN];
String str = String(EIOT_USERNAME)+":"+String(EIOT_PASSWORD);
str.toCharArray(uname, USER_PWD_LEN);
memset(unameenc,0,sizeof(unameenc));
base64_encode(unameenc, uname, strlen(uname));
oldInputState = !digitalRead(INPUT_PIN);
}
void loop() {
float humd = myHumidity.readHumidity();
float temp = myHumidity.readTemperature();
delay(1000);
int inputState = (temp);
if (inputState != oldInputState);
{
sendInputState(inputState);
oldInputState = inputState;
}
}
void wifiConnect()
{
Serial.print("Connecting to AP");
WiFi.begin(AP_SSID, AP_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(2000);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
}
void sendInputState(bool inputState)
{
WiFiClient client;
while(!client.connect(EIOT_IP_ADDRESS, EIOT_PORT)) {
Serial.println("connection failed");
wifiConnect();
}
String url = "";
//String url = "";
//String command = "";
// if (inputState)
// command = " ControlOn";
// else
//command = "ControlOff";
delay(1000);
{
// Read temperature as Celsius
//int t = myHumidity.readTemperature();
//delay(1000);
// Wire.beginTransmission (0x40);
// Wire.read ();
url = "/Api/EasyIoT/Control/Module/Virtual/"+String(EIOT_NODE) +"/ControlLevel/""Temp>"+String(myHumidity.readTemperature())+"-"
"Humid>"+String(myHumidity.readHumidity());
Serial.print("POST data to URL: ");
Serial.println(url);
client.print(String("POST ") + url + " HTTP/1.1\r\n" +
"Host: " + String(EIOT_IP_ADDRESS) + "\r\n" +
"Connection: close\r\n" +
"Authorization: Basic " + unameenc + " \r\n" +
"Content-Length: 0\r\n" +
"\r\n");
delay(1000);
// Reading temperature and humidity
// int h = myHumidity.readHumidity();
//delay(1000);
// url +String(myHumidity.readHumidity());
// Serial.print("POST data to URL: ");
//Serial.println(url);
client.print(String("POST ") + url + " HTTP/1.1\r\n" +
"Host: " + String(EIOT_IP_ADDRESS) + "\r\n" +
"Connection: close\r\n" +
"Authorization: Basic " + unameenc + " \r\n" +
"Content-Length: 0\r\n" +
"\r\n");
{
delay(2000);
}
while(client.available()){
String line = client.readStringUntil('\r');
Serial.print(line);
}
Serial.println();
Serial.println("Connection closed");
delay(4000);
}
}
Please Log in or Create an account to join the conversation.
piman wrote: Hi can anyone help this is as far as I've got so far with my I2c temperature and humidity sensor I can get the data to EasyIoT server but it doesn't seem to log the data, also I cannot get them to display the data individually.
Thanks for your help in advance/* Created by Igor Jarc See http://iot-playground.com for details Please use community forum on website do not contact author directly External libraries: - https://github.com/adamvr/arduino-base64 This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License version 2 as published by the Free Software Foundation. */ #include <ESP8266WiFi.h> #include <Base64.h> #include <Wire.h> #include "HTU21D.h" //AP definitions #define AP_SSID "xxx" #define AP_PASSWORD "xxx" // EasyIoT server definitions #define EIOT_USERNAME "admin" #define EIOT_PASSWORD "test" #define EIOT_IP_ADDRESS "192.168.1.xxx" #define EIOT_PORT 80 #define EIOT_NODE "N1S0" #define INPUT_PIN 2 #define OUTPUT_PIN 4 //#define INPUT_PIN 5 #define USER_PWD_LEN 40 char unameenc[USER_PWD_LEN]; bool oldInputState; bool getEvent(HTU21D myHumidity); //Create an instance of the object HTU21D myHumidity; void setup() { Serial.begin(9600); // pinMode(2, INPUT); // pinMode(4, OUTPUT); // pinMode(5, INPUT); myHumidity.begin(); //Wire.begin(4, 5); wifiConnect(); char uname[USER_PWD_LEN]; String str = String(EIOT_USERNAME)+":"+String(EIOT_PASSWORD); str.toCharArray(uname, USER_PWD_LEN); memset(unameenc,0,sizeof(unameenc)); base64_encode(unameenc, uname, strlen(uname)); oldInputState = !digitalRead(INPUT_PIN); } void loop() { float humd = myHumidity.readHumidity(); float temp = myHumidity.readTemperature(); delay(1000); int inputState = (temp); if (inputState != oldInputState); { sendInputState(inputState); oldInputState = inputState; } } void wifiConnect() { Serial.print("Connecting to AP"); WiFi.begin(AP_SSID, AP_PASSWORD); while (WiFi.status() != WL_CONNECTED) { delay(2000); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); } void sendInputState(bool inputState) { WiFiClient client; while(!client.connect(EIOT_IP_ADDRESS, EIOT_PORT)) { Serial.println("connection failed"); wifiConnect(); } String url = ""; //String url = ""; //String command = ""; // if (inputState) // command = " ControlOn"; // else //command = "ControlOff"; delay(1000); { // Read temperature as Celsius //int t = myHumidity.readTemperature(); //delay(1000); // Wire.beginTransmission (0x40); // Wire.read (); url = "/Api/EasyIoT/Control/Module/Virtual/"+String(EIOT_NODE) +"/ControlLevel/""Temp>"+String(myHumidity.readTemperature())+"-" "Humid>"+String(myHumidity.readHumidity()); Serial.print("POST data to URL: "); Serial.println(url); client.print(String("POST ") + url + " HTTP/1.1\r\n" + "Host: " + String(EIOT_IP_ADDRESS) + "\r\n" + "Connection: close\r\n" + "Authorization: Basic " + unameenc + " \r\n" + "Content-Length: 0\r\n" + "\r\n"); delay(1000); // Reading temperature and humidity // int h = myHumidity.readHumidity(); //delay(1000); // url +String(myHumidity.readHumidity()); // Serial.print("POST data to URL: "); //Serial.println(url); client.print(String("POST ") + url + " HTTP/1.1\r\n" + "Host: " + String(EIOT_IP_ADDRESS) + "\r\n" + "Connection: close\r\n" + "Authorization: Basic " + unameenc + " \r\n" + "Content-Length: 0\r\n" + "\r\n"); { delay(2000); } while(client.available()){ String line = client.readStringUntil('\r'); Serial.print(line); } Serial.println(); Serial.println("Connection closed"); delay(4000); } }
Please Log in or Create an account to join the conversation.
Please Log in or Create an account to join the conversation.