Discuss here different C compiler set ups, and compiling executables for the ESP8266

User avatar
By al555bike
#42890 Hi,
Hope this is the correct place for this post. I have a NodeMCU programming it with Arduino IDE. The chip is amazing. So easy to get onto internet.
I have some simple programs from the examples supplied which work well. One sends data to thingspeak, the other works as a web server sending a web page with temperature included.
The thing is I need to have a program which does both. Sends data to thingspeak every hour but can be polled with a browser when required. I just can't get my head around how to combine the two. The code below calls the getTemp() function every 30 seconds to update temperature, and delivers a web page with the temp. I can call the function to send data to thingspeak every hour with a counter in getTemp(). Do I need to set up another server?
Thanks
Code: Select all#include <ESP8266WiFi.h>
#include <Ticker.h>
 
const char* ssid = "My SSID"; // edit your wifi name here
const char* password = "My Password"; // type your password here
const char* apiKey = "My key for thingspeak";
Ticker secondTick;
void getTemp(void);
float temp =0; // variable to hold temperature data
int analog = 0; // variable to hold analog data
volatile int eventCount=0;
volatile boolean tempEvent = false;
void ISREvent()
{
  eventCount++;
  if(eventCount==3){   
    eventCount=0;
    tempEvent=true;
  }
WiFiServer server(80);
 
void setup() {
  Serial.begin(115200);// preapring to use serial communication
  delay(10);
  secondTick.attach(10,ISREvent);//attach the ISR routine this will give 10 seconds * 3 in ISR = 30 secs.
  // Connect to WiFi network
  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");
  // Start the server
  server.begin();
  Serial.println("Server started");
  // Print the IP address
  Serial.print("Use this URL to connect: ");
  Serial.print("http://");
  Serial.print(WiFi.localIP());
  Serial.println("/");
}
 
void loop() {
  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client) {
   if(tempEvent==true)
      {
        getTemp();
        tempEvent=false;
      }
    return;
  }
  // Wait until the client sends some data
  Serial.println("new client");
  while(!client.available()){
    delay(1);
  }
  // Read the first line of the request
  String request = client.readStringUntil('\r');
  Serial.println(request);
  client.flush();
  // Return the response as a webpage with temp.
//removed to keep code small 
  delay(1);
  Serial.println("Client disonnected");
  Serial.println("");
}

void getTemp()
{
//temperature taken here.
//counter here which when 1 hour passed calls thingspeak with temp
}

void sendDataThingSpeak(float temp)
{
  if (client.connect(server,80)) {  //   "184.106.153.149" or api.thingspeak.com
    String postStr = apiKey;
           postStr +="&field1=";
           postStr += String(temp);
           postStr += "\r\n\r\n";
 
     client.print("POST /update HTTP/1.1\n");
     client.print("Host: api.thingspeak.com\n");
     client.print("Connection: close\n");
     client.print("X-THINGSPEAKAPIKEY: "+apiKey+"\n");
     client.print("Content-Type: application/x-www-form-urlencoded\n");
     client.print("Content-Length: ");
     client.print(postStr.length());
     client.print("\n\n");
     client.print(postStr);
 
     Serial.print("Temperature: ");
     Serial.print(temp);
     Serial.println("% send to Thingspeak");
  }
  client.stop();
}
User avatar
By jwarnes71
#42892 They are $3 each. Just have two. Ive already planned in my head what to do with my project just from reading your post
Ive got one sending data to thingspeak and I'll have another serving a webpage for control of a relay to turn on power to the device.
User avatar
By krzychb
#42894 Hi al555bike,

This is a project tht shows step by step how to set up a web server to present measurements in a web browser as well as send data to the cloud. Everything on a single module.

It is starting with simple concepts and adding functionality step by step as far as up to openHAB integration with MQTT and OTA.

I fully agree this chip is amazing.

Krzysztof