D
D
DYm00n2016-03-15 22:55:32
WiFi
DYm00n, 2016-03-15 22:55:32

ESP12 not sending data to Thingspeak?

The third day I'm struggling with the problem, there is a finished sketch

#include <ESP8266WiFi.h> // Импортируем библиотеку для работы с esp8266
#include <OneWire.h> //Библиотека для работы с OneWire
OneWire  ds(2);  // Инициализируем OneWire шину на 2 порте
//надо подтянуть резистором на 4,7к к плюсу !

const int pinPhoto = A0;
int raw = 0;
const char *ssid = "***"; //Имя вашей WIFI сети
const char *password = "***"; // Пароль вашей Wifi сети

const char* host = "184.106.153.149";//Ip Сервера TS
const int httpPort = 80; //Порт сервера
const char* privateKey = "мой ключ";//API KEY TS

byte addr[8];
float temperature; //Переменная с температурой

float getTemp() {
  byte data[12];

  if (!ds.search(addr)) {
    Serial.println("No more addresses.");
    while (1);
  }
  ds.reset_search();

  if (OneWire::crc8(addr, 7) != addr[7]) {
    Serial.println("CRC is not valid!");
    while (1);
  }

  ds.reset();
  ds.select(addr);
  ds.write(0x44);
  delay(1000);

  ds.reset();
  ds.select(addr);
  ds.write(0xBE);

  for (int i = 0; i < 9; i++) {
    data[i] = ds.read();
  }

  int raw = (data[1] << 8) | data[0];
  if (data[7] == 0x10) raw = (raw & 0xFFF0) + 12 - data[6];
  return raw / 16.0;
}

void setup() {
  Serial.begin(115200);
  pinMode( pinPhoto, INPUT );// Скорость общения
  delay(10);
  //Даем запустится вафле
  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
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void loop() {
  //Пишем в сериал то что мы подключаемся
  Serial.print("connecting to ");
  Serial.println(host);

  // Используем WiFi клиент
  WiFiClient client;
  WiFiClient client2;
  if (!client.connect(host, httpPort) && !client2.connect(host, httpPort)) {
    Serial.println("connection failed"); // пишем ошибку
    return;
  }

  temperature = getTemp(); // получаем температуру с датчика
  raw = analogRead( pinPhoto );

  String url = "/update?key=";
  url += privateKey;
  url += "&field2=";
  url += raw;
 
  String url2 = "/update?key=";
  url2 += privateKey;
  url2 += "&field3=";
  url2 += temperature;

  // Отправляем запрос на сервер
  client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n");
  Serial.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n");
  client2.print(String("GET ") + url2 + " HTTP/1.1\r\n" + "Host: " + host + "\r\n");
  Serial.print(String("GET ") + url2 + " HTTP/1.1\r\n" + "Host: " + host + "\r\n");
  delay(10);

  // Если сервер ответил то передадим ответ в сериал порт
  while (client.available()) {
    String line = client.readStringUntil('\r');
  }
  while (client2.available()) {
    String line = client2.readStringUntil('\r');
    Serial.print(line);
  }

  Serial.println();
  Serial.println("closing connection");

  delay(1000 * 60);
}

The meaning of this idea is to simultaneously (or with a minimum delay) take readings from the DS18B20 and the photoresistor and upload them. But the problem with this sketch is this - according to the port monitor - everything is ok, but the data does not appear on the site. The API for the site is exactly right. The port monitor shows
Connecting to "***"
......
WiFi connected
IP address:
192.168.88.227
connecting to 184.106.153.149
GET /update?key=мой ключ&field2=21.62 HTTP/1.1
Host: 184.106.153.149
GET /update?key=мой ключ&field3=1024 HTTP/1.1
Host: 184.106.153.149

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question