Advertisement
Guest User

ESP Temperaturesensor

a guest
Feb 3rd, 2016
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.33 KB | None | 0 0
  1. /*
  2.  * RevSpace SensorNode
  3.  *
  4.  * 1wire Temp sensor on an ESP8266 connected to GPIO 5 (don't forget pullup 4.7k!)
  5.  *
  6.  *
  7.  */
  8. #include <EEPROM.h>
  9. #include <OneWire.h>
  10. #include <ESP8266WiFi.h>
  11. #include <WiFiUdp.h>
  12. #include <WiFiClient.h>
  13.  
  14. #include <PubSubClient.h>
  15.  
  16. // WiFi settings
  17. const char ssid[] = "revspace-pub-2.4ghz";  //  your network SSID (name)
  18. const char pass[] = "";       // your network password
  19. const char* mqtt_server = "mosquitto.space.revspace.nl";
  20.  
  21. WiFiClient espClient;
  22. PubSubClient client(mqtt_server, 1883, onMqttMessage, espClient);
  23.  
  24. OneWire  ds(5);  // on pin 10 (a 4.7K resistor is necessary)
  25.  
  26. long lastMsg = 0;
  27. char msg[50];
  28. int value = 0;
  29. long lastReconnectAttempt = 0;
  30. float celsius, fahrenheit;
  31. String adres1wire;
  32.  
  33. void setup() {
  34.   // put your setup code here, to run once:
  35.   Serial.begin(115200);
  36.   Serial.println();
  37.  
  38.   // We start by connecting to a WiFi network
  39.   Serial.print("Connecting to ");
  40.  
  41.   Serial.println(ssid);
  42.   WiFi.begin(ssid, pass);
  43.  
  44.   while (WiFi.status() != WL_CONNECTED) {
  45.     delay(50);
  46.     Serial.print(".");
  47.   }
  48.   Serial.println("");
  49.  
  50.   Serial.print("WiFi connected to: ");
  51.   Serial.println(ssid);
  52.  
  53.   Serial.print("IP address: ");
  54.   Serial.println(WiFi.localIP());
  55.   Serial.println();
  56.   temperatuur();
  57. }
  58.  
  59. void loop() {
  60.  
  61.   // put your main code here, to run repeatedly:
  62.   if (!client.connected()) {
  63.     Serial.println(".");
  64.     long verstreken = millis();
  65.     if (verstreken - lastReconnectAttempt > 5000) {
  66.       lastReconnectAttempt = verstreken;
  67.       // Attempt to reconnect
  68.       if (reconnect()) {
  69.         lastReconnectAttempt = 0;
  70.       }
  71.     }
  72.   } else {
  73.     // Client connected
  74.     client.loop();
  75.   }
  76.  
  77.   temperatuur();
  78.  
  79.   delay(20000);
  80.  
  81. }
  82.  
  83. boolean reconnect() {
  84.   if (client.connect("sensor01")) {
  85.     Serial.println("Reconnected to MQTT");
  86.     // Once connected, publish an announcement...
  87.     client.publish("revspace/sensorgrid", "hello world!");
  88.     client.loop();
  89.   }
  90.   return client.connected();
  91. }
  92.  
  93. void onMqttMessage(char* topic, byte* payload, unsigned int length) {
  94.  
  95. }
  96.  
  97. void temperatuur() {
  98.   byte i;
  99.   byte present = 0;
  100.   byte type_s;
  101.   byte data[12];
  102.   byte addr[8];
  103.   adres1wire = "";
  104.   if ( !ds.search(addr)) {
  105.     Serial.println("No more addresses.");
  106.     Serial.println();
  107.     ds.reset_search();
  108.     delay(250);
  109.     return;
  110.   }
  111.  
  112.   Serial.print("ROM =");
  113.   for ( i = 0; i < 8; i++) {
  114.     Serial.write(' ');
  115.     Serial.print(addr[i], HEX);
  116.   }
  117.   for ( i = 0; i < 8; i++) {
  118.     if (addr[i] < 16) {
  119.       adres1wire += "0";
  120.     }
  121.     adres1wire += String(addr[i], HEX);
  122.   }
  123.   Serial.println();
  124.  
  125.   if (OneWire::crc8(addr, 7) != addr[7]) {
  126.     Serial.println("CRC is not valid!");
  127.     return;
  128.   }
  129.   Serial.println();
  130.  
  131.   // the first ROM byte indicates which chip
  132.   switch (addr[0]) {
  133.     case 0x10:
  134.       Serial.println("  Chip = DS18S20");  // or old DS1820
  135.       type_s = 1;
  136.       break;
  137.     case 0x28:
  138.       Serial.println("  Chip = DS18B20");
  139.       type_s = 0;
  140.       break;
  141.     case 0x22:
  142.       Serial.println("  Chip = DS1822");
  143.       type_s = 0;
  144.       break;
  145.     default:
  146.       Serial.println("Device is not a DS18x20 family device.");
  147.       return;
  148.   }
  149.  
  150.   ds.reset();
  151.   ds.select(addr);
  152.   ds.write(0x44, 1);        // start conversion, with parasite power on at the end
  153.  
  154.   delay(1000);     // maybe 750ms is enough, maybe not
  155.   // we might do a ds.depower() here, but the reset will take care of it.
  156.  
  157.   present = ds.reset();
  158.   ds.select(addr);
  159.   ds.write(0xBE);         // Read Scratchpad
  160.  
  161.   Serial.print("  Data = ");
  162.   Serial.print(present, HEX);
  163.   Serial.print(" ");
  164.   for ( i = 0; i < 9; i++) {           // we need 9 bytes
  165.     data[i] = ds.read();
  166.     Serial.print(data[i], HEX);
  167.     Serial.print(" ");
  168.   }
  169.   Serial.print(" CRC=");
  170.   Serial.print(OneWire::crc8(data, 8), HEX);
  171.   Serial.println();
  172.  
  173.   // Convert the data to actual temperature
  174.   // because the result is a 16 bit signed integer, it should
  175.   // be stored to an "int16_t" type, which is always 16 bits
  176.   // even when compiled on a 32 bit processor.
  177.   int16_t raw = (data[1] << 8) | data[0];
  178.   if (type_s) {
  179.     raw = raw << 3; // 9 bit resolution default
  180.     if (data[7] == 0x10) {
  181.       // "count remain" gives full 12 bit resolution
  182.       raw = (raw & 0xFFF0) + 12 - data[6];
  183.     }
  184.   } else {
  185.     byte cfg = (data[4] & 0x60);
  186.     // at lower res, the low bits are undefined, so let's zero them
  187.     if (cfg == 0x00) raw = raw & ~7;  // 9 bit resolution, 93.75 ms
  188.     else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
  189.     else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
  190.     //// default is 12 bit resolution, 750 ms conversion time
  191.   }
  192.   celsius = (float)raw / 16.0;
  193.   Serial.print("  Temperature = ");
  194.   Serial.print(celsius);
  195.   Serial.println("°C");
  196.  
  197.   char netter[42];
  198.   String publishadress = "revspace/sensors/esptemp/" + adres1wire;
  199.   Serial.println();
  200.   Serial.print("Publishing to ");
  201.   Serial.println(publishadress);
  202.   publishadress.toCharArray(netter, 42);
  203.  
  204.   char temperatuur[10];
  205.   String temperatuurkladje;
  206.   temperatuurkladje = celsius;
  207.   temperatuurkladje += " °C";
  208.   temperatuurkladje.toCharArray(temperatuur, 10);
  209.   client.loop();
  210.   if (!client.connected()) {
  211.     reconnect();
  212.   }
  213.   client.publish(netter, temperatuur, 1);
  214.  
  215.   client.loop();
  216.   delay(500);
  217. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement