#define USE_WIFI //#define USE_BLUETOOTH // esp32 dev module #include "DHT.h" #ifdef USE_WIFI #include #endif #include #include #ifdef USE_BLUETOOTH #include "BluetoothSerial.h" #endif #if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED) #error Bluetooth is not enabled! Please run `make menuconfig` to and enable it #endif #ifdef USE_BLUETOOH BluetoothSerial SerialBT; #endif using namespace std; // pins du esp32 associé au convertisseur analogue digital static unsigned char tbpins[]={ //36,39, non disponible sur la carte // Dans l'ordre sur le coté gauche, commence à la position 5 34,35,32,33,25,26,27,14,12, // Il y a un GND entre 12 et 13 13, // Sur le coté droit 2,4,15 }; // pins utilisées pour les relais static unsigned char tbrelays[]={ 23,22,21,19,18,5,17,16 }; #define DHTPIN tbpins[0] // Digital pin connected to the DHT sensor #define DHTTYPE DHT22 DHT dht(DHTPIN, DHTTYPE); #ifdef USE_WIFI static void scannetwork(String &bestssid, bool debug) { // WiFi.scanNetworks will return the number of networks found int n = WiFi.scanNetworks(); if (debug) Serial.println("scan done"); if (n == 0) { if (debug) Serial.println("no networks found"); } else { if (debug){ Serial.print(n); Serial.println(" networks found"); } int bestrssi=-100; for (int i = 0; i < n; ++i) { // Print SSID and RSSI for each network found auto ssid = WiFi.SSID(i); int rssi = WiFi.RSSI(i); if (debug){ Serial.print(i + 1); Serial.print(": "); Serial.print(ssid); Serial.print(" ("); Serial.print(rssi); Serial.print(")"); Serial.println((WiFi.encryptionType(i) == WIFI_AUTH_OPEN)?" ":"*"); delay(10); } // Permet de tester le comportement si le router WIFI n'est pas encore prêt // comme après une panne d'électricité. #if 1 if (ssid == "maison" && rssi > bestrssi){ bestssid = ssid; bestrssi = rssi; }else if (ssid == "serre" && rssi > bestrssi){ bestssid = ssid; bestrssi = rssi; } #endif } if (debug){ Serial.print ("bestssid="); Serial.println (bestssid); Serial.print ("bestrssi="); Serial.println(bestrssi); } } } static void scannetwork() { String bestssid; scannetwork(bestssid,true); } enum CONNECTSTATE {idle,connecting,connected}; struct WIFISTATUS{ String ssid; unsigned long last; CONNECTSTATE state; unsigned wifitry; void reset(){ ssid.clear(); last = 0; state = idle; } WIFISTATUS(){ reset(); wifitry=0; } }; static WIFISTATUS status; static void wificonnect(bool debug) { status.reset(); String bestssid; scannetwork(bestssid,debug); if (bestssid != ""){ status.ssid = bestssid; WiFi.begin(bestssid.c_str(),"53955395"); status.state = connecting; } } #endif #include ESP32AnalogRead adc; #define LED 2 const int adcresol=4096; void setup() { pinMode(LED,OUTPUT); analogReadResolution(12); Serial.begin(115200); for (int pin=0; pin < sizeof(tbpins)/sizeof(tbpins[0]); pin++) pinMode(tbpins[pin],INPUT); adc.attach(tbpins[0]); // analogReference(DEFAULT); dht.begin(); #ifdef USE_WIFI WiFi.mode(WIFI_STA); WiFi.disconnect(); delay(100); wificonnect(false); Serial.print("setup status="); Serial.println(WiFi.status()); #endif #ifdef USE_BLUETOOTH SerialBT.begin("ESP32"); #endif } const float VOLT = 3.3; static bool debug = false; float readvolt_real(int pin) { #if 0 if (debug){ Serial.print("pin="); Serial.print(pin); int v = analogReadMilliVolts(pin); Serial.print(" milli="); Serial.print(v); } const float VPS = VOLT / adcresol; //volts per step unsigned val = analogRead(pin); if (debug){ Serial.print(" val="); Serial.print(val); } float volt = val * VPS; if (debug){ Serial.print (" volt="); Serial.println(volt,4); } #if 0 int reading = val; volt = -0.000000000000016 * pow(reading,4) + 0.000000000118171 * pow(reading,3)- 0.000000301211691 * pow(reading,2)+ 0.001109019271794 * reading + 0.034143524634089; if (debug){ Serial.print (" volt="); Serial.println(volt,4); } #endif #endif adc.attach(pin); float volt = adc.readVoltage(); if (debug){ Serial.print (" adcvolt="); Serial.println(volt,4); } return volt; } static float tbvolts[40]; float readvolt(int pin) { return tbvolts[pin]; } float readlum(int pin) { float myfloat = readvolt(pin); return myfloat; } float readhumid(int pin, float &temp) { float h = dht.readHumidity(); // read humidity temp = dht.readTemperature(); return h; } float readtemp(int pin) { float myfloat = readvolt(pin); //if (pin==35) myfloat = 1.65; //Serial.print (" myfloat="); //Serial.println(myfloat); float res = (VOLT/myfloat-1)*12000; const double A= 1.279336835E-3; const double B= 2.045395490E-4; const double C= 2.537322864E-7; float temp = 1.0/(A+B*log(res)+C*powf(log(res),3))-273.15; //printf ("log %lf\n",3600*log(res)); //debug_printf("Values: HEX 0x%02x DEC %d reading %4.3f volts. resistance %4.3f ohms temp %4.3f\n", val, val, myfloat,res,temp); return temp; } #ifdef USE_WIFI static WiFiServer wserver(23); // Port telnet #endif static void parsecommand (const char *line, struct WIFISTATUS &status, std::function fct) { //Serial.print("I received: "); //Serial.println(line); bool is_read = strncmp(line,"read",4)==0; debug = strncmp(line,"READ",4)==0; bool dofin = false; // envoi le message fin ou pas à la fin if (is_read || debug){ int pos = 0; const char *pt = line+4; float dhttemp=0; bool dhttemp_filled = false; while (*pt != '\0'){ int pin = tbpins[pos]; switch(*pt){ case 'T': { float temp; if (dhttemp_filled){ dhttemp_filled = false; temp = dhttemp; pos--; }else{ temp = readtemp (pin); //Serial.print ("pin="); //Serial.println(pin); } if (debug){ Serial.print("pos="); Serial.print(pos); Serial.print(" "); Serial.print("pin="); Serial.print(pin); Serial.print(" "); } fct(String("temp=")+String(temp)); } break; case 'V': { float volts = readvolt (pin); fct(String("volt=")+String(volts)); } break; case 'L': { float lum = readlum (pin); fct(String("lum=")+String(lum)); } break; case 'H': { float humid = readhumid (pin,dhttemp); dhttemp_filled = true; fct(String("humid=")+String(humid)); pos--; // Annule le pos++ plus loin parce que le DHT n'utilise pas d'entrée analogue //Serial.print ("dhttemp="); //Serial.println(dhttemp); } break; case '_': fct(String("skip")); break; default: fct(String("Invalid letter: ")+String(*pt)); } pt++; pos++; } dofin=true; debug = false; }else if (strncmp(line,"pin",3)==0 && isdigit(line[3]) && (line[4] == '0' || line[4] == '1')){ int pin = tbrelays[line[3]-'0']; pinMode(pin,OUTPUT); digitalWrite(pin,line[4]=='0' ? LOW : HIGH); fct(String("pin=")+line[3]+" -> "+String(pin)+" "+String(line[4]=='1')); dofin=true; }else if (strncmp(line,"scan",4)==0){ #ifdef USE_WIFI scannetwork(); #else fct(String("WIFI non compilé")); #endif dofin=true; }else if (strncmp(line,"connect",7)==0){ #ifdef USE_WIFI wificonnect(true); #else fct(String("WIFI non compilé")); #endif dofin=true; }else if (strncmp(line,"reconnect",9)==0){ fct (String("Sequence de reconnexion")); WiFi.disconnect(); fct (String("Status=")+String(WiFi.status())); status.last = micros(); status.state = idle; dofin = true; }else if (strncmp(line,"disconnect",10)==0){ fct(String("Deconnexion WIFI")); WiFi.disconnect(); dofin = true; }else if (strncmp(line,"status",6)==0){ if (status.last == 0){ fct(String("WIFI pas en attente")); }else{ float diff = (4000000-(micros()-status.last))/1000000.0; fct(String("Prochain essai WIFI dans ")+String(diff)); } fct(String("connected=")+String(status.state)); fct(String("Nombre d'essai=")+String(status.wifitry)); #ifdef USE_WIFI static const char *tbt[]={"IDLE","NO_SSID_AVAIL","SCAN_COMPLETED", "CONNECTED","CONNECT_FAILED","CONNECTION_LOST","DISCONNECTED" }; fct(String("WiFi.status()=")+String(tbt[WiFi.status()])); int ip = ntohl(WiFi.localIP()); char tmp[20]; snprintf (tmp,sizeof(tmp)-1,"%d.%d.%d.%d",(ip&0xff000000)>>24,(ip&0xff0000)>>16,(ip&0xff00)>>8,ip&0xff); fct(String("WiFi.localIP()=")+String(tmp)); #endif dofin=true; }else if (strncmp(line,"ledon",5)==0){ digitalWrite(LED,HIGH); fct(String("LED ON")); dofin=true; }else if (strncmp(line,"ledoff",6)==0){ digitalWrite(LED,LOW); fct(String("LED OFF")); dofin=true; #ifdef USE_BLUETOOTH }else if (strncmp(line,"blue",4)==0){ SerialBT.println("allo"); #endif }else{ fct(String("Commande invalide")); } if (dofin) fct(String("fin")); } template void doserial(T &t, struct WIFISTATUS &status, String &line) { // read the incoming byte: char byte = t.read(); if (byte == '\n' || byte == '\r'){ parsecommand (line.c_str(),status,[&t](const String &line) { t.println(line); }); line.clear(); }else if (line.length() < 20){ line += byte; } } void loop() { for (int i=0; i<4; i++){ int pin = tbpins[i]; float &volt = tbvolts[pin]; volt = (volt*9+readvolt_real(pin))/10; } //static unsigned long last=0; //static unsigned wifitry=0; //static bool connected = false; #ifdef USE_WIFI { auto wstatus = WiFi.status(); if (wstatus == WL_CONNECTED){ if (status.state == connecting){ status.state = connected; wserver.begin(); } }else if (wstatus == WL_CONNECT_FAILED){ status.state = idle; status.last = micros(); }else if(status.state == connected){ if (wstatus == WL_CONNECTION_LOST || wstatus == WL_DISCONNECTED){ status.state = idle; status.last = micros(); } }else if (status.last == 0){ if (status.state == idle){ status.last = micros(); }else if (wstatus == WL_IDLE_STATUS){ }else if (wstatus == WL_NO_SSID_AVAIL){ status.last = micros(); }else if (wstatus == WL_SCAN_COMPLETED){ } }else if (status.last != 0 && status.last + 4000000 > micros()){ status.last = 0; status.wifitry++; wificonnect(false); } } WiFiClient client = wserver.available(); if (client) { while (client.connected()) { String line; while (client.available()>0) { char c = client.read(); if (c == '\n'){ parsecommand (line.c_str(),status,[&client](const String &line) { client.write(line.c_str()); client.write("\n"); }); line.clear(); }else if (c != '\r'){ line += c; } } delay(10); } client.stop(); Serial.println("Client disconnected"); } #endif #ifdef USE_BLUETOOTH if (SerialBT.available() > 0) { static String line; doserial(SerialBT,status,line); } #endif if (Serial.available() > 0) { static String line; doserial(Serial,status,line); } }