-
Notifications
You must be signed in to change notification settings - Fork 0
/
thingSpeak_beer_temperature.ino
150 lines (109 loc) · 4.82 KB
/
thingSpeak_beer_temperature.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/* This code have been modified for read two sensors DS18B20 and a MQ-4 1 medidor de Metano, gas natural comprimido (GNP) and writes in a
specific channel in Thingspeak
Modified by: Norberto Moreno 2019 */
/*
WriteMultipleFields
Description: Writes values to fields 1,2,3,4 and status in a single ThingSpeak update every 20 seconds.
Hardware: ESP8266 based boards
!!! IMPORTANT - Modify the secrets.h file for this project with your network connection and ThingSpeak channel details. !!!
Note:
- Requires ESP8266WiFi library and ESP8622 board add-on. See https://github.com/esp8266/Arduino for details.
- Select the target hardware from the Tools->Board menu
- This example is written for a network using WPA encryption. For WEP or WPA, change the WiFi.begin() call accordingly.
ThingSpeak ( https://www.thingspeak.com ) is an analytic IoT platform service that allows you to aggregate, visualize, and
analyze live data streams in the cloud. Visit https://www.thingspeak.com to sign up for a free account and create a channel.
Documentation for the ThingSpeak Communication Library for Arduino is in the README.md folder where the library was installed.
See https://www.mathworks.com/help/thingspeak/index.html for the full ThingSpeak documentation.
For licensing information, see the accompanying license file.
Copyright 2018, The MathWorks, Inc.
*/
#include "ThingSpeak.h"
#include "secrets.h"
#include <ESP8266WiFi.h>
// Sensor DS18B20
#include <OneWire.h>
#include <DallasTemperature.h>
#include <SoftwareSerial.h>
// DS18B20 en PIN 6 Digital
#define ONE_WIRE_BUS 4
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
//Dirección para cada sensor (Se deben sacar)
DeviceAddress sensor1 = {0x28, 0x86, 0xA4, 0x56, 0x0B, 0x00, 0x00, 0x7A}; //Prensa Cable
DeviceAddress sensor2 = {0x28, 0x92, 0xF4, 0x22, 0x0C, 0x00, 0x00, 0xB4}; //Extension de cable
float sensorData;
float ppmRead = A0;
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password
int keyIndex = 0; // your network key Index number (needed only for WEP)
WiFiClient client;
unsigned long myChannelNumber = SECRET_CH_ID;
const char * myWriteAPIKey = SECRET_WRITE_APIKEY;
int number = 0;
void setup() {
Serial.begin(115200); // Initialize serial
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client); // Initialize ThingSpeak
//Establecemos resolución de sensores
/************************************************
* Resolution Increment Time
* 9 bit 0.5 degrees C 93.75 mSec
* 10 bit 0.25 degrees C 187.5 mSec
* 11 bit 0.125 degrees C 375 mSec
* 12 bit 0.0625 degrees C 750 mSec
*************************************************/
sensors.setResolution(sensor1, 12);
sensors.setResolution(sensor2, 12);
}
// Particulas por millon gas function
/*float getPpm(void) {
float sensorData= analogRead(ppmRead);
float percentLecture = map(sensorData, 300, 1023, 0, 100);
return percentLecture;
}*/
void loop() {
// Connect or reconnect to WiFi
if(WiFi.status() != WL_CONNECTED){
Serial.print("Attempting to connect to SSID: ");
Serial.println(SECRET_SSID);
while(WiFi.status() != WL_CONNECTED){
WiFi.begin(ssid, pass); // Connect to WPA/WPA2 network. Change this line if using open or WEP network
Serial.print(".");
delay(5000);
}
Serial.println("\nConnected.");
}
//float lecturaSensor = getPpm();
// Sensor de temperatura DS18B20
sensors.requestTemperatures();
float dataTemp1 = sensors.getTempC(sensor1);
float dataTemp2 = sensors.getTempC(sensor2);
Serial.print("Sensor 1: ");
Serial.println(dataTemp1);
Serial.print("Sensor 2: ");
Serial.println(dataTemp2);
// set the fields with the values
//ThingSpeak.setField(1, lecturaSensor);
ThingSpeak.setField(2, dataTemp1);
ThingSpeak.setField(3, dataTemp2);
//ThingSpeak.setField(4, number4);
// Write to ThingSpeak. There are up to 8 fields in a channel, allowing you to store up to 8 different
// pieces of information in a channel. Here, we write to field 1.
// int x = ThingSpeak.writeField(myChannelNumber, 2, lecturaSensor, myWriteAPIKey);
int x = ThingSpeak.writeFields(myChannelNumber,myWriteAPIKey);
if(x == 200){
Serial.println("Channel update successful.");
/*Serial.print("ppm Monoxido %: ");
Serial.print(lecturaSensor,2);
Serial.println("%"); */
}
else{
Serial.println("Problem updating channel. HTTP error code " + String(x));
}
/* change the value
number++;
if(number > 99){
number = 0;
}*/
delay(20000); // Wait 20 seconds to update the channel again
}