Example sketches for the new Arduino IDE for ESP8266

Moderator: igrr

User avatar
By martinayotte
#29708 ESP8286WiFi library is already providing few methods for that.
Instead of using uint8_t* macAddress(uint8_t* mac), simply use String macAddress(void).
Same thing for uint8_t* softAPmacAddress(uint8_t* mac), use String softAPmacAddress(void) instead.
User avatar
By canedje
#33469
Piman wrote:Hi am not a coder and like to try things out for myself but I come to a point where I need help, :?
I am trying to send the data from an ESP 8266 to a raspberry PI server using an example found on the net which works fine, but trying to get it to send multiple values i.e.: temperature humidity and switch values, through one TCP connection without having to disconnect and reconnect for each value string. I would appreciate if somebody with a little more experience than myself can help me out in explaining on how this could be achieved and or provide examples of how this could be done Thank You For Your Time Andy.
Code: Select all#include <ESP8266WiFi.h>
#include <Base64.h>

//AP definitions
#define AP_SSID "your AP SSID"
#define AP_PASSWORD "your AP password"

//IoT server definitions
#define IOT_USERNAME    "admin"
#define IOT_PASSWORD    "test"
#define IOT_IP_ADDRESS  "192.168.1.5"
#define IOT_PORT        80
#define IOT_NODE        "N8S0"

#define INPUT_PIN         2
#define USER_PWD_LEN      40


char unameenc[USER_PWD_LEN];
bool oldInputState;


void setup() {
  Serial.begin(115200);
  pinMode(INPUT_PIN, INPUT);
 
  wifiConnect();
   
  char uname[USER_PWD_LEN];
  String str = String(IOT_USERNAME)+":"+String(IOT_PASSWORD); 
  str.toCharArray(uname, USER_PWD_LEN);
  memset(unameenc,0,sizeof(unameenc));
  base64_encode(unameenc, uname, strlen(uname));
 
  oldInputState = !digitalRead(INPUT_PIN);
}

void loop() {
  int inputState = digitalRead(INPUT_PIN);; 
 
  if (inputState != oldInputState)
  {
    sendInputState(inputState);
    oldInputState = inputState;
  }
}

void wifiConnect()
{
    Serial.print("Connecting to AP");
    WiFi.begin(AP_SSID, AP_PASSWORD);
    while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.print(".");
  }
 
  Serial.println("");
  Serial.println("WiFi connected"); 
}

void sendInputState(bool inputState)

   WiFiClient client;
   
   while(!client.connect(IOT_IP_ADDRESS, IOT_PORT)) {
    Serial.println("connection failed");
    wifiConnect();
  }
 
  String url = "";
  String command = "";
 
  if (inputState)
    command = "ControlOn";
  else
    command = "ControlOff";
 
  url = "/Api/IoT/Control/Module/Virtual/"+ String(IOT_NODE) + "/"+command; // generate IoT server node URL

  Serial.print("POST data to URL: ");
  Serial.println(url);
 
  client.print(String("POST ") + url + " HTTP/1.1\r\n" +
               "Host: " + String(IOT_IP_ADDRESS) + "\r\n" +
               "Connection: close\r\n" +
               "Authorization: Basic " + unameenc + " \r\n" +
               "Content-Length: 0\r\n" +
               "\r\n");

  delay(100);
    while(client.available()){
    String line = client.readStringUntil('\r');
    Serial.print(line);
  }
 
  Serial.println();
  Serial.println("Connection closed");
}



I'm trying the same type of project to organize.
I'm able to send variables to an HTTP adress and port.
What I'm looking for is the code (In Python) at the Rapberry to read this values.
I do not use IoT you mentioned. (I don't know what that is?)
Are you able to give me an example of that code?
User avatar
By Gokul
#42226 Hi everybody,,

I'm working on similar problem.. using arduino i'm measuring 10 sensors and i've used esp8266 as client interfaced with arduino which connects to local router...!

at pc side, i have written a server code using node.js which listens to particular router's ip and port..!

but, these 10 data's aren't sent to the node.js

I have attached the code with this post

i couldn't figure out the problem.. please help me in this regard,,,,!!!!!
You do not have the required permissions to view the files attached to this post.