Close

Getting a basic node going

A project log for Raspberry Pi Temperature Sensor Network

The Raspberry Pi will be a server for various NodeMCU ESP8266 "nodes" which will each host a temperature sensor

quintin-balsdonQuintin Balsdon 09/02/2017 at 08:580 Comments

Today I soldered the PCB together after doing a minor code check on a bread board. The particular NodeMCU boards I had bought from eBay required me to push and hold the FLASH button, press and release the RESET button, and then flashing the firmware.

I managed to burn out 2 boards when I accidentally connected A0 and D0 in series. Don't do that.

To get the NodeMCU board going with Arduino, you need to add:

http://arduino.esp8266.com/stable/package_esp8266com_index.json

To the additional boards manager in preferences. You will also need to then go to the boards manager and download the ESP8266 components. If you can't see the port your board is connected to, download the driver from here:

https://www.silabs.com/products/development-tools/software/usb-to-uart-bridge-vcp-drivers

I had also added the OneWire library to my Arduino: http://www.arduinolibraries.info/libraries/one-wire

Then the basic code was fairly simple:

#include <OneWire.h>
#define DS18S20_Pin D8
#define BLUE_PIN D6
#define GREEN_PIN D5
#define RED_PIN D4
OneWire ds(DS18S20_Pin);
int CURRENT_COL = RED_PIN;
void setup() {
  Serial.begin(115200);
  Serial.println("");
  Serial.println("Hello ESP8266");
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(RED_PIN, OUTPUT);
  pinMode(GREEN_PIN, OUTPUT);
  pinMode(BLUE_PIN, OUTPUT);
}
float getTemp(){
 //returns the temperature in Celsius
 byte data[12];
 byte addr[8];
 if ( !ds.search(addr)) {
   //no more sensors on chain, reset search
   ds.reset_search();
   return -1001;
 }
 if ( OneWire::crc8( addr, 7) != addr[7]) {
   Serial.println("CRC is not valid!");
   return -1002;
 }
 if ( addr[0] != 0x10 && addr[0] != 0x28) {
   Serial.print("Device is not recognized");
   return -1003;
 }
 ds.reset();
 ds.select(addr);
 ds.write(0x44,1); // start conversion, with parasite power on at the end
 byte present = ds.reset();
 ds.select(addr);  
 ds.write(0xBE); // Read Scratchpad
 
 for (int i = 0; i < 9; i++) { // we need 9 bytes
  data[i] = ds.read();
 }
 
 ds.reset_search();
 
 byte MSB = data[1];
 byte LSB = data[0];
 float tempRead = ((MSB << 8) | LSB); //using two's compliment
 float TemperatureSum = tempRead / 16;
 
 return TemperatureSum;
}
void loop() {
  digitalWrite(RED_PIN, LOW);
  digitalWrite(GREEN_PIN, LOW);
  digitalWrite(BLUE_PIN, LOW);
  digitalWrite(CURRENT_COL,
  HIGH);
  
  digitalWrite(LED_BUILTIN, LOW);   
  Serial.println("LED OFF");
  delay(1000);                     
  digitalWrite(LED_BUILTIN, HIGH);
  Serial.println("LED ON");
  delay(500);  
  float temperature = getTemp();
  String temp = String(temperature);
  Serial.print("TEMPERATURE: ");
  Serial.println(temp);
  delay(250);
  if (CURRENT_COL == RED_PIN) {
    CURRENT_COL = BLUE_PIN;
  } else if (CURRENT_COL == BLUE_PIN){
    CURRENT_COL = GREEN_PIN;
  } else {
    CURRENT_COL = RED_PIN;
  }
}

All this does is print the temperature to the serial and cycles through the RED, GREEN and BLUE colours

Discussions