EasyIoT Cloud - BH1750 light sensor

8 years 11 months ago #2815 by gregbor
Hi,
this is my first post here :)
my modified code:
iot-playground.com/2-uncategorised/68-es...oud-rest-api#program
to send data from BH1750 light sensor to EasyIoT Cloud.

I use esp8266 development board v2.

Greg
/*
 
 Code based on:
 https://github.com/DennisSc/easyIoT-ESPduino/blob/master/sketches/ds18b20.ino
 http://iot-playground.com/2-uncategorised/68-esp8266-wifi-ds18b20-temperature-sensor-easyiot-cloud-rest-api#program
 
 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 version 2 as published by the Free Software Foundation.
 */
#include <ESP8266WiFi.h>
#include <Wire.h>
#include <BH1750.h>

//AP definitions - update this
#define AP_SSID     "xxx"
#define AP_PASSWORD "xxx"

// EasyIoT Cloud definitions - change EIOT_CLOUD_INSTANCE_PARAM_ID

#define EIOT_CLOUD_INSTANCE_PARAM_ID    "xxx"
#define REPORT_INTERVAL 60 // in sec

#define EIOT_CLOUD_ADDRESS     "cloud.iot-playground.com"
#define EIOT_CLOUD_PORT        40404

BH1750 lightMeter;

uint16_t oldLux;


void setup() {
  Serial.begin(115200);
  lightMeter.begin(); 
  wifiConnect();
    
  oldLux = -1;
}

void loop() {
  uint16_t lux;
  
  do {
      lux = lightMeter.readLightLevel();
      Serial.print("Light: ");
      Serial.print(lux);
      Serial.println(" lx");
  } 
  while (lux == 0 || lux == (-127.0));
  
  if (lux != oldLux)
  {
    sendLux(lux);
    oldLux = lux;
  }
  
  int cnt = REPORT_INTERVAL;
  
  while(cnt--)
    delay(1000);
}

void wifiConnect()
{
    Serial.print("Connecting to AP");
    WiFi.begin(AP_SSID, AP_PASSWORD);
    while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.print(".");
  }
  
  Serial.println("");
  Serial.println("WiFi connected");  
}

void sendLux(uint16_t lux)
{  
   WiFiClient client;
   
   while(!client.connect(EIOT_CLOUD_ADDRESS, EIOT_CLOUD_PORT)) {
    Serial.println("connection failed");
    wifiConnect(); 
  }

  String url = "";
  // URL: "/RestApi/SetParameter/[insatnce id]/[parameter id]/[value]
  url += "/RestApi/SetParameter/"+ String(EIOT_CLOUD_INSTANCE_PARAM_ID) + "/"+String(lux); // generate EasIoT cloud update parameter URL

  Serial.print("POST data to URL: ");
  Serial.println(url);
  
  client.print(String("POST ") + url + " HTTP/1.1\r\n" +
               "Host: " + String(EIOT_CLOUD_ADDRESS) + "\r\n" + 
               "Connection: close\r\n" + 
               "Content-Length: 0\r\n" + 
               "\r\n");

  delay(100);
    while(client.available()){
    String line = client.readStringUntil('\r');
    Serial.print(line);
  }
  
  Serial.println();
  Serial.println("Connection closed");
}
The following user(s) said Thank You: EasyIoT

Please Log in or Create an account to join the conversation.

8 years 10 months ago #2823 by EasyIoT

gregbor wrote: Hi,
this is my first post here :)
my modified code:
iot-playground.com/2-uncategorised/68-es...oud-rest-api#program
to send data from BH1750 light sensor to EasyIoT Cloud.

I use esp8266 development board v2.

Greg

/*
 
 Code based on:
 https://github.com/DennisSc/easyIoT-ESPduino/blob/master/sketches/ds18b20.ino
 http://iot-playground.com/2-uncategorised/68-esp8266-wifi-ds18b20-temperature-sensor-easyiot-cloud-rest-api#program
 
 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 version 2 as published by the Free Software Foundation.
 */
#include <ESP8266WiFi.h>
#include <Wire.h>
#include <BH1750.h>

//AP definitions - update this
#define AP_SSID     "xxx"
#define AP_PASSWORD "xxx"

// EasyIoT Cloud definitions - change EIOT_CLOUD_INSTANCE_PARAM_ID

#define EIOT_CLOUD_INSTANCE_PARAM_ID    "xxx"
#define REPORT_INTERVAL 60 // in sec

#define EIOT_CLOUD_ADDRESS     "cloud.iot-playground.com"
#define EIOT_CLOUD_PORT        40404

BH1750 lightMeter;

uint16_t oldLux;


void setup() {
  Serial.begin(115200);
  lightMeter.begin(); 
  wifiConnect();
    
  oldLux = -1;
}

void loop() {
  uint16_t lux;
  
  do {
      lux = lightMeter.readLightLevel();
      Serial.print("Light: ");
      Serial.print(lux);
      Serial.println(" lx");
  } 
  while (lux == 0 || lux == (-127.0));
  
  if (lux != oldLux)
  {
    sendLux(lux);
    oldLux = lux;
  }
  
  int cnt = REPORT_INTERVAL;
  
  while(cnt--)
    delay(1000);
}

void wifiConnect()
{
    Serial.print("Connecting to AP");
    WiFi.begin(AP_SSID, AP_PASSWORD);
    while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.print(".");
  }
  
  Serial.println("");
  Serial.println("WiFi connected");  
}

void sendLux(uint16_t lux)
{  
   WiFiClient client;
   
   while(!client.connect(EIOT_CLOUD_ADDRESS, EIOT_CLOUD_PORT)) {
    Serial.println("connection failed");
    wifiConnect(); 
  }

  String url = "";
  // URL: "/RestApi/SetParameter/[insatnce id]/[parameter id]/[value]
  url += "/RestApi/SetParameter/"+ String(EIOT_CLOUD_INSTANCE_PARAM_ID) + "/"+String(lux); // generate EasIoT cloud update parameter URL

  Serial.print("POST data to URL: ");
  Serial.println(url);
  
  client.print(String("POST ") + url + " HTTP/1.1\r\n" +
               "Host: " + String(EIOT_CLOUD_ADDRESS) + "\r\n" + 
               "Connection: close\r\n" + 
               "Content-Length: 0\r\n" + 
               "\r\n");

  delay(100);
    while(client.available()){
    String line = client.readStringUntil('\r');
    Serial.print(line);
  }
  
  Serial.println();
  Serial.println("Connection closed");
}


Thank you for this post.
In near future REST API will be changed a little with much more options(like plug and play). API library will be also available to easy add new type of sensor with less lines of code.

Please Log in or Create an account to join the conversation.

8 years 6 months ago #3351 by Jansen
Hi gregbor,

How did you connect the BH1750? I don't see the pin assignments in your code.

Please Log in or Create an account to join the conversation.

8 years 6 months ago #3362 by gregbor

Please Log in or Create an account to join the conversation.

8 years 6 months ago #3363 by Jansen
Thanks, I managed to get it working. I destroyed two esp8266 in the process though. I have no idea what I did wrong. :(

Please Log in or Create an account to join the conversation.

8 years 6 months ago #3369 by NightOne
Destroying ESP8266s is sadly rather easy and it normally has to do with voltage..... more then 3.3v and you are normally rewarded with a puff of blue smoke.... sadly it seems that they are also unhappy with too little current... they like current when connecting with wifi.... so to avoid disapointment in the future its a good idea to give it healthy current at 3.3v and try smooth the current with a capacitor. I have all my ESP on 5V 1amp phone chargers dropping the voltage to 3.3v though a DC to DC convertor at 800mA....I have never had a problem since.

Please Log in or Create an account to join the conversation.

Time to create page: 0.220 seconds

Forum latest

  • No posts to display.