/* ESP8266 VANN SENSOR. 47K koblet fra D0 til A0 som pullup. Sensor er vanlig kabel der "probe" er 1cm avisolerte ledere i enden(tinnet). sender mqtt melding når detekterer vann til paho-ding-pes, som vises med lyd på http://apper.duckdns.org/ding/ sender også get request til php for å lagre til log fil http://apper.duckdns.org/ding/log.php?key=x&txt=test */ int sensorPin = A0; // select the input pin for the potentiometer int ledPin = 2; // select the pin for the LED int sensorValue = 0; // variable to store the value coming from the sensor int sensorVccPin = D0; bool senstate=false; //--- NEOPIXEL RGB STATUS LED #include #define DATA_PIN 1 // NB! esp8266 har direkte mapping her D1=1 #define NUM_LEDS 1 CRGB leds[NUM_LEDS]; // WIFI #include "credentials.h" #include #include #include ESP8266WiFiMulti wifimulti; boolean wifistate=false; //const char* ssid = ""; //const char* password = ""; // Mqtt #include const unsigned int mqttport = 1883; //const char* mqtt_server = "broker.mqtt-dashboard.com"; //const char* mqtt_server = "broker.hivemq.com"; //const char* mqtt_server = "iot.eclipse.org"; const char* mqtt_server = "test.mosquitto.org"; //MQTT USER + PASSWORD IF NEEDED. REMEMBER UNCOMMENT IN RECONNECT //#define mpttuser "" //#define mpttpass "" char publishTopic[] = "paho-ding-pes"; //publish funk tar kun char, ikke string WiFiClient espClient; PubSubClient client(espClient); void setup() { Serial.begin(9600); Serial.println("vannsensor ding alarm til http://apper.duckdns.org/ding"); // declare the ledPin as an OUTPUT: pinMode(ledPin, OUTPUT); digitalWrite(ledPin, HIGH); pinMode(sensorVccPin, OUTPUT); digitalWrite(sensorVccPin, LOW); // fastled FastLED.addLeds(leds, NUM_LEDS); FastLED.setBrightness( 200 ); //max brightnes is 255 leds[0] = CRGB::Red; //lys rosa FastLED.show(); delay(1000); //setup_wifi(); //client.setServer(mqtt_server, mqttport); //Serial.println("PubSub Mqtt paho Real Time started"); //multiwifi: wifimulti.addAP(WIFI_SSID1,WIFI_PASSWD1); //SSID1,PASSWORD1 fra cred file wifimulti.addAP(WIFI_SSID2,WIFI_PASSWD2); //SSID2,PASSWORD2 fra cred file } void loop() { // wifi connector:(testa reconnect ok, når ap faller ut) if(wifimulti.run()!=WL_CONNECTED){ //IF NO NETWORK IS CONNECTED Serial.println("venter på aksesspunkt"); wifistate=false; leds[0] = CRGB::Red; // gul når wifi tilkoblet FastLED.show(); delay(2000); return; } else if(wifimulti.run()==WL_CONNECTED && wifistate==false){ //wifi oneshot connected Serial.println(WiFi.SSID()); wifistate=true; leds[0] = CRGB::Orange; // gul når wifi tilkoblet FastLED.show(); client.setServer(mqtt_server, mqttport); // koble til mqtt server når wifi tilkoblet Serial.println("PubSub Mqtt paho Real Time started"); } // mqtt connector: if (!client.connected()) { reconnect(); } client.loop(); // read sensor digitalWrite(sensorVccPin, HIGH); // analog power on // read the value from the sensor: sensorValue = analogRead(sensorPin); digitalWrite(sensorVccPin, LOW); // analog power off Serial.println(sensorValue); // sensor oneshot trigger if (senstate==false && sensorValue < 700){ digitalWrite(ledPin, LOW); Serial.println("WATER"); senstate=true; Serial.println(" Publish ding water"); client.publish(publishTopic, "ding Vann"); sendGet("Sensor vann!"); } else if (senstate==true && sensorValue > 800){ digitalWrite(ledPin, HIGH); Serial.println("DRY"); senstate=false; Serial.println(" Publish ding dry "); client.publish(publishTopic, "ding Tørr"); sendGet("Sensor tørr"); } delay(4000); } /* void setup_wifi() { delay(10); // We start by connecting to a WiFi network Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(250); digitalWrite(ledPin, HIGH); delay(250); digitalWrite(ledPin, LOW); Serial.print("."); } leds[0] = CRGB::Orange; // oransje når wifi ok FastLED.show(); Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); digitalWrite(ledPin, HIGH); } */ void reconnect() { // Loop until we're reconnected while (!client.connected()) { Serial.print("Connecting broker "); Serial.print(mqtt_server); Serial.print("..."); //String aStupidWasteOfResource = "Pissing away resources uselessly"; //char copy[50]; //aStupidWasteOfResource.toCharArray(copy, 50); char mac[20]; WiFi.macAddress().toCharArray(mac, 20); // Attempt to connect mqtt broker //if (client.connect(mac, mpttuser, mpttpass)) { // med passord if (client.connect(mac)) { // anonym påloggingi leds[0] = CRGB::Green; // grønn når mqtt ok FastLED.show(); Serial.println("connected"); // Once connected, publish an announcement... client.publish(publishTopic, "Vannsensor tilkoblet"); sendGet("Vannsensor tilkoblet"); // ... and resubscribe // client.subscribe(subscribeTopic); } else { leds[0] = CRGB::Orange; // gul når mqtt feiler FastLED.show(); Serial.print("failed to connect client, rc="); Serial.print(client.state()); Serial.println(" try again in 5 seconds"); // Wait 5 seconds before retrying delay(5000); } } } void sendGet(char* thetext){ WiFiClient client; HTTPClient http; String newtext = urlencode(thetext); Serial.print("[HTTP] begin...\n"); if (http.begin(client, String("http://apper.duckdns.org/ding/log.php?key=x&txt=")+newtext)) { // HTTP Serial.print("[HTTP] GET...\n"); // start connection and send HTTP header int httpCode = http.GET(); // httpCode will be negative on error if (httpCode > 0) { // HTTP header has been send and Server response header has been handled Serial.printf("[HTTP] GET... code: %d\n", httpCode); // file found at server if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) { String payload = http.getString(); Serial.println(payload); } } else { Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str()); } http.end(); } else { Serial.printf("[HTTP} Unable to connect\n"); } } String urlencode(String str) { String encodedString=""; char c; char code0; char code1; char code2; for (int i =0; i < str.length(); i++){ c=str.charAt(i); if (c == ' '){ encodedString+= '+'; } else if (isalnum(c)){ encodedString+=c; } else{ code1=(c & 0xf)+'0'; if ((c & 0xf) >9){ code1=(c & 0xf) - 10 + 'A'; } c=(c>>4)&0xf; code0=c+'0'; if (c > 9){ code0=c - 10 + 'A'; } code2='\0'; encodedString+='%'; encodedString+=code0; encodedString+=code1; //encodedString+=code2; } yield(); } return encodedString; }