Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Sender,Sensor code. Similar to Master, Receiver but not identical.,
- //C:\Users\Dell\Documents\Arduino\WorkingAugust22\SantoSensor_2.ino\SantoSensor_2.ino.ino
- //C:\Users\Dell\Documents\Arduino\WorkingAugust22\SantosSensor3\SantosSensor3.ino, Wed Aug 10 13:32:09 NZST 2022.
- //Compiled above and ran but had not plugged in Master Wemos with slightly different code.
- /* SantosSensor4.ino Wed Aug 10 16:38:10 NZST 2022. Making strucs better. Representing Master/Slave idea
- SantosSensor4.ino
- Tue Aug 16 12:05:02 NZST 2022 Include new struc variables to make it compatible with SantosMaster3.ino C:\Users\Dell\Documents\Arduino\WorkingAugust22\SantosSensor7\SantosSensor7.ino
- Next version uses HTU sensor in subroutine,Thu Aug 18 13:00:29 NZST 2022
- C:\Users\Dell\Documents\Arduino\WorkingAugust22\HTU_0SantosSensor7\HTU_0SantosSensor7.ino compiles, but not tested yet. Thu Aug 18 14:10:16 NZST 2022
- */
- #include <ESP8266WiFi.h>
- //03
- #include <espnow.h> // #include <Adafruit_Sensor.h> Need this?
- #include <Wire.h>
- #include "SparkFunHTU21D.h"
- //Create an instance of the object
- HTU21D myHumidity;
- uint8_t broadcastAddress[] = {0x98,0xF4,0xAB,0xBF,0xEC,0xCC}; //Mac address of Wemos2
- //{0x18,0xFE,0x34,0xF9,0x2E,0x4A} ; //Mac address of Wemos1-
- // Define variables to store Sensor readings to be sent to Master
- float temperature;
- float humidity;
- unsigned long milliSecsA;
- uint8_t orders;
- uint8_t stack[10];
- uint8_t stackPtr;
- uint8_t bufferPtr;
- uint8_t bigBuffer[200];
- // Define variables to store incoming readings from Master
- float incomingTemp;
- float incomingHum;
- unsigned long incomingMillisA;
- uint8_t incomingOrders;
- uint8_t incomingStac[10];
- uint8_t incomingStackPtr;
- uint8_t incomingBufferPtr;
- uint8_t incomingBigBuffer[200];
- // Updates Sensor readings every 10 seconds
- const long interval = 10000;
- unsigned long previousMillis = 0; // will store last time Sensor was updated
- // Variable to store if sending data was successful
- String success;
- //Structure example to send data
- //Must match the receiver structure
- typedef struct struct_message {
- float temp;
- float hum;
- unsigned long millisA; //for timing
- uint8_t ord;
- uint8_t stac[10];
- uint8_t stacPtr;
- uint8_t bufPtr;
- uint8_t bigBuf[200];
- } struct_message;
- // Create a struct_message called SensorReadings to hold sensor readings; to be sent to master
- struct_message SensorReadings;
- // Create a struct_message to hold incoming master readings
- struct_message incomingReadings;
- // Callback when data is sent
- void OnDataSent(uint8_t *mac_addr, uint8_t sendStatus) {
- Serial.print("Last Packet Send Status (From Sensor I: ");
- if (sendStatus == 0){
- Serial.println("Delivery success");
- }
- else{
- Serial.println("Delivery fail");
- }
- }
- // Callback when data is received
- void OnDataRecv(uint8_t * mac, uint8_t *incomingData, uint8_t len) {
- memcpy(&incomingReadings, incomingData, sizeof(incomingReadings));
- Serial.print("OnDataRecv() running. Bytes received: ");
- Serial.println(len);
- incomingTemp = incomingReadings.temp;
- incomingHum = incomingReadings.hum;
- incomingMillisA= incomingReadings.millisA;
- incomingOrders=incomingReadings.ord;
- for(int i =0;i<10;i++) incomingStac[i]=incomingReadings.stac[i];
- incomingStackPtr=incomingReadings.stacPtr;
- incomingBufferPtr=incomingReadings.bufPtr;
- for(int i =0;i<10;i++) incomingBigBuffer[i]=incomingReadings.bigBuf[i];
- Serial.println("OnDataRecv() finished");
- //Now all the struct fields from have been put into slave sensor variables ...
- //Like incomingxxx=incomingReadings.xxx, where xxx's are similar but not identical.
- }
- void getReadings(){
- // Read the sender's sensor data. Read Temperature. Fake,right now.
- Serial.println("\n now doing getReadings()))))))))))))))))");
- temperature = 1112.3 ; //dht.readTemperature();
- // Read temperature as Fahrenheit (isFahrenheit = true)
- //float t = dht.readTemperature(true);
- if (isnan(temperature)){
- Serial.println("Failed to read from Sensor");
- temperature = 0.0;
- }
- humidity = 1145.6; //dht.readHumidity();
- if (isnan(humidity)){
- Serial.println("Failed to read from Sensor");
- humidity = 0.0;
- }
- }
- void printIncomingReadings(){
- // Display Readings in Serial Monitor. Nothing so far
- Serial.println("\nINCOMING READINGS from Master ----------------------");
- Serial.print("Temperature: ");
- Serial.print(incomingTemp);
- Serial.println(" ºC");
- Serial.print("Humidity: ");
- Serial.print(incomingHum);
- Serial.println(" %");
- Serial.print(incomingMillisA);
- Serial.println(" ..m..");
- Serial.print(incomingOrders);
- Serial.println(" incomingOrders..");
- Serial.print(incomingStackPtr);
- Serial.println("incomingStackPtr");
- for(int i =0;i<10;i++) Serial.print( incomingStac[i]);
- Serial.println(" <--incomingStac[]");
- for(int i =0;i<200;i++) Serial.print( incomingBigBuffer[i]);
- Serial.println(" <--incomingBigBuffer[]");
- }
- void setup() {
- // Init Serial Monitor
- Serial.begin(115200);
- myHumidity.begin();
- // Set device as a Wi-Fi Station
- WiFi.mode(WIFI_STA);
- WiFi.disconnect();
- // Init ESP-NOW
- if (esp_now_init() != 0) {
- Serial.println("Error initializing ESP-NOW");
- return;
- }
- // Set ESP-NOW Role
- esp_now_set_self_role(ESP_NOW_ROLE_COMBO);
- // Once ESPNow is successfully Init, we will register for Send CB to
- // get the status of Trasnmitted packet
- esp_now_register_send_cb(OnDataSent);
- // Register peer
- esp_now_add_peer(broadcastAddress, ESP_NOW_ROLE_COMBO, 1, NULL, 0);
- // Register for a callback function that will be called when data is received
- esp_now_register_recv_cb(OnDataRecv);
- }
- void loop() {
- unsigned long currentMillis = millis();
- if (currentMillis - previousMillis >= interval) {
- // save the last time you updated the DHT values
- previousMillis = currentMillis; //NB programmable delay.
- //Get Sensor 1 readings
- getReadings();
- //Set values to send
- SensorReadings.temp = temperature;
- SensorReadings.hum = humidity;
- SensorReadings.millisA=millis();
- // Send message via ESP-NOW
- esp_now_send(broadcastAddress, (uint8_t *) &SensorReadings, sizeof(SensorReadings));
- // Print incoming readings
- printIncomingReadings();
- Serial.print(" ================Orders= ");
- Serial.println( incomingReadings.ord);
- }
- }
- void getHDU() { //read via HDU21 the temp and humidity
- Serial.println("Nothing HDUish here yet.");
- float humd = myHumidity.readHumidity();
- float temp = myHumidity.readTemperature();
- Serial.print("Time:");
- Serial.print(millis());
- Serial.print(" Temperature:");
- Serial.print(temp, 1);
- Serial.print("C");
- Serial.print(" Humidity:");
- Serial.print(humd, 1);
- Serial.print("%");
- Serial.println();
- delay(1000);
- }
- /*
- HTU21D Humidity Sensor Example Code
- By: Nathan Seidle
- SparkFun Electronics
- Date: September 15th, 2013
- License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license).
- Uses the HTU21D library to display the current humidity and temperature
- Open serial monitor at 9600 baud to see readings. Errors 998 if not sensor is detected. Error 999 if CRC is bad.
- Hardware Connections (Breakoutboard to Arduino):
- -VCC = 3.3V
- -GND = GND
- -SDA = A4 (use inline 330 ohm resistor if your board is 5V)
- -SCL = A5 (use inline 330 ohm resistor if your board is 5V)
- */
- /*
- #include <Wire.h>
- #include "SparkFunHTU21D.h"
- //Create an instance of the object
- HTU21D myHumidity;
- void setup()
- {
- Serial.begin(9600);
- Serial.println("HTU21D Example!");
- myHumidity.begin();
- }
- void loop()
- {
- float humd = myHumidity.readHumidity();
- float temp = myHumidity.readTemperature();
- Serial.print("Time:");
- Serial.print(millis());
- Serial.print(" Temperature:");
- Serial.print(temp, 1);
- Serial.print("C");
- Serial.print(" Humidity:");
- Serial.print(humd, 1);
- Serial.print("%");
- Serial.println();
- delay(1000);
- */
- /***************************************************
- This is an example for the HTU21D-F Humidity & Temp Sensor
- Designed specifically to work with the HTU21D-F sensor from Adafruit
- ----> https://www.adafruit.com/products/1899
- These displays use I2C to communicate, 2 pins are required to
- interface
- ****************************************************/
- /*
- #include <Wire.h>
- #include "Adafruit_HTU21DF.h"
- // Connect Vin to 3-5VDC
- // Connect GND to ground
- // Connect SCL to I2C clock pin (A5 on UNO)
- // Connect SDA to I2C data pin (A4 on UNO)
- Adafruit_HTU21DF htu = Adafruit_HTU21DF();
- void setup() {
- Serial.begin(9600);
- Serial.println("HTU21D-F test");
- if (!htu.begin()) {
- Serial.println("Couldn't find sensor!");
- while (1);
- }
- }
- void loop() {
- float temp = htu.readTemperature();
- float rel_hum = htu.readHumidity();
- Serial.print("Temp: "); Serial.print(temp); Serial.print(" C");
- Serial.print("\t\t");
- Serial.print("Humidity: "); Serial.print(rel_hum); Serial.println(" \%");
- delay(500);
- }
- */
Advertisement
Add Comment
Please, Sign In to add comment