Chat freely about anything...

User avatar
By tytower
#17430 EDIT - I think this all relates to Arduino IDE 1.6.1. (Or I have been fed a load of crap) In the newer 1.6.4 version it seems the pins follow the board numbers
GPIO2 is pin 2 in the code for sure I'm using it now for a DHT sensor on one
Last edited by tytower on Mon Jul 20, 2015 3:46 pm, edited 9 times in total.
User avatar
By tytower
#17449 This is where I am at present to post data to data.sparkfun.com . Its disjointed and messy but does send reading from DHT22 Sensor to the server . It can be used with just the DHT sensor attached
-Help appreciated

Code: Select all#include <SPI.h> // Required to use BMP180
#include <DHT.h>
#include <stdio.h>
#define DHT_PIN 2 //DHT dht;//DHT22 for my sensorRHT03-others in library
DHT dht(DHT_PIN,DHT22,15);
#define TIMEOUT 3000 //3 second timout
#define SPARKFUN_UPDATE_TIME 60000         //Update SparkFun data server every 60000 ms (1 minute).

unsigned long timer1 = 0;
unsigned long timer2 = 0;
char server[] = "data.sparkfun.com";    // name address for data.sparkFun (using DNS)
// Set the static IP address to use if the DHCP fails to assign

const String publicKey = "RMzp9ANyqzfjw4drzNnZ";
const String privateKey = "-------------Your key----------------";
const byte NUM_FIELDS = 4;
const String fieldNames[NUM_FIELDS] = {"humidity" , "pressure2" , "temp" , "temp2"};
String fieldData[NUM_FIELDS];

//Character array to hold results
char results[4];
//const int requestInterval = 20000;
long lastAttemptTime = 0;
float humidity=0;
float temp=0;
long pressure2 = 0;
float temp2;
 // pins used for the connection with the sensor
// the others you need are controlled by the SPI library):
const int dataReadyPin = 3; //Gotta check these BMP180
const int chipSelectPin = 4;//Gotta check these BMP180
double T,P,p0,a;
//float temp2 = 0.0;

long lastReadingTime = 0;

#include <SFE_BMP180.h>
#include <Wire.h>
#include <ESP8266WiFi.h>
// You will need to create an SFE_BMP180 object, here called "pressure":
WiFiClient client;
SFE_BMP180 pressure;
#define ALTITUDE 750.0 // Altitude of My HQ in  meters

void setup()
{ Serial.begin(115200);

const char* ssid = "ME-HOME";
const char* password = "";

// Create an instance of the server
// specify the port to listen on as an argument
WiFiServer server(80);

  Serial.begin(115200);
  delay(10);

  // Connect to WiFi network
  Serial.println();
  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");
 
  // Start the server
  server.begin();
  Serial.println("Server started");

  // Print the IP address
  Serial.println(WiFi.localIP());

   Serial.println("all going");

  // Initialize the sensor (it is important to get calibration values stored on the device).

  if (pressure.begin())
    Serial.println("BMP180 init success");
  else
  {
    // Oops, something went wrong, this is usually a connection problem,
    // see the comments at the top of this sketch for the proper connections.

    Serial.println("BMP180 init fail\n\n");
    //while(1); // Pause forever.Action blocked as mine not working atm
  }
 
 //Setup pressure and temp2
 //Sensor's memory register addresses:
const int PRESSURE = 0x1F;      //3 most significant bits of pressure
const int PRESSURE_LSB = 0x20;  //16 least significant bits of pressure
const int TEMPERATURE = 0x21;   //16 bit temperature reading

  //Configure SCP1000 for low noise configuration:
  writeRegister(0x02, 0x2D);
  writeRegister(0x01, 0x03);
  writeRegister(0x03, 0x02);
 // give the sensor and Ethernet shield time to set up:
  delay(1000);

  //Set the sensor to high resolution mode tp start readings:
  writeRegister(0x03, 0x0A);
}

void loop()
{
char status;  // Loop here getting pressure readings every 10 seconds.
  status = pressure.startTemperature();
  if (status != 0)
  {
    // Wait for the measurement to complete:
    delay(status);

    // Retrieve the completed temperature measurement:
    // Note that the measurement is stored in the variable T.
    // Function returns 1 if successful, 0 if failure.

    status = pressure.getTemperature(T);
    if (status != 0)
    {
      status = pressure.startPressure(3);
      if (status != 0)
      {
        // Wait for the measurement to complete:
        delay(status);

        // Retrieve the completed pressure measurement:
        // Note that the measurement is stored in the variable P.
        // Note also that the function requires the previous temperature measurement (T).
        // (If temperature is stable, you can do one temperature measurement for a number of pressure measurements.)
        // Function returns 1 if successful, 0 if failure.

        status = pressure.getPressure(P,T);
        if (status != 0)
        {
          // Print out the measurement:
         // Serial.print("absolute pressure: ");
          //Serial.print(P,2);
         
          fieldData[1]=String(P,2);
         
          fieldData[3]=String(T,2);
         
        }
        else Serial.println("error retrieving BMP pressure measurement\n");
      }
      else Serial.println("error starting BMP pressure measurement\n");
    }
    else Serial.println("error retrieving BMP temperature measurement\n");
  }
  else Serial.println("error starting BMP temperature measurement\n");

  delay(25000);  // Pause for 5 seconds
 
  //Update sparkfun data server every 60 seconds.
     // Convert each value to a string for our request to App Engine
  delay(200);
  humidity = dht.readHumidity();
  temp = dht.readTemperature();  //temp = (temp*9/5) + 32 - 1; //To Farenheight calibrate here
 
  String temp_string = dtostrf(temp*100, 4, 0, results);
  String humidity_string = dtostrf(humidity*100, 4, 0, results);
  delay(200);
  fieldData[0]=String(humidity);
  fieldData[2]=String(temp);
 
  //get pressure and temp2
  if (millis() - lastReadingTime > 1000){
    // if there's a reading ready, read it:
    // don't do anything until the data ready pin is high:
    if (digitalRead(dataReadyPin) == HIGH) {
      getData();
      // timestamp the last time you got a reading:
      lastReadingTime = millis();
      delay(200);
    }
 
 
  if(millis() > timer1 + SPARKFUN_UPDATE_TIME)
  {
    timer1 = millis(); //Update timer1 with the current time in miliseconds since startup.
    delay(500);
    Serial.println("Posting by timer !");
    postData(); //Send data to sparkfun data server.
    Serial.println("Just after timer1\n");
   Serial.println(fieldData[0] );
   Serial.println(fieldData[1] );
   Serial.println(fieldData[2] );
   Serial.println(fieldData[3] );
    delay(500);
  }
  }
}

void postData()
{
  
  // Make a TCP connection to remote host
  if (client.connect(server, 80))
             {
    // Post the data! Request should look a little something like:
    // GET /input/publicKey?private_key=privateKey&light=1024&switch=0&name=Jim HTTP/1.1\n
    // Host: data.sparkfun.com\n
    // Connection: close\n
    // \n
    client.print("GET /input/");
    client.print(publicKey);
    client.print("?private_key=");
    client.print(privateKey);
    for (int i=0; i<NUM_FIELDS; i++)
              {
      client.print("&");
      client.print(fieldNames[i]);
      client.print("=");
      client.print(fieldData[i]);
               }
    client.println(" HTTP/1.1");
    client.print("Host: ");
    client.println(server);
    client.println("Connection: close");
    client.println();
  //Serial.println( fieldNames[],fieldData[]); 
    
            }
  else
        {
    Serial.println(F("Connection failed"));
         } 
  while (client.connected())
  {
    if ( client.available() )
    {
      char c = client.read();
      Serial.print(c);
    }      
  }
  Serial.println();
  client.stop();
}
[code]

void getData() {
  Serial.println("Getting reading");
  //Read the temperature data
  int tempData = readRegister(0x21, 2);

  // convert the temperature to celsius and display it:
  temp2 = (float)tempData / 20.0;

  //Read the pressure data highest 3 bits:
  byte  pressureDataHigh = readRegister(0x1F, 1);   
  pressureDataHigh &= 0b00000111; //you only needs bits 2 to 0

  //Read the pressure data lower 16 bits:
  unsigned int pressureDataLow = readRegister(0x20, 2);   
  //combine the two parts into one 19-bit number:
  pressure2 = ((pressureDataHigh << 16) | pressureDataLow)/4;
  
  Serial.print("Temperature: ");
  Serial.print(temp2);
  Serial.println(" degrees C");
  Serial.print("Pressure: " + String(pressure2));
  Serial.println(" Pa");
}
//Send a write command to SCP1000
void writeRegister(byte registerName, byte registerValue) {
  // SCP1000 expects the register name in the upper 6 bits
  // of the byte:
  registerName <<= 2;
  // command (read or write) goes in the lower two bits:
  registerName |= 0b00000010; //Write command

  // take the chip select low to select the device:
  digitalWrite(chipSelectPin, LOW);

  SPI.transfer(registerName); //Send register location
  SPI.transfer(registerValue); //Send value to record into register

  // take the chip select high to de-select:
  digitalWrite(chipSelectPin, HIGH);
}


//Read register from the SCP1000:
unsigned int readRegister(byte registerName, int numBytes) {
  byte inByte = 0;           // incoming from  the SPI read
  unsigned int result = 0;   // result to return

  // SCP1000 expects the register name in the upper 6 bits
  // of the byte:
  registerName <<=  2;
  // command (read or write) goes in the lower two bits:
  registerName &= 0b11111100; //Read command

  // take the chip select low to select the device:
  digitalWrite(chipSelectPin, LOW);
  // send the device the register you want to read:
  int command = SPI.transfer(registerName);
  // send a value of 0 to read the first byte returned:
  inByte = SPI.transfer(0x00);
  
  result = inByte;
  // if there's more than one byte returned,
  // shift the first byte then get the second byte:
  if (numBytes > 1){
    result = inByte << 8;
    inByte = SPI.transfer(0x00);
    result = result |inByte;
  }
  // take the chip select high to de-select:
  digitalWrite(chipSelectPin, HIGH);
  // return the result:
  return(result);
Last edited by tytower on Mon May 18, 2015 1:52 pm, edited 5 times in total.
User avatar
By tytower
#17519 I am not out of the woods yet.
I find that the DHT22 is being read now but what it produces is "nan" when it comes back .
I suspect this might be some sort of timing issue with the device library as the ESP8622 is not running at the same speed as the ATMega328P
If you can help with that please I would appreciate it