หน้าเว็บ

วันอังคารที่ 28 พฤษภาคม พ.ศ. 2562

Code Arduino ESP8266 SCI Smart Farm

Code Arduino ESP8266 SCI Smart Farm
- Connect Wifi
- อ่านค่าความชื้น
- ส่งค่า Home Assistant (192.168.0.202) --> Home Assistant ทำ Auto Scrip สั่งปิดเปิด SunOff อีกที

  1. #include <ESP8266WiFi.h>
  2. #include <PubSubClient.h>
  3.  
  4. // Update these with values suitable for your network.
  5. const char* ssid = "xxxxxx";
  6. const char* password = "xxxxxx";
  7. const char* mqtt_server = "192.168.2.202";
  8. int sensorPin = A0; // select the input pin for the potentiometer
  9. int sensorValue = 0; // variable to store the value coming from the sensor
  10.  
  11. WiFiClient espClient;
  12. PubSubClient client(espClient);
  13. long lastMsg = 0;
  14. char msg[50];
  15. int value = 0;
  16. char output;
  17. unsigned long seconds = 1000L; //Notice the L
  18. unsigned long minutes = seconds * 60;
  19.  
  20. void setup() {
  21.   pinMode(BUILTIN_LED, OUTPUT);     // Initialize the BUILTIN_LED pin as an output
  22.   Serial.begin(115200);
  23.   setup_wifi();
  24.   client.setServer(mqtt_server, 1883);
  25.   client.setCallback(callback);
  26.    
  27. }
  28.  
  29. void setup_wifi() {
  30.  
  31.   delay(10);
  32.   // We start by connecting to a WiFi network
  33.   Serial.println();
  34.   Serial.print("Connecting to ");
  35.   Serial.println(ssid);
  36.  
  37.   WiFi.begin(ssid, password);
  38.  
  39.   while (WiFi.status() != WL_CONNECTED) {
  40.     delay(500);
  41.     Serial.print(".");
  42.   }
  43.  
  44.   Serial.println("");
  45.   Serial.println("WiFi connected");
  46.   Serial.println("IP address: ");
  47.   Serial.println(WiFi.localIP());
  48. }
  49.  
  50. void callback(char* topic, byte* payload, unsigned int length) {
  51.   Serial.print("Message arrived [");
  52.   Serial.print(topic);
  53.   Serial.print("] ");
  54.   for (int i = 0; i < length; i++) {
  55.     Serial.print((char)payload[i]);
  56.   }
  57.   Serial.println();
  58.  
  59.   // Switch on the LED if an 1 was received as first character
  60.   if ((char)payload[0] == '1') {
  61.     digitalWrite(BUILTIN_LED, LOW);   // Turn the LED on (Note that LOW is the voltage level
  62.     // but actually the LED is on; this is because
  63.     // it is acive low on the ESP-01)
  64.   } else {
  65.     digitalWrite(BUILTIN_LED, HIGH);  // Turn the LED off by making the voltage HIGH
  66.   }
  67.  
  68. }
  69.  
  70. void reconnect() {
  71.  
  72.   // Loop until we're reconnected
  73.   while (!client.connected()) {
  74.     Serial.print("Attempting MQTT connection...");
  75.     // Attempt to connect
  76.     //client.connect(clientName, username, password )
  77.     if (client.connect("ESP8266Client","sci", "x1x2x3")) {
  78.       Serial.println("connected");
  79.       // Once connected, publish an announcement...
  80.       client.publish("outTopic", "connected MQTT");
  81.       // ... and resubscribe
  82.       client.subscribe("inTopic");
  83.     } else {
  84.       Serial.print("failed, rc=");
  85.       Serial.print(client.state());
  86.       Serial.println(" try again in 5 seconds");
  87.       // Wait 5 seconds before retrying
  88.       delay(5000);
  89.     }
  90.   }
  91. }
  92. void loop() {
  93.  
  94.   if (!client.connected()) {
  95.     reconnect();
  96.   }
  97.   client.loop();
  98.   // 1000 = 1s
  99.   //1 hour
  100.   //delay(60UL * 60UL * 1000UL);  //60 minutes each of 60 seconds each of 1000 milliseconds all unsigned longs
  101.   //delay(20000);
  102.  
  103.   long now = millis();
  104.   if (now - lastMsg > 3000) {
  105.     lastMsg = now;
  106.     //read the value from the sensor:
  107.     sensorValue = analogRead(sensorPin);
  108.     Serial.print("sensor = " );
  109.     Serial.println(sensorValue);
  110.     sprintf (msg,"%d", sensorValue);
  111.     client.publish("garden/moisture", msg);
  112.   }  
  113.  
  114. }

ไม่มีความคิดเห็น:

แสดงความคิดเห็น