Can you tell what your problem is? Do you get errors? What do you see on the serial monitor?
Could you please post the code with code tags? https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet#code
Simple sketch to send ds18b20 data to pimatic
Can you tell what your problem is? Do you get errors? What do you see on the serial monitor?
Could you please post the code with code tags? https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet#code
Like my projects and help? Consider donating electroneum etnjwAKGPqF6omQWRmpp9u2BPyVDG9VuyRQjNJ1S8yfBdfR9qeUQ46kRy8KS2CNqbpNLRrsgmNW6F2TMzxmZgPrh6KctrkrYbm
See also https://forum.pimatic.org/topic/1179/how-to-post-code-snippets-and-alike-on-the-forum
Thanks
"It always takes longer than you expect, even when you take into account Hofstadter's Law.", Hofstadter's Law
An alternative is to use the MQTT-Protocol, which is quite easy to implement within esp!
Here’s the output from the serial monitor.
.....
WiFi connected
IP address:
192.168.2.141
Requesting temperatures...DONE
Temperature for Device 1 is: 19.00Connecting to 192.168.2.20
HTTP/1.1 401 Unauthorized
X-Powered-By: Express
WWW-Authenticate: Basic realm=Authorization Required
Content-Type: text/html; charset=utf-8
Content-Length: 12
ETag: W/"c-4G0bpw8TMen5oRPML4h9Pw"
Date: Tue, 14 Feb 2017 18:33:44 GMT
Connection: keep-alive
Unauthorized
closing connection
There is a part of the script that goes to mvegte.myqnapcloud.com
This guy stores his data on a server in the cloud?
Base from this script is
https://github.com/incmve/ESP-pimatic/blob/master/ESP-pimaticv1.ino
It must be totaly stripped, but i don’t know how.
No it doesnt go to the qnapcloud, the line client.print("PimaticHost: mvegte.myqnapcloud.com\r\n");
tells the client (pimatic) it connects to who the other side is.
But your script is quite a mess. With this script you are going to collect the DS18B20 temperature every 10 seconds?!
Why don’t you use ESPimatic, it does exactly what you want with a nice webinterface, and takes the trouble away for you: https://github.com/koffienl/ESPimatic
Try this:
// Import required libraries
#include "ESP8266WiFi.h"
#include <OneWire.h>
#include <DallasTemperature.h>
// Define pins (2 on Arduino)
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
// loop paramters
long ds18b20_sendInterval = 60000; // 1 minute
long ds18b20_lastInterval = 0;
// WiFi parameters
const char* ssid = "MyNetWork";
const char* password = "Secret123";
// pimatic parameters
const char* PimaticUser = "admin";
const char* PimaticPassword = "adminorsomething";
const char* PimaticHost = "192.168.2.20";
const int PimaticPort = 8081;
void setup() {
// Start Serial
Serial.begin(115200);
delay(10);
sensors.begin();
// We start by connecting to a 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");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void loop()
{
if (millis() - ds18b20_lastInterval > ds18b20_sendInterval || FirstBoot == 1)
{
ds18b20_lastInterval = millis();
String temp = get_ds18b20();
handle_pimatic(String(temp), PimaticTemperatureVariableName);
}
}
void handle_pimatic(String data, String variable)
{
String yourdata;
base64 encoder;
String auth = PimaticUser;
auth += ":";
auth += PimaticPassword;
if (!client.connect(PimaticHost.c_str(), PimaticPort))
{
Serial.println("connection failed");
return;
}
else
{
yourdata = "{\"type\": \"value\", \"valueOrExpression\": \"" + data + "\"}";
client.print("PATCH /api/variables/");
client.print(variable);
client.print(" HTTP/1.1\r\n");
client.print("Authorization: Basic ");
client.print(encoder.encode(auth));
client.print("\r\n");
client.print("Host: " + host + "\r\n");
client.print("Content-Type:application/json\r\n");
client.print("Content-Length: ");
client.print(yourdata.length());
client.print("\r\n\r\n");
client.print(yourdata);
}
}
float get_ds18b20()
{
float temperaturefloat;
sensors.requestTemperatures(); // Get the temperature
temperaturefloat = sensors.getTempCByIndex(0); // Get temperature in Celcius
return temperaturefloat;
}
Like my projects and help? Consider donating electroneum etnjwAKGPqF6omQWRmpp9u2BPyVDG9VuyRQjNJ1S8yfBdfR9qeUQ46kRy8KS2CNqbpNLRrsgmNW6F2TMzxmZgPrh6KctrkrYbm
After uploading this sketch to the Arduino app, the sketch won’t compile.
Th first error comes with : Firstboot is not declared.
After declaring firstboot (if i done it right) the next error comes with get_ds18b20();
Like i said, i’m not a script kiddo and now i’m stuck.
Koffienl has adapt the script so it would be less messy than mine :o)
Thanks for that, but now i don’t now it anymore to get over the errors.
Any help will be welcome.
Not to be rude, but if you don’t know how to solve these simple errors upon compiling, I really suggest to take something of the shelf like ESPimatic or espeasy.
This compiles with ESP8266 Arduino 2.3.0:
// Import required libraries
#include "ESP8266WiFi.h"
#include <OneWire.h>
#include <DallasTemperature.h>
#include "Base64.h"
// Define pins (2 on Arduino)
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
// loop paramters
long ds18b20_sendInterval = 60000; // 1 minute
long ds18b20_lastInterval = 0;
// WiFi parameters
const char* ssid = "MyNetWork";
const char* password = "Secret123";
// pimatic parameters
const char* PimaticUser = "admin";
const char* PimaticPassword = "adminorsomething";
const char* PimaticHost = "192.168.2.20";
const int PimaticPort = 8081;
WiFiClient client;
void setup() {
// Start Serial
Serial.begin(115200);
delay(10);
sensors.begin();
// We start by connecting to a 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");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void loop()
{
if (millis() - ds18b20_lastInterval > ds18b20_sendInterval )
{
ds18b20_lastInterval = millis();
float temp = get_ds18b20();
handle_pimatic(String(temp), PimaticTemperatureVariableName);
}
}
void handle_pimatic(String data, String variable)
{
String yourdata;
base64 encoder;
String auth = PimaticUser;
auth += ":";
auth += PimaticPassword;
if (!client.connect(PimaticHost, PimaticPort))
{
Serial.println("connection failed");
return;
}
else
{
yourdata = "{\"type\": \"value\", \"valueOrExpression\": \"" + data + "\"}";
client.print("PATCH /api/variables/");
client.print(variable);
client.print(" HTTP/1.1\r\n");
client.print("Authorization: Basic ");
client.print(encoder.encode(auth));
client.print("\r\n");
client.print("Host: 192.198.1.1\r\n");
client.print("Content-Type:application/json\r\n");
client.print("Content-Length: ");
client.print(yourdata.length());
client.print("\r\n\r\n");
client.print(yourdata);
}
}
float get_ds18b20()
{
float temperaturefloat;
sensors.requestTemperatures(); // Get the temperature
temperaturefloat = sensors.getTempCByIndex(0); // Get temperature in Celcius
return temperaturefloat;
}```
Like my projects and help? Consider donating electroneum etnjwAKGPqF6omQWRmpp9u2BPyVDG9VuyRQjNJ1S8yfBdfR9qeUQ46kRy8KS2CNqbpNLRrsgmNW6F2TMzxmZgPrh6KctrkrYbm
@Snarf2000 said in Simple sketch to send ds18b20 data to pimatic:
I can manage it for example with espeasy or by espimatic.
But my Wemos has to do a lot more and i would like to keep all te scripts in 1 file.
what “more” are you talking about? what else do you want to achieve?
still not sure why not using espimatic or espeasy as they support many sensors and do lots of stuff automatically
pimatic v0.9 has been released!
Support Pimatic and get some free stickers
Like us on Facebook
make it so !
I also tried tot make an ir device with selfmade code but then deepsleep stopped working and i couldnt fix it.
So i made the transit tot espimatic, which turned out to be a great option. Just try it, you wont regret it.
It has connection tot pimatic buildin, you only have tot set it up.
After a few hours i get it working like i want.
Thanks for the help.
I have been struggling wite the Base64 for a while and could’t find it first.
After switching from versions of boards and versions of Base64 i get it all done.
// Import required libraries
#include <ESP8266WiFi.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Base64.h>
// Define pins (2 on Arduino)
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
// loop paramters
long ds18b20_sendInterval = 60000; // 1 minutes
long ds18b20_lastInterval = 0;
// WiFi parameters
const char* ssid = "MyWiFiNetwork";
const char* password = "MyPass";
// pimatic parameters
const char* PimaticUser = "admin";
const char* PimaticPassword = "Secret";
const char* PimaticHost = "192.168.2.20";
const int PimaticPort = 8087;
WiFiClient client;
void setup() {
// Start Serial
Serial.begin(115200);
delay(10);
sensors.begin();
// We start by connecting to a 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");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void loop()
{
if (millis() - ds18b20_lastInterval > ds18b20_sendInterval )
{
ds18b20_lastInterval = millis();
float temp = get_ds18b20();
handle_pimatic(String(temp), "esptemp");
}
}
void handle_pimatic(String data, String variable)
{
String yourdata;
base64 encoder;
String auth = PimaticUser;
auth += ":";
auth += PimaticPassword;
if (!client.connect(PimaticHost, PimaticPort))
{
Serial.println("connection failed");
return;
}
else
{
yourdata = "{\"type\": \"value\", \"valueOrExpression\": \"" + data + "\"}";
client.print("PATCH /api/variables/");
client.print(variable);
client.print(" HTTP/1.1\r\n");
client.print("Authorization: Basic ");
client.print(encoder.encode(auth));
client.print("\r\n");
client.print("Host: 192.168.2.20\r\n");
client.print("Content-Type:application/json\r\n");
client.print("Content-Length: ");
client.print(yourdata.length());
client.print("\r\n\r\n");
client.print(yourdata);
// To see something in Serial Monitor remove the next commenting
// Serial.println(yourdata);
}
}
float get_ds18b20()
{
float temperaturefloat;
sensors.requestTemperatures(); // Get the temperature
temperaturefloat = sensors.getTempCByIndex(0); // Get temperature in Celcius
return temperaturefloat;
}