I’ve finally got round to creating a new Arduino “sketch” for my ESP8266 Wireless Temperature Sensor.
The setup is slightly different to before, so here’s a quick breakdown:
A few things to note:
The code can be found below, at the project home https://github.com/nerobot/Home-Automation
So so far, so good!
I’ve now created a small “Config.h” file to contain all the wifi and MQTT config, password, and username details. This should help keep the main file a bit neater (and lessen the chance of me giving away too much information by mistake )
Github project updated appropriately.
The setup is slightly different to before, so here’s a quick breakdown:
- I’m using a version of a nodemcu board for now. It just makes developing a bit easier as I can just plug it into my computer via usb, and not worry about serial converters.
- I’m still using the DS1631 I2C temperature. There don’t seem to be many people using this, I’m liking it (it’s easy enough to setup, has good resolution, and has good accuracy compared to some others).
- I’m using the DS1631 Arduino library available here: https://github.com/millerlp/DS1631
- I’m using MQTT to send the temperature to my MQTT broker (on a Raspberry Pi).
- I’m using the Pubsubclient Arduino library to setup MQTT on the board.
- Once a minute, the system will read the temperature from the DS1631 and send it via MQTT to the Raspberry Pi.
Circuit
As I’m looking at adding more to this circuit later on, I currently don’t have a useful circuit diagram . I will put one up soon.A few things to note:
- During programming and testing, the board is powered via USB from the PC. As the nodemcu board has a built-in regulator, this just makes things a bit easier.
- However, quiet a bit of heat is generated from the regulator, messing with the temperature sensing.
- Therefore during “normal” operation, the board is powered from 3.3v supply (delivered by a switching regulator, a good distance from the board).
- The circuit is currently VERY messy and needs improving before I create an kind of PCB.
Code
I’m currently coding this product using the Arduino IDE as I was starting to struggle with the ESP8266 SDK. Although I’m not a fan of the Arduino IDE, I have found it easier for coding, etc. so I’ll see how well it works.The code can be found below, at the project home https://github.com/nerobot/Home-Automation
I’ve been able to compile and upload the above to my nodemcu board and by subscribing to the MQTT topic in node-red, I can see the temperature (it just needs multiplying by 0.0625 to give the actual temperature)./* DS1631_MQTT_Temperature_Sensor_0.1 20 Feb 16 First full attempt at using the Arduino IDE to create a MQTT DS1631 Temperature Sensor. */ #include <ESP8266WiFi.h> #include <PubSubClient.h> #include <Ticker.h> #include <Wire.h> #include <DS1631.h> // Update these with values suitable for your network. const char* ssid = "SSID"; const char* password = "SSID_PASS"; const char* mqtt_server = "MQTT_SERVER"; const char* mqtt_username = "MQTT_USERNAME"; const char* mqtt_password = "MQTT_PASSWORD"; const char* mqtt_topic = "/esp8266/mainRoom/temperature/1"; const char* esp8266_client = "ESP8266_MR_1"; // Temperature Sensor DS1631 Temp1(0); // Ticker / Timer Ticker flipper; int ticker_delay = 60; // Wifi WiFiClient espClient; PubSubClient client(espClient); char msg[50]; 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(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); } void callback(char* topic, byte* payload, unsigned int length) { Serial.print("Message arrived ["); Serial.print(topic); Serial.print("] "); for (int i = 0; i < length; i++) { Serial.print((char)payload[i]); } Serial.println(); } void reconnect() { // Loop until we're reconnected while (!client.connected()) { Serial.print("Attempting MQTT connection..."); // Attempt to connect if (client.connect(esp8266_client, mqtt_username, mqtt_password)) { Serial.println(" MQTT connected"); } else { Serial.print("failed, rc="); Serial.print(client.state()); Serial.println(" try again in 5 seconds"); // Wait 5 seconds before retrying delay(5000); } } } void getTemp(){ Serial.println("Getting Temperature."); uint16_t temp = Temp1.readTempOneShotInt(); uint8_t Th = temp >> 8; uint8_t Tl = temp & 0xFF; if(Th>=0x80) //if sign bit is set, then temp is negative Th = Th - 256; temp = (uint16_t)((Th << 8) + Tl); temp >>= 4; float T_dec = temp * 0.0625; // Display T° on "Serial Monitor" Serial.print("Temperature : "); Serial.println(T_dec); // Sending the unconverted temperature via MQTT snprintf (msg, 75, "%d", temp); client.publish(mqtt_topic, msg); } void setup() { Serial.begin(115200); // Setting up wifi Serial.println("Setting up wifi."); setup_wifi(); client.setServer(mqtt_server, 1883); client.setCallback(callback); // Setting up timer Serial.println("Setting up timer."); flipper.attach(ticker_delay, getTemp); // Setting up DS1631 Serial.println("Setting up DS1631."); Wire.begin(2, 14); int config = Temp1.readConfig(); Serial.print("Config settings before: "); Serial.println(config, BIN); Temp1.writeConfig(13); config = Temp1.readConfig(); Serial.print("Config settings after: "); Serial.println(config, BIN); } void loop() { if (!client.connected()) { reconnect(); } client.loop(); }
So so far, so good!
Update – 21 Feb 2016
Very quick update on this project.I’ve now created a small “Config.h” file to contain all the wifi and MQTT config, password, and username details. This should help keep the main file a bit neater (and lessen the chance of me giving away too much information by mistake )
Github project updated appropriately.