Barometer Logger – Day 3

Today’s goal was to make WiFi work.
First I tried sample code on WiFi Usage on Seeed Studio. Scanning access points and connecting to a WiFi network just worked.

Next I tried making a HTTP request to an external web server. By using HTTPClient and WiFiClient classes, it just worked.
I then created a simple PHP script for the web server which receives a request from the controller and then record it on a log file. With the help of Github Copilot, it was done within a few minutes. I also found Copilot is helpful for the Arduino programming by using Visual Studio Code instead of Arduino IDE.
I could now confirm the sensor data values on the web which were sent periodically.

I then tried to detach the controller from the Mac and just powered it over USB cable.
Strangely, the log didn’t update. Without a connection to a host pc, the board doesn’t work.
Scrutinizing the code, I found a line below in setup() function.
while ( !Serial ) delay(100);
The program stopped here because serial communication is not available.
Removing the line, the log started updating again.

So, the most of the work which I expected to struggle – board and dev env setup, sensor connection, WiFi setup – were finished like a charm, within 3 days, a few hours each.
What’s remaining is to finish the project – drawing the temperature and barometer plot to see trends and sending an alert email. These are just web stuffs and I did the similar project in gluplot so there should be no hard part.

Following is the entire code for the controller.


/***************************************************************************
  This is a library for the BMP280 humidity, temperature & pressure sensor

  Designed specifically to work with the Adafruit BMP280 Breakout
  ----> http://www.adafruit.com/products/2651

  These sensors use I2C or SPI to communicate, 2 or 4 pins are required
  to interface.

  Adafruit invests time and resources providing this open source code,
  please support Adafruit andopen-source hardware by purchasing products
  from Adafruit!

  Written by Limor Fried & Kevin Townsend for Adafruit Industries.
  BSD license, all text above must be included in any redistribution
 ***************************************************************************/
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_BMP280.h>
#include "WiFi.h"
#include <HTTPClient.h>
#include <WiFiClient.h>
#include <UrlEncode.h>

const char* ssid = "******"; // put your SSID
const char* password = "*****"; // put your password

#define BMP_SCK  (13)
#define BMP_MISO (12)
#define BMP_MOSI (11)
#define BMP_CS   (10)

Adafruit_BMP280 bmp; // I2C
//Adafruit_BMP280 bmp(BMP_CS); // hardware SPI
//Adafruit_BMP280 bmp(BMP_CS, BMP_MOSI, BMP_MISO,  BMP_SCK);

HTTPClient http;
WiFiClient client;

void setup() {
  Serial.begin(9600);
  //while ( !Serial ) delay(100);   // wait for native usb
  Serial.println(F("BMP280 test"));
  unsigned status;
  //status = bmp.begin(BMP280_ADDRESS_ALT, BMP280_CHIPID);
  status = bmp.begin();
  if (!status) {
    Serial.println(F("Could not find a valid BMP280 sensor, check wiring or "
                      "try a different address!"));
    Serial.print("SensorID was: 0x"); Serial.println(bmp.sensorID(),16);
    Serial.print("        ID of 0xFF probably means a bad address, a BMP 180 or BMP 085\n");
    Serial.print("   ID of 0x56-0x58 represents a BMP 280,\n");
    Serial.print("        ID of 0x60 represents a BME 280.\n");
    Serial.print("        ID of 0x61 represents a BME 680.\n");
    while (1) delay(10);
  }

  /* Default settings from datasheet. */
  bmp.setSampling(Adafruit_BMP280::MODE_NORMAL,     /* Operating Mode. */
                  Adafruit_BMP280::SAMPLING_X2,     /* Temp. oversampling */
                  Adafruit_BMP280::SAMPLING_X16,    /* Pressure oversampling */
                  Adafruit_BMP280::FILTER_X16,      /* Filtering. */
                  Adafruit_BMP280::STANDBY_MS_1000); /* Standby time. */

  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 loop() {

    char buf[256];
    sprintf(buf, "%.3lf, %.3lf", bmp.readTemperature(), bmp.readPressure() / 100.0);
    Serial.println(buf);
    String request = "http://****/***.php?data=" + urlEncode(String(buf)); // your server's URL
    if (!http.begin(client, request))
    {
      Serial.println("Failed to begin http.");
      delay(3000);
      return;
    }
    Serial.print("Doing Get: ");
    int httpCode = http.GET();
    Serial.printf("[HTTP] GET... code: %d\n", httpCode); 
    if (httpCode = HTTP_CODE_OK) {
      String parseMe = http.getString();
      //Serial.println(parseMe);
    }
    delay(1000 * 60 * 5); // 5 minutes cycle.
}

Leave a Reply

Your email address will not be published. Required fields are marked *