first commit
This commit is contained in:
391
W1D1_SERVER/W1D1_SERVER.ino
Executable file
391
W1D1_SERVER/W1D1_SERVER.ino
Executable file
@@ -0,0 +1,391 @@
|
||||
#include <ESP8266WiFi.h>
|
||||
|
||||
//#include <Adafruit_BMP085.h>
|
||||
//#include <Wire.h>
|
||||
//#include "DHT.h"
|
||||
|
||||
// DHT
|
||||
#define DHTPIN A0 // what pin we're connected to
|
||||
#define DHTTYPE DHT11 // DHT 11
|
||||
// Initialize DHT sensor for normal 16mhz Arduino
|
||||
//DHT dht(DHTPIN, DHTTYPE);
|
||||
|
||||
// Baromètre
|
||||
// ################# Barometre ####
|
||||
//Adafruit_BMP085 bmp;
|
||||
// #####################
|
||||
|
||||
float temperature = 0.0;
|
||||
float pressure = 0.0;
|
||||
float pression = 0.0;
|
||||
float presiune = 0.0;
|
||||
float humidite = 0.0;
|
||||
|
||||
const unsigned long activation = 111269;
|
||||
const unsigned long idTemp=1969;
|
||||
const unsigned long idPressure=2069;
|
||||
const unsigned long idPression=2169;
|
||||
const unsigned long idLum=2269;
|
||||
const unsigned long idHum=2369;
|
||||
const unsigned long desactivation = 962111;
|
||||
const unsigned int delai = 11;
|
||||
|
||||
// WIFI
|
||||
const char* ssid = "Livebox-37cc"; // Le nom de votre réseau Wifi
|
||||
const char* password = "8A6060920A8A86896F770F2C47";
|
||||
|
||||
int ledPin = 5; // 16=Pin 2
|
||||
|
||||
|
||||
int ledPinRed = 15; // 16=Pin 2, 15=D10
|
||||
int red = 512;
|
||||
|
||||
|
||||
// RGB
|
||||
char colorBuff[4];
|
||||
int redVal;
|
||||
int redTemp=1;
|
||||
int greVal;
|
||||
int bluVal;
|
||||
|
||||
WiFiServer server(80);
|
||||
|
||||
|
||||
#define DEBUG true
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
delay(10);
|
||||
|
||||
pinMode(ledPin, OUTPUT);
|
||||
digitalWrite(ledPin, LOW);
|
||||
|
||||
pinMode(ledPinRed, OUTPUT);
|
||||
digitalWrite(ledPinRed, HIGH);
|
||||
|
||||
|
||||
// Connect to WiFi network
|
||||
Serial.println();
|
||||
Serial.println();
|
||||
Serial.print("Connecting to ");
|
||||
Serial.println(ssid);
|
||||
|
||||
WiFi.begin(ssid, password);
|
||||
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(500);
|
||||
Serial.print(".");
|
||||
}
|
||||
Serial.println("");
|
||||
Serial.println("WiFi connected");
|
||||
|
||||
// Start the server
|
||||
server.begin();
|
||||
Serial.println("Server started");
|
||||
|
||||
// Print the IP address
|
||||
Serial.print("Use this URL to connect: ");
|
||||
Serial.print("http://");
|
||||
Serial.print(WiFi.localIP());
|
||||
Serial.println("/");
|
||||
|
||||
// Wire.begin(1, 2);
|
||||
|
||||
}
|
||||
|
||||
int getValueFrom(String request, int deb, int fin) {
|
||||
int value = 0;
|
||||
|
||||
int bufLength = ((fin) - (deb+7)); //the 7 is for the "redVal=" string
|
||||
|
||||
if(bufLength > 4){ //dont overflow the buffer (we only want 3 digits)
|
||||
bufLength = 4;
|
||||
}
|
||||
request.substring((deb+7), (fin-1)).toCharArray(colorBuff, bufLength); //transfer substring to buffer (cuts from past the "r=" to just before the "g", stores the length into a buffer)
|
||||
value = atoi(colorBuff); //converts the array (3 digits) into an interger, this will be the red value
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
void loop() {
|
||||
int bufLength;
|
||||
|
||||
// Check if a client has connected
|
||||
WiFiClient client = server.available();
|
||||
if (!client) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Wait until the client sends some data
|
||||
Serial.println("new client");
|
||||
while(!client.available()){
|
||||
delay(1);
|
||||
}
|
||||
|
||||
// Read the first line of the request
|
||||
String request = client.readStringUntil('\r');
|
||||
Serial.println(request);
|
||||
client.flush();
|
||||
|
||||
// Match the request
|
||||
|
||||
int value = LOW;
|
||||
if (request.indexOf("/LED=ON") != -1) {
|
||||
digitalWrite(ledPin, HIGH);
|
||||
value = HIGH;
|
||||
red-=100;
|
||||
}
|
||||
if (request.indexOf("/LED=OFF") != -1) {
|
||||
digitalWrite(ledPin, LOW);
|
||||
value = LOW;
|
||||
red+=100;
|
||||
}
|
||||
|
||||
if(request.indexOf("submit") != -1) { //this is used to ignore "favicon.ico" and other requests
|
||||
int Pos_r = request.indexOf("redVal");
|
||||
int Pos_g = request.indexOf("greVal", Pos_r); //finds a location of a string "g", after the position of "r"
|
||||
int Pos_b = request.indexOf("bluVal", Pos_g);
|
||||
// int Pos_rad = request.indexOf("matrad",Pos_b);
|
||||
int End = request.indexOf("H", Pos_b);
|
||||
if(End < 0){
|
||||
End = request.length() + 1;
|
||||
}
|
||||
//red
|
||||
redVal = getValueFrom(request, Pos_r, Pos_g);
|
||||
|
||||
//Green
|
||||
greVal = getValueFrom(request, Pos_g, Pos_b);
|
||||
//blue
|
||||
bluVal = getValueFrom(request, Pos_b, End);
|
||||
|
||||
#ifdef DEBUG
|
||||
Serial.println("Red is: ");
|
||||
Serial.println(redVal);
|
||||
redTemp=redVal;
|
||||
Serial.println("Green is: ");
|
||||
Serial.println(greVal);
|
||||
Serial.println("Blue is: ");
|
||||
Serial.println(bluVal);
|
||||
Serial.println("radio button is: ");
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
analogWrite(ledPinRed, redVal);
|
||||
|
||||
// Set ledPin according to the request
|
||||
//digitalWrite(ledPin, value);
|
||||
|
||||
// Return the response
|
||||
client.println("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n");
|
||||
client.println("<!DOCTYPE HTML>\r\n<html>\r\n<head>\r\n<title>ESP Server Test</title>");
|
||||
client.println("<meta name='viewport' content='width=device-width', initial-scale='1'>");
|
||||
client.println("<br>");
|
||||
client.println("<body>");
|
||||
|
||||
client.print("Led pin is now: ");
|
||||
|
||||
if(value == HIGH) {
|
||||
client.print("On");
|
||||
} else {
|
||||
client.print("Off");
|
||||
}
|
||||
|
||||
/////////////////////////////
|
||||
//
|
||||
////////////////////////////
|
||||
digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
|
||||
delay(10); // wait for a second
|
||||
digitalWrite(13, LOW);
|
||||
|
||||
// barometre();
|
||||
// doDHT();
|
||||
|
||||
#ifdef DEBUG
|
||||
Serial.print("Send");
|
||||
#endif
|
||||
|
||||
myMessageSend(idTemp,temperature * 100);
|
||||
myMessageSend(idPressure,pressure * 10);
|
||||
myMessageSend(idHum,humidite);
|
||||
myMessageSend(idPression,pression * 10);
|
||||
|
||||
// LUX
|
||||
// R=K*L^-gamma
|
||||
// R étant la résistance pour un niveau d'éclairement L.
|
||||
int lum = analogRead(1);
|
||||
|
||||
int lux = (1000.0 * lum / 1024.0);
|
||||
myMessageSend(idLum,lux);
|
||||
|
||||
|
||||
client.println("<TABLE>");
|
||||
|
||||
client.println("<TR><TD>Click <a href=\"/LED=ON\">here</a> turn the LED on pin 2 ON</TD></TR>");
|
||||
client.println("<TR><TD>Click <a href=\"/LED=OFF\">here</a> turn the LED on pin 2 OFF</TD></TR>");
|
||||
|
||||
client.print("<TR><TD>Temperature=");client.print(temperature);client.print("</TD></TR>");
|
||||
client.print("<TR><TD>Pressure =");client.print(pressure);client.print("</TD></TR>");
|
||||
client.print("<TR><TD>Humidite =");client.print(humidite);client.print("</TD></TR>");
|
||||
client.print("<TR><TD>Pression =");client.print(pression);client.print("</TD></TR>");
|
||||
client.print("<TR><TD>Luminosite =");client.print(lux);client.print("</TD></TR>");
|
||||
|
||||
client.println("</TABLE>");
|
||||
|
||||
client.println("<form method=get>"),
|
||||
client.println("<br>Red: <input type='range' min='0' max='255' name=redVal value=" + String(redVal) + ">"); //was onchange event
|
||||
client.println("<span id='range'>0</span>");
|
||||
client.println("<script type='text/javascript'>\r\nfunction showValue(newValue)\r\n{\r\ndocument.getElementById('range').innerHTML=newValue;\r\n}\r\n</script>\r\n");
|
||||
client.print("<br><br>Green: <input type='range' min='0' max='255' name=greVal value=" + String(greVal) + ">");
|
||||
client.print("<br><br>Blue: <input type='range' min='0' max='255' name=bluVal value=" + String(bluVal) + ">");
|
||||
client.println(" <br><br><input name=H type=submit value=submit><br><br>"); //</form>
|
||||
client.println("</form>");
|
||||
client.println("<script type='text/javascript'></script>\r\n");
|
||||
client.println("</center>");
|
||||
client.println("</BODY>");
|
||||
client.println("</HTML>");
|
||||
|
||||
delay(1);
|
||||
Serial.println("Client disonnected");
|
||||
Serial.println("");
|
||||
|
||||
|
||||
|
||||
// analogWrite(Green, min(255, 255.0 * (512 + y) / 1024.0));
|
||||
// analogWrite(Blue, min(255, 255.0 * (512 + z) / 1024.0));
|
||||
|
||||
//getPage();
|
||||
|
||||
|
||||
}
|
||||
|
||||
// Commande pour barometre humidité température
|
||||
// Virtual Device
|
||||
// http://192.168.0.10:8080/json.htm?type=command¶m=udevice&idx=160&nvalue=0&svalue=23.3;50;2;1024.20;1024&battery=89
|
||||
|
||||
//void doDHT() {
|
||||
// // Reading temperature or humidity takes about 250 milliseconds!
|
||||
// // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
|
||||
// float h = dht.readHumidity();
|
||||
// humidite = h;
|
||||
//
|
||||
// // Read temperature as Celsius
|
||||
// float t = dht.readTemperature();
|
||||
// // Read temperature as Fahrenheit
|
||||
// float f = dht.readTemperature(true);
|
||||
//
|
||||
// // Check if any reads failed and exit early (to try again).
|
||||
// if (isnan(h) || isnan(t) || isnan(f)) {
|
||||
// // Serial.println("Failed to read from DHT sensor!");
|
||||
// return;
|
||||
// } else {
|
||||
// // Compute heat index
|
||||
// // Must send in temp in Fahrenheit!
|
||||
// float hi = dht.computeHeatIndex(f, h);
|
||||
//#ifdef DEBUG
|
||||
// Serial.print("Humidity: ");
|
||||
// Serial.print(h);
|
||||
// Serial.print(" %\t");
|
||||
// Serial.print("Temperature: ");
|
||||
// Serial.print(t);
|
||||
// Serial.print(" *C ");
|
||||
// Serial.print(f);
|
||||
// Serial.print(" *F\t");
|
||||
// Serial.print("Heat index: ");
|
||||
// Serial.print(hi);
|
||||
// Serial.println(" *F");
|
||||
//#endif
|
||||
// }
|
||||
//}
|
||||
//
|
||||
//void barometre() {
|
||||
// /* See Example: TypeA_WithDIPSwitches */
|
||||
//// mySwitch.switchOn("00001", "10000");
|
||||
//// delay(1000);
|
||||
// // BMP
|
||||
// if (bmp.begin()) {
|
||||
// temperature = bmp.readTemperature();
|
||||
// pressure= bmp.readPressure() / 100.0;
|
||||
// pression = pressure / 101.325;
|
||||
// pression = pression * 0.760 * 100;
|
||||
// // http://en.wikipedia.org/wiki/Atmospheric_pressure#Mean_sea_level_pressure
|
||||
// // Serial.print("Presiure la nivelul marii (calculata) = ");
|
||||
// presiune = bmp.readSealevelPressure(19) / 101.325;
|
||||
// presiune = presiune * 0.760;
|
||||
// #ifdef DEBUG
|
||||
// Serial.print("Temperature="); Serial.println(temperature);
|
||||
// Serial.print("pressure="); Serial.println(pressure);
|
||||
// Serial.print("pression="); Serial.println(pression);
|
||||
// #endif
|
||||
//
|
||||
// }
|
||||
//}
|
||||
|
||||
void myMessageSend(long id, long value) {
|
||||
|
||||
// #ifdef DEBUG
|
||||
// Serial.print("Send id="); Serial.print(id);
|
||||
// Serial.print(" value="); Serial.println(value);
|
||||
// #endif
|
||||
|
||||
|
||||
|
||||
//delay(5000);
|
||||
//delayMicroseconds(TWOTIME*8);
|
||||
}
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
// Read current supply voltage
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
// long readVcc() {
|
||||
// bitClear(PRR, PRADC); ADCSRA |= bit(ADEN); // Enable the ADC
|
||||
// long result;
|
||||
// // Read 1.1V reference against Vcc
|
||||
// #if defined (__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
|
||||
// ADMUX = _BV(MUX5) | _BV(MUX0); // For ATtiny84
|
||||
// #elif defined (__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
|
||||
// ADMUX = _BV(MUX3) | _BV(MUX2);
|
||||
// #else
|
||||
// ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1); // For ATmega328
|
||||
// #endif
|
||||
// // ADCSRB = 0;
|
||||
//
|
||||
// delay(2); // Wait for Vref to settle
|
||||
// ADCSRA |= _BV(ADSC); // Convert
|
||||
// while (bit_is_set(ADCSRA,ADSC));
|
||||
// result = ADCL;
|
||||
// result |= ADCH<<8;
|
||||
// result = 1126400L / result; // Back-calculate Vcc in mV
|
||||
// // ADCSRA &= ~ bit(ADEN); bitSet(PRR, PRADC); // Disable the ADC to save power
|
||||
//
|
||||
// // analogReference(DEFAULT);
|
||||
//
|
||||
// return result; // Vcc in millivolts
|
||||
//}
|
||||
|
||||
//const char http_site[] = "192.168.0.10";
|
||||
//const int http_port = 80;
|
||||
//
|
||||
//// Perform an HTTP GET request to a remote page
|
||||
//bool getPage() {
|
||||
//
|
||||
// WiFiClient client;
|
||||
//
|
||||
// // Attempt to make a connection to the remote server
|
||||
// if ( !client.connect(http_site, http_port) ) {
|
||||
// return false;
|
||||
// }
|
||||
// ///json.htm?type=command¶m=udevice&idx=158&svalue=255
|
||||
// // Make an HTTP GET request
|
||||
// client.println("GET /index.html HTTP/1.1");
|
||||
// client.print("Host: ");
|
||||
// client.println(http_site);
|
||||
// client.println("Connection: close");
|
||||
// client.println();
|
||||
//
|
||||
// while (client.available() ) {
|
||||
// char c = client.read();
|
||||
// Serial.print(c);
|
||||
// }
|
||||
// Serial.println("");
|
||||
// return true;
|
||||
//}
|
||||
Reference in New Issue
Block a user