- Posts: 5
- Thank you received: 1
/*
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");
}
Please Log in or Create an account to join the conversation.
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"); }
Please Log in or Create an account to join the conversation.
Please Log in or Create an account to join the conversation.
Please Log in or Create an account to join the conversation.
Please Log in or Create an account to join the conversation.
Please Log in or Create an account to join the conversation.