Close

ESP8266/arduino-DDNS-externá IP adresa,teplomer,thingspeak

Úvodní stránka Fórum Vaše projekty Arduino ESP8266/arduino-DDNS-externá IP adresa,teplomer,thingspeak

  • Toto téma obsahuje celkem 1 odpověď. Do diskuze (1 diskutující) se naposledy zapojil uživatel marh a poslední změna proběhla před 7 roky a 6 měsíci.
Aktuálně jsou na stránce zobrazeny 2 příspěvky - 1. až 2. (celkem z 2)
  • Autor
    Příspěvky
  • #11731
    marh
    Účastník

    DDNS-ext.IP adresa/TEPLOMER
    Upload teploty/externej IP adresy/testovacej správy a čítanie jedného polia na/z THINGSPEAK
    Pre ESP8266
    Kód bude fungovať aj s ARDUINO ale treba upraviť časť kódu pre sieť,,ESP8266 používa WIFI

    čo to robí:
    1. snímanie dvoch teplôt pomocou DS18B20
    2. zistenie ext.IP adresy zo servera api.ipify.org
    posielanie IP adresy na 4 časti –alebo sa to dá posielať do Status
    je to dobré ak router/kamera a pod. neumožňuje zisťovanie ext.IP adresy=DDNS a vy sa
    potrebujete dostať do VPN/domácej siete/satelitu/kamery ……
    niektoré DDNS servery sú platené a musíte si ich zaplatiť aby vám fungovalo DDNS
    DDNS = ak sa zmení vaša ext.IP tak ESP8266/ARDUINO vám to zisti a vy sa môžete pripojiť ,len si zmenite nastavenie pre stream kamery/vpn …kdekoľvek ste
    3. server THINGSPEAK umožňuje príjem správ v max. dĺžke 255 znakov,,,STATUS musíte povoliť
    dal som tam príklad na jednu testovaciu správu
    na server sa odosiela rovnaký text ale posúva sa smerom dole,,,,
    miesto správy môžete posielať teplotu/IP ADRESU/…….lebo do poli sa môžu zapisovať len čísla
    4. čítanie jedného polia 8 – jej poslednej hodnoty,,,napr . Posledné číslo IP adresy

    zmente si hodnoty v kóde: text je úvodzovkách
    1.
    String apiKey = „číslo api kľúča na Thingspeak“;

    2.
    const char* ssid = „meno wifi vášho routra“;
    3.
    const char* password = „heslo do wifi siete“;

    ———-
    Popisky som dal do angličtiny

    Kód bude fungovať aj s ARDUINO ale treba upraviť časť kódu pre sieť,,ESP8266 používa WIFI
    ESP8266 E12 nastavenie v ARDUINO programe ( nie lua code):
    inštalujte si ESP8266 do ARDUINO programu LINUX/PC
    // boards
    // board: GENERIC ESP8266 MODULE or ADAFRUIT HUZZAH ESP8266 or for NODEMCU
    // settings QIO 40MHz SERIAL 80MHz 4M ck 115200 com? AVRSIPmkll

    iné poznámky pre Vás:
    // DS18B20 plug to pin GPIO12 ESP8266-12 = D6(MISO).
    Pridajte si svoj kód alebo si upravte čo nepotrebujete.
    Môžete si zmeniť rozlíšenie merania DS18B20.
    Môžete zakomentovať funkcie, ktoré nepotrebujete ,, sériový port, meranie teploty, čítanie z Thingspeak….
    Zmeňte si čas uploadovania.
    Môžete povoliť hlboký spánok na ESP8266 ale spojte pin GPIO16=D0 s RST ( ESP.deepSleep(100e6); )

    píšte komentáre/pripomienky a otázky ohľadom kódu/funkcii
    kód môžete šíriť 🙂
    ak treba tak na pranie môžem niečo zmeniť ak niekto tomu veľmi nerozumie

    kód:

    
    
    // ARDUINO IDE 1.6.7,,,scratch mDNS_Web_Server
    //for ESP8266,,,,,If arduino - you change code for WIFI and ...
    //functions:
    // temperature on thingspeak
    // find out public ip
    // extract ip or mac address on parts
    // sending external IP address on Thingspeak to four fields
    // sending test msg on THINGSPEAK Status
    // read from thingspeak-- no complet=example // read one field
    // deep sleeping for ESP8226 - if you enable
    //source:
    //https://stackoverflow.com/questions/35227449/convert-ip-or-mac-address-from-string-to-byte-array-arduino-or-c
    //https://www.ipify.org
    //https://github.com/mathworks/thingspeak-arduino
    //https://uk.mathworks.com/help/thingspeak/get-a-channel-feed.html
    //
    
    //setting for ARDUINO ide
    // boards 
    // board:   GENERIC ESP8266 MODULE or ADAFUIT HUZZAH ESP8266 or for NODEMCU
    // settings QIO    40MHz SERIAL 80MHz 4M ck 115200 com?  AVRSIPmkll
    
    //sleeping for ESP8266 E12 : it is on end of LOOP
    //ESP.deepSleep(100e6); // 20e6 is 20 seconds,connect pin  GPIO16=D0 on RST
    
    /*
      ESP8266 mDNS responder sample
    
      This is an example of an HTTP server that is accessible
      via  URL thanks to mDNS responder.
    
      Instructions:
      - Update WiFi SSID and password as necessary.
      - Flash the sketch to the ESP8266 board
      - Install host software:
        - For Linux, install Avahi (http://avahi.org/).
        - For Windows, install Bonjour (http://www.apple.com/support/bonjour/).
        - For Mac OSX and iOS support is built in through Bonjour already.
      - Point your browser to , you should see a response.
    
     */
    
    #include <ESP8266WebServer.h>
    #include <ESP8266WiFi.h>
    #include <ESP8266mDNS.h>
    #include <WiFiClient.h>
    
    #include <OneWire.h> 
    #include <DallasTemperature.h>
    //define Data Types
    byte bytes[4];
    String ddd = "111.111.111.111"; //after power on=reset only
    String line = "";
    
    // replace with your channel’s thingspeak API key,
    String apiKey = "číslo api kľúča na Thingspeak";
    
    // replace with your NAME=SSID and PASSWORD for router,,with " "
    const char* ssid = "meno wifi vášho routra";
    const char* password = "heslo do wifi siete";
    
    //server THINGSPEAK
    const char* server = "api.thingspeak.com";
    
    // DS18B20 plug to pin GPIO12 ESP8266-12 = D6(MISO)
    #define ONE_WIRE_BUS 2
    // define resolution for sensors 9bit/0.5  10bit/0.25  11bit/0.125  12bit/0.0625 
    #define TEMPERATURE_PRECISION 11
    
    // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
    OneWire oneWire(ONE_WIRE_BUS);
    
    // Pass our oneWire reference to Dallas Temperature. 
    DallasTemperature sensors(&oneWire);
    
    // arrays to hold device addresses
    DeviceAddress insideThermometer, outsideThermometer;
    
    float teplota;
    String termOUT;
    String termIN;
    
    WiFiClient client;
    
    void setup() {
      Serial.begin(115200);
      delay(10);
      WiFi.begin(ssid, password);
      Serial.println("");
      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");
    
      Serial.println("");
      Serial.print("Connected to ");
      Serial.println(ssid);
      Serial.print("IP address: ");
      Serial.println(WiFi.localIP());
    
      // Start up the library
      sensors.begin();
      
      // locate devices on the bus
      Serial.print("Found ");
      Serial.print(sensors.getDeviceCount(), DEC);
      Serial.println(" devices.");
      // report parasite power requirements
      Serial.print("Parasite power is: "); 
      if (sensors.isParasitePowerMode()) Serial.println("ON");
      else Serial.println("OFF");
      // search for devices on the bus and assign based on an index.
      if (!sensors.getAddress(insideThermometer, 0)) Serial.println("Unable to find address for Device 0"); 
      if (!sensors.getAddress(outsideThermometer, 1)) Serial.println("Unable to find address for Device 1"); 
    
      // set the resolution to 9 bit
      sensors.setResolution(insideThermometer, TEMPERATURE_PRECISION);
      sensors.setResolution(outsideThermometer, TEMPERATURE_PRECISION);
    
      Serial.print("Device 0 Resolution: ");
      Serial.print(sensors.getResolution(insideThermometer), DEC); 
      Serial.println();
    
      Serial.print("Device 1 Resolution: ");
      Serial.print(sensors.getResolution(outsideThermometer), DEC); 
      Serial.println();
    }
    
    // function to print a device address
    void printAddress(DeviceAddress deviceAddress)
    {
      for (uint8_t i = 0; i < 8; i++)
      {
        if (deviceAddress[i] < 16) Serial.print("0");
        Serial.print(deviceAddress[i], HEX);
      }
    }
    
    // function to print the temperature for a device
    void printTemperature(DeviceAddress deviceAddress)
    {
      float tempC = sensors.getTempC(deviceAddress);
      Serial.print("Temp C: ");
      Serial.print(tempC);
      Serial.print(" Temp F: ");
      Serial.print(DallasTemperature::toFahrenheit(tempC));
    }
    
    void printResolution(DeviceAddress deviceAddress)
    {
      Serial.print("Resolution: ");
      Serial.print(sensors.getResolution(deviceAddress));
      Serial.println();    
    }
    
    // main function to print information about a device
    void printData(DeviceAddress deviceAddress)
    {
      Serial.print("Device Address: ");
      printAddress(deviceAddress);
      Serial.print(" ");
      printTemperature(deviceAddress);
      Serial.print(" ");
      printResolution(deviceAddress);
        Serial.println();
      
    }
    
    void loop(void)
    { 
    
      // call sensors.requestTemperatures() to issue a global temperature 
      // request to all devices on the bus
      Serial.print("Requesting temperatures...");
      sensors.requestTemperatures();
      Serial.println("DONE");
        // print the device information
          Serial.print("Teplota vnutri: ");
      printData(insideThermometer);
        Serial.print("Teplota vonku: ");
      printData(outsideThermometer);
      float tint = sensors.getTempC(insideThermometer);
      float tout = sensors.getTempC(outsideThermometer);
    
      // thingspeak needs minimum 15 sec delay between updates
     //change delay for next update
    delay(20000);
    
    // find out public ip
    // it finds out ext.IP address with html for arduino,,It should only send an address without html text,,,read more on ipify.org
    //you can use other IP check servers to find out external IP address
        if (client.connect("api.ipify.org", 80)) {
        //Serial.println("connected");
        client.println("GET /?format=txt HTTP/1.0");
        client.println("Host: api.ipify.org");
        client.println();
      } else {
        Serial.println("connection failed");
      }
      delay(50);
      int b = 1;
      while (client.connected()) {
        b = b + 1;
        line = client.readStringUntil('\n');
          // part for IP address
         Serial.println(line);
    }
    // find out public ip--end
    
    // String to char for IP
    //how it works---see sketch "extract ip or mac address on parts.txt"
    char ipstr[20];
    line.toCharArray(ipstr, 20);
    const char* str = ipstr;
    char sep = '.';
    int maxBytes = 4;
    int base = 10;
        for (int i = 0; i < maxBytes; i++) {
            bytes[i] = strtoul(str, NULL, base);  // Convert byte
            Serial.println(bytes[i]);
    
            str = strchr(str, sep);               // Find next separator
            if (str == NULL || *str == '\0') {
                break;                            // No more separators, exit
            }
            str++;                                // Point to next character after separator
        }
     // String to char for IP--end
    
    //preparing string - IP to String for Thingspeak
    
    String abc = "&field5=";
    abc += String(bytes[0]);
    abc +="&field6=";
    abc += String(bytes[1]);
    abc +="&field7=";
    abc += String(bytes[2]);
    abc +="&field8=";
    abc += String(bytes[3]);
    // if it does not find out IP,it will not send ZERO
    //CHANGE code if you want to send new IP address at a change only without minute update
    if (bytes[0] > 0 ) {
      ddd = abc;
     }
    
    client.stop();
    // find out public ip---end
    
    // data to thingspeak
    if (client.connect(server,80)) { // "184.106.153.149" or api.thingspeak.com
    String postStr = apiKey;
    postStr +="&field1=";
    postStr += String(tint);
    postStr +="&field2=";
    postStr += String(tout);
    //add IP
    postStr += ddd;
    //you can write msg on THINGSPEAK, max 255 characters,,enable status on THINGSPEAK
    postStr += "&status=";
    postStr += "I love you";
    
    postStr += "\r\n\r\n";
    
    client.print("POST /update HTTP/1.1\n");
    client.print("Host: api.thingspeak.com\n");
    client.print("Connection: close\n");
    client.print("X-THINGSPEAKAPIKEY: "+apiKey+"\n");
    client.print("Content-Type: application/x-www-form-urlencoded\n");
    client.print("Content-Length: ");
    client.print(postStr.length());
    client.print("\n\n");
    client.print(postStr);
    }
    
       Serial.println("thing text");
      client.stop(); 
    // data to thingspeak - end
    
    // read from thingspeak
    Serial.println("read TS…");
    if (client.connect(server,80)){
    client.println("GET /channels/179963/fields/8/last") ;
    client.println("Host: api.thingspeak.com");
    client.println();
      } else {
        Serial.println("connection failed");
      }
      delay(50);
      int a = 1;
      while (client.connected()) {
        a = a + 1;
        String thinga = client.readStringUntil('\n');
        
          Serial.println(thinga);
    }
    client.stop();
    Serial.println("stop connection…");
    
    // read from thingspeak - end
    
    //sleeping  enable/disable
    // 20e6 is 20 seconds,connect pin  GPIO16=D0 on RST
    //ESP.deepSleep(100e6);
    }
    
    ***
    <strong>info o pinoch na ESP8266</strong>
    

    ————–ARDUINO IDE

    //add this to code/sketch,,,,you will remember I/O pins on ESP8266

    #define D0 16
    #define D1 5 // I2C Bus SCL (clock)
    #define D2 4 // I2C Bus SDA (data)
    #define D3 0
    #define D4 2 // Same as „LED_BUILTIN“, but inverted logic
    #define D5 14 // SPI Bus SCK (clock)
    #define D6 12 // SPI Bus MISO
    #define D7 13 // SPI Bus MOSI
    #define D8 15 // SPI Bus SS (CS)
    #define D9 3 // RX0 (Serial console)
    #define D10 1 // TX0 (Serial console)

    #11744
    marh
    Účastník

    v kóde treba vymazať:
    ***
    info o pinoch na ESP8266

    alebo otvoriť prílohu

    nemôžem upraviť príspevok

Aktuálně jsou na stránce zobrazeny 2 příspěvky - 1. až 2. (celkem z 2)
  • Pro reakci na toto téma se musíte přihlásit.