Created
January 9, 2016 01:13
-
-
Save anonymous/e55531b910851ccf23e3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Basic Arduino example for K-Series sensor | |
Created by Jason Berger | |
Co2meter.com | |
Rewritten for WiFi-functionality of an ESP8266 | |
and connected to a Senseair S8 CO2-sensor | |
20151116: Added Philips Hue functionality | |
by Markus Ulsass | |
*/ | |
#include <ESP8266WiFi.h> | |
const char* ssid = "yourSSIDHere"; | |
const char* password = "yourPasswordHere"; | |
// const char* host = "192.168.178.42"; // VM Ubunto local IP | |
// const char* host = "172.16.10.248"; // VM Ubunto local IP | |
const char* host = "IP.of.your.platform"; // AAL markus1.protonet.info | |
byte valCo2Status = 0; | |
byte readCO2[] = {0xFE, 0X44, 0X00, 0X08, 0X02, 0X9F, 0X25}; //Command packet to read Co2 (see app note) | |
byte response[] = {0, 0, 0, 0, 0, 0, 0}; //create an array to store the response | |
//multiplier for value. default is 1. set to 3 for K-30 3% and 10 for K-33 ICB | |
int valMultiplier = 1; | |
// Philips Hue settings | |
const char* bridge_ip = "172.16.10.49"; // Hue Bridge IP | |
const int port = 80; | |
String user = "3888d6d56bbdd14a78eb87cf131ec0cd"; | |
String light = "2"; | |
// Philips Hue commands | |
String hue_green = "{\"on\":true, \"sat\":254, \"bri\":64,\"hue\":25500}"; | |
String hue_yellow = "{\"on\":true, \"sat\":254, \"bri\":128,\"hue\":12750}"; | |
String hue_red = "{\"on\":true, \"sat\":254, \"bri\":192,\"hue\":0}"; | |
void setup() | |
{ | |
Serial.begin(9600); //Opens the virtual serial port with a baud of 9600 | |
Serial.swap(); // Serial on ESP8266 normally connected to GPIO1 (TX) and GPIO3 (RX), swap | |
// maps it to GPIO15 (TX) and GPIO13 (RX) which is Pin 8 and 7 on ESPToy | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
} | |
} | |
void sendRequest(byte packet[]) | |
{ | |
while (!Serial.available()) //keep sending request until we start to get a response | |
{ | |
Serial.write(readCO2, 7); | |
delay(50); | |
} | |
int timeout = 0; //set a timeoute counter | |
while (Serial.available() < 7 ) //Wait to get a 7 byte response | |
{ | |
timeout++; | |
if (timeout > 10) //if it takes to long there was probably an error | |
{ | |
while (Serial.available()) //flush whatever we have | |
Serial.read(); | |
break; //exit and try again | |
} | |
delay(50); | |
} | |
for (int i = 0; i < 7; i++) | |
{ | |
response[i] = Serial.read(); | |
} | |
} | |
unsigned long getValue(byte packet[]) | |
{ | |
int high = packet[3]; //high byte for value is 4th byte in packet in the packet | |
int low = packet[4]; //low byte for value is 5th byte in the packet | |
unsigned long val = high * 256 + low; //Combine high byte and low byte with this formula to get value | |
return val * valMultiplier; | |
} | |
// Function to control Philips Hue light by sending | |
// a HTTP-PUT | |
void hueControl(String color) { | |
// Serial.print("Connecting to "); | |
// Serial.println(bridge_ip); | |
// Use WiFiClient class to create TCP connections | |
WiFiClient client; | |
if (!client.connect(bridge_ip, port)) { | |
// Serial.println("Connection failed"); | |
return; | |
} | |
// This will send the request to the server | |
client.println("PUT /api/" + user + "/lights/" + light + "/state"); | |
client.println("Host: " + String(bridge_ip) + ":" + String(port)); | |
client.println("User-Agent: ESP8266/1.0"); | |
client.println("Connection: close"); | |
client.println("Content-type: text/xml; charset=\"utf-8\""); | |
client.print("Content-Length: "); | |
client.println(color.length()); // PUT COMMAND HERE | |
client.println(); | |
client.println(color); // PUT COMMAND HERE | |
delay(10); | |
// Read all the lines of the reply from server and print them to Serial | |
/* while (client.available()) { | |
String line = client.readStringUntil('\r'); | |
Serial.print(line); | |
} | |
Serial.println(); | |
Serial.println("Closing connection"); */ | |
} | |
void loop() | |
{ | |
sendRequest(readCO2); | |
unsigned long valCO2 = getValue(response); | |
if (valCO2 <=1000 && valCo2Status != 1) { | |
hueControl(hue_green); | |
valCo2Status = 1; | |
} else if (valCO2 >1000 && valCO2<=1500 && valCo2Status != 2) { | |
hueControl(hue_yellow); | |
valCo2Status = 2; | |
} else if (valCO2 >1500 && valCo2Status != 3) { | |
hueControl(hue_red ); | |
valCo2Status = 3; | |
} | |
// Use WiFiClient class to create TCP connections | |
WiFiClient client; | |
const int httpPort = 5000; | |
if (!client.connect(host, httpPort)) { | |
return; | |
} | |
// We now create a URI for the request | |
String url = "/update?status="; | |
url += valCO2; | |
url += "&location=HardwareDev"; | |
// This will send the request to the server | |
client.print(String("GET ") + url + " HTTP/1.1\r\n" + | |
"Host: " + host + "\r\n" + | |
"Connection: close\r\n\r\n"); | |
delay(10); | |
/*int num = 0; | |
String var = "?co2=" + String(valCO2); | |
// String var = "{\"rf\":" + String(humidity) + ",\"C\":" + String (celsius) + "}"; | |
num = var.length(); | |
// Sending the http post to the host | |
client.println("POST /172.16.10.66/co2/ HTTP/1.1"); | |
client.println("Host: 172.16.10.66"); | |
client.println("Content-Type: text/plain"); | |
client.println("Content-Length: " + String(num)); | |
client.println(); | |
client.print(var); | |
client.println(); | |
delay(10);*/ | |
/* Read all the lines of the reply from server and print them to Serial | |
while (client.available()) { | |
String line = client.readStringUntil('\r'); | |
Serial.print(line); | |
} | |
Serial.println(); | |
Serial.println("closing connection");*/ | |
// Delay between URL-requests | |
// Do not keep the host too busy ;) | |
delay(1000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment