Looking for esp8266 moisture sensor sketch that works with current libraries



  • I am trying my hand at setting up my first esp8266 gateway with attached moisture sensor but continue to have compilation errors that others have experienced. The fix seemed to be to find the right mysensors libraries but I am not having any luck (tried 2.00-2.32)..

    Would love to work with a sketch that is known to work with 2.3.1-2.3.2

    Errors:

    /root/Arduino/libraries/MySensors/core/MyGatewayTransportEthernet.cpp:28:22: error: 'MY_ESP8266_SSID' was not declared in this scope
     #define MY_WIFI_SSID MY_ESP8266_SSID
                          ^
    /root/Arduino/libraries/MySensors/core/MyGatewayTransportEthernet.cpp:163:19: note: in expansion of macro 'MY_WIFI_SSID'
      (void)WiFi.begin(MY_WIFI_SSID, MY_WIFI_PASSWORD, 0, MY_WIFI_BSSID);
                       ^
    /root/Arduino/libraries/MySensors/core/MyGatewayTransportEthernet.cpp:34:26: error: 'MY_ESP8266_PASSWORD' was not declared in this scope
     #define MY_WIFI_PASSWORD MY_ESP8266_PASSWORD
    

    Sketch:

    // Enable debug prints to serial monitor
    #define MY_DEBUG
    
    // Use a bit lower baudrate for serial prints on ESP8266 than default in MyConfig.h
    #define MY_BAUD_RATE 9600
    
    // Enables and select radio type (non attached at the moment)
    //#define MY_RADIO_NRF24
    //#define MY_RADIO_RFM69
    
    #define MY_GATEWAY_ESP8266
    
    #define MY_ESP8266_SSID "****"
    #define MY_ESP8266_PASSWORD "****"
    
    // Enable UDP communication
    //#define MY_USE_UDP  // If using UDP you need to set MY_CONTROLLER_IP_ADDRESS below
    
    // Set the hostname for the WiFi Client. This is the hostname
    // it will pass to the DHCP server if not static.
    #define MY_ESP8266_HOSTNAME "test-sensor-gateway"
    
    // Enable MY_IP_ADDRESS here if you want a static ip address (no DHCP)
    //#define MY_IP_ADDRESS 192,168,178,87
    
    // If using static ip you can define Gateway and Subnet address as well
    //#define MY_IP_GATEWAY_ADDRESS 192,168,178,1
    //#define MY_IP_SUBNET_ADDRESS 255,255,255,0
    
    // The port to keep open on node server mode
    #define MY_PORT 5003
    
    // How many clients should be able to connect to this gateway (default 1)
    #define MY_GATEWAY_MAX_CLIENTS 2
    
    // Controller ip address. Enables client mode (default is "server" mode).
    // Also enable this if MY_USE_UDP is used and you want sensor data sent somewhere.
    //#define MY_CONTROLLER_IP_ADDRESS 192, 168, 178, 68
    
    // Enable inclusion mode
    //#define MY_INCLUSION_MODE_FEATURE
    
    // Enable Inclusion mode button on gateway
    //#define MY_INCLUSION_BUTTON_FEATURE
    // Set inclusion mode duration (in seconds)
    //#define MY_INCLUSION_MODE_DURATION 60
    // Digital pin used for inclusion mode button
    //#define MY_INCLUSION_MODE_BUTTON_PIN  3
    
    // Set blinking period
    //#define MY_DEFAULT_LED_BLINK_PERIOD 300
    
    // Flash leds on rx/tx/err
    // Led pins used if blinking feature is enabled above
    //#define MY_DEFAULT_ERR_LED_PIN 16  // Error led pin
    //#define MY_DEFAULT_RX_LED_PIN  16  // Receive led pin
    //#define MY_DEFAULT_TX_LED_PIN  16  // the PCB, on board LED
    
    #if defined(MY_USE_UDP)
    #include <WiFiUdp.h>
    #endif
    
    #include <ESP8266WiFi.h>
    #include <MySensors.h>
    
    #define CHILD_ID 0
    
    int AirValue = 694;
    int WaterValue = 344;
    int intervals = (AirValue - WaterValue)/3; 
    int soilMoistureValue = 0;
    
    MyMessage msg(CHILD_ID, V_LEVEL);
    unsigned long SLEEP_TIME = 30000;
    int sensorPin = A0;
      
    void setup()
    {
      // Setup locally attached sensors
    }
    
    void presentation()
    {
      // Present locally attached sensors here
      sendSketchInfo("Soil Moisture Sensor Capacitive", "1.0");
      present(CHILD_ID, S_MOISTURE);
    
    }
    
    void loop()
    {
      // Send locally attached sensors data here
      int soilMoistureValue;
      soilMoistureValue = analogRead(sensorPin);
     
    
      if(soilMoistureValue > WaterValue && soilMoistureValue < (WaterValue + intervals))
        {
          send(msg.set(100));
        
        }
        else if(soilMoistureValue > (WaterValue + intervals) && soilMoistureValue < (AirValue - intervals))
          {
            send(msg.set(50));
         
          }
        else if(soilMoistureValue < AirValue && soilMoistureValue > (AirValue - intervals))
        {
           send(msg.set(0));
            
        
         }
        
    sleep(SLEEP_TIME);
    // note-to-self: search for possible wait() alternative or deep-sleep
    }
    


  • Fixed it with the following changes: .(Still interested in finding more esp8266 sketches)

    // Enable debug prints to serial monitor
    #define MY_DEBUG
    
    // Use a bit lower baudrate for serial prints on ESP8266 than default in MyConfig.h
    #define MY_BAUD_RATE 9600
    
    // Enables and select radio type (non attached at the moment)
    //#define MY_RADIO_NRF24
    //#define MY_RADIO_RFM69
    
    #define MY_GATEWAY_ESP8266
    
    
    #define MY_WIFI_SSID ""
    #define MY_WIFI_PASSWORD ""
    
    // Enable UDP communication
    //#define MY_USE_UDP  // If using UDP you need to set MY_CONTROLLER_IP_ADDRESS below
    
    // Set the hostname for the WiFi Client. This is the hostname
    // it will pass to the DHCP server if not static.
    #define MY_HOSTNAME "test-sensor-gateway"
    
    // Enable MY_IP_ADDRESS here if you want a static ip address (no DHCP)
    //#define MY_IP_ADDRESS 192,168,178,87
    
    // If using static ip you can define Gateway and Subnet address as well
    //#define MY_IP_GATEWAY_ADDRESS 192,168,178,1
    //#define MY_IP_SUBNET_ADDRESS 255,255,255,0
    
    // The port to keep open on node server mode
    #define MY_PORT 5003
    
    // How many clients should be able to connect to this gateway (default 1)
    #define MY_GATEWAY_MAX_CLIENTS 2
    
    // Controller ip address. Enables client mode (default is "server" mode).
    // Also enable this if MY_USE_UDP is used and you want sensor data sent somewhere.
    //#define MY_CONTROLLER_IP_ADDRESS 192, 168, 178, 68
    
    // Enable inclusion mode
    //#define MY_INCLUSION_MODE_FEATURE
    
    // Enable Inclusion mode button on gateway
    //#define MY_INCLUSION_BUTTON_FEATURE
    // Set inclusion mode duration (in seconds)
    //#define MY_INCLUSION_MODE_DURATION 60
    // Digital pin used for inclusion mode button
    //#define MY_INCLUSION_MODE_BUTTON_PIN  3
    
    // Set blinking period
    //#define MY_DEFAULT_LED_BLINK_PERIOD 300
    
    // Flash leds on rx/tx/err
    // Led pins used if blinking feature is enabled above
    //#define MY_DEFAULT_ERR_LED_PIN 16  // Error led pin
    //#define MY_DEFAULT_RX_LED_PIN  16  // Receive led pin
    //#define MY_DEFAULT_TX_LED_PIN  16  // the PCB, on board LED
    
    #if defined(MY_USE_UDP)
    #include <WiFiUdp.h>
    #endif
    
    #include <ESP8266WiFi.h>
    #include <MySensors.h>
    
    #define CHILD_ID 0
    
    int AirValue = 694;
    int WaterValue = 344;
    int intervals = (AirValue - WaterValue)/3; 
    int soilMoistureValue = 0;
    
    MyMessage msg(CHILD_ID, V_LEVEL);
    unsigned long SLEEP_TIME = 30000;
    int sensorPin = A0;
      
    void setup()
    {
      // Setup locally attached sensors
    }
    
    void presentation()
    {
      // Present locally attached sensors here
      sendSketchInfo("Soil Moisture Sensor Capacitive", "1.0");
      present(CHILD_ID, S_MOISTURE);
    
    }
    
    void loop()
    {
      // Send locally attached sensors data here
      int soilMoistureValue;
      soilMoistureValue = analogRead(sensorPin);
     
    
      if(soilMoistureValue > WaterValue && soilMoistureValue < (WaterValue + intervals))
        {
          send(msg.set(100));
        
        }
        else if(soilMoistureValue > (WaterValue + intervals) && soilMoistureValue < (AirValue - intervals))
          {
            send(msg.set(50));
         
          }
        else if(soilMoistureValue < AirValue && soilMoistureValue > (AirValue - intervals))
        {
           send(msg.set(0));
            
        
         }
        
    sleep(SLEEP_TIME);
    // note-to-self: search for possible wait() alternative or deep-sleep
    }
    
    

  • Mod

    @mrhutchinsonmn esp8266 does not support sleep(). The sleep call will return immediately.

    On the other hand, sleeping a gateway will cause any connection to a controller to fail, so if sleep had worked, not much else would.

    Was your intention to sleep the esp, or did the sleep call just come along with copied code?



  • @mfalkvidd was part of the sketch. I could see right away that sleep did not work 🙂


Log in to reply
 

Suggested Topics

  • 4
  • 3
  • 9
  • 9
  • 4
  • 8

0
Online

11.4k
Users

11.1k
Topics

112.7k
Posts