Channel updates via browser but not via Ardunio sketch

Raymond Norton il 10 Mar 2022
Ultime attività Modificato da Raymond Norton il 10 Mar 2022

I am working with an Arduino nodemcu board and a Tmp36 sensor. My channel updates fine if I use my browser with the following: https://api.thingspeak.com/update?api_key=xxxxxxxxx&field1=71, but does not update with the following sketch, although the serial monitor shows the commands are executed:

//Source code to the Temperature Sensor and ThingSPeak Server Blog
String ssid     = "XXX";  // SSID to connect to
String password = "XXX"; // Our virtual wifi has no password (so dont do your banking stuff on this network)
String host     = "api.thingspeak.com"; // Open Weather Map API
const int httpPort   = 443;
String uri     = "/update?api_key=XXXX&field1=";
int setupESP8266(void) {
  // Start our ESP8266 Serial Communication
  Serial.begin(115200);   // Serial connection over USB to computer
  Serial.println("AT");   // Serial connection on Tx / Rx port to ESP8266
  delay(10);        // Wait a little for the ESP to respond
  if (!Serial.find("OK")) return 1;
    // Connect to 123D Circuits Simulator Wifi
    Serial.println("AT+CWJAP=\"" + ssid + "\",\"" + password + "\"");
    delay(10);        // Wait a little for the ESP to respond
    if (!Serial.find("OK")) return 2;
    // Open TCP connection to the host:
    Serial.println("AT+CIPSTART=\"TCP\",\"" + host + "\"," + httpPort);
    delay(50);        // Wait a little for the ESP to respond
    if (!Serial.find("OK")) return 3;
    return 0;
  }
void anydata(void) {
    int temp = map(analogRead(A0),20,358,-40,125);
    // Construct our HTTP call
    String httpPacket = "GET " + uri + String(temp) + " HTTP/1.1\r\nHost: " + host + "\r\n\r\n";
    int length = httpPacket.length();
    // Send our message length
    Serial.print("AT+CIPSEND=");
    Serial.println(length);
    delay(10); // Wait a little for the ESP to respond if (!Serial.find(">")) return -1;
    // Send our http request
    Serial.print(httpPacket);
    delay(1000); // Wait a little for the ESP to respond
    if (!Serial.find("SEND OK\r\n")) return;
}
void setup() {
    setupESP8266();
}
void loop() {
   anydata();
    delay(10000);
  }
Christopher Stapels
Christopher Stapels il 10 Mar 2022 (Modificato il 10 Mar 2022)

I recommend you use the ThingSpeak library , and program the board directly to send the data via wifi. The AT command set makes things a good deal harder in my opinion. I haven't found a case where using that method is completely necessary except for legacy systems. I used Node MCU ESP8266 to build many of the examples in the doc. Here is one example using the library, but there are also examples that come with the library that may be easier to follow.

Raymond Norton
Raymond Norton il 10 Mar 2022

Thank you for your reply. I actually had that thermister example running just fine but have not found one for the TMP36 yet. I am not skilled yet with identifying how I could modify the thermister sketch for my TMP36, so was hoping to find a project that mirrored it rather than use the AT commands.

Christopher Stapels
Christopher Stapels il 10 Mar 2022

The TMP 36 has a voltage output, so you can read it like the analog moisture sensor in this example . That example it written for use cases that do not use the library, I would recommend using the ThingSpeak library but reading the analog sensor as shown in that example.

Raymond Norton
Raymond Norton il 10 Mar 2022 (Modificato il 10 Mar 2022)

[Solved] This is what I came up with for a solution. Thank you for pointing me in the right direction!

#include "ThingSpeak.h"
//#include "secrets.h"
const unsigned long postingInterval = 120L * 1000L;
unsigned long myChannelNumber = xxx;
const char * myWriteAPIKey = "xxx";
#include <ESP8266WiFi.h>
long lastUpdateTime = 0;
char ssid[] = "xxx";   // your network SSID (name)
char pass[] = "xxx";   // your network password
int keyIndex = 0;            // your network key index number (needed only for WEP)
WiFiClient  client;
#define TMPPin A0
String temperatureString = "";
void setup() {
  Serial.begin(115200);
  delay(100);
    WiFi.mode(WIFI_STA);
    ThingSpeak.begin(client);
  }
void loop() {
    startwifi();
    readsensor();
}
 void startwifi(){
  // Connect or reconnect to WiFi
  if (WiFi.status() != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(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.");
    }
   }
    void readsensor(){  
    int tmpValue = analogRead(TMPPin);
    float voltage = tmpValue * 3.1;// converting that reading to voltage
    voltage /= 1024.0;
    float temperatureC = (voltage - 0.5) * 100 ;  //converting from 10 mv per degree wit 500 mV offset
    //to degrees ((voltage - 500mV) times 100)
    float temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;  //now convert to Fahrenheit
    temperatureString = " " + String(temperatureF);
    Serial.println(temperatureString);
    delay(10000);
    // Write value to Field 1 of a ThingSpeak Channel
    int httpCode = ThingSpeak.writeField(myChannelNumber, 1, temperatureString, myWriteAPIKey);
    if (httpCode == 200) {
      Serial.println("Channel write successful.");
      Serial.println(temperatureString);
    }
    else {
      Serial.println("Problem writing to channel. HTTP error code " + String(httpCode));
    }
    // Wait 20 seconds to update the channel again
    delay(20000);
  }
Raymond Norton
Raymond Norton il 10 Mar 2022 (Modificato il 10 Mar 2022)

Thank you! That is helpful!